1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| <template> <div class="container"> <div class="title"> <span>请手写输入您的签名</span> <div v-on:click="handleReset" class="reset">重置</div> </div> <canvas id="canvas" width="570" height="240" v-on:mousedown="handleMouseDown" v-on:mouseup="handleMouseUp" v-on:mousemove="handleMouseMove" v-on:touchstart="handleTouchStart" v-on:touchmove="handleTouchMove" v-on:touchend="handleTouchEnd" >抱歉,您的浏览器不支持电子签名,请更换或升级当前浏览器后重试!</canvas> </div> </template> <script> export default { name: 'Signature', props: { onSign: { type: Function, default: () => {}, }, onSaveImage: { type: Function, default: () => {}, }, }, data() { return { mouse: { current: { x: 0, y: 0, }, down: false, }, }; }, computed: { currentMouse() { const c = document.getElementById('canvas'); const rect = c.getBoundingClientRect();
return { x: this.mouse.current.x - rect.left, y: this.mouse.current.y - rect.top, }; }, }, methods: { draw() { if (this.mouse.down) { const c = document.getElementById('canvas');
const ctx = c.getContext('2d');
ctx.clearRect(0, 0, c.width, c.height);
ctx.lineTo(this.currentMouse.x, this.currentMouse.y); // 颜色 ctx.strokeStyle = '#000'; // 宽度 ctx.lineWidth = 2; ctx.stroke(); } }, handleMouseDown(event) { this.mouse.down = true; this.mouse.current = { x: event.pageX, y: event.pageY, };
const c = document.getElementById('canvas'); const ctx = c.getContext('2d');
ctx.moveTo(this.currentMouse.x, this.currentMouse.y); }, handleMouseUp() { this.mouse.down = false; }, handleMouseMove(event) { this.mouse.current = { x: event.pageX, y: event.pageY, };
this.draw(event); }, handleTouchStart(event) { const canvas = document.getElementById('canvas'); const touch = event.touches[0]; const me = new MouseEvent('mousedown', { clientX: touch.clientX, clientY: touch.clientY, }); canvas.dispatchEvent(me); }, handleTouchMove(event) { const canvas = document.getElementById('canvas'); const touch = event.touches[0]; const me = new MouseEvent('mousemove', { clientX: touch.clientX, clientY: touch.clientY, }); canvas.dispatchEvent(me); }, handleTouchEnd() { const canvas = document.getElementById('canvas'); const me = new MouseEvent('mouseup', {}); canvas.dispatchEvent(me); }, handleDownload() { const canvasElement = document.getElementById('canvas');
const MIME_TYPE = 'image/png';
const imgURL = canvasElement.toDataURL(MIME_TYPE); // 创建a标签模拟点击触发浏览器下载机制 const dlLink = document.createElement('a'); dlLink.download = +new Date(); dlLink.href = imgURL; dlLink.dataset.downloadurl = [MIME_TYPE, dlLink.download, dlLink.href].join(':');
document.body.appendChild(dlLink); dlLink.click(); document.body.removeChild(dlLink); }, handleReset() { const canvas = document.getElementById('canvas'); // 重置canvas画布 canvas.width = canvas.width; canvas.height = canvas.height; this.mouse.current = { x: 0, y: 0, }; }, }, mounted() { const c = document.getElementById('canvas'); const ctx = c.getContext('2d'); ctx.translate(0.5, 0.5); ctx.imageSmoothingEnabled = false; this.draw(); }, }; </script> <style scoped lang="less"> .container { border: 0.01rem solid #E4E4E4; background: #fff; border-radius: .04rem; padding: .3rem; .title { display: flex; justify-content: space-between; align-items: center; .reset { width: 1.26rem; height: 0.6rem; line-height: 0.6rem; text-align: center; border: 0.01rem solid #F8875F; color: #F8875F; border-radius: 0.04rem; } } } </style>
|