1.新建两个canvas标签,一个空白画布 一个使用的画布
2.点击确认的时候比较两个画布 data URL 数据,一致说明画布没内容
第一个是空白的
<view a:if="{{showCanvas}}" class="sign-box2">
<canvas id="awesomeCanvas2"/>
</view>
第二个是使用的
<view a:if="{{showCanvas}}" class="sign-box">
<view class="canvas-box" catchTouchStart="canvasStart" catchTouchMove="canvasMove" catchTouchEnd="canvasEnd" catchTouchCancel="canvasCancel">
<canvas id="awesomeCanvas"/>
</view>
<view class="canvas-btn-box">
<view catchTap="canvasBoxCancel">取消</view>
<view catchTap="canvasBoxBack">撤销</view>
<view catchTap="makeSure">确认</view>
</view>
</view>
// 打开canvas弹窗 初始化
dosign() {
if (this.data.isabled == "false") {
return false;
}
dd.hideKeyboard();
this.setData({
showCanvas: true
});
this.data.ctx = dd.createCanvasContext("awesomeCanvas");
this.data.ctx.save();
this.data.ctx.beginPath();
this.data.ctx.setLineJoin("bevel");
this.data.ctx.setLineWidth(5);
this.data.ctx.setLineCap("round");
this.data.ctx2 = dd.createCanvasContext("awesomeCanvas2");
this.data.ctx2.save();
this.data.ctx2.beginPath();
this.data.ctx2.setLineJoin("bevel");
this.data.ctx2.setLineWidth(5);
this.data.ctx2.setLineCap("round");
},
// 撤销 清空画布内容
canvasBoxBack() {
this.data.ctx.restore()
this.data.ctx.draw();
this.data.ctx.closePath();
this.data.ctx.beginPath();
this.data.ctx.setLineJoin("bevel");
this.data.ctx.setLineWidth(5);
this.data.ctx.setLineCap("round");
},
// 取消
canvasBoxCancel() {
this.setData({
showCanvas: false
})
},
canvasStart(e) {
this.data.lineBegin = true
let x = e.touches[0].clientX - e.target.offsetLeft,
y = e.touches[0].clientY - e.target.offsetTop; // 获取触摸点在画板(canvas)的坐标
this.data.point.x = x;
this.data.point.y = y;
},
canvasMove(e) {
if (this.data.lineBegin) {
let x = e.touches[0].clientX - e.target.offsetLeft,
y = e.touches[0].clientY - e.target.offsetTop; // 获取触摸点在画板(canvas)的坐标
this.data.ctx.moveTo(this.data.point.x, this.data.point.y);
this.data.ctx.lineTo(x, y);
this.data.ctx.stroke();
this.data.ctx.draw();
this.data.point.x = x, this.data.point.y = y; // 重置点坐标为上一个坐标
}
},
canvasEnd() {
if (this.data.lineBegin) {
this.data.lineBegin = false
}
},
canvasCancel() {
this.data.ctx.closePath();
this.data.lineBegin = false
},
//签名保存
makeSure() {
let that = this
// 空白的尺寸
this.data.ctx2.toDataURL(
{
destWidth: 93.75,
destHeight: 50,
success(res) {
that.data.ctx.toDataURL(
{
destWidth: 93.75,
destHeight: 50,
success(res2) {
if (res != res2) { // 用空白画布和使用的画布比较 看数据是否一致,一致说明没有写内容
that.data.ctx.toTempFilePath(
{
destWidth: 93.75,
destHeight: 50,
success(res) {
that.setData({
['obj.IsGenerateJoint']: true,
showCanvas: false,
AutographImage: res.apFilePath,
})
let upObj = {
code: "电子签名附件",
url: res.apFilePath,
}
that.uploadpicImage(upObj)
},
error(err) {
}
})
}
},
error(err) {
}
})
},
error(err) {
}
})
},