如何在画布中隐藏元素/对象?

时间:2018-11-08 04:30:13

标签: javascript canvas html5-canvas

我想隐藏第二个元素并在选中第二个复选框并取消选中第一个时显示第一个元素。

但是现在当我使用“ c.clearRect(0,0,canvas.width,canvas.height);”时,这两个元素都消失了。

var checkBox = document.getElementById("myCheck");
var checkBox2 = document.getElementById("myCheck2");
var text = document.getElementById("text");
if (myCheck.checked == true){
  c.fillRect(20, 20, 150, 100);
} else {
   c.clearRect(0, 0, canvas.width, canvas.height);
}

if (myCheck2.checked == true) {
  c.fillRect(500, 100, 100, 100);  //I want hide this element**strong text**
} else {
  c.clearRect(0, 0, canvas.width, canvas.height);
}

1 个答案:

答案 0 :(得分:0)

您可以在画框之前致电c.clearRect();

c.clearRect(0, 0, canvas.width, canvas.height);

var checkBox = document.getElementById("myCheck");
var checkBox2 = document.getElementById("myCheck2");
var text = document.getElementById("text");

if (checkBox.checked == true){
  c.fillRect(20, 20, 150, 100);
} else {
   c.clearRect(0, 0, canvas.width, canvas.height);
}

if (checkBox2.checked == true) {
  c.fillRect(500, 100, 100, 100);  //I want delete this element**strong text**
}