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
| let content = null let touchs = []; let canvasw = 0; let canvash = 0;
export default {
onLoad() { content = uni.createCanvasContext('firstCanvas') content.setStrokeStyle("#000") content.setLineWidth(5) content.setLineCap('round') content.setLineJoin('round') }, methods: { start: function(event) { let point = { x: event.changedTouches[0].x, y: event.changedTouches[0].y } touchs.push(point);
}, move: function(e) { let point = { x: e.touches[0].x, y: e.touches[0].y } touchs.push(point) if (touchs.length >= 2) { this.draw(touchs) } },
end: function(e) { console.log("触摸结束" + e) this.isEnd = true for (let i = 0; i < touchs.length; i++) { touchs.pop() }
},
cancel: function(e) { console.log("触摸取消" + e) },
tap: function(e) { console.log("长按手势" + e) },
error: function(e) { console.log("画布触摸错误" + e) },
draw: function(touchs) { let point1 = touchs[0] let point2 = touchs[1] touchs.shift() content.moveTo(point1.x, point1.y) content.lineTo(point2.x, point2.y) content.stroke() content.draw(true) }, clearClick: function() { this.isEnd = false content.clearRect(0, 0, canvasw, canvash) content.draw(true) }, overSign() { uni.canvasToTempFilePath({ canvasId: 'firstCanvas', success: function(res) { const srcUrl = res.tempFilePath } }) } } }
|