-
Notifications
You must be signed in to change notification settings - Fork 2
/
shapesketch_inflate.html
136 lines (112 loc) · 3.72 KB
/
shapesketch_inflate.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>inflate</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';
})();
const state = {
shapes: [],
path: [],
mouse_down: false,
};
window.onmousedown = e => state.mouse_down = true;
window.onmouseup = e => {
state.mouse_down = false;
/* find intersections */
{
const path = state.path;
const lines = [];
for (let i = 0; i < path.length; i++) {
let a = path[i];
let b = path[((i + 1) == path.length) ? 0 : i + 1];
let min_x, max_x;
if (a.x < b.x) min_x = a, max_x = b;
else min_x = b, max_x = a;
const line = { a: min_x, b: max_x, cross: false };
lines.push(line);
}
const open_lines = [];
for (const a of lines) {
for (const b of lines) {
if (a == b) continue;
const u =
((a.a.x - b.a.x)*(b.a.y - b.b.y) - (a.a.y - b.a.y)*(b.a.x - b.b.x)) /
((a.a.x - a.b.x)*(b.a.y - b.b.y) - (a.a.y - a.b.y)*(b.a.x - b.b.x));
const t =
-((a.a.x - a.b.x)*(a.a.y - b.a.y) - (a.a.y - a.b.y)*(a.a.x - b.a.x)) /
((a.a.x - a.b.x)*(b.a.y - b.b.y) - (a.a.y - a.b.y)*(b.a.x - b.b.x));
if (u > 0 && u < 1 && t > 0 && t < 1)
a.cross = b.cross = true;
}
}
{
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.save();
{
for (const line of lines) {
ctx.strokeStyle = line.cross ? "magenta" : "blue";
ctx.beginPath();
ctx.moveTo(line.a.x, line.a.y);
ctx.lineTo(line.b.x, line.b.y);
ctx.lineWidth = window.devicePixelRatio;
ctx.stroke();
ctx.closePath();
}
}
ctx.restore();
}
debugger;
}
state.shapes.push(state.path);
state.path = [];
}
window.onmousemove = e => {
if (state.mouse_down) {
const p = { x: e.pageX * window.devicePixelRatio,
y: e.pageY * window.devicePixelRatio };
state.path.push(p);
}
};
requestAnimationFrame(function render(now) {
requestAnimationFrame(render);
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.save();
{
const paths = [...state.shapes, state.path]
for (const path of paths) {
if (path.length == 0) continue;
ctx.strokeStyle = "red";
ctx.beginPath();
ctx.moveTo(path[0].x, path[0].y);
for (let i = 0; i < path.length; i++) {
const p = path[path.length - 1 - i];
ctx.lineTo(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>