I’m an experienced Java programmer but am looking at some JavaScript/HTML5 stuff for the first time in about a decade. I’m completely stumped on what should be the simplest thing ever.
As an example I just wanted to draw something and add an event handler to it. I’m sure I’m doing something stupid, but I’ve searched all over and nothing that is suggested (e.g. the answer to this question: Add onclick property to input with JavaScript) works. I’m using Firefox 10.0.1. My code follows. You’ll see several commented lines and at the end of each is a description of what (or what doesn’t) happen.
What’s the correct syntax here? I’m going crazy!
<html>
<body>
<canvas id="myCanvas" width="300" height="150"/>
<script language="JavaScript">
var elem = document.getElementById('myCanvas');
// elem.onClick = alert("hello world"); - displays alert without clicking
// elem.onClick = alert('hello world'); - displays alert without clicking
// elem.onClick = "alert('hello world!')"; - does nothing, even with clicking
// elem.onClick = function() { alert('hello world!'); }; - does nothing
// elem.onClick = function() { alert("hello world!"); }; - does nothing
var context = elem.getContext('2d');
context.fillStyle = '#05EFFF';
context.fillRect(0, 0, 150, 100);
</script>
</body>
When you draw to a
canvaselement, you are simply drawing a bitmap in immediate mode.The elements (shapes, lines, images) that are drawn have no representation besides the pixels they use and their colour.
Therefore, to get a click event on a
canvaselement (shape), you need to capture click events on thecanvasHTML element and use some math to determine which element was clicked, provided you are storing the elements’ width/height and x/y offset.To add a
clickevent to yourcanvaselement, use…To determine which element was clicked…
jsFiddle.
This code attaches a
clickevent to thecanvaselement, and then pushes one shape (called anelementin my code) to anelementsarray. You could add as many as you wish here.The purpose of creating an array of objects is so we can query their properties later. After all the elements have been pushed onto the array, we loop through and render each one based on their properties.
When the
clickevent is triggered, the code loops through the elements and determines if the click was over any of the elements in theelementsarray. If so, it fires analert(), which could easily be modified to do something such as remove the array item, in which case you’d need a separate render function to update thecanvas.For completeness, why your attempts didn’t work…
This is assigning the return value of
alert()to theonClickproperty ofelem. It is immediately invoking thealert().In JavaScript, the
'and"are semantically identical, the lexer probably uses['"]for quotes.You are assigning a string to the
onClickproperty ofelem.JavaScript is case sensitive. The
onclickproperty is the archaic method of attaching event handlers. It only allows one event to be attached with the property and the event can be lost when serialising the HTML.Again,
' === ".