[RxJS] Drag and Drop Example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.