Skip to content

Commit 4787f0a

Browse files
committed
Corrected drag tutorial.
The old code would center the circle on the mouse on mousedown. This new code is the same on the drag example of the site.
1 parent b114fe8 commit 4787f0a

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
@@ -25,11 +25,17 @@
2525
dragger.x = dragger.y = 100;
2626
dragger.addChild(circle, label);
2727
stage.addChild(dragger);
28+
29+
dragger.on("mousedown", function (evt) {
30+
// keep a record on the offset between the mouse position and the container
31+
// position. currentTarget will be the container that the event listener was added to:
32+
evt.currentTarget.offset = {x: this.x - evt.stageX, y: this.y - evt.stageY};
33+
});
2834

2935
dragger.on("pressmove",function(evt) {
30-
// currentTarget will be the container that the event listener was added to:
31-
evt.currentTarget.x = evt.stageX;
32-
evt.currentTarget.y = evt.stageY;
36+
// Calculate the new X and Y based on the mouse new position plus the offset.
37+
evt.currentTarget.x = evt.stageX + evt.currentTarget.offset.x;
38+
evt.currentTarget.y = evt.stageY + evt.currentTarget.offset.y;
3339
// make sure to redraw the stage to show the change:
3440
stage.update();
3541
});

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)