Bind event to shape on canvas

Posted by Ben Shelock on Stack Overflow See other posts from Stack Overflow or by Ben Shelock
Published on 2010-05-09T15:51:07Z Indexed on 2010/05/09 15:58 UTC
Read the original article Hit count: 180

Filed under:
|
|

How can I bind an event to a shape drawn on a canvas? I presumed this would work but I get an error.

<html>
    <head>
    <script type="application/javascript">
    function draw() {   
        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");      
        ctx.fillStyle = "rgb(200,0,0)";
        var rec = ctx.fillRect (0, 0, 55, 50);

        rec.onclick = function(){
            alert('something');
        }
    }
    </script>
    </head>
    <body onLoad="draw()">
        <canvas id="canvas" width="300" height="300"></canvas>
    </body>
</html>

© Stack Overflow or respective owner

Related posts about canvas

Related posts about JavaScript