0

I am fairly new to javascript and HTML5, so excuse me if it turns out to be a silly mistake

I am drawing 3 canvas programmatically with some text and a rectangle and i want a click event on each one of them, also I need to know which canvas has been clicked, i wrote the following code but doMouseDown function is executed even without click

<canvas id="myCanvas1" width="452" height="80" style="border:0px solid #d3d3d3;"></canvas>
<canvas id="myCanvas2" width="452" height="80" style="border:0px solid #d3d3d3;"></canvas>
<canvas id="myCanvas3" width="452" height="80" style="border:0px solid #d3d3d3;"></canvas>

       <script>
        function init()
        {
            var rect = { w: 300, h: 60 };
            var point = { x: 150, y: 10 };

            for (var i = 1 ; i < 4 ; i++) {
                var canvasStr = "myCanvas" + Number(i);
                var c = document.getElementById(canvasStr);
                var context = c.getContext("2d");

                // text
                context.font = "22pt Arial";
                context.lineWidth = 2;
                context.fillStyle = "#000000";

                var studentStr = "Student " + Number(i);
                context.fillText(studentStr, 5, 50);

                // rectangle
                context.strokeStyle = "black";
                context.strokeRect(point.x, point.y, rect.w, rect.h);

                context.fillStyle = "#00FF00";
                context.fillRect(point.x, point.y, rect.w, rect.h);
                c.addEventListener('mousedown', doMouseDown(canvasStr), false);
            }
        }

        function doMouseDown(canvasStr)
        {
            alert(canvasStr);
        }

        init();
    </script>

How can i fix it and know which canvas has been clicked (canvasStr in this case)

3 Answers 3

1

You can do it this way

c.addEventListener("mousedown", function () {
                    doMouseDown(canvasStr)
                }, false);

and then write in that function

 function doMouseDown(canvasStr) {
             alert(canvasStr);
          }
Sign up to request clarification or add additional context in comments.

Comments

1

if you're attaching listeners in a loop, you have to create a closure, otherwise canvasStr will allways be == myCanvas3:

(function(str) {
    c.addEventListener('mousedown', doMouseDown.bind(str), false);
}(canvasStr));

Then your callback should be:

function doMouseDown(e, canvasStr) {
    alert(canvasStr);
} 

First argument e is the Event object which is always passed to callbacks.

ADDED There is no point in doing this:

var canvasStr = "myCanvas" + Number(i);

i is already a number and you are adding it to a string. Just write:

var canvasStr = "myCanvas" + i;

Comments

1

Another way is to wrap all 3 canvases in a container div and listen for events on the container

http://jsfiddle.net/m1erickson/g7fsS/

<div id="container">
    <canvas id="myCanvas1" width="45" height="80"></canvas>
    <canvas id="myCanvas2" width="45" height="80"></canvas>
    <canvas id="myCanvas3" width="45" height="80"></canvas>
</div>           


document.getElementById("container").addEventListener("click",function(e){
    console.log(e.target.id);
},false);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.