简体   繁体   中英

Clear Canvas not working

I have a basic page with a canvas. In Javascript I run a function after pressing a button that changes the background color and draws a basic rect shape.

I have a second button though that uses another JS function in order to clean the canvas but it doesn't work.

function clear() {
  var c = document.getElementById("can1");
  var ctx = c.getContext("2d");

  ctx.clearRect(0, 0, c.width, c.height);
  c.style.backgroundColor = white;
}

I have also a codepen-link so that you can see the whole code. Codepen page

Thank you in advance.

Renaming the function will do the trick.

When you are calling to clear() function in the onclick event, you will be executing document.clear() .

You can take a look here for more answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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