1

I displayed an image onto the canvas, and I'm trying to add a click function to it so that it fires off an event. However, .click() doesn't seem to work. Is there another function I can use?

jQuery:

function homeScreen() 
{       
    var backImage = new Image();
    var playButton = new Image();
    backImage.onload = function() 
    {       
        context.drawImage(backImage, 0, 0);
        context.drawImage(playButton, 400, 600);                
    };
    backImage.src="images/homeBackground.jpg";
    playButton.src="images/play.png";

    $(playButton).click(function() {
        alert("Hey");
    });
}
3
  • 1
    It's not possible to attach an event listener to a canvas region. Attach it to the canvas element, and pick an action depending on coordinates of the mouse event. Or instead put the buttons as <img> elements on top of the canvas and add click handlers to them. Commented Jan 11, 2014 at 23:46
  • What's a "jQuery image"? Commented Jan 12, 2014 at 0:53
  • 1
    @nnnnnn this is a jQuery image: brand.jquery.org/resources/jquery-mark-dark.gif Commented Jan 12, 2014 at 10:51

3 Answers 3

1

I don't think you can listen for events from images that are drawn on the canvas. So what I think is that you can listen for click events on the document or just on the canvas and then check if the click occured on the area of the canvas occupied by the button.

$('canvas').on('click', function(e) {
  if (e.pageX >= leftEdgeOfButton && e.pageX <= rightEdgeOfButton
        && e.pageY >= topEdgeOfButton && e.pageY <= bottomEdgeOfButton) {
    // handle click event
  }
});

As for the position of the edges of the button, I'm pretty sure you can calculate those values: the left and top edges are probably just the same as the coordinate values you used to define where the image should be drawn, and the right/bottom are just the left or right edges plus the width or height of the button.

If the canvas is not of the same size with the whole screen, you can add the left and top offset of the canvas to the e.pageX and e.pageY to make and adjustment.

$('canvas').on('click', function(e) {
  if (e.pageX + leftOffsetOfCanvas >= leftEdgeOfButton 
        && e.pageX + leftOffsetOfCanvas <= rightEdgeOfButton
        && e.pageY + leftOffsetOfCanvas >= topEdgeOfButton 
        && e.pageY + leftOffsetOfCanvas <= bottomEdgeOfButton) {
    // handle click event
  }
});

You can use jQuery's .offset() function to get the top and left offsets of the canvas element.

Sign up to request clarification or add additional context in comments.

Comments

1

Just do:

playButton.onclick = function() {
    //func stuff
}

2 Comments

An image that is only drawn to a canvas and not added to dom is clickable? While I don't know I can't believe it.
Why would that work if $(playButton).click(... didn't?
0

Use .on() method on dynamically created html

$(playButton).on("click",function() {
        alert("Hey");
    });

4 Comments

$(...).click(callback ) and $(...).on('click', callback) is exactly the same thing.
@t.niese var playButton = new Image(); playButton is dynamically created
The code you have shown with .on() is exactly equivalent to the .click() code shown in the question. Note that .click() works on dynamically added elements if .click() is called after the elements are added. But the whole dynamic/static issue isn't the problem here: the OP is trying to handle clicks on what was drawn on a part of a canvas element, which is not the same thing as handling clicks on an img element.
Take a look at jQuery:event-alias.js line:1, click just forwards to on. Whether you need delegate or direct binding is also not about if the element is created dynamically, but if you are able to get a reference to it at the time you want to add an event listener to it.

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.