|
|
@@ -14,6 +14,14 @@
|
|
|
用法:
|
|
|
<hand-signature @confirm="onSign" @clear="onClear" />
|
|
|
onSign(url) { this.signatureDataUrl = url }
|
|
|
+
|
|
|
+ 实现方式(v2 · signature.canvas2d.rewrite):
|
|
|
+ - 改用新版 canvas `type="2d"` + `getContext('2d')` + `fields({node:true})` 接口(参照 RadarChart.vue 标准写法)。
|
|
|
+ - 触摸坐标优先取微信 2d 提供的相对画布坐标 `touches[0].x/y`,不再依赖一次性缓存的 rect
|
|
|
+ (旧实现 rect 只在 mounted 取一次,页面/布局变化后坐标偏移导致「字迹显示不出来」)。
|
|
|
+ - 绘制为同步 `ctx.stroke()`,无需异步 `ctx.draw()`,笔迹实时可见。
|
|
|
+ - 导出用 `uni.canvasToTempFilePath({ canvas: this._canvasNode, ... })` 传 2d canvas 节点。
|
|
|
+ - 预填签名仍走 image 层叠加回显,不画进 2d canvas(hasDrawn 语义沿用:已手绘则盖住预填)。
|
|
|
-->
|
|
|
<template>
|
|
|
<view class="hand-signature">
|
|
|
@@ -26,7 +34,8 @@
|
|
|
</view>
|
|
|
<view class="signature-canvas-wrap">
|
|
|
<canvas
|
|
|
- canvas-id="hsCanvas"
|
|
|
+ :id="canvasId"
|
|
|
+ type="2d"
|
|
|
class="signature-canvas"
|
|
|
:style="'width:100%;height:' + height + 'px;'"
|
|
|
@touchstart.stop.prevent="onTouchStart"
|
|
|
@@ -85,6 +94,21 @@ export default {
|
|
|
},
|
|
|
data: function() {
|
|
|
return {
|
|
|
+ // 2d canvas 节点(导出用,canvasToTempFilePath 传 canvas 而非 canvasId)
|
|
|
+ _canvasNode: null,
|
|
|
+ // canvas id 唯一化(参照 RadarChart:this._uid 保证多实例唯一)
|
|
|
+ canvasId: 'hsCanvas_' + (this._uid || 0),
|
|
|
+ // 实际绘制尺寸与像素比(fields size + dpr)
|
|
|
+ canvasWidth: 0,
|
|
|
+ canvasHeight: 0,
|
|
|
+ dpr: 1,
|
|
|
+ // 画布左上角(fields rect 返回),触摸坐标兜底用
|
|
|
+ _rectLeft: 0,
|
|
|
+ _rectTop: 0,
|
|
|
+ // 组件已销毁标志:mounted 的延迟初始化与重试链需据此停止
|
|
|
+ _destroyed: false,
|
|
|
+ // mounted 延迟初始化定时器引用(beforeDestroy 时清理)
|
|
|
+ _initTimer: null,
|
|
|
ctx: null,
|
|
|
drawing: false,
|
|
|
lastX: 0,
|
|
|
@@ -93,8 +117,7 @@ export default {
|
|
|
hasDrawn: false,
|
|
|
agreed: false,
|
|
|
currentDataUrl: this.value || '',
|
|
|
- savingLive: false,
|
|
|
- rect: null
|
|
|
+ savingLive: false
|
|
|
}
|
|
|
},
|
|
|
watch: {
|
|
|
@@ -108,49 +131,121 @@ export default {
|
|
|
},
|
|
|
mounted: function() {
|
|
|
var self = this
|
|
|
- this.ctx = uni.createCanvasContext('hsCanvas', this)
|
|
|
- if (!this.ctx) {
|
|
|
- this.ctx = uni.createCanvasContext('hsCanvas')
|
|
|
+ // 等 DOM 布局完成后初始化 Canvas(带重试,应对微信小程序首次渲染 Canvas 节点延迟;参照 RadarChart)
|
|
|
+ var retryCount = 0
|
|
|
+ var maxRetry = 5
|
|
|
+ function tryInit() {
|
|
|
+ if (self._destroyed) return
|
|
|
+ self.initCanvas(function(success) {
|
|
|
+ if (self._destroyed) return
|
|
|
+ if (!success && retryCount < maxRetry) {
|
|
|
+ retryCount++
|
|
|
+ self._initTimer = setTimeout(tryInit, 60)
|
|
|
+ } else if (!success) {
|
|
|
+ console.log('[HandSignature] 初始化失败,重试 ' + maxRetry + ' 次后放弃', self.canvasId)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ self._initTimer = setTimeout(tryInit, 100)
|
|
|
+ },
|
|
|
+ beforeDestroy: function() {
|
|
|
+ this._destroyed = true
|
|
|
+ if (this._initTimer) {
|
|
|
+ clearTimeout(this._initTimer)
|
|
|
+ this._initTimer = null
|
|
|
}
|
|
|
- // 获取画布实际位置与尺寸(导出/坐标换算用);canvas 可能在 nextTick 后才有布局,失败重试一次
|
|
|
- this.loadRect()
|
|
|
- this.$nextTick(function() {
|
|
|
- if (!self.rect) {
|
|
|
- setTimeout(function() { self.loadRect() }, 300)
|
|
|
- }
|
|
|
- })
|
|
|
},
|
|
|
methods: {
|
|
|
- loadRect: function() {
|
|
|
+ /* ===========================
|
|
|
+ * Canvas 初始化(新版 type=2d 接口)
|
|
|
+ * =========================== */
|
|
|
+ initCanvas: function(cb) {
|
|
|
var self = this
|
|
|
- try {
|
|
|
- // 与全站组件一致:用 .in(this) 将选择器限定到本组件作用域(组件内 canvas 必须带组件实例)
|
|
|
- var query = uni.createSelectorQuery().in(this)
|
|
|
- query.select('.signature-canvas').boundingClientRect(function(rect) {
|
|
|
- if (rect) {
|
|
|
- self.rect = rect
|
|
|
- // 有预存签名时清空画布(用空白覆盖),回显交给 image 层
|
|
|
- if (self.currentDataUrl && !self.hasContent) {
|
|
|
- var ctx = self.ctx
|
|
|
- if (ctx) {
|
|
|
- ctx.setFillStyle('#ffffff')
|
|
|
- ctx.fillRect(0, 0, rect.width, rect.height)
|
|
|
- ctx.draw(true)
|
|
|
- }
|
|
|
+ uni.createSelectorQuery().in(this)
|
|
|
+ .select('#' + this.canvasId)
|
|
|
+ .fields({ node: true, size: true, rect: true })
|
|
|
+ .exec(function(res) {
|
|
|
+ // 兜底:回调内任何异常都必须走 cb(false),否则 mounted 的重试机制永远不触发 → 永久空白且无日志
|
|
|
+ try {
|
|
|
+ if (self._destroyed) return
|
|
|
+ if (!res || !res[0]) {
|
|
|
+ console.log('[HandSignature] 查询失败: 未找到 canvas 节点', self.canvasId)
|
|
|
+ if (cb) cb(false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var info = res[0]
|
|
|
+ if (!info.node) {
|
|
|
+ console.log('[HandSignature] node 为空: canvas type=2d 节点获取失败', self.canvasId)
|
|
|
+ if (cb) cb(false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var canvas = info.node
|
|
|
+ var width = info.width
|
|
|
+ var height = info.height
|
|
|
+ if (!width || !height) {
|
|
|
+ // 兜底:fields size 为 0(flex 塌陷等)时按窗口宽度换算(画布宽 100%)
|
|
|
+ console.log('[HandSignature] 查询尺寸为 0,改用窗口宽度兜底换算', self.canvasId)
|
|
|
+ var winWidth = 375
|
|
|
+ try {
|
|
|
+ var winInfo = uni.getWindowInfo()
|
|
|
+ winWidth = winInfo.windowWidth || 375
|
|
|
+ } catch (e2) {}
|
|
|
+ width = Math.round(winWidth)
|
|
|
+ height = Math.round(self.height || 200)
|
|
|
+ console.log('[HandSignature] 兜底尺寸: ' + width + 'x' + height, self.canvasId)
|
|
|
}
|
|
|
+
|
|
|
+ var pixelRatio = 2
|
|
|
+ try {
|
|
|
+ var system = uni.getWindowInfo()
|
|
|
+ pixelRatio = system.pixelRatio || 2
|
|
|
+ } catch (e) {
|
|
|
+ console.log('[HandSignature] uni.getWindowInfo() 不可用,使用默认 pixelRatio=2', e)
|
|
|
+ }
|
|
|
+
|
|
|
+ canvas.width = width * pixelRatio
|
|
|
+ canvas.height = height * pixelRatio
|
|
|
+
|
|
|
+ var ctx = canvas.getContext('2d')
|
|
|
+ if (!ctx) {
|
|
|
+ console.log('[HandSignature] canvas.getContext(2d) 返回 null', self.canvasId)
|
|
|
+ if (cb) cb(false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ctx.scale(pixelRatio, pixelRatio)
|
|
|
+
|
|
|
+ self.ctx = ctx
|
|
|
+ self._canvasNode = canvas
|
|
|
+ self.canvasWidth = width
|
|
|
+ self.canvasHeight = height
|
|
|
+ self.dpr = pixelRatio
|
|
|
+ self._rectLeft = info.left || 0
|
|
|
+ self._rectTop = info.top || 0
|
|
|
+
|
|
|
+ console.log('[HandSignature] 初始化成功', 'size=' + width + 'x' + height, 'canvasId=' + self.canvasId, 'pixelRatio=' + pixelRatio)
|
|
|
+
|
|
|
+ if (cb) cb(true)
|
|
|
+ } catch (e) {
|
|
|
+ console.warn('[HandSignature] 初始化异常:', e)
|
|
|
+ if (cb) cb(false)
|
|
|
}
|
|
|
- }).exec()
|
|
|
- } catch (e) {}
|
|
|
+ })
|
|
|
},
|
|
|
+ /* ===========================
|
|
|
+ * 触摸坐标(2d 相对画布,不依赖旧 getPos 缓存 rect)
|
|
|
+ * =========================== */
|
|
|
getPos: function(e) {
|
|
|
- var touch = e.touches && e.touches[0]
|
|
|
- var cx = touch ? touch.clientX : (e.clientX || 0)
|
|
|
- var cy = touch ? touch.clientY : (e.clientY || 0)
|
|
|
- var rect = this.rect
|
|
|
- if (rect) {
|
|
|
- return { x: cx - rect.left, y: cy - rect.top }
|
|
|
+ // 优先:type=2d 时微信触摸事件直接提供相对画布的 x/y(不依赖缓存 rect,坐标不会偏移)
|
|
|
+ var t = e.touches && e.touches[0]
|
|
|
+ var x = (t && t.x) || 0
|
|
|
+ var y = (t && t.y) || 0
|
|
|
+ // 兜底:t.x 未定义(如 H5 旧事件)时用 clientX/clientY - 画布左上角(fields rect 实时返回)
|
|
|
+ if (t && t.x === undefined) {
|
|
|
+ x = (t.clientX || 0) - (this._rectLeft || 0)
|
|
|
+ y = (t.clientY || 0) - (this._rectTop || 0)
|
|
|
}
|
|
|
- return { x: cx, y: cy }
|
|
|
+ return { x: x, y: y }
|
|
|
},
|
|
|
onTouchStart: function(e) {
|
|
|
var pos = this.getPos(e)
|
|
|
@@ -163,15 +258,15 @@ export default {
|
|
|
var pos = this.getPos(e)
|
|
|
var ctx = this.ctx
|
|
|
if (!ctx) return
|
|
|
+ // 同步绘制(2d 接口直接 stroke,无需异步提交重绘)
|
|
|
ctx.beginPath()
|
|
|
ctx.moveTo(this.lastX, this.lastY)
|
|
|
ctx.lineTo(pos.x, pos.y)
|
|
|
- ctx.setStrokeStyle(this.strokeColor)
|
|
|
- ctx.setLineWidth(this.strokeWidth)
|
|
|
- ctx.setLineCap('round')
|
|
|
- ctx.setLineJoin('round')
|
|
|
+ ctx.strokeStyle = this.strokeColor
|
|
|
+ ctx.lineWidth = this.strokeWidth
|
|
|
+ ctx.lineCap = 'round'
|
|
|
+ ctx.lineJoin = 'round'
|
|
|
ctx.stroke()
|
|
|
- ctx.draw(false)
|
|
|
this.lastX = pos.x
|
|
|
this.lastY = pos.y
|
|
|
this.hasContent = true
|
|
|
@@ -198,13 +293,11 @@ export default {
|
|
|
return
|
|
|
}
|
|
|
this.savingLive = true
|
|
|
- var rect = this.rect || {}
|
|
|
- var w = rect.width || 300
|
|
|
- var h = rect.height || this.height
|
|
|
- // 注意第二个参数 this:canvas 位于自定义组件内,必须传入组件实例,
|
|
|
- // 否则微信小程序按页面级 canvas-id 查找不到组件内 canvas,导出必然失败(签名「不好用」的根因之一)。
|
|
|
+ var w = this.canvasWidth || 300
|
|
|
+ var h = this.canvasHeight || this.height
|
|
|
+ // 2d 接口:传 canvas 节点(不再传 canvasId),第二个参数 this 仍指向组件实例
|
|
|
uni.canvasToTempFilePath({
|
|
|
- canvasId: 'hsCanvas',
|
|
|
+ canvas: this._canvasNode,
|
|
|
x: 0,
|
|
|
y: 0,
|
|
|
width: w,
|
|
|
@@ -258,13 +351,11 @@ export default {
|
|
|
this.savingLive = false
|
|
|
},
|
|
|
handleClear: function() {
|
|
|
- var self = this
|
|
|
if (this.savingLive) return
|
|
|
var ctx = this.ctx
|
|
|
if (ctx) {
|
|
|
- var rect = this.rect || {}
|
|
|
- ctx.clearRect(0, 0, rect.width || 600, rect.height || this.height)
|
|
|
- ctx.draw(true)
|
|
|
+ // 同步清空(2d 接口无需 ctx.draw())
|
|
|
+ ctx.clearRect(0, 0, this.canvasWidth || 600, this.canvasHeight || this.height)
|
|
|
}
|
|
|
this.hasContent = false
|
|
|
this.hasDrawn = false
|
|
|
@@ -365,4 +456,4 @@ export default {
|
|
|
.btn-sign-confirm[disabled] {
|
|
|
opacity: 0.5;
|
|
|
}
|
|
|
-</style>
|
|
|
+</style>
|