3

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??

Shiv Kumar Ganesh
  • 3,799
  • 10
  • 46
  • 85

2 Answers2

7

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

shimul
  • 71
  • 2
1

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.

Niek H.
  • 585
  • 1
  • 5
  • 19
  • Does paint method means that it redraws every details on the canvas again, rather than manipulating the last image or shape? – Shiv Kumar Ganesh Mar 22 '12 at 13:24
  • The paint method is responsible for drawing the shape. You can manipulate the shape by changing x, y, width or height. – Niek H. Mar 22 '12 at 13:50
  • but what about the existing shapes in the canvas. As in if I have an Existing rectangle Do I need to remove it by calling a method?? – Shiv Kumar Ganesh Mar 22 '12 at 14:07
  • You have to repaint the canvas when you move the rectangle. I'm not sure what the best method is for that. – Niek H. Mar 23 '12 at 14:04