Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify shapes in Canvas?

If I define a canvas and draw few shapes onto it, then how can I pinpoint each of the shape or image so as to declare events and other properties on the each shape. Consider I have a Rectangle and a triangle. SO can I have some mechanism so as to define them as specific entity and can I deal with them individually. I know about KineticJS but I would like to implement this functionality on my own(For learning purpose). Can anyone pinpoint the way to do it. Or may be an algorithmic approach??

like image 748
Shiv Kumar Ganesh Avatar asked Nov 14 '25 13:11

Shiv Kumar Ganesh


2 Answers

I would like explain pinpoint using mouse events

First of all you have to implement a method to get mouse position

    function getMousePos(canvas, evt){
    // get canvas position
    var obj = canvas;
    wrapper = document.getElementById('wrapper');
    var top = 0;
    var left = 0;
    while (obj && obj.tagName != 'BODY') {
        top += obj.offsetTop;
        left += obj.offsetLeft;
        obj = obj.offsetParent;
    }

    // return relative mouse position
    var mouseX = evt.clientX - left + window.pageXOffset+wrapper.scrollLeft;
    var mouseY = evt.clientY - top + window.pageYOffset+wrapper.scrollTop;
    return {
        x: mouseX,
        y: mouseY
    };
}
  1. Rectangle

Say, we have a rectangle with following values x1, y1, w, h

$(canvas).mousemove(function(e){

        //Now call the method getMousePos
         var mouseX, mouseY;
         var mousePos = getMousePos(canvas, e);
         mouseX=mousePos.x;
         mouseY=mousePos.y; 

        // check if move on the rect

        if(mouseX>x1 && mouseX<x1+w && mouseY>y1 && mouseY<y1+h)
        {
            alert('mouse on rect')
        }        
});
  1. Circle

Say, we have a circle with following values x, y, r

$(canvas).mousemove(function(e){

        //Now call the method getMousePos
         var mouseX, mouseY;
         var mousePos = getMousePos(canvas, e);
         mouseX=mousePos.x;
         mouseY=mousePos.y; 

        // check if move on the rect

       if(Math.pow(mouseX-x,2)+Math.pow(mouseY-y,2)<Math.pow(r,2))
        {
            alert('mouse on circle')
        }        
});

By this way we can pinpoint a object of canvas

like image 84
shimul Avatar answered Nov 17 '25 10:11

shimul


You can't use any existing functionality in the DOM for that. So you have to write it yourself. You could start by making an object model like this:

function Shape(x, y) {
  var obj = {};
  obj.x = x;
  obj.y = y;

  obj.paint = function(canvasTarget) {
  }

  return obj;
}

function Rectangle(x, y, width, height) {
  var obj = Shape(x, y);
  obj.width = width;
  obj.height = height;

  obj.paint = function(canvasTarget) {
     //paint rectangle on the canvas
  }

  return obj;
}

function Canvas(target) {
  var obj = {};
  obj.target = target
  obj.shapes = [];

  obj.paint = function() {
     //loop through shapes and call paint
  }

  obj.addShape(shape) {
     this.shapes.push(shape);
  }        
}

After making the object model you could use it to draw the shapes like this:

var cnv = new Canvas();
cnv.addShape(new Rectangle(10,10,100,100));
cnv.paint();

Then you can handle the onclick event on the canvas and determine which shape is clicked on.

like image 21
Niek H. Avatar answered Nov 17 '25 09:11

Niek H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!