Skip to content

Commit 2e67adf

Browse files
authored
fix: 🐛 修复 signature 组件设置background-color为透明色导致撤销无效 (#1224)
Closes: #1223
1 parent 00ab76b commit 2e67adf

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

src/uni_modules/wot-design-uni/components/wd-signature/wd-signature.vue

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,45 @@ const canvasState = reactive({
9191
ctx: null as UniApp.CanvasContext | null // canvas上下文
9292
})
9393
94+
/**
95+
* 判断颜色是否为透明色
96+
* @param color 颜色值(支持 rgba/hsla/hex/transparent)
97+
*/
98+
function isTransparentColor(color: string | undefined): boolean {
99+
if (!color) return true
100+
const transparentKeywords = ['transparent', '#0000', '#00000000']
101+
102+
// 标准透明关键字
103+
if (transparentKeywords.includes(color.toLowerCase())) {
104+
return true
105+
}
106+
107+
// 匹配 rgba(r, g, b, a)
108+
const rgbaMatch = color.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*(\d*\.?\d+))?\)$/i)
109+
if (rgbaMatch) {
110+
const alpha = rgbaMatch[4] ? parseFloat(rgbaMatch[4]) : 1
111+
return alpha === 0
112+
}
113+
114+
// 匹配 hsla(h, s, l, a)
115+
const hslaMatch = color.match(/^hsla?\(\s*(\d+)(?:deg)?\s*,\s*(\d+)%\s*,\s*(\d+)%(?:\s*,\s*(\d*\.?\d+))?\)$/i)
116+
if (hslaMatch) {
117+
const alpha = hslaMatch[4] ? parseFloat(hslaMatch[4]) : 1
118+
return alpha === 0
119+
}
120+
121+
// 匹配 #RRGGBBAA 或 #RGBA
122+
const hexMatch = color.match(/^#([0-9a-f]{8}|[0-9a-f]{4})$/i)
123+
if (hexMatch) {
124+
const hex = hexMatch[1]
125+
const alphaHex = hex.length === 8 ? hex.slice(6, 8) : hex.slice(3, 4).repeat(2)
126+
const alpha = parseInt(alphaHex, 16)
127+
return alpha === 0
128+
}
129+
130+
return false
131+
}
132+
94133
watch(
95134
() => props.penColor,
96135
() => {
@@ -302,8 +341,8 @@ const redrawCanvas = () => {
302341
if (!ctx) return
303342
304343
// 清除画布并设置背景
305-
if (isDef(props.backgroundColor)) {
306-
ctx.setFillStyle(props.backgroundColor)
344+
if (!isTransparentColor(props.backgroundColor)) {
345+
ctx.setFillStyle(props.backgroundColor as string)
307346
ctx.fillRect(0, 0, canvasState.canvasWidth, canvasState.canvasHeight)
308347
} else {
309348
ctx.clearRect(0, 0, canvasState.canvasWidth, canvasState.canvasHeight)
@@ -570,8 +609,8 @@ function clearCanvas() {
570609
const { canvasWidth, canvasHeight, ctx } = canvasState
571610
if (ctx) {
572611
ctx.clearRect(0, 0, canvasWidth, canvasHeight)
573-
if (isDef(props.backgroundColor)) {
574-
ctx.setFillStyle(props.backgroundColor)
612+
if (!isTransparentColor(props.backgroundColor)) {
613+
ctx.setFillStyle(props.backgroundColor as string)
575614
ctx.fillRect(0, 0, canvasWidth, canvasHeight)
576615
}
577616
ctx.draw()

0 commit comments

Comments
 (0)