+ 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
4 Réponses
+ 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>
+ 4
it still doesn't work
0
Try adding a couple spaces (tabs)
0
Because the function name clear is in confict with some internal functions. You have to change the function name to make it work.