小程序自适应canvas
wxml
<canvas style="width: 686rpx;height: 686rpx;background:#f1f1f1;" canvas-id="qrcCanvas"/> <input value='{{qrcStr}}' bindblur="onQrcStrBlur" type="text" maxlength="255" />
js
data:{ qrStr:'xxx' 需要生成字段 canvasId: "qrcCanvas",//需要绘画的元素 } //适配不同屏幕大小的canvas setCanvasSize(){ var size={}; try { var res = wx.getSystemInfoSync(); var scale = 750/686;//不同屏幕下canvas的适配比例;设计稿是750宽 var width = res.windowWidth/scale; var height = width;//canvas画布为正方形 size.w = width; size.h = height; } catch (e) { // Do something when catch error console.log("获取设备信息失败"+e); } return size; //返回大小 } , createQrCode(str,canvasId,cavW,cavH){ //调用插件中的draw方法,绘制二维码图片 qrCode.api.draw(str,canvasId,cavW,cavH); }, //获取input输入的值 onQrcStrBlur(e) { this.setData({qrcStr: e.detail.value}); }, //在 onReady调用 onReady: function() { let size = this.setCanvasSize();//动态设置画布大小 this.createQrCode(this.data.qrcStr, this.canvasId, size.w, size.h); },
相关推荐
星星有所不知 2020-10-12
MIKUScallion 2020-07-05
jinxiutong 2020-05-10
MIKUScallion 2020-04-11
MIKUScallion 2020-02-22
jinxiutong 2020-02-13
大地飞鸿 2020-02-11
jinxiutong 2020-02-10
大地飞鸿 2020-11-12
jinxiutong 2020-07-26
songfens 2020-07-05
songfens 2020-06-11
songfens 2020-06-08
northwindx 2020-05-31
northwindx 2020-05-31
northwindx 2020-05-27
northwindx 2020-05-25