-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
231 lines (185 loc) · 5.67 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Pocket Particles</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
<style>
html,
body {
font-family: "Open Sans", sans-serif;
}
* {
box-sizing: border-box;
}
#particles,
#wrapper {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
#wrapper {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
overflow-y: auto;
text-align: center;
}
header {
padding: 1em;
}
main {
padding: 1em;
}
h4 {
color: #888;
}
.list {
display: flex;
justify-content: center;
align-items: stretch;
flex-direction: column;
gap: 1em;
}
.list > a {
border-radius: 0.5em;
border: 1px solid #888;
padding: 0.5em;
}
</style>
</head>
<body>
<canvas id="particles"></canvas>
<div id="wrapper">
<header>
<h1>Pocket Particles</h1>
<h4>Scalable Particle Structures</h4>
</header>
<main>
<h3>Examples</h3>
<div class="list">
<a href="./examples/web.html">Interactive Point Graph</a>
<a href="./examples/dots.html">Dots (Search Performance)</a>
<a href="./examples/visualized.html">Pocket Visualization</a>
</div>
<h3>Docs</h3>
<div class="list">
<a href="https://github.com/trmid/pocket#pocket-particles">github.com/trmid/pocket</a>
</div>
</main>
</div>
<script type="module">
import { Pocket, Particle } from "./pocket.js";
window.addEventListener("load", () => {
const colors = [
{ r: 254, g: 200, b: 0 },
{ r: 255, g: 228, b: 129 },
{ r: 93, g: 93, b: 93 }
]
// Get the canvas element and context
const canvas = document.getElementById("particles");
const ctx = canvas.getContext('2d');
// Define Pocket and Particle array vars
let pocket, particles;
const reset = () => {
// Reset canvas bounds
canvas.width = window.outerWidth;
canvas.height = window.outerHeight;
// Create a new Pocket
pocket = new Pocket();
// Add 2000 random particles within the bounds (0, 0) and (canvas.width, canvas.height)
for (let i = 0; i < 2000; i++) {
// Put the random particle in the Pocket
const p = pocket.put(new Particle({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
data: {
id: i,
color: colors[Math.floor(Math.random() * colors.length)],
p1: undefined,
p2: undefined,
vel: {
x: 0,
y: 0
}
}
}));
p.data.start = {
x: p.x,
y: p.y
};
}
// Loop through each particle and connect it to adjacent particles
particles = pocket.all();
for (const p of particles) {
p.retrieve();
const close_1 = pocket.closest(p).retrieve();
const close_2 = pocket.closest(p);
p.data.p1 = close_1;
p.data.p2 = close_2;
pocket.put(p);
pocket.put(close_1);
}
}
// Call reset
reset();
// Add an event listener to reset on window resize
window.addEventListener("resize", reset);
// Set a click listener for the canvas
document.addEventListener("pointermove", (e) => {
const pos = { x: e.pageX, y: e.pageY };
for (const p of pocket.search(Math.min(canvas.width, canvas.height) / 20, pos)) {
p.data.vel.x += e.movementX;
p.data.vel.y += e.movementY;
}
});
const draw = () => {
requestAnimationFrame(draw);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (const p of particles) {
if (p.data.vel.x != 0 && p.data.vel.y != 0) {
const new_pos = {
x: (p.x + p.data.vel.x / 10.0) * 0.9 + 0.1 * p.data.start.x,
y: (p.y + p.data.vel.y / 10.0) * 0.9 + 0.1 * p.data.start.y
};
p.moveTo(new_pos);
p.data.vel.x *= 0.99;
p.data.vel.y *= 0.99;
}
// Pull connected particles
const avg_vel = {
x: (p.data.vel.x + p.data.p1.data.vel.x + p.data.p2.data.vel.x) / 3,
y: (p.data.vel.y + p.data.p1.data.vel.y + p.data.p2.data.vel.y) / 3
};
p.data.vel.x = avg_vel.x;
p.data.vel.y = avg_vel.y;
p.data.p1.data.vel.x = avg_vel.x;
p.data.p1.data.vel.y = avg_vel.y;
p.data.p2.data.vel.x = avg_vel.x;
p.data.p2.data.vel.y = avg_vel.y;
// Render Particle
if (p.data.p1 && p.data.p2) {
const opacity = Math.min(100, Math.floor(Math.sqrt(p.data.vel.x * p.data.vel.x + p.data.vel.y * p.data.vel.y))) / 100.0;
if (opacity > 0.01) {
ctx.fillStyle = `rgba(${p.data.color.r}, ${p.data.color.g}, ${p.data.color.b}, ${opacity})`;
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(p.data.p1.x, p.data.p1.y);
ctx.lineTo(p.data.p2.x, p.data.p2.y);
ctx.lineTo(p.x, p.y);
ctx.fill();
}
}
}
};
draw();
});
</script>
</body>
</html>