Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 2a43b27

Browse files
committed
Update pairwise.md
1 parent 8417d8b commit 2a43b27

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

doc/api/core/operators/pairwise.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,43 @@ var subscription = source.subscribe(
2929
// => Completed
3030
```
3131

32+
#### Example (Draw line)
33+
```html
34+
<canvas id="canvas" width="600" height="600"/>
35+
```
36+
37+
```js
38+
var canvas = document.getElementById("canvas");
39+
var g = canvas.getContext("2d");
40+
g.rect(0, 0, canvas.width, canvas.height);
41+
g.fillStyle = "rgb(0,0,0)";
42+
g.fill();
43+
44+
var mouseMove = Rx.Observable.fromEvent(document, 'mousemove');
45+
var mouseDown = Rx.Observable.fromEvent(document.getElementById('canvas'), 'mousedown');
46+
var mouseUp = Rx.Observable.fromEvent(document.getElementById('canvas'), 'mouseup');
47+
48+
mouseDown.flatMap(function(ev) {
49+
return mouseMove.map(function(ev) {
50+
return {
51+
x: ev.clientX,
52+
y: ev.clientY
53+
};
54+
}).pairwise().takeUntil(mouseUp);
55+
56+
}).subscribe(function(pos) {
57+
g.beginPath();
58+
g.lineWidth = 1;
59+
60+
g.strokeStyle = "rgb(255, 0, 0)";
61+
62+
g.moveTo(pos[0].x, pos[0].y);
63+
g.lineTo(pos[1].x, pos[1].y);
64+
65+
g.stroke();
66+
});
67+
```
68+
3269
### Location
3370

3471
File:

0 commit comments

Comments
 (0)