I currently have two circles in the <canvas> with HTML5 and JavaScript.
Now I'm trying to add an image (made) that changes depending on the mouse and click.
Basically this is an implementation of the play / pause button with an additional color change when the user clicks the mouse button.
I canβt understand how events work on shapes in HTML5, since they are not objects ... Here is my code at the moment:
window.onload = function() { var canvas = document.getElementsByTagName('canvas')[0]; var context = canvas.getContext('2d'); var centerX = canvas.width / 2; var centerY = canvas.height / 2; //Outer circle context.beginPath(); context.arc(centerX, centerY, 150, 0, Math.PI * 2, false); context.fillStyle = "#000"; context.fill(); context.stroke(); //Inner cicle context.beginPath(); context.arc(centerX, centerY, 75, 0, Math.PI * 2, false); context.fillStyle = "#fff"; context.fill(); context.stroke(); //Play / Pause button var imagePlay = new Image(); var imageHeight = 48/2; imagePlay.onload = function() { context.drawImage(imagePlay, centerX - imageHeight, centerY - imageHeight); }; imagePlay.src = "images/play.gif"; }
1) How to handle events on shapes created using <canvas> ?
2) How to clear / delete images on <canvas> when replacing it with another?
Thanks in advance!
javascript html5 javascript-events canvas
m_vdbeek
source share