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
| import fmtEvent from "mini-ali-ui-rpx/es/_util/fmtEvent"; const noop = function noop() {}; Component({ mixins: [], data: { ctx: null, points: [], signImage: '' }, props: { show:false, animation:true, zIndex:100, title:'手写签名', cancelButtonText:'取消', confirmButtonText:'完成', onCancel:noop, onConfirm:noop, width: 300, height: 225, dpr: 2 }, didMount() { this.data.ctx = dd.createCanvasContext('sign'); this.data.ctx.setStrokeStyle("#000"); this.data.ctx.setLineWidth(3); this.data.ctx.setLineCap('round'); this.data.ctx.setLineJoin('round'); this.data.ctx.scale(this.props.dpr,this.props.dpr) this.data.ctx.save() }, didUpdate() {}, didUnmount() {}, methods: { start (e) { console.log(e) let point = { x: e.changedTouches[0].x, y: e.changedTouches[0].y } this.$spliceData({points: [0, 0 ,point ]}) }, move (e) { let point = { x: e.touches[0].x, y: e.touches[0].y } this.$spliceData({points: [this.data.points.length, 0 ,point ]}) if (this.data.points.length >= 2) { this.draw(); } }, end (e) { this.setData({ points: [] }) this.data.ctx.save() }, cancel (e) { console.log("触摸取消" + e); }, tap(e) { console.log("长按手势" , e); }, error(e) { console.log("画布触摸错误" + e); }, draw() { let point1 = this.data.points[0]; let point2 = this.data.points[1]; this.$spliceData({points: [0, 1 ]}) this.data.ctx.moveTo(point1.x, point1.y); this.data.ctx.lineTo(point2.x, point2.y); this.data.ctx.stroke(); this.data.ctx.draw(); }, clearClick() { this.data.ctx.save(); this.data.ctx.beginPath() this.data.ctx.clearRect(0, 0,this.props.width, this.props.height);
this.data.ctx.draw(); }, saveClick() { const that = this; this.data.ctx.toTempFilePath({ success(res) { console.log(res) that.setData({ signImage: res.filePath }) }, }); }, onCancel(e){ const event = fmtEvent(this.props, e); this.props.onCancel(event); }, onConfirm(e){ this.saveClick() e.detail.value = this.data.signImage const event = fmtEvent(this.props, e); this.props.onConfirm(event); }, }, });
|