fix(Signature): fix checking if canvas is empty when backgoundColor is set (#12304)

* fix(Signature): fix checking if canvas is empty when set background color

* fix(Signature): delete console and uncomment code

* chore(Signature): reuse setCanvasBgColor function

---------

Co-authored-by: Junyu Zhou <junyu@vivino.cn.com>
This commit is contained in:
Junyu Zhou 2023-09-23 17:51:14 +08:00 committed by GitHub
parent 4abc67e4ac
commit 8eac20b9cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,13 +97,19 @@ export default defineComponent({
const empty = document.createElement('canvas'); const empty = document.createElement('canvas');
empty.width = canvas.width; empty.width = canvas.width;
empty.height = canvas.height; empty.height = canvas.height;
if (props.backgroundColor) {
const emptyCtx = empty.getContext('2d');
setCanvasBgColor(emptyCtx);
}
return canvas.toDataURL() === empty.toDataURL(); return canvas.toDataURL() === empty.toDataURL();
}; };
const setCanvasBgColor = () => { const setCanvasBgColor = (
if (state.ctx && props.backgroundColor) { ctx: CanvasRenderingContext2D | null | undefined,
state.ctx.fillStyle = props.backgroundColor; ) => {
state.ctx.fillRect(0, 0, state.width, state.height); if (ctx && props.backgroundColor) {
ctx.fillStyle = props.backgroundColor;
ctx.fillRect(0, 0, state.width, state.height);
} }
}; };
@ -134,7 +140,7 @@ export default defineComponent({
if (state.ctx) { if (state.ctx) {
state.ctx.clearRect(0, 0, state.width, state.height); state.ctx.clearRect(0, 0, state.width, state.height);
state.ctx.closePath(); state.ctx.closePath();
setCanvasBgColor(); setCanvasBgColor(state.ctx);
} }
emit('clear'); emit('clear');
}; };
@ -147,7 +153,7 @@ export default defineComponent({
// ensure canvas is rendered // ensure canvas is rendered
nextTick(() => { nextTick(() => {
setCanvasBgColor(); setCanvasBgColor(state.ctx);
}); });
} }
}); });