Prev Demo
Mouse Events
Next Demo
Write code here
J
|
B
|
A
<!DOCTYPE html> <html> <head> <title> Mouse Events</title> </head> <body> <canvas id="myCanvas" width="600" height="300"></canvas> <script> function writeMessage(canvas, message) { var context = canvas.getContext('2d'); context.clearRect(0, 0, 600, 300); context.font = '20pt Calibri'; context.fillStyle = 'blue'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { var rect = canvas.getBoundingClientRect(); return { x: evt.clientX - rect.left, y: evt.clientY - rect.top }; } var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function (evt) { var mousePos = getMousePos(canvas, evt); var message = 'Mouse Moving: ' + mousePos.x + ',' + mousePos.y; writeMessage(canvas, message); }, false); </script> </body> </html>
Note: We DO NOT save your trial code in our database.
Output