I need to create draggable random rectangles using Shape object. This function creates new rectangle where the mouse was clicked.
var rects = [];
var i=0;
var stage;
window.onload = function() {
canvas = document.getElementById("drawingCanvas");
context = canvas.getContext("2d");
stage = new createjs.Stage("drawingCanvas");
canvas.onclick = canvasClick;
}
function canvasClick(e) {
var d=Math.random()*150;
rects[i] = new createjs.Shape();
rects[i].graphics.beginStroke("black").
beginFill("RGB"+"(" + parseInt(Math.random()*255) +"," +
parseInt(Math.random()*255)+"," +parseInt(Math.random()*255)+")").
drawRect(e.offsetX-d/2,e.offsetY-d/2,d,d);
stage.addChild(rects[i]);
rects[i].on("mousedown", function (evant) {
console.log("jdfh");
var offset = {
x: evant.target.x - evant.stageX,
y: evant.target.y - evant.stageY
};
rects[i].on("pressmove", function (evant) {
evant.target.x = evant.stageX + offset.x;
evant.target.y = evant.stageY + offset.y;
stage.update();
});
});
i++;
stage.update();
}
But when I try to drag rectngle it says: rect.js:47 Uncaught TypeError: Cannot read property 'on' of undefined
at a.<anonymous> (file:///C:/Users/Vasya/Downloads/redactor%20(1)/rect/public_html/rect.js:47:39)
at easeljs-0.8.2.min.js:12
at a.b._dispatchEvent (easeljs-0.8.2.min.js:12)
at a.b.dispatchEvent (easeljs-0.8.2.min.js:12)
at a.b._dispatchMouseEvent (easeljs-0.8.2.min.js:13)
at a.b._handlePointerDown (easeljs-0.8.2.min.js:13)
at a.b._handleMouseDown (easeljs-0.8.2.min.js:13)
at HTMLCanvasElement.f (easeljs-0.8.2.min.js:13)