简体   繁体   English

仅清除html5画布中的矩形(我的代码清除所有内容)

[英]Clear only the rectangles in the html5 canvas (my code clear everything)

Here my jsfiddle code: https://jsfiddle.net/6u7bLkwc/2/ 这是我的jsfiddle代码: https ://jsfiddle.net/6u7bLkwc/2/

When i click on Clear IT button, i want to remove only the rectangles in the canvas, image must still there ... and my second problem is that when it clear everything, after that i can't make again a rectangles into the canvas. 当我单击“清除IT”按钮时,我只想删除画布中的矩形,图像必须仍然存在...而我的第二个问题是,当它清除所有内容时,此后我无法再次在画布中制作一个矩形。

And here the code that erase everything: 这是擦除所有内容的代码:

function clearIt() {
ctx.globalCompositeOperation = "destination-out";
}

Note: Click and drag your mouse on the image in order to create rectangles. 注意:在图像上单击并拖动鼠标以创建矩形。

Replace 更换

function clearIt() {
    ctx.globalCompositeOperation = "destination-out";
}

with: 有:

function clearIt() {
    shapes = [];
    draw();
}

Just assign shapes to be an empty array after you press the button. 按下按钮后,只需将shapes分配为空数组即可。

function clearIt() {
  shapes = []
}

Updated fiddle: 更新的小提琴:

https://jsfiddle.net/6u7bLkwc/3/ https://jsfiddle.net/6u7bLkwc/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM