首页 > 编程语言 >小程序 canvas type="2d"

小程序 canvas type="2d"

时间:2022-11-25 10:46:19浏览次数:36  
标签:canvas const success res ctx 2d type wx

<!-- 标签是在组件当中使用,组件和页面使用是有区别的 --> <canvas style="width:{{imgWidth}}px;height:{{imgHeight}};position:fixed;left:9999px" type="2d" id="myCanvas" class="canvas"></canvas>     // 下载静态码 myCanvas是我的背景图片 // 这里的处理,调用this.getQRCodeImg生成二维码图片再绘制背景图片     handleDownload() {       this.getQRCodeImg().then(data=> {         wx.createSelectorQuery().in(this).select('#myCanvas').fields({           node: true,           size: true,         })         .exec( async res=> {           const canvas = res[0].node           const width = res[0].width           this.canvasWidth = width           const height = res[0].height           this.canvasHeight = height           const ctx = canvas.getContext('2d')           const dpr = wx.getSystemInfoSync().pixelRatio           canvas.width = res[0].width * dpr           canvas.height = res[0].height * dpr           ctx.scale(dpr, dpr)           ctx.clearRect(0, 0, wx.getSystemInfoSync().windowWidth, wx.getSystemInfoSync().windowHeight)           const img = canvas.createImage()           img.src = '../../image/back-img.png' // 可以是本地地址也可以是网络地址           let imgHeight = null           wx.getImageInfo({             src:'../../image/back-img.png',             success(res){               // 屏幕宽度/图片宽度  *  图片高度               imgHeight = wx.getSystemInfoSync().windowWidth/res.width * res.height               console.log(res);             }           })           img.onload = () => ctx.drawImage(img, 0, 0, wx.getSystemInfoSync().windowWidth, imgHeight)           const codeImg = canvas.createImage()           codeImg.src = data // 可以是本地地址也可以是网络地址           wx.getImageInfo({             src:data,             success(res){               // console.log(res);             }           })           codeImg.onload = () => ctx.drawImage(codeImg, codePosition, 146, 151, 151)           wx.showModal({             title: '提示',             content: '是否下载图片??',             success: (res)=> {               if (res.confirm) {                 wx.canvasToTempFilePath({                   canvas: canvas,                   success: (res) => {                     // console.log(resx.tempFilePath);                     wx.saveImageToPhotosAlbum({                       filePath: res.tempFilePath,                       success: function (data) {                         wx.showToast({                           title: '已保存到相册',                           icon: 'success',                           duration: 2000                         })                       },                       fail: function (err) {                       },                       complete(res) {                         wx.hideLoading();                         console.log(res);                       }                     })                   },                   fail: (ex) => {                       console.log(ex);                   }                 },this);               }             }           })         })       })     }, // 生成图片 staticCode是我的另外一个Canvas它是一个二维码 getQRCodeImg() {       const ctx = wx.createCanvasContext('staticCode')       return new Promise((resolve,rejects)=> {         setTimeout(() => {           ctx.draw(false, setTimeout(() => {             wx.canvasToTempFilePath({               width: codewidth,               height: codewidth,               canvasId: 'staticCode',               success: res => {                 resolve(res.tempFilePath)               },             fail: error => {               rejects(error)             }             }, this)           },300))         }, 200)       })     },

标签:canvas,const,success,res,ctx,2d,type,wx
From: https://www.cnblogs.com/tangyuqi/p/16924390.html

相关文章

  • [Typescript] 115. Hard - Drop String
    Dropthespecifiedcharsfromastring.Forexample:typeButterfly=DropString<'foobar!','fb'>//'ooar!' /*_____________YourCodeHere_____________......
  • TypeScript类型(二)
    对象 示例://#regionjs写法//object表示一个js对象leta:object;a={};a=function(){};//#endregion//#regionTypeScript写法//{}用来指定对......
  • 使用html2canvas和jspdf将页面保存位pdf
    使用html2canvas和jspdf将页面保存位pdf<scriptsrc="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js"></script><scriptsrc="https://unpkg.com/html2canvas@......
  • WeNet和ESPnet中下采样模块(Conv2dSubsampling)
    关于WeNet和ESPnet两个工具下采样模块都是相同的操作,首先将输入序列扩充一个维度(因为要使用二维卷积),然后通过两个二维卷积,其中第一个卷积的输入通道为“1”,输出通道为odi......
  • TypeScript类型声明
    基本类型类型声明类型声明是TS非常重要的一个特点通过类型声明可以指定TS中变量(参数、形参)的类型指定类型后,当为变量赋值时,TS编译器会自动检查值是否符合类型......
  • C语言学习【Typedef】
    C语言允许为一个数据类型起一个新的别名,就像给人起“绰号”一样。Typedef可以为int起个绰号为MoneytypedefintMoney;Money=0;这就是typedef的基本用法既:typedef......
  • maven制作archetype(原型)
    1.在本地新建第一个符合公司规范的maven项目2.创建archetype到本地仓库因为idea会生成很多不必要的文件,所以首先创建一个archetype.properties,配置需要过滤的文件#打包过滤......
  • 小程序 XQTypeScriptFramework 使用
    说明XQTypeScriptFramework隶属于XQFramework下JS基础性框架部分XQFramework励志将开发将常用开发语言基础性框架统一汇总,为全站开发使用到的基础语法进行统一,拜......
  • Android绘图Canvas、Paint类基本用法
    这些用法都是英文的字面意思,所以解释部分就放在代码的注释那里了,要画图,就要继承View类重写onDraw方法,要注意的是构造方法也重写一下吧,话不多说直接上代码~~~importandroid.......
  • C++中的Type Alias
    在C++中,我们通常使用typedef来实现typealias.比如:#include<cstdint>//Cstandardinttypedefuint32_tpoints_t;//points_tisaliasofuint32_ttypedefuin......