-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraw_carbonated.html
100 lines (87 loc) · 2.62 KB
/
draw_carbonated.html
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>draw</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<canvas id="glcanvas"></canvas>
<script>"use strict";
const canvas = document.getElementById("glcanvas");
const ctx = canvas.getContext('2d');
(window.onresize = () => {
canvas.width = window.innerWidth*window.devicePixelRatio,
canvas.height = window.innerHeight*window.devicePixelRatio
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
})();
let raw_paths = [[]];
let refined_paths = [[]];
let mouse_down = false;
window.onmousedown = e => mouse_down = true;
window.onmouseup = e => {
mouse_down = false;
raw_paths.push([]);
refined_paths.push([]);
}
window.onmousemove = e => {
if (mouse_down) {
const mouse = { x: e.pageX * window.devicePixelRatio,
y: e.pageY * window.devicePixelRatio };
raw_paths[raw_paths.length - 1].push(mouse);
const path = raw_paths[raw_paths.length - 1];
if (path.length > 2) {
const out = [path[0]];
for (const p of path) {
out.push({ x: p.x + Math.random() * 10, y: p.y + Math.random() * 10 });
}
out.push(mouse);
refined_paths[refined_paths.length - 1] = out;
}
}
};
requestAnimationFrame(function render(now) {
requestAnimationFrame(render);
ctx.fillStyle = "rgb(14, 95, 171)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.save();
{
{
const paths = refined_paths;
ctx.strokeStyle = 'white';
for (const path of paths) {
ctx.beginPath();
for (let i = 0; i < path.length; i++) {
const p = path[i];
ctx[i ? 'lineTo' : 'moveTo'](p.x, p.y);
}
ctx.lineWidth = 4 * window.devicePixelRatio;
ctx.stroke();
ctx.closePath();
}
}
{
const paths = raw_paths;
ctx.strokeStyle = "rgb(200, 200, 200)";
for (const path of paths) {
ctx.beginPath();
for (let i = 0; i < path.length; i++) {
const p = path[i];
ctx[i ? 'lineTo' : 'moveTo'](p.x, p.y);
}
ctx.lineWidth = window.devicePixelRatio;
ctx.stroke();
ctx.closePath();
}
}
}
ctx.restore();
})
function lerp(v0, v1, t) { return (1 - t) * v0 + t * v1; }
function inv_lerp(min, max, p) { return (p - min) / (max - min); }
</script>
</body>
</html>