Why isn't clearRect working in the following code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Why isn't clearRect working in the following code?

All things are going well, except when I press the clear button the canvas doesn't get cleared

23rd Mar 2017, 5:37 AM
Prabhakar Dev
Prabhakar Dev - avatar
4 Answers
+ 7
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <button onclick="clear()">CLEAR</button><br> <canvas id="c" height="3000px" width="3000px"style="border:5px solid #D3D3D3;" onclick="draw(event)"></canvas> <script type="text/javascript"> var c=document.getElementById("c") var ct=c.getContext("2d") ct.beginPath() ct.moveTo(40,40) function draw(event){ var x=event.clientX var y=event.clientY ct.lineTo(x,y) ct.stroke() ct.strokeStyle='red' ct.moveTo(x,y) } function clear(){ ct.clearRect(0,0,c.width,c.height) ct.moveTo(40,40) } </script> </body> </html>
23rd Mar 2017, 5:37 AM
Prabhakar Dev
Prabhakar Dev - avatar
+ 4
it still doesn't work
25th Mar 2017, 8:08 AM
Prabhakar Dev
Prabhakar Dev - avatar
0
Try adding a couple spaces (tabs)
25th Mar 2017, 1:32 AM
Blake Bates
Blake Bates - avatar
0
Because the function name clear is in confict with some internal functions. You have to change the function name to make it work.
21st Jul 2019, 9:15 AM
moya
moya  - avatar