简体   繁体   中英

Javascript Clear Canvas

I have a button to clear the drawings of my canvas element as follows:

<button id="button_clear" onclick="clear()" > Clear ! </button>

<canvas id="myCanvas" width="300" height="300" style="border:1px solid #d3d3d3;">

Clear function is like that :

function clear(){

  var c = document.getElementById("myCanvas");
  var ctx = c.getContext("2d");
  ctx.clearRect(0, 0, c.width, c.height);
  ctx.beginPath();

}

However when I click on the clear button, my function does not do anything at all.

Try to avoid using function names something like click() , clear() , ... Change your function name and check if it works. Check this answer for more detail information https://stackoverflow.com/a/5454341/5344661

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