I made some code that wouldn’t work. An error popped up in the Chrome console saying “Syntax error: Unexpected identifier”. Error is on line 19. Here is the code:
13. var canvas=document.getElementById("canvas") 14. var ctx=canvas.getContext("2d") 15. function getMousePos(canvas,evt){ 16. var rect=canvas.getBindingClientRect() 17. return{ 18. x:evt.clientX-rect.left 19. y:evt.clientY-rect.top 20. } 21. } 22. canvas.addEventListener("mouseclick",function(evt){ 23. var mousePos=getMousePos(canvas,evt) 24. ctx.fillRect(mousePos.x-15,mousePos.y-15,10,10) 25. },false)
Advertisement
Answer
from line 17: add a comma on line 18 as @thg435 mentioned
return { x:evt.clientX-rect.left, y:evt.clientY-rect.top }