本文实例为大家分享了微信小程序实现手指触摸画板的具体代码,供大家参考,具体内容如下
先看效果图:
wxml
js
},//初始化函数
initCanvas: function () {
// 使用 wx.createContext 获取绘图上下文 context
context = wx.createCanvasContext('canvas');
context.beginPath()
context.setStrokeStyle('#000000');
context.setLineWidth(4);
context.setLineCap('round');
context.setLineJoin('round');
},//事件监听
canvasIdErrorCallback: function (e) {
console.error(e.detail.errMsg)
},canvasStart: function (event) {
isButtonDown = true;
arrz.push(0);
arrx.push(event.changedTouches[0].x);
arry.push(event.changedTouches[0].y);
},canvasMove: function (event) {
if (isButtonDown) {
arrz.push(1);
arrx.push(event.changedTouches[0].x);
arry.push(event.changedTouches[0].y);
};
for (var i = 0; i < arrx.length; i++) {
if (arrz[i] == 0) {
context.moveTo(arrx[i],arry[i])
} else {
context.lineTo(arrx[i],arry[i])
};
};
context.clearRect(0,canvasw,canvash);
context.setStrokeStyle('#000000');
context.setLineWidth(4);
context.setLineCap('round');
context.setLineJoin('round');
context.stroke();
context.draw(false);
},canvasEnd: function (event) {
isButtonDown = false;
},//清除画布
cleardraw: function () {
//清除画布
arrx = [];
arry = [];
arrz = [];
context.clearRect(0,canvash);
context.draw(true);
},//提交签名内容
setSign: function () {
var that = this;
if (arrx.length == 0) {
wx.showModal({
title: '提示',content: '签名内容不能为空!',showCancel: false
});
return false;
};
console.log("不是空的,canvas即将生成图片")
//生成图片
wx.canvasToTempFilePath({
canvasId: 'canvas',success: function (res) {
console.log("canvas可以生成图片")
console.log(res.tempFilePath,'canvas图片地址');
that.setData({ canvasimgsrc: res.tempFilePath })
//code 比如上传操作
},fail: function () {
console.log("canvas不可以生成图片")
wx.showModal({
title: '提示',content: '微信当前版本不支持,请更新到最新版本!',showCancel: false
});
},complete: function () {
}
})
},/**
}
})
css