I'm trying to clone a shape (square) every time I click a button. I also want to be able to drag the shapes around the canvas. This is my current code:
-inline script
var stage;
var update = true;
var count = 1;
function init(){
stage = new createjs.Stage('myCanvas');
var rect = new createjs.Shape();
rect.graphics.beginFill('#000000').drawRect(0,0,50,50);
rect.name = "rect_" + count;
count++;
var rectName = rect.name
var dragger = new createjs.Container(rectName);
dragger.x = dragger.y = 100;
dragger.addChild(rect);
stage.addChild(dragger);
dragger.on("pressmove",function(evt) {
// currentTarget will be the container that the event listener was added to:
evt.currentTarget.x = evt.stageX;
evt.currentTarget.y = evt.stageY;
// make sure to redraw the stage to show the change:
stage.update();
});
stage.update();
}
$(function(){
$('button').click(function(){
console.log('fire!');
init();
})
})
-file.html
<body onload='init()'>
<canvas id='myCanvas' width='500' height='500'></canvas>
<button class='makeBox'>New Box!</button>
</body>
As you can see I can create a shape and drag it around. But when I click the button it refreshes the shape instead of cloning it. I read this post but I was still a little confused on how to implement cloning.
duplicating objects with easeljs
Can someone please point me in the right direction. If you want bonus points make it so the shapes don't overlap each other. I would appreciate it so much :)