Repository navigation
Expand file tree
/
Copy pathparticle-trace.html
More file actions
488 lines (427 loc) · 13.2 KB
/
Copy pathparticle-trace.html
File metadata and controls
488 lines (427 loc) · 13.2 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Particle Trace</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: #0a0a0a;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
padding: 16px;
gap: 14px;
overflow-y: auto;
}
canvas {
display: block;
border-radius: 8px;
width: min(calc(100vw - 32px), calc((100vh - 240px) * 16 / 9));
aspect-ratio: 16 / 9;
flex-shrink: 0;
image-rendering: pixelated;
}
.panel {
background: rgba(255,255,255,0.055);
border: 1px solid rgba(255,255,255,0.09);
border-radius: 14px;
padding: 13px 16px;
width: min(calc(100vw - 32px), 640px);
display: flex;
flex-direction: column;
gap: 9px;
}
.row {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
}
.lbl {
font-size: 11px;
color: rgba(255,255,255,0.35);
width: 72px;
flex-shrink: 0;
text-transform: uppercase;
letter-spacing: 0.05em;
}
input[type=range] {
flex: 1;
min-width: 80px;
accent-color: rgba(255,255,255,0.55);
cursor: pointer;
height: 3px;
}
.val {
font-size: 11px;
color: rgba(255,255,255,0.45);
font-variant-numeric: tabular-nums;
min-width: 36px;
text-align: right;
}
input[type=color] {
-webkit-appearance: none;
appearance: none;
width: 28px;
height: 28px;
border: none;
border-radius: 6px;
padding: 2px;
cursor: pointer;
background: none;
flex-shrink: 0;
}
input[type=color]::-webkit-color-swatch-wrapper { padding: 0; }
input[type=color]::-webkit-color-swatch {
border: 1px solid rgba(255,255,255,0.18);
border-radius: 5px;
}
.btns {
display: flex;
gap: 6px;
flex-wrap: wrap;
margin-top: 1px;
}
.btns button, .btn {
background: rgba(255,255,255,0.06);
border: 1px solid rgba(255,255,255,0.09);
color: rgba(255,255,255,0.65);
padding: 5px 13px;
border-radius: 7px;
font-size: 11px;
cursor: pointer;
font-family: inherit;
transition: all 0.12s;
}
.btns button:hover, .btn:hover { background: rgba(255,255,255,0.12); color: rgba(255,255,255,0.9); }
.btns button.rec { background: #ff3b30; border-color: #ff3b30; color: #fff; }
.btn.active { background: rgba(255,255,255,0.14); color: rgba(255,255,255,0.9); border-color: rgba(255,255,255,0.2); }
.top-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
flex-wrap: wrap;
}
.title {
font-size: 11px;
color: rgba(255,255,255,0.28);
letter-spacing: 0.08em;
text-transform: uppercase;
}
.hr { height: 1px; background: rgba(255,255,255,0.07); margin: 1px 0; }
</style>
</head>
<body>
<canvas id="c"></canvas>
<div class="panel">
<div class="top-row">
<span class="title">Particle Trace</span>
<div style="display:flex;gap:6px">
<button class="btn" onclick="handleUpload()">↑ Upload Image</button>
<button class="btn" id="btnInvert" onclick="toggleInvert()">Invert</button>
</div>
</div>
<div class="hr"></div>
<div class="row">
<span class="lbl">Colors</span>
<div style="display:flex;gap:6px">
<input type="color" id="c0" value="#ffe4c8">
<input type="color" id="c1" value="#50b4ff">
<input type="color" id="c2" value="#ff7830">
</div>
<button class="btn" style="margin-left:auto" onclick="recolor()">Recolor</button>
</div>
<div class="row">
<span class="lbl">Particles</span>
<input type="range" id="sCount" min="5000" max="80000" value="30000" step="1000">
<span class="val" id="vCount">30k</span>
</div>
<div class="row">
<span class="lbl">Speed</span>
<input type="range" id="sSpeed" min="0.2" max="4" value="1.2" step="0.1">
<span class="val" id="vSpeed">1.2</span>
</div>
<div class="row">
<span class="lbl">Fade</span>
<input type="range" id="sFade" min="1" max="12" value="4" step="0.5">
<span class="val" id="vFade">4.0</span>
</div>
<div class="hr"></div>
<div class="btns">
<button id="btnPlay" onclick="togglePlay()">⏸ Pause</button>
<button onclick="clearBuf()">Clear</button>
<button onclick="savePng()">↓ PNG</button>
<button id="btnRec" onclick="toggleRecord()">⏺ Record</button>
</div>
</div>
<input id="fileIn" type="file" accept="image/*" style="display:none" onchange="handleFile(event)">
<script>
// ---- Dimensions ----
const W = 960, H = 540; // render buffer
const FW = 200, FH = 113; // flow field resolution
const canvas = document.getElementById('c');
canvas.width = W; canvas.height = H;
const ctx = canvas.getContext('2d');
// ---- Field arrays ----
const field = new Float32Array(FW * FH);
const gradX = new Float32Array(FW * FH);
const gradY = new Float32Array(FW * FH);
// ---- Pixel buffer ----
const imgd = ctx.createImageData(W, H);
const px = imgd.data;
for (let i = 3; i < px.length; i += 4) px[i] = 255; // alpha
// ---- Particle state ----
const MAX_N = 80000;
const pX = new Float32Array(MAX_N);
const pY = new Float32Array(MAX_N);
const pL = new Int32Array(MAX_N); // life
const pCI = new Uint8Array(MAX_N); // color index (0/1/2)
const pR = new Uint8Array(MAX_N);
const pG = new Uint8Array(MAX_N);
const pB = new Uint8Array(MAX_N);
let N = 30000;
let inverted = false;
let colors = [[255, 228, 200], [80, 180, 255], [255, 120, 48]];
// ---- Helpers ----
function hexToRgb(hex) {
return [parseInt(hex.slice(1,3),16), parseInt(hex.slice(3,5),16), parseInt(hex.slice(5,7),16)];
}
function readColors() {
colors = [
hexToRgb(document.getElementById('c0').value),
hexToRgb(document.getElementById('c1').value),
hexToRgb(document.getElementById('c2').value),
];
}
function fieldSample(x, y) {
const fx = Math.min(FW-1, Math.max(0, (x / W * FW) | 0));
const fy = Math.min(FH-1, Math.max(0, (y / H * FH) | 0));
const i = fy * FW + fx;
return [field[i], gradX[i], gradY[i]];
}
// ---- Particle spawn ----
function spawnP(i) {
let x = 0, y = 0;
for (let t = 0; t < 30; t++) {
x = Math.random() * W;
y = Math.random() * H;
const [b] = fieldSample(x, y);
if (Math.random() < b * 0.92 + 0.08) break;
}
pX[i] = x; pY[i] = y;
pL[i] = 60 + (Math.random() * 300) | 0;
const ci = (Math.random() * colors.length) | 0;
pCI[i] = ci;
pR[i] = colors[ci][0]; pG[i] = colors[ci][1]; pB[i] = colors[ci][2];
}
function initParticles() {
for (let i = 0; i < MAX_N; i++) spawnP(i);
}
// ---- Gradient ----
function computeGrad() {
for (let y = 0; y < FH; y++) {
for (let x = 0; x < FW; x++) {
const l = x > 0 ? field[y*FW+x-1] : field[y*FW+x];
const r = x < FW-1 ? field[y*FW+x+1] : field[y*FW+x];
const t = y > 0 ? field[(y-1)*FW+x] : field[y*FW+x];
const b = y < FH-1 ? field[(y+1)*FW+x] : field[y*FW+x];
gradX[y*FW+x] = (r - l) * 0.5;
gradY[y*FW+x] = (b - t) * 0.5;
}
}
}
// ---- Default field: concentric petal rings ----
function makeDefault() {
for (let y = 0; y < FH; y++) {
for (let x = 0; x < FW; x++) {
const nx = (x / FW - 0.5) * 2;
const ny = (y / FH - 0.5) * 2;
const r = Math.sqrt(nx*nx + ny*ny);
const a = Math.atan2(ny, nx);
const ring = Math.pow(Math.max(0, Math.sin(r * 5 * Math.PI) * 0.5 + 0.5), 1.5);
const petal = Math.sin(a * 7) * 0.22 + 0.78;
field[y * FW + x] = ring * petal * Math.max(0, 1 - r * 0.75);
}
}
computeGrad();
initParticles();
}
// ---- Image load ----
function loadImgData(img) {
const oc = document.createElement('canvas');
oc.width = FW; oc.height = FH;
const ox = oc.getContext('2d');
// Contain: preserve aspect ratio, center, letterbox with black
const scale = Math.min(FW / img.width, FH / img.height);
const sw = img.width * scale, sh = img.height * scale;
ox.fillStyle = '#000';
ox.fillRect(0, 0, FW, FH);
ox.drawImage(img, (FW - sw) / 2, (FH - sh) / 2, sw, sh);
const d = ox.getImageData(0, 0, FW, FH).data;
for (let i = 0; i < FW * FH; i++) {
const lum = (d[i*4]*0.299 + d[i*4+1]*0.587 + d[i*4+2]*0.114) / 255;
const a = d[i*4+3] / 255;
const v = lum * a;
field[i] = inverted ? 1 - v : v;
}
computeGrad();
clearBuf();
initParticles();
}
// ---- Clear ----
function clearBuf() {
px.fill(0);
for (let i = 3; i < px.length; i += 4) px[i] = 255;
}
// ---- Upload ----
function handleUpload() {
document.getElementById('fileIn').click();
}
function handleFile(e) {
const file = e.target.files[0];
if (!file) return;
const url = URL.createObjectURL(file);
const img = new Image();
img.onload = () => { loadImgData(img); URL.revokeObjectURL(url); };
img.onerror = () => URL.revokeObjectURL(url);
img.src = url;
e.target.value = ''; // allow re-uploading same file
}
function toggleInvert() {
inverted = !inverted;
document.getElementById('btnInvert').classList.toggle('active', inverted);
for (let i = 0; i < FW * FH; i++) field[i] = 1 - field[i];
clearBuf();
initParticles();
}
// Update all particles assigned to color slot n with its new value
function applyColor(n) {
const [r, g, b] = colors[n];
for (let i = 0; i < MAX_N; i++) {
if (pCI[i] === n) { pR[i] = r; pG[i] = g; pB[i] = b; }
}
}
function recolor() {
readColors();
for (let i = 0; i < MAX_N; i++) {
const ci = (Math.random() * colors.length) | 0;
pCI[i] = ci;
pR[i] = colors[ci][0]; pG[i] = colors[ci][1]; pB[i] = colors[ci][2];
}
}
// ---- Main loop ----
let playing = true, lastTs = null;
function frame(ts) {
if (lastTs !== null && playing) {
const speed = parseFloat(document.getElementById('sSpeed').value);
const fadeV = parseFloat(document.getElementById('sFade').value);
const fade = 1 - fadeV * 0.007;
const n = N;
// Update particles
for (let i = 0; i < n; i++) {
const fx = Math.min(FW-1, Math.max(0, (pX[i] / W * FW) | 0));
const fy = Math.min(FH-1, Math.max(0, (pY[i] / H * FH) | 0));
const idx = fy * FW + fx;
const b = field[idx];
const gx = gradX[idx], gy = gradY[idx];
const gm = Math.sqrt(gx*gx + gy*gy);
// Flow: blend brightness-angle (smooth areas) ↔ edge tangent (gradient areas)
const angle = b * Math.PI * 6;
let ex = 0, ey = 0;
if (gm > 0.005) { ex = -gy / gm; ey = gx / gm; }
const em = Math.min(1, gm * 9);
pX[i] += (Math.cos(angle) * (1 - em) + ex * em) * speed;
pY[i] += (Math.sin(angle) * (1 - em) + ey * em) * speed;
pL[i]--;
if (pL[i] <= 0 || pX[i] < 0 || pX[i] >= W || pY[i] < 0 || pY[i] >= H) {
spawnP(i);
}
}
// Fade pixel buffer
for (let i = 0; i < W * H; i++) {
const b = i << 2;
px[b] = (px[b] * fade) | 0;
px[b+1] = (px[b+1] * fade) | 0;
px[b+2] = (px[b+2] * fade) | 0;
}
// Draw particles (1px, additive)
for (let i = 0; i < n; i++) {
const x = pX[i] | 0, y = pY[i] | 0;
if (x < 0 || x >= W || y < 0 || y >= H) continue;
const b = (y * W + x) << 2;
px[b] = Math.min(255, px[b] + (pR[i] * 18 >> 8));
px[b+1] = Math.min(255, px[b+1] + (pG[i] * 18 >> 8));
px[b+2] = Math.min(255, px[b+2] + (pB[i] * 18 >> 8));
}
ctx.putImageData(imgd, 0, 0);
}
lastTs = ts;
requestAnimationFrame(frame);
}
// ---- Controls ----
document.getElementById('sCount').addEventListener('input', () => {
N = parseInt(document.getElementById('sCount').value);
document.getElementById('vCount').textContent = Math.round(N / 1000) + 'k';
});
[['sSpeed','vSpeed',1], ['sFade','vFade',1]].forEach(([s,v,dp]) => {
document.getElementById(s).addEventListener('input', () => {
document.getElementById(v).textContent =
parseFloat(document.getElementById(s).value).toFixed(dp);
});
});
['c0','c1','c2'].forEach((id, n) => {
document.getElementById(id).addEventListener('input', () => {
colors[n] = hexToRgb(document.getElementById(id).value);
applyColor(n);
});
});
function togglePlay() {
playing = !playing;
if (playing) lastTs = null;
document.getElementById('btnPlay').textContent = playing ? '⏸ Pause' : '▶ Play';
}
function savePng() {
const a = document.createElement('a');
a.download = 'particle-trace.png';
a.href = canvas.toDataURL('image/png');
a.click();
}
let mediaRecorder = null, recordChunks = [];
function toggleRecord() {
const btn = document.getElementById('btnRec');
if (!mediaRecorder) {
const mimeType = ['video/webm;codecs=vp9','video/webm;codecs=vp8','video/webm']
.find(m => MediaRecorder.isTypeSupported(m));
const stream = canvas.captureStream(30);
mediaRecorder = new MediaRecorder(stream, { mimeType, videoBitsPerSecond: 8_000_000 });
recordChunks = [];
mediaRecorder.ondataavailable = e => { if (e.data.size > 0) recordChunks.push(e.data); };
mediaRecorder.onstop = () => {
const blob = new Blob(recordChunks, { type: mimeType });
const a = document.createElement('a');
a.download = 'particle-trace.webm';
a.href = URL.createObjectURL(blob);
a.click();
URL.revokeObjectURL(a.href);
mediaRecorder = null;
};
mediaRecorder.start();
btn.textContent = '⏹ Stop'; btn.classList.add('rec');
if (!playing) togglePlay();
} else {
mediaRecorder.stop();
btn.textContent = '⏺ Record'; btn.classList.remove('rec');
}
}
// ---- Init ----
makeDefault();
requestAnimationFrame(frame);
</script>
</body>
</html>