<canvas>您的手机不支持在线签署</canvas> const canvasPaint = {};//定义一个全局对象,把canvas的各种状态存进去 canvasPaint.canvas = document.getElementById("canvas"); canvasPaint.ctx = document.getElementById("canvas").getContext("2d"); canvasPaint.ctx.lineCap = 'round';//让结束线帽呈现圆滑状 canvasPaint.ctx.lineJoin = 'round';//交汇时呈现圆滑状 canvasPaint.ctx.strokeWidth = 5;//描边宽度 canvasPaint.ctx.lineWidth = 5;//线条宽度
初始化好画布后,我们需要监听画布上的滑动事件
canvasPaint.canvas.addEventListener('touchstart', startEventHandler, {passive: false}); function startEventHandler(event) { event.preventDefault(); canvasPaint.ctx.beginPath();//每次都是一个新路径,不写会和上个字的最后一笔连起来 canvasPaint.canvas.addEventListener('touchmove', moveEventHandler, {passive: false}); canvasPaint.canvas.addEventListener('touchend', endEventHandler, {passive: false}); }
passive: false 和 event.preventDefault() 这两个是绝配哦, event.preventDefault() 阻止默认行为,防止在画布上写字时触发了浏览器自带的下拉动作之类的。那 passive: false 是谷歌56版本后提出的新属性,设置为 false 就是告诉浏览器我有阻止默认行为的代码,刚开始不要给我滑动,你需要执行我的 event.preventDefault() 这句代码,如果设置为了 true ,浏览器会自动忽略这句代码,从而不能阻止成功,默认是 true ,所以这里就是坑之一了。
我们继续编写移动划线逻辑
function moveEventHandler(event) { event.preventDefault(); var coverPos = canvasPaint.canvas.getBoundingClientRect(); canvasPaint.mouseX = event.clientX - coverPos.left; canvasPaint.mouseY = event.clientY - coverPos.top; if (canvasPaint.canPaint) {//后续为拖动画布功能设置的状态 canvasPaint.ctx.lineTo(//使用lineTo将移动过的坐标绘制成线 canvasPaint.mouseX, canvasPaint.mouseY ); canvasPaint.ctx.stroke();//绘制 } } function endEventHandler(event) { event.preventDefault(); //抬起手指时取消move和end事件的监听 canvasPaint.canvas.removeEventListener('touchmove', moveEventHandler, false); canvasPaint.canvas.removeEventListener('touchend', endEventHandler, false); }
canvas--清除屏幕功能
这个功能比较简单就一句话
function clearCanvas() { canvasPaint.ctx.clearRect(0, 0, canvasPaint.canvas.width, canvasPaint.canvas.height); }
提交签名功能
首先需要将画布上的文字转换为img对象,然后使用drawImage绘制到协议上去
preLoadImg(['/assets/index/images/agree.jpg', canvasPaint.canvas.toDataURL()], result); //agree.jpg为协议名,canvasPaint.canvas.toDataURL()就是签好的字转换为base64的结果 function preLoadImg(source, callBack, args) { var pr = []; source.forEach(url => { var p = loadImage(url) .then(function (img) { return img; }) .catch(function (err) { console.log(err); }); pr.push(p); }); Promise.all(pr) .then(function (imgArray) { callBack(imgArray, args); }); } function loadImage(url) { return new Promise((resolve, reject) => { var img = new Image(); img.onload = function () { resolve(img); }; img.onerror = reject; img.src = url; }); }
由于img赋值src是异步的,我们必须要一个完整的image对象,所以我们使用promise包装,使得我们所有图片都转换完之后再将结果传入回调函数(result)中
function result(imgArr) { drawName(imgArr); } function drawName(imgArr) { //绘制名字和底部的名字和日期 canvasPaint.canvas2 = document.getElementById('canvas2'); canvasPaint.context2 = canvasPaint.canvas2.getContext('2d'); canvasPaint.ratio = canvasPaint.canvas.height / canvasPaint.canvas.width; //计算画布比例 canvasPaint.context2.drawImage(imgArr[0], 0, 0, 500, 707);//img0是底图原协议 canvasPaint.context2.save(); canvasPaint.context2.translate(50, 190); canvasPaint.context2.rotate(270 * Math.PI / 180); canvasPaint.context2.drawImage(imgArr[1], 80, 50, 33, 33 * canvasPaint.ratio);//画反转后的名字 canvasPaint.context2.restore(); canvasPaint.context2.save(); canvasPaint.context2.translate(67, 723);//下方的字 canvasPaint.context2.rotate(270 * Math.PI / 180); canvasPaint.context2.drawImage(imgArr[1], 80, 50, 33, 33 * canvasPaint.ratio);//画反转后的名字 canvasPaint.context2.restore(); canvasPaint.context2.save(); canvasPaint.context2.translate(400, 625);//下方的字 canvasPaint.context2.font = "11px 微软雅黑"; canvasPaint.context2.fillStyle = "#000"; canvasPaint.context2.textAlign = "center"; canvasPaint.context2.textBaseline = "middle"; var time = new Date().toLocaleString().split(' ')[0]; canvasPaint.context2.fillText(time, 0, 0); canvasPaint.context2.restore(); prevDrawStatement(); }
这里最主要的还是要理解下画布的rotate和translate方法,就可以把文字旋转任意角度和放到任意位置了
长字手写--画布拖动
上面签字完成后,我们其实已经用了另一个canvas合成了文字和原协议,现在我们要做无限拖动功能,其实也很简单。
在此之前我们需要清空之前的画布