Improving our Mouse drag event
Our mouse drag event was a little too simple. Notice that if we drag around the sprite, it always positions itself at the top-left corner of the mouse. Ideally we drag event to offset it coordinates, based on where the mouse is when the mouse down event Occurre D. This would make we mouse drag more closely resemble moving a real object with our finger.
Let's see if your can adjust the coordinates in the mouse drag event, based on the MouseDown location on the sprite. The mouse events is sequences, and they look something like this:
spritecontainermousemoves = tento410 - ,,,])
Each item in the mouse event sequences contains a x, Y value that represents this absolute location of the mouse event on The page. The Movesprite () function uses these coordinates to position the sprite. Each item in the sequence also contains a pair of OffsetX and OffsetY properties that indicate the position of The mouse event relative to the event target.
function(Sprite, Spritecontainer) {//All of the mouse event sequences look like this: //seq ([{pagex:22, pagey:3423, offsetx:14, offsety:22},,,]) varspritemousedowns = Observable.fromevent (Sprite, "MouseDown"), Spritecontainermousemoves= Observable.fromevent (Spritecontainer, "MouseMove"), Spritecontainermouseups= Observable.fromevent (Spritecontainer, "MouseUp"), //Create a sequence that looks like this: //seq ([{pagex:22, pagey:4080},,, {pagex:24, pagey:4082},,,])Spritemousedrags =//For every mouse off event on the sprite ...spritemousedowns. Concatmap (function(contactpoint) {//... retrieve all the mouse move events on the sprite container ... returnspritecontainermousemoves. //... until a mouse up event occurs.Takeuntil (spritecontainermouseups). Map (function(movepoint) {return{PageX:movePoint.pageX-Contactpoint.offsetx, PageY:movePoint.pageY-contactpoint.offsety}; }); }); //for each mouse drag event, move the sprite to the absolute page position.Spritemousedrags.foreach (function(dragpoint) {Sprite.style.left= Dragpoint.pagex + "px"; Sprite.style.top= Dragpoint.pagey + "px"; });}
[RxJS] Drag and Drop Example