Reputation: 139
I want to make a draggable mask inside Animate CC Canvas.
box is a movieClip that contains a vector black square.
bg is a movieClip that contains a bitmap that i want to apply the box as a mask to and drag around.
Both display on screen, but the dragBox is only draggable, but it won't mask the backgroundImage. What am I missing here?
var backgroundImage = new lib.bg();
backgroundImage.x = backgroundImage.y = 0;
stage.addChild(backgroundImage);
var dragBox = new lib.box();
dragBox.x = dragBox.y = 0;
stage.addChild(dragBox);
backgroundImage.mask = dragBox;
stage.update();
dragBox.addEventListener("pressmove", dragMe.bind(this));
function dragMe(evt) {
this.addChild(evt.currentTarget);
var p = this.globalToLocal(evt.stageX, evt.stageY);
evt.currentTarget.x = p.x;
evt.currentTarget.y = p.y;
}
Upvotes: 0
Views: 499
Reputation: 11294
Masks should not be a child of the stage. Adding it to the stage makes it both the mask, as well as a visible child.
There are two easy approaches.
Here is a quick sample: https://jsfiddle.net/lannymcnie/og4pmo73/
stagemousedown
, stagemouseup
, and stagemousemove
events, so you can listen to those, and handle it accordingly. Its not as clean as the pressmove
event, but its not much more work.Here is a quick sample: https://jsfiddle.net/lannymcnie/og4pmo73/1/
var offset = new createjs.Point();
var listener = null;
stage.on("stagemousedown", function(e) {
offset.setValues(s.x - e.stageX, s.y-e.stageY);
listener = stage.on("stagemousemove", function(e) {
s.x = e.stageX+offset.x; s.y = e.stageY+offset.y;
});
});
stage.on("stagemouseup", function(e) {
stage.off("stagemousemove", listener);
});
Hope that helps!
Upvotes: 1