这个JS代码在控制台中执行时工作正常,但公共方法clean()
不会消除画布上的所有内容。
代码有其他方法,但失败的是清理。
ctx
有一个值,所以它不是null。
如何修复以便清理画布?感谢
signRect = function() {
let tr_sign = null;
let ctx = null;
let canvas_sign = null;
const makeCanvas = () => {
if (canvas_sign) return canvas_sign;
canvas_sign = document.createElement('canvas');
canvas_sign.position = 'absolute';
canvas_sign.width = 300;
canvas_sign.height = 170;
canvas_sign.id = 'signature';
canvas_sign.style.border = '1px solid black';
canvas_sign.style.top = '-75px';
canvas_sign.style.left = '-50px';
canvas_sign.style.opacity = '0.5';
canvas_sign.style.position = 'absolute';
ctx = canvas_sign.getContext('2d');
ctx.fillStyle = "#000";
ctx.lineWidth = 1;
return canvas_sign;
};
return {
clean: () => {
ctx.clearRect(0, 0, canvas_sign.width, canvas_sign.height);
ctx.beginPath();
}
}
};