-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
42 lines (33 loc) 路 1.11 KB
/
Copy pathapp.js
File metadata and controls
42 lines (33 loc) 路 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const Bacon = window.Bacon;
// Initialize canvas.
const canvas = document.querySelector('.canvas');
const ctx = canvas.getContext('2d');
let infiniteX = Infinity;
let infiniteY = Infinity;
let colorHue = 0;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.lineWidth = 70;
// Initialize event streams.
const pointerDownStream = Bacon.fromEvent(document, 'pointerdown');
const pointerMoveStream = Bacon.fromEvent(document, 'pointermove');
const pointerUpStream = Bacon.fromEvent(document, 'pointerup');
const drawingStream = pointerDownStream.flatMap(pointerDownEvent => {
return pointerMoveStream.takeUntil(pointerUpStream);
});
// Subscription callback.
drawingStream.onValue(pointerMoveEvent => {
const { clientX, clientY } = pointerMoveEvent;
ctx.strokeStyle = `hsl(${colorHue}, 100%, 60%)`;
ctx.beginPath();
if (Math.abs(infiniteX - clientX) < 100 && Math.abs(infiniteY - clientY) < 100) {
ctx.moveTo(infiniteX, infiniteY);
}
ctx.lineTo(clientX, clientY);
ctx.stroke();
infiniteX = clientX;
infiniteY = clientY;
colorHue++;
});