Interactive drawing with kineticjs

jrabary picture jrabary · May 3, 2012 · Viewed 7.7k times · Source

I'd like to draw a rectangle with click and drag. How can I do this ? Where do I have to put my click event listener ? On the stage or on the layer ? I have the following code but it doesn't work :

stage = new  Kinetic.Stage({...})
layer = new Kinetic.Layer({...})

stage.add(layer)

stage.on('click', function() {
   var pos  = stage.getMousePosition();
   var rect = new Kinetic.Rect({
       x: pos.x,
       y: pos.y,
       width: 10,
       height: 10,
   });
   layer.add(rect);
   layer.draw(); 
})

Thanks.

Answer

As far as i know there is no "click" event on stage in kineticjs. You should use something like this:

stage.getContainer().addEventListener('mousedown', function(evt) {});