File tree Expand file tree Collapse file tree
tutorials/Mouse Interaction Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 } ) ;
Original file line number Diff line number Diff 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+ }
224227circle.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});
228231circle.on("pressup", function(evt) { console.log("up"); })
229232 </ textarea >
You can’t perform that action at this time.
0 commit comments