清除画布会导致旧内容在下次绘制时重新出现
我有一个清晰的按钮,似乎可以工作......但实际上它不能。当我返回画布时,我可以看到绘图。
I have a clear button which seems to work..but actually its not. When I click back on the canvas I can see the drawing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你清理画布的方式没有问题,问题在于你的逻辑。
每次您
单击
或移动鼠标时,都会调用addClick
方法,该方法会填充数组clickX
、clickY
和< code>clickDrag 与坐标;然后调用redraw
方法,在canvas
上绘制点。因此,当您清除画布时,您无法重置这些数组,因此当再次调用重绘方法时(清除画布后),旧点也会被绘制。
因此,当您清除
canvas
时,请尝试清空数组clickX
、clickY
和clickDrag
,如下所示;这样当你重新开始时就不会考虑旧的观点。There is no problem in the way you are clearing the canvas, the problem lies in your logic.
Everytime you
click
or move the mouse theaddClick
method is called, which populates the arraysclickX
,clickY
andclickDrag
with the coordinates; and then theredraw
method is called which plots the points on thecanvas
.So when you clear the
canvas
you are failing to reset these arrays, so when theredraw
method is called again (after clearing the canvas), the old points get plotted too.So try emptying the arrays
clickX
,clickY
andclickDrag
as shown below, when you clear yourcanvas
; so that the old points are not considered when you start fresh.