话不多说,直接上代码!
index.wxml
在这里插入代码片
<view class="canvas-box">
<canvas disable-scroll="true" bindtouchstart="touchstart" bindtouchmove="touchmove" id="canvas" type="2d" force-use-old-canvas="true"></canvas>
</view>
index.js
//获取canvas节点,节点信息传入初始化中
onLoad: function (options) {
wx.createSelectorQuery()
.select("#canvas")
.fields({
node: true,
size: true,
})
.exec(this.init.bind(this));
}, 50);
},
//初始化画布
init(res) {
const canvas = res[0].node;
const ctx = canvas.getContext("2d");
const dpr = wx.getSystemInfoSync().pixelRatio;
canvas.width = res[0].width;
canvas.height = res[0].height;
console.log(res);
ctx.clearRect(0, 0, canvas.width, canvas.height);