Skip to content

Instantly share code, notes, and snippets.

@luokebi
Last active December 25, 2015 02:29
Show Gist options
  • Save luokebi/6902887 to your computer and use it in GitHub Desktop.
Save luokebi/6902887 to your computer and use it in GitHub Desktop.
var stage = new createjs.Stage("testCanvas");
var shape, hit;
var oldX,oldY;
stage.addEventListener('stagemousedown',mouseDown,false);
function mouseDown(e){
console.log(stage.getObjectUnderPoint(e.stageX,e.stageY));
if (stage.getObjectUnderPoint(e.stageX,e.stageY) == null) {
shape = new createjs.Shape();
stage.addChild(shape);
hit = new createjs.Shape();
oldX = e.stageX;
oldY = e.stageY;
stage.addEventListener('stagemousemove',mouseMove,false);
shape.addEventListener("mousedown", function(evt) {
stage.mouseEnabled = false;
// bump the target in front of its siblings:
var o = evt.target;
o.parent.addChild(o);
o.offset = {x:o.x-evt.stageX, y:o.y-evt.stageY};
},false);
shape.addEventListener("pressmove", function(evt) {
evt.bubbles = false;
var o = evt.target;
o.x = evt.stageX+ o.offset.x;
o.y = evt.stageY+ o.offset.y;
// indicate that the stage should be updated on the next tick:
update = true;
},false);
}
}
stage.addEventListener('stagemouseup',mouseUp,false);
function mouseMove(e) {
shape.graphics.clear();
shape.graphics.beginStroke("#000").setStrokeStyle(8,"round").drawRect(oldX, oldY, e.stageX-oldX, e.stageY-oldY);
stage.update();
}
function mouseUp(e) {
hit.graphics.beginFill("#000").drawRect(oldX, oldY, e.stageX-oldX, e.stageY-oldY);
shape.hitArea = hit;
stage.removeEventListener('stagemousemove',mouseMove,false);
console.log("off");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment