当前位置: 首页 > news >正文

canvas保存图片

需求:上面有几个按钮,其中有一个切换是图片

用v-if会导致图片加载慢

实现方法:

一进来就加载,通过监听元素显示,用于控制canvas的宽高,从而达到隐藏的效果

组件dowolad.vue

<template><view style="margin-top: 20rpx;"><canvas canvas-id="myCanvas" :style="scaleObj"></canvas><button :loading="loading" :disabled="loading" @click="saveImg" class="confirmBnt" v-if="!weixin && show">保存二维码</button></view></template><script>export default {props: ['show'],data() {return {show: true,canvasId: 'myCanvas',imagePath: '',canvaseAttr:{width: 0,height: 0,},scaleObj:{},loading: false,weixin: false};},mounted(){this.weixin = this.$wechat.isWeixin();this.init();},watch:{show:{handler(newData, oldData){let scale = {width: 0,height: 0,}if(newData){scale.width = this.canvaseAttr.width+'px';scale.height = this.canvaseAttr.height+'px';}this.scaleObj = scale;},deep: true,immediate: true}},methods: {init(){this.getImageInfo();},getImageInfo() {let that = this;uni.getSystemInfo({success: (window) => {const screenWidth = window.windowWidth;uni.getImageInfo({src: 'https://media.sammu.top/pay_bank_img.png',success: (res) => {let scale = screenWidth/res.width;let height = res.height*scale;that.canvaseAttr = {width: screenWidth,height: height,}that.imagePath = res.path;that.drawCanvas();},fail: (err) => {console.log(err);}});},});},drawCanvas() {this.$nextTick(()=>{const ctx = uni.createCanvasContext('myCanvas', this);ctx.drawImage(this.imagePath, 0, 0, this.canvaseAttr.width, this.canvaseAttr.height);ctx.draw();})},saveImg(){const ctx = uni.createCanvasContext(this.canvasId, this);let that = this;that.loading = true;// 绘制完成  ctx.draw(true, function () {uni.canvasToTempFilePath({canvasId: that.canvasId,success: (res) => {// #ifdef H5that.saveH5Canvas(res)// #endif// #ifdef APPuni.saveImageToPhotosAlbum({filePath: res.tempFilePath,success: () => {uni.showToast({title: '保存成功',icon: 'none'})that.loading = false;uni.hideLoading();},fail: () => {uni.showToast({title: '保存失败',icon: 'none'})that.loading = false;uni.hideLoading();}});// #endif},fail: (err) => {that.loading = false;console.log(err, "错误信息");}});})},saveH5Canvas(res) {let that = this;try {// 在 h5 中,res.tempFilePath 返回的是 base64 类型要处理,通过 a 标签的形式下载var arr = res.tempFilePath.split(',');var bytes = atob(arr[1]);let ab = new ArrayBuffer(bytes.length);let ia = new Uint8Array(ab);for (let i = 0; i < bytes.length; i++) {ia[i] = bytes.charCodeAt(i);}var blob = new Blob([ab], {type: 'application/octet-stream'});var url = URL.createObjectURL(blob);var a = document.createElement('a');a.href = url;a.download = new Date().valueOf() + ".png";var e = document.createEvent('MouseEvents');e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);a.dispatchEvent(e);URL.revokeObjectURL(url);uni.showToast({title: '保存成功',icon: 'none'})that.loading = false;uni.hideLoading();} catch (e) {uni.showToast({title: '保存失败',icon: 'none'})that.loading = false;uni.hideLoading();}    },},}</script><style lang="scss" scoped>.confirmBnt{position: fixed;bottom: 20rpx;width: calc(100% - 20rpx);left: 50%;transform: translateX(-50%);background: var(--view-theme);border-radius: 24rpx;height: 108rpx;line-height: 108rpx;font-size: 40rpx;font-weight: 700;text-align: center;color: #fff;}</style>

调用

<dowload ref="dowloadRef" :show="active == 1"></dowload>

http://www.lryc.cn/news/503194.html

相关文章:

  • DNS到底有什么用?
  • 什么是CRM系统?CRM系统的功能、操作流程、生命周期
  • 美畅物联丨JS播放器录像功能:从技术到应用的全面解析
  • 我们来学mysql -- 事务并发之不可重复读(原理篇)
  • ABAQUS进行焊接仿真分析(含子程序)
  • BAPI_GOODSMVT_CREATE物料凭证增强字段
  • tomcat的优化和动静分离
  • [ShaderLab] 【Unity】【图像编程】理解 Unity Shader 的结构
  • vue的前端架构 介绍各自的优缺点
  • 可信AI与零知识证明的概念
  • JavaScript逆向时,常用的11个hook
  • PCL点云库入门——PCL库可视化之CloudViewer类简单点云信息显示
  • C++ 【衔接篇】
  • qcreator 调试原理
  • Windows 系统中的组策略编辑器如何打开?
  • scala的泛型类
  • 基于Couchbase的数据构建方案:数仓分层
  • 信创改造-Spring Boot 项目部署至 TongWeb
  • supervision - 好用的计算机视觉 AI 工具库
  • 程序的调试
  • 使用html 和javascript 实现微信界面功能2
  • 虚幻引擎Actor类生命周期
  • 记录2024-leetcode-字符串DP
  • 爬虫获取的数据如何有效存储和管理?
  • [Unity] AppLovin Max接入Native 广告 IOS篇
  • 康耐视智能相机(Insight)通过ModbusTCP发送字符串到倍福(BECKHOFF)PLC中
  • TIFS投稿记录(IEEE Transactions on Information Forensics Security)
  • 极越汽车,加速跌落
  • 深入解析MySQL事务隔离级别与锁机制在银行账户业务中的应用
  • postman可以通的请求,前端通不了(前端添加Content-type,后端收不到请求)