Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript canvas detect click on shape

I have a problem with the click function in javascript. This is my code:

var canvas = document.getElementsByTagName("canvas")[0];
var ctx = canvas.getContext('2d');

BigCircle = function(x, y, color, circleSize) {
    ctx.shadowBlur = 10;
    ctx.shadowColor = color;
    ctx.beginPath();
    ctx.arc(x, y, circleSize, 0, Math.PI * 2, true);
    ctx.fill();
    ctx.closePath();
};

var bigGreen = new BigCircle(1580, 800, '#5eb62b', 180);

function init() {
    $("#bigGreen").click(function(e){
    alert("test");              
    });
}
$(document).ready(function() {
    init();
});

But the click event is not working! Does anybody know why? Thank you so much in advance!

like image 317
user1590534 Avatar asked Sep 27 '12 16:09

user1590534


People also ask

How to detect click on canvas?

To detect the click, the canvas element is first selected using the querySelector() method. The addEventListener() method is used on this element to listen the 'mousedown' event. This event is triggered whenever a mouse button is pressed down.

Does canvas detect mouse movement?

Rumors circulated earlier this semester among students about a new function on Canvas that professors can use to monitor students' mouse movements and the new tabs they opened. But these rumors are false, said Mario Guerra, UT Canvas service manager.

How do you add an event listener in canvas?

Alternatively, you can use the more generic addEventListener() method: canvas . addEventListener ( 'mousedown' , function ( e ) { // React to the mouse down event } ); In addition to onmousedown, you can also assign functions to onmousemove, onmouseup, onmouseover, and onmouseout.


2 Answers

You can now use hit regions in Chrome and Firefox:

https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Hit_regions_and_accessibility#Hit_regions

ctx.beginPath();
ctx.arc(70, 80, 10, 0, 2 * Math.PI, false);
ctx.fill();
ctx.addHitRegion({id: "bigGreen"});

and add a callback

canvas.onclick = function (event)
{
    if (event.region) {
        alert('You clicked ' + event.region);
    }
}

or just use one of the many canvas APIs:

http://www.fabricjs.com/

http://www.createjs.com/easeljs

http://www.paperjs.org

etc...

like image 75
RobotEyes Avatar answered Oct 07 '22 06:10

RobotEyes


without seeing your html this question is a little bit unclear, it seems you would like to draw something on a canvas and use jquery to add click events for the circle, this isn't possible.

you can use jquery to get the click event ON the canvas and from the cursor position you can calculate if the user clicked the circle or not, but jquery won't help you here you have to do the math yourself.

jquery does only work for dom elements.

BigCircle = function(ctx,x, y, color, circleSize) {
    ctx.beginPath();
    ctx.arc(x, y, circleSize, 0, Math.PI * 2, true);
    ctx.fillStyle=color
    ctx.fill();
    ctx.closePath();
    this.clicked=function(){
      ctx.fillStyle='#ff0000'
      ctx.fill();
    }
};

function init() {
  var canvas = document.getElementsByTagName("canvas")[0];
  var ctx = canvas.getContext('2d');
  var bigGreen = new BigCircle(ctx,50, 50, '#5eb62b', 50);
  $('#canvas').click(function(e){
    var x = e.clientX
      , y = e.clientY          
    if(Math.pow(x-50,2)+Math.pow(y-50,2) < Math.pow(50,2))   
      bigGreen.clicked()
  })    
}


$(document).ready(function() {
    init();   
});

jsfiddle is here http://jsfiddle.net/yXVrk/1/

like image 26
supernova Avatar answered Oct 07 '22 07:10

supernova