Skip to content

Commit ee1141d

Browse files
authored
Merge pull request #794 from weltonrodrigo/master
Corrected drag tutorial.
2 parents 537907c + 4787f0a commit ee1141d

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

tutorials/Mouse Interaction/drag.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@
2828
dragger.x = dragger.y = 100;
2929
dragger.addChild(circle, label);
3030
stage.addChild(dragger);
31+
32+
dragger.on("mousedown", function (evt) {
33+
// keep a record on the offset between the mouse position and the container
34+
// position. currentTarget will be the container that the event listener was added to:
35+
evt.currentTarget.offset = {x: this.x - evt.stageX, y: this.y - evt.stageY};
36+
});
3137

3238
dragger.on("pressmove",function(evt) {
33-
// currentTarget will be the container that the event listener was added to:
34-
evt.currentTarget.x = evt.stageX;
35-
evt.currentTarget.y = evt.stageY;
39+
// Calculate the new X and Y based on the mouse new position plus the offset.
40+
evt.currentTarget.x = evt.stageX + evt.currentTarget.offset.x;
41+
evt.currentTarget.y = evt.stageY + evt.currentTarget.offset.y;
3642
// make sure to redraw the stage to show the change:
3743
stage.update();
3844
});

tutorials/Mouse Interaction/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ <h2>Drag and drop</h2>
221221
which point a <code>pressup</code> event will be dispatched.
222222
</p>
223223
<textarea class="brush: js;" readonly>
224+
circle.on("mousedown"), function(evt) {
225+
evt.target.offset = {x: this.x - evt.stageX, y: this.y - evt.stageY};
226+
}
224227
circle.on("pressmove", function(evt) {
225-
evt.target.x = evt.stageX;
226-
evt.target.y = evt.stageY;
228+
evt.target.x = evt.stageX + evt.target.offset.x;
229+
evt.target.y = evt.stageY + evt.target.offset.y;
227230
});
228231
circle.on("pressup", function(evt) { console.log("up"); })
229232
</textarea>

0 commit comments

Comments
 (0)