I found myself wanting to achieve something like this:

Drawing circles by dragging the mouse with RxJS

Here you can find the demo and the final code.

What I needed to do

  1. 😊 Import RxJS
  2. πŸ˜‰ Get the events from the mouse
  3. 😎 Draw a circle when the user drags the mouse

Simple right?

Not really

Not really.

1. Import RxJS

This is pretty straightforward. You just have to find a platform that allows you to add RxJS right away like this:

Adding RxJS in JS Bin

jsbin.com

And although JS Bin is perfect, I wanted to use Glitch, which has a lot of cool features as well. In this case you'd have to find RxJS in a UMD format and import it in your index.html:

<script src="https://unpkg.com/@reactivex/rxjs@5.5.6/dist/global/Rx.js"></script>

Importing the RxJS UMD bundle in Glitch

Look at line #8.

2. Get the events from the mouse

This is when RxJS starts to shine 🌟🌟🌟

You might already know this, but you can easily capture events from the DOM by using the fromEvent() Observable operator:

const move$ = Rx.Observable.fromEvent(document, "mousemove");

move$.subscribe(console.log);

Which outputs the following information:

MouseEvent { isTrusted: true, screenX: 638, screenY: 373, ... }

This was my face after:

A surprised reaction

I actually have better hair.

Once the canvas.js script is added it would look like this:

move$.subscribe(drawCircle);

Which would draw a circle every time I move the mouse:

Drawing a circle for every mousemove event

That there was a quantum leap! 😱 What's inside the canvas script? You might be wondering. Well, just Canvas stuff. Nothing to worry about, but if you want to take a look at the code, click here.

Our app is drawing circles every time we move our mouse, but we need to draw circles only when the user is dragging the mouse (clicking and moving at the same time).

3. Draw a circle when the user drags the mouse

β€œHuh! This should be simple” I said.

This is what I needed:

  1. Create observables for two more types of events: mousedown and mouseup (so we could achieve the drag effect).
  2. Make sure the emissions of our first observable (move$) started when the mousedown observable emitted.
  3. Make sure the emissions of our first observable (move$) stopped when the mouseup observable emitted.

Simple right?

Not really

Not really.

My first approach to this problem was to use RxJS operators skipUntil and takeUntil to achieve the desired behavior.

We would skip every move$ emission until the down$ observable emitted. And then would take every move$ emission until the up$ observable emitted. This is how it looked:

const move$ = Rx.Observable.fromEvent(document, "mousemove");
const down$ = Rx.Observable.fromEvent(document, "mousedown");
const up$ = Rx.Observable.fromEvent(document, "mouseup");

move$.skipUntil(down$).takeUntil(up$).subscribe(drawCircle);

And it worked! πŸŽ‰

…

Just once 😒😒😒

It turns out that when the takeUntil() operator is applied, the observable completes, which means that no more emissions will be received (after the user releases the click button).

Uhmmm πŸ€”πŸ€”πŸ€”

Unless you use the repeat() operator to keep the subscription alive! πŸŽ‰πŸŽ‰πŸŽ‰

move$.skipUntil(down$).takeUntil(up$).repeat().subscribe(drawCircle);

And it worked! πŸŽ‰

…

In a very awful way 😒😒😒

While in the first place it allows me to keep the Observable alive, it repeats the events indefinitely which looks like an overkill.

To quote Matthew Podwysocki:

β€œRxJS has so many ways to get the same answer”

Here is when Matt and CΓ©dric gave me a hand. They helped me spot a different way to achieve it using the mergeMap() operator.

A confused first reaction to mergeMap

My first glance at it.

I didn't really understood how it worked until I watched this video and played with its jsfiddle: If you type in the first box nothing will be emitted until you type in the second box πŸ‘‡πŸ‘‡πŸ‘‡

We don't want to draw anything until the user has clicked. So we can use the mergeMap operator here (which achieves the same result as skipUntil above):

down$.mergeMap(() => move$).subscribe(drawCircle);

BUT this time the inner observable (a.k.a. move$) will re-subscribe every time the outer observable (a.k.a. down$) emits.

Exactly

Exactly!

That means we can use the takeUntil operator safely. Because the overall observable will remain alive! πŸ’š

down$.mergeMap(() => move$.takeUntil(up$)).subscribe(drawCircle);

Line #10.

Yes, that's it. To achieve mouse dragging you just need mergeMap and takeUntil πŸ˜‰

I hope this post was helpful to you. If you have any question or feedback, put it in the comments. I reply to all of them! πŸ‘‡πŸ‘‡πŸ‘‡

Thanks for reading: Code / Demo

Thanks to Matt and CΓ©dric again, they were super collaborative and helpful!