Repository navigation
Expand file tree
/
Copy pathflow-ribbons.html
More file actions
556 lines (487 loc) · 16.1 KB
/
Copy pathflow-ribbons.html
File metadata and controls
556 lines (487 loc) · 16.1 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flow Ribbons</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: #111;
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;
transition: background 0.4s;
}
canvas {
display: block;
border-radius: 8px;
width: min(calc(100vw - 32px), calc((100vh - 220px) * 16 / 9));
aspect-ratio: 16 / 9;
flex-shrink: 0;
}
.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: 82px;
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;
}
.val {
font-size: 11px;
color: rgba(255,255,255,0.45);
font-variant-numeric: tabular-nums;
min-width: 30px;
text-align: right;
}
.seg {
display: flex;
gap: 1px;
background: rgba(255,255,255,0.05);
border-radius: 8px;
padding: 3px;
}
.seg button {
background: none;
border: none;
color: rgba(255,255,255,0.35);
padding: 4px 10px;
border-radius: 6px;
font-size: 11px;
cursor: pointer;
font-family: inherit;
transition: all 0.12s;
white-space: nowrap;
}
.seg button.active {
background: rgba(255,255,255,0.13);
color: rgba(255,255,255,0.85);
}
.seg button:hover:not(.active) {
color: rgba(255,255,255,0.6);
}
.btns {
display: flex;
gap: 6px;
flex-wrap: wrap;
margin-top: 1px;
}
.btns button {
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 { background: rgba(255,255,255,0.12); color: rgba(255,255,255,0.9); }
.btns button.rec { background: #ff3b30; border-color: #ff3b30; color: #fff; }
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;
}
.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">Flow Ribbons</span>
<div class="seg" id="seg-count">
<button data-v="2">2</button>
<button data-v="3" class="active">3</button>
<button data-v="4">4</button>
<button data-v="5">5</button>
<button data-v="6">6</button>
<button data-v="7">7</button>
<button data-v="8">8</button>
<button data-v="9">9</button>
<button data-v="10">10</button>
</div>
</div>
<div class="hr"></div>
<div class="row">
<span class="lbl">Colors</span>
<div id="color-pickers" style="display:flex;gap:6px;flex-wrap:wrap">
<input type="color" class="ribbon-color" data-i="0" value="#285ce1" title="Ribbon 1">
<input type="color" class="ribbon-color" data-i="1" value="#28e1cb" title="Ribbon 2">
<input type="color" class="ribbon-color" data-i="2" value="#28e19e" title="Ribbon 3">
<input type="color" class="ribbon-color" data-i="3" value="#6328e1" title="Ribbon 4">
<input type="color" class="ribbon-color" data-i="4" value="#28cbe1" title="Ribbon 5">
<input type="color" class="ribbon-color" data-i="5" value="#e15528" title="Ribbon 6">
<input type="color" class="ribbon-color" data-i="6" value="#e1b828" title="Ribbon 7">
<input type="color" class="ribbon-color" data-i="7" value="#9b28e1" title="Ribbon 8">
<input type="color" class="ribbon-color" data-i="8" value="#28e172" title="Ribbon 9">
<input type="color" class="ribbon-color" data-i="9" value="#28a9e1" title="Ribbon 10">
</div>
</div>
<div class="row">
<span class="lbl">Thickness</span>
<input type="range" id="sThick" min="0.2" max="2.5" value="1" step="0.05">
<span class="val" id="vThick">1.00</span>
</div>
<div class="row">
<span class="lbl">Background</span>
<div class="seg" id="seg-bg">
<button data-v="0" class="active">Light</button>
<button data-v="1">Dark</button>
</div>
</div>
<div class="row">
<span class="lbl">Speed</span>
<input type="range" id="sSpeed" min="0" max="3" value="1" step="0.05">
<span class="val" id="vSpeed">1.00</span>
</div>
<div class="hr"></div>
<div class="btns">
<button id="btnPlay" onclick="togglePlay()">⏸ Pause</button>
<button onclick="savePng()">↓ PNG</button>
<button id="btnRec" onclick="toggleRecord()">⏺ Record</button>
</div>
</div>
<script>
const canvas = document.getElementById('c');
const gl = canvas.getContext('webgl2', { preserveDrawingBuffer: true });
if (!gl) {
document.body.innerHTML = '<p style="color:#fff;padding:40px;font-family:sans-serif">WebGL2 is required. Please use a modern browser.</p>';
}
// ---- State ----
let playing = true;
let elapsed = 0;
let lastTs = null;
let count = 3;
let darkBg = 0;
let hues = [0.62, 0.48, 0.44, 0.72, 0.52, 0.04, 0.13, 0.77, 0.40, 0.55]; // ocean palette
let mediaRecorder = null, recordChunks = [];
function hexToHue(hex) {
const r = parseInt(hex.slice(1,3),16)/255;
const g = parseInt(hex.slice(3,5),16)/255;
const b = parseInt(hex.slice(5,7),16)/255;
const max = Math.max(r,g,b), min = Math.min(r,g,b), d = max - min;
if (d < 1e-6) return 0;
let h;
if (max === r) h = ((g - b) / d % 6 + 6) % 6;
else if (max === g) h = (b - r) / d + 2;
else h = (r - g) / d + 4;
return h / 6;
}
// ---- Canvas pixel sizing ----
const DPR = Math.min(devicePixelRatio, 2);
// Set a known initial size immediately — avoids black-canvas on first rAF
canvas.width = 1280;
canvas.height = 720;
gl.viewport(0, 0, 1280, 720);
function resizeCanvas() {
const w = Math.round(canvas.clientWidth * DPR);
const h = Math.round(canvas.clientHeight * DPR);
if (w > 0 && h > 0 && (canvas.width !== w || canvas.height !== h)) {
canvas.width = w;
canvas.height = h;
gl.viewport(0, 0, w, h);
}
}
new ResizeObserver(resizeCanvas).observe(canvas);
resizeCanvas();
// ---- Shaders ----
const VS = `#version 300 es
in vec2 aPos;
void main() { gl_Position = vec4(aPos, 0.0, 1.0); }
`;
const FS = `#version 300 es
precision highp float;
out vec4 fragColor;
uniform vec2 uRes;
uniform float uTime;
uniform float uSpeed;
uniform int uCount;
uniform int uDark;
uniform float uHue0;
uniform float uHue1;
uniform float uHue2;
uniform float uHue3;
uniform float uHue4;
uniform float uHue5;
uniform float uHue6;
uniform float uHue7;
uniform float uHue8;
uniform float uHue9;
uniform float uThickness;
const float PI = 3.14159265;
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
float getHue(int i) {
if (i == 0) return uHue0;
if (i == 1) return uHue1;
if (i == 2) return uHue2;
if (i == 3) return uHue3;
if (i == 4) return uHue4;
if (i == 5) return uHue5;
if (i == 6) return uHue6;
if (i == 7) return uHue7;
if (i == 8) return uHue8;
return uHue9;
}
void ribbon(inout vec3 col, vec2 uv, float t,
float cy, float amp, float freq,
float phOff, float spd, float baseW, int idx) {
float ph = t * spd + phOff;
// Wave: main sine + golden-ratio harmonic for organic shape
float wy = cy
+ amp * sin(uv.x * freq + ph)
+ amp * 0.18 * sin(uv.x * freq * 1.618 + ph * 0.73 + 1.1);
// Slope (first derivative) — computed early, drives both width and lighting
float dy = amp * freq * cos(uv.x * freq + ph)
+ amp * 0.18 * freq * 1.618 * cos(uv.x * freq * 1.618 + ph * 0.73 + 1.1);
// Twist narrowing: ribbon tilted edge-on appears thinner, like a physical ribbon
float tilt = 1.0 / sqrt(1.0 + dy * dy * 2.0);
// Taper: subtly wider on the left (perspective cue)
float w = baseW * uThickness * tilt * (1.2 - 0.28 * uv.x);
float dist = abs(uv.y - wy);
float cov = smoothstep(w, w * 0.04, dist);
if (cov < 0.003) return;
float slope = abs(dy);
float light = 1.0 - 0.22 * slope / (1.8 + slope);
// Curvature (second derivative) → specular ridge at bends
float d2y = -amp * freq * freq * sin(uv.x * freq + ph)
- amp * 0.18 * freq * freq * 1.618 * 1.618 * sin(uv.x * freq * 1.618 + ph * 0.73 + 1.1);
float spec = smoothstep(0.8, 4.5, abs(d2y)) * cov * cov;
// Iridescent hue shift: hue drifts along x and with slope
float hue = getHue(idx) + uv.x * 0.07 + slope * 0.015;
float sat = uDark == 1 ? 0.82 : 0.70;
float val = uDark == 1
? 0.60 + cov * 0.40 * light
: 0.55 + cov * 0.45 * light;
vec3 c = hsv2rgb(vec3(hue, sat, val));
c += vec3(spec * (uDark == 1 ? 0.48 : 0.28));
// Soft glow halo in dark mode
if (uDark == 1) {
float glow = smoothstep(baseW * uThickness * tilt * 4.5, 0.0, dist);
col += hsv2rgb(vec3(getHue(idx), 0.68, 0.26)) * glow * 0.22;
}
col = mix(col, c, cov * 0.88);
}
void main() {
vec2 uv = gl_FragCoord.xy / uRes;
float t = uTime * uSpeed * 0.28;
vec3 col = uDark == 1
? vec3(0.05, 0.05, 0.07)
: vec3(0.98, 0.97, 0.96);
// cy amp freq phOff spd baseW
if (uCount >= 1) ribbon(col, uv, t, 0.40, 0.22, PI*1.4, 0.00, 0.38, 0.058, 0);
if (uCount >= 2) ribbon(col, uv, t, 0.60, 0.19, PI*1.7, 2.10, -0.30, 0.050, 1);
if (uCount >= 3) ribbon(col, uv, t, 0.50, 0.15, PI*1.5, 4.20, 0.46, 0.042, 2);
if (uCount >= 4) ribbon(col, uv, t, 0.44, 0.12, PI*2.0, 1.05, -0.33, 0.035, 3);
if (uCount >= 5) ribbon(col, uv, t, 0.56, 0.17, PI*1.3, 3.15, 0.41, 0.050, 4);
if (uCount >= 6) ribbon(col, uv, t, 0.35, 0.20, PI*1.6, 5.50, -0.35, 0.045, 5);
if (uCount >= 7) ribbon(col, uv, t, 0.65, 0.14, PI*1.9, 0.70, 0.52, 0.038, 6);
if (uCount >= 8) ribbon(col, uv, t, 0.48, 0.18, PI*2.2, 2.80, -0.42, 0.046, 7);
if (uCount >= 9) ribbon(col, uv, t, 0.55, 0.11, PI*1.2, 1.40, 0.36, 0.032, 8);
if (uCount >= 10) ribbon(col, uv, t, 0.42, 0.16, PI*2.4, 3.80, -0.28, 0.044, 9);
fragColor = vec4(col, 1.0);
}
`;
// ---- Compile + link ----
function mkShader(type, src) {
const s = gl.createShader(type);
gl.shaderSource(s, src);
gl.compileShader(s);
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS))
console.error('Shader compile error:', gl.getShaderInfoLog(s));
return s;
}
const prog = gl.createProgram();
gl.attachShader(prog, mkShader(gl.VERTEX_SHADER, VS));
gl.attachShader(prog, mkShader(gl.FRAGMENT_SHADER, FS));
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS))
console.error('Link error:', gl.getProgramInfoLog(prog));
gl.useProgram(prog);
// ---- Fullscreen quad ----
const buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, -1,1, 1,1]), gl.STATIC_DRAW);
const aPosLoc = gl.getAttribLocation(prog, 'aPos');
gl.enableVertexAttribArray(aPosLoc);
gl.vertexAttribPointer(aPosLoc, 2, gl.FLOAT, false, 0, 0);
// ---- Uniform handles ----
const U = {
res: gl.getUniformLocation(prog, 'uRes'),
time: gl.getUniformLocation(prog, 'uTime'),
speed: gl.getUniformLocation(prog, 'uSpeed'),
count: gl.getUniformLocation(prog, 'uCount'),
dark: gl.getUniformLocation(prog, 'uDark'),
hue0: gl.getUniformLocation(prog, 'uHue0'),
hue1: gl.getUniformLocation(prog, 'uHue1'),
hue2: gl.getUniformLocation(prog, 'uHue2'),
hue3: gl.getUniformLocation(prog, 'uHue3'),
hue4: gl.getUniformLocation(prog, 'uHue4'),
hue5: gl.getUniformLocation(prog, 'uHue5'),
hue6: gl.getUniformLocation(prog, 'uHue6'),
hue7: gl.getUniformLocation(prog, 'uHue7'),
hue8: gl.getUniformLocation(prog, 'uHue8'),
hue9: gl.getUniformLocation(prog, 'uHue9'),
thick: gl.getUniformLocation(prog, 'uThickness'),
};
// ---- Render loop ----
function frame(ts) {
if (lastTs !== null && playing) elapsed += (ts - lastTs) / 1000;
lastTs = ts;
gl.uniform2f(U.res, canvas.width, canvas.height);
gl.uniform1f(U.time, elapsed);
gl.uniform1f(U.speed, parseFloat(document.getElementById('sSpeed').value));
gl.uniform1i(U.count, count);
gl.uniform1i(U.dark, darkBg);
gl.uniform1f(U.hue0, hues[0]);
gl.uniform1f(U.hue1, hues[1]);
gl.uniform1f(U.hue2, hues[2]);
gl.uniform1f(U.hue3, hues[3]);
gl.uniform1f(U.hue4, hues[4]);
gl.uniform1f(U.hue5, hues[5]);
gl.uniform1f(U.hue6, hues[6]);
gl.uniform1f(U.hue7, hues[7]);
gl.uniform1f(U.hue8, hues[8]);
gl.uniform1f(U.hue9, hues[9]);
gl.uniform1f(U.thick, parseFloat(document.getElementById('sThick').value));
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
// ---- Segmented controls ----
function setupSeg(id, cb) {
document.getElementById(id).addEventListener('click', e => {
const btn = e.target.closest('[data-v]');
if (!btn) return;
document.querySelectorAll(`#${id} button`).forEach(b =>
b.classList.toggle('active', b === btn));
cb(parseInt(btn.dataset.v));
});
}
function updatePickerVisibility() {
document.querySelectorAll('.ribbon-color').forEach(el => {
el.style.display = parseInt(el.dataset.i) < count ? '' : 'none';
});
}
updatePickerVisibility();
setupSeg('seg-count', v => { count = v; updatePickerVisibility(); });
setupSeg('seg-bg', v => {
darkBg = v;
document.body.style.background = v === 1 ? '#111' : '#e8e4df';
});
document.getElementById('sSpeed').addEventListener('input', e =>
document.getElementById('vSpeed').textContent =
parseFloat(e.target.value).toFixed(2));
document.getElementById('sThick').addEventListener('input', e =>
document.getElementById('vThick').textContent =
parseFloat(e.target.value).toFixed(2));
document.getElementById('color-pickers').addEventListener('input', e => {
const el = e.target.closest('.ribbon-color');
if (!el) return;
hues[parseInt(el.dataset.i)] = hexToHue(el.value);
});
// ---- Play / pause ----
function togglePlay() {
playing = !playing;
if (playing) lastTs = null;
document.getElementById('btnPlay').textContent = playing ? '⏸ Pause' : '▶ Play';
}
// ---- PNG export ----
function savePng() {
const a = document.createElement('a');
a.download = 'flow-ribbons.png';
a.href = canvas.toDataURL('image/png');
a.click();
}
// ---- Video export ----
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 = 'flow-ribbons.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');
}
}
</script>
</body>
</html>