fancy-ui-vue 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/dist/components/animated-testimonials/AnimatedTestimonials.vue.js +23 -12
- package/dist/components/bg-falling-stars/FallingStarsBg.vue.js +10 -1
- package/dist/components/bg-falling-stars/bg-falling-stars-core.d.ts +6 -0
- package/dist/components/bg-falling-stars/bg-falling-stars-core.js +24 -2
- package/dist/components/bg-stars/StarsBackground.vue.js +1 -1
- package/dist/components/bg-stars/StarsBackground.vue2.js +13 -1
- package/dist/components/bg-stars/bg-stars-core.d.ts +6 -0
- package/dist/components/bg-stars/bg-stars-core.js +21 -1
- package/dist/components/book/Book.vue.js +5 -83
- package/dist/components/book/Book.vue2.js +82 -1
- package/dist/components/box-reveal/BoxReveal.vue.js +5 -47
- package/dist/components/box-reveal/BoxReveal.vue2.js +48 -1
- package/dist/components/compare/StarField.vue.js +1 -1
- package/dist/components/container-text-flip/ContainerTextFlip.vue.js +1 -1
- package/dist/components/container-text-flip/ContainerTextFlip.vue2.js +14 -5
- package/dist/components/flickering-grid/FlickeringGrid.vue.js +19 -1
- package/dist/components/flickering-grid/flickering-grid-core.d.ts +6 -0
- package/dist/components/flickering-grid/flickering-grid-core.js +30 -1
- package/dist/components/flip-words/FlipWords.vue.js +1 -1
- package/dist/components/flip-words/FlipWords.vue2.js +14 -7
- package/dist/components/frost-glass/FrostGlass.vue.d.ts +31 -0
- package/dist/components/frost-glass/FrostGlass.vue.js +90 -0
- package/dist/components/frost-glass/FrostGlass.vue2.js +4 -0
- package/dist/components/frost-glass/frost-glass-core.d.ts +155 -0
- package/dist/components/frost-glass/frost-glass-core.js +820 -0
- package/dist/components/frost-glass/index.d.ts +2 -0
- package/dist/components/gift-reveal/GiftReveal.vue.d.ts +50 -0
- package/dist/components/gift-reveal/GiftReveal.vue.js +7 -0
- package/dist/components/gift-reveal/GiftReveal.vue2.js +177 -0
- package/dist/components/gift-reveal/gift-reveal-core.d.ts +109 -0
- package/dist/components/gift-reveal/gift-reveal-core.js +116 -0
- package/dist/components/gift-reveal/index.d.ts +2 -0
- package/dist/components/hyper-text/HyperText.vue.js +6 -97
- package/dist/components/hyper-text/HyperText.vue2.js +101 -0
- package/dist/components/letter-pullup/LetterPullup.vue.js +6 -38
- package/dist/components/letter-pullup/LetterPullup.vue2.js +40 -0
- package/dist/components/logo-cloud/AnimatedLogoCloud.vue.js +1 -1
- package/dist/components/matrix-rain/MatrixRain.vue.js +20 -1
- package/dist/components/matrix-rain/matrix-rain-core.d.ts +6 -0
- package/dist/components/matrix-rain/matrix-rain-core.js +28 -6
- package/dist/components/noise-reveal/NoiseReveal.vue.js +7 -0
- package/dist/components/noise-reveal/noise-reveal-core.js +23 -7
- package/dist/components/ornament/Ornament.vue.d.ts +29 -0
- package/dist/components/ornament/Ornament.vue.js +112 -0
- package/dist/components/ornament/Ornament.vue2.js +4 -0
- package/dist/components/ornament/index.d.ts +2 -0
- package/dist/components/ornament/ornament-core.d.ts +177 -0
- package/dist/components/ornament/ornament-core.js +861 -0
- package/dist/components/ripple/Ripple.vue.js +1 -1
- package/dist/components/ripple/Ripple.vue2.js +6 -1
- package/dist/components/snowfall/Snowfall.vue.d.ts +29 -0
- package/dist/components/snowfall/Snowfall.vue.js +81 -0
- package/dist/components/snowfall/Snowfall.vue2.js +4 -0
- package/dist/components/snowfall/index.d.ts +2 -0
- package/dist/components/snowfall/snowfall-core.d.ts +119 -0
- package/dist/components/snowfall/snowfall-core.js +483 -0
- package/dist/components/sparkles/Sparkles.vue.js +10 -1
- package/dist/components/sparkles/sparkles-core.d.ts +6 -0
- package/dist/components/sparkles/sparkles-core.js +26 -2
- package/dist/components/sparkles-text/SparklesText.vue.js +6 -88
- package/dist/components/sparkles-text/SparklesText.vue2.js +100 -0
- package/dist/components/string-lights/StringLights.vue.d.ts +35 -0
- package/dist/components/string-lights/StringLights.vue.js +7 -0
- package/dist/components/string-lights/StringLights.vue2.js +98 -0
- package/dist/components/string-lights/index.d.ts +2 -0
- package/dist/components/string-lights/string-lights-core.d.ts +157 -0
- package/dist/components/string-lights/string-lights-core.js +228 -0
- package/dist/components/terminal-text/TerminalText.vue.js +1 -1
- package/dist/components/terminal-text/TerminalText.vue2.js +36 -4
- package/dist/components/text-generate-effect/TextGenerateEffect.vue.js +5 -72
- package/dist/components/text-generate-effect/TextGenerateEffect.vue2.js +74 -1
- package/dist/components/text-reveal-card/TextRevealCard.vue.js +5 -101
- package/dist/components/text-reveal-card/TextRevealCard.vue2.js +98 -1
- package/dist/components/text-reveal-card/TextRevealStars.vue.js +1 -1
- package/dist/components/text-reveal-card/TextRevealStars.vue2.js +29 -12
- package/dist/index.d.ts +5 -0
- package/dist/index.js +393 -386
- package/dist/styles.css +1669 -185
- package/package.json +2 -2
- package/dist/components/hyper-text/HyperText.vue3.js +0 -5
- package/dist/components/letter-pullup/LetterPullup.vue3.js +0 -5
- package/dist/components/sparkles-text/SparklesText.vue3.js +0 -5
|
@@ -0,0 +1,861 @@
|
|
|
1
|
+
const ORNAMENT_DEFAULTS = {
|
|
2
|
+
color: "#b9c0cc",
|
|
3
|
+
finish: "gloss",
|
|
4
|
+
band: "none",
|
|
5
|
+
size: 120,
|
|
6
|
+
threadLength: 96,
|
|
7
|
+
swing: 0.2,
|
|
8
|
+
glow: 0.5,
|
|
9
|
+
seed: 1
|
|
10
|
+
};
|
|
11
|
+
const MAX_PIXEL_RATIO = 2;
|
|
12
|
+
const MAX_STEP = 1 / 20;
|
|
13
|
+
const SUB_STEP = 1 / 120;
|
|
14
|
+
const GRAVITY = 2600;
|
|
15
|
+
const SWING_DAMPING = 0.085;
|
|
16
|
+
const TWIST_FREQUENCY = 2.1;
|
|
17
|
+
const TWIST_DAMPING = 0.14;
|
|
18
|
+
const TWIST_COUPLING = 1.4;
|
|
19
|
+
const IDLE_AMPLITUDE = 0.16;
|
|
20
|
+
const REST_EPSILON = 15e-4;
|
|
21
|
+
const TAU = Math.PI * 2;
|
|
22
|
+
function finiteOr(value, fallback) {
|
|
23
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
24
|
+
}
|
|
25
|
+
function ornamentGeometry(size, threadLength) {
|
|
26
|
+
const d = Math.max(8, finiteOr(size, ORNAMENT_DEFAULTS.size));
|
|
27
|
+
const thread = Math.max(0, finiteOr(threadLength, ORNAMENT_DEFAULTS.threadLength));
|
|
28
|
+
const radius = d / 2;
|
|
29
|
+
const loopRadius = d * 0.045;
|
|
30
|
+
const loopTube = Math.max(0.75, d * 0.011);
|
|
31
|
+
const capTop = thread + loopRadius * 2 - loopTube;
|
|
32
|
+
const capHeight = d * 0.11;
|
|
33
|
+
const capWidth = d * 0.26;
|
|
34
|
+
const arm = capTop + capHeight * 0.62 + radius;
|
|
35
|
+
const width = d * 2;
|
|
36
|
+
const height = Math.ceil(arm + radius + d * 0.36);
|
|
37
|
+
const room = width / 2 - radius * 1.04;
|
|
38
|
+
const maxAngle = Math.asin(Math.min(1, Math.max(0, room / arm)));
|
|
39
|
+
return {
|
|
40
|
+
width,
|
|
41
|
+
height,
|
|
42
|
+
radius,
|
|
43
|
+
loopRadius,
|
|
44
|
+
loopTube,
|
|
45
|
+
capTop,
|
|
46
|
+
capHeight,
|
|
47
|
+
capWidth,
|
|
48
|
+
arm,
|
|
49
|
+
maxAngle
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function fallbackSphere(color) {
|
|
53
|
+
return `radial-gradient(circle at 34% 30%, rgb(255 255 255 / 0.9), ${color} 24%, color-mix(in srgb, ${color} 55%, black) 72%, color-mix(in srgb, ${color} 30%, black))`;
|
|
54
|
+
}
|
|
55
|
+
function parseHexColor(value, fallback) {
|
|
56
|
+
if (!value) return [fallback[0], fallback[1], fallback[2]];
|
|
57
|
+
const hex = value.trim().replace(/^#/, "");
|
|
58
|
+
const full = hex.length === 3 ? hex.split("").map((c) => c + c).join("") : hex;
|
|
59
|
+
if (!/^[0-9a-fA-F]{6}$/.test(full)) return [fallback[0], fallback[1], fallback[2]];
|
|
60
|
+
const n = Number.parseInt(full, 16);
|
|
61
|
+
return [(n >> 16 & 255) / 255, (n >> 8 & 255) / 255, (n & 255) / 255];
|
|
62
|
+
}
|
|
63
|
+
const GOLD = [0.93, 0.76, 0.46];
|
|
64
|
+
const SILVER = [0.86, 0.88, 0.91];
|
|
65
|
+
function metalFor(color) {
|
|
66
|
+
const [r, g, b] = color;
|
|
67
|
+
const max = Math.max(r, g, b);
|
|
68
|
+
const min = Math.min(r, g, b);
|
|
69
|
+
const chroma = max - min;
|
|
70
|
+
let hue = 0;
|
|
71
|
+
if (chroma > 0) {
|
|
72
|
+
if (max === r) hue = ((g - b) / chroma + 6) % 6;
|
|
73
|
+
else if (max === g) hue = (b - r) / chroma + 2;
|
|
74
|
+
else hue = (r - g) / chroma + 4;
|
|
75
|
+
}
|
|
76
|
+
const degrees = hue * 60;
|
|
77
|
+
const saturation = max === 0 ? 0 : chroma / max;
|
|
78
|
+
const warm = degrees >= 28 && degrees <= 72 && saturation > 0.3;
|
|
79
|
+
const metal = warm ? SILVER : GOLD;
|
|
80
|
+
return [metal[0], metal[1], metal[2]];
|
|
81
|
+
}
|
|
82
|
+
function mulberry32(seed) {
|
|
83
|
+
let a = Math.floor(finiteOr(seed, 1)) >>> 0;
|
|
84
|
+
return () => {
|
|
85
|
+
a = a + 1831565813 >>> 0;
|
|
86
|
+
let t = a;
|
|
87
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
88
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
89
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const PENDULUM_REST = {
|
|
93
|
+
angle: 0,
|
|
94
|
+
velocity: 0,
|
|
95
|
+
twist: 0,
|
|
96
|
+
twistVelocity: 0,
|
|
97
|
+
time: 0
|
|
98
|
+
};
|
|
99
|
+
function swingFrequency(arm) {
|
|
100
|
+
return Math.sqrt(GRAVITY / Math.max(20, arm));
|
|
101
|
+
}
|
|
102
|
+
function breeze(time, swing, phase, maxAngle) {
|
|
103
|
+
const amount = Math.min(Math.max(finiteOr(swing, 0), 0), 1);
|
|
104
|
+
if (amount === 0) return 0;
|
|
105
|
+
const wave = 0.68 * Math.sin(0.83 * time + phase) + 0.32 * Math.sin(1.37 * time + phase * 2.1);
|
|
106
|
+
return wave * Math.min(IDLE_AMPLITUDE * amount, maxAngle * 0.6);
|
|
107
|
+
}
|
|
108
|
+
function softLimit(angle, limit) {
|
|
109
|
+
if (limit <= 0) return 0;
|
|
110
|
+
return limit * Math.tanh(angle / limit);
|
|
111
|
+
}
|
|
112
|
+
function stepPendulum(state, dt, params) {
|
|
113
|
+
let remaining = Math.min(Math.max(dt, 0), MAX_STEP);
|
|
114
|
+
let { angle, velocity, twist, twistVelocity, time } = state;
|
|
115
|
+
const omega = swingFrequency(params.arm);
|
|
116
|
+
const swinging = params.swing > 0;
|
|
117
|
+
while (remaining > 1e-9) {
|
|
118
|
+
const h = Math.min(SUB_STEP, remaining);
|
|
119
|
+
remaining -= h;
|
|
120
|
+
if (swinging) time += h;
|
|
121
|
+
if (!params.dragging) {
|
|
122
|
+
const target = breeze(time, params.swing, params.phase, params.maxAngle);
|
|
123
|
+
const accel = -omega * omega * Math.sin(angle - target) - 2 * SWING_DAMPING * omega * velocity;
|
|
124
|
+
velocity += accel * h;
|
|
125
|
+
angle += velocity * h;
|
|
126
|
+
if (Math.abs(angle) > params.maxAngle) {
|
|
127
|
+
angle = Math.sign(angle) * params.maxAngle;
|
|
128
|
+
if (Math.sign(velocity) === Math.sign(angle)) velocity *= -0.35;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const twistAccel = -TWIST_FREQUENCY * TWIST_FREQUENCY * twist - 2 * TWIST_DAMPING * TWIST_FREQUENCY * twistVelocity + TWIST_COUPLING * velocity;
|
|
132
|
+
twistVelocity += twistAccel * h;
|
|
133
|
+
twist += twistVelocity * h;
|
|
134
|
+
}
|
|
135
|
+
if (!swinging && !params.dragging && isSettled({ angle, velocity, twist, twistVelocity })) {
|
|
136
|
+
return { ...PENDULUM_REST, time };
|
|
137
|
+
}
|
|
138
|
+
return { angle, velocity, twist, twistVelocity, time };
|
|
139
|
+
}
|
|
140
|
+
function isSettled(state) {
|
|
141
|
+
return Math.abs(state.angle) < REST_EPSILON && Math.abs(state.velocity) < REST_EPSILON * 4 && Math.abs(state.twist) < REST_EPSILON * 4 && Math.abs(state.twistVelocity) < REST_EPSILON * 8;
|
|
142
|
+
}
|
|
143
|
+
function isMoving(state, swing, dragging) {
|
|
144
|
+
return dragging || swing > 0 || !isSettled(state);
|
|
145
|
+
}
|
|
146
|
+
const VERTEX_SHADER = `
|
|
147
|
+
attribute vec2 aPos;
|
|
148
|
+
void main() { gl_Position = vec4(aPos, 0.0, 1.0); }
|
|
149
|
+
`;
|
|
150
|
+
const FRAGMENT_SHADER = `
|
|
151
|
+
precision highp float;
|
|
152
|
+
|
|
153
|
+
uniform vec2 uRes;
|
|
154
|
+
uniform float uScale;
|
|
155
|
+
uniform float uAngle;
|
|
156
|
+
uniform float uTwist;
|
|
157
|
+
uniform float uRadius;
|
|
158
|
+
uniform float uArm;
|
|
159
|
+
uniform float uThread;
|
|
160
|
+
uniform float uLoopR;
|
|
161
|
+
uniform float uLoopTube;
|
|
162
|
+
uniform float uCapTop;
|
|
163
|
+
uniform float uCapH;
|
|
164
|
+
uniform float uCapW;
|
|
165
|
+
uniform vec3 uColor;
|
|
166
|
+
uniform vec3 uMetal;
|
|
167
|
+
uniform float uFinish;
|
|
168
|
+
uniform float uBand;
|
|
169
|
+
uniform float uGlow;
|
|
170
|
+
uniform float uSeed;
|
|
171
|
+
|
|
172
|
+
const vec3 KEY = vec3(-0.5, 0.62, 0.6);
|
|
173
|
+
const vec3 VIEW = vec3(0.0, 0.0, 1.0);
|
|
174
|
+
|
|
175
|
+
float box(vec2 p, vec2 c, vec2 h, float soft) {
|
|
176
|
+
vec2 d = abs(p - c) - h;
|
|
177
|
+
float outside = length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
|
|
178
|
+
return 1.0 - smoothstep(-soft, soft, outside);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// The studio the sphere reflects. dir is in world space: y up, z toward the viewer.
|
|
182
|
+
vec3 studio(vec3 dir) {
|
|
183
|
+
float up = dir.y;
|
|
184
|
+
// Ambient: dark charcoal walls, a little lighter overhead.
|
|
185
|
+
vec3 c = mix(vec3(0.045, 0.047, 0.055), vec3(0.11, 0.115, 0.13), smoothstep(-0.2, 0.9, up));
|
|
186
|
+
// Warm floor bounce.
|
|
187
|
+
c += vec3(0.34, 0.27, 0.21) * smoothstep(-0.05, -0.75, up);
|
|
188
|
+
vec2 f = dir.xy / max(0.35, dir.z + 0.65);
|
|
189
|
+
// The front lights fade out smoothly as the direction turns away from the
|
|
190
|
+
// viewer. A hard cut here left a sharp wedge in reflections near the cut,
|
|
191
|
+
// and the fade ends before the projection below starts to stretch.
|
|
192
|
+
float front = smoothstep(-0.25, 0.05, dir.z);
|
|
193
|
+
if (front > 0.0) {
|
|
194
|
+
vec3 lit = vec3(0.0);
|
|
195
|
+
// Key soft box, upper left, with a brighter centre.
|
|
196
|
+
float key = box(f, vec2(-0.46, 0.5), vec2(0.2, 0.15), 0.06);
|
|
197
|
+
lit += vec3(2.6, 2.5, 2.35) * key * (0.75 + 0.25 * box(f, vec2(-0.46, 0.5), vec2(0.08, 0.05), 0.08));
|
|
198
|
+
// A window, upper right: four panes and their bars.
|
|
199
|
+
vec2 w = f - vec2(0.52, 0.42);
|
|
200
|
+
float pane = box(w, vec2(0.0), vec2(0.15, 0.17), 0.025);
|
|
201
|
+
float bars = max(1.0 - smoothstep(0.006, 0.016, abs(w.x)), 1.0 - smoothstep(0.006, 0.016, abs(w.y)));
|
|
202
|
+
lit += vec3(1.05, 1.12, 1.25) * pane * (1.0 - bars * 0.9);
|
|
203
|
+
// A low fill card on the right.
|
|
204
|
+
lit += vec3(0.32, 0.33, 0.36) * box(f, vec2(0.7, -0.15), vec2(0.1, 0.25), 0.12);
|
|
205
|
+
c += lit * front;
|
|
206
|
+
}
|
|
207
|
+
// Two strip lights behind, which is where the rim catches.
|
|
208
|
+
float back = smoothstep(0.1, -0.5, dir.z);
|
|
209
|
+
c += vec3(1.15, 1.12, 1.08) * back * (1.0 - smoothstep(0.08, 0.2, abs(dir.x + 0.72))) * smoothstep(-0.6, 0.2, up);
|
|
210
|
+
c += vec3(0.75, 0.8, 0.9) * back * (1.0 - smoothstep(0.06, 0.18, abs(dir.x - 0.78))) * smoothstep(-0.5, 0.3, up);
|
|
211
|
+
return c;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Light arriving from a whole hemisphere around n: a blurred studio, for velvet.
|
|
215
|
+
vec3 studioDiffuse(vec3 n) {
|
|
216
|
+
float key = max(dot(n, normalize(KEY)), 0.0);
|
|
217
|
+
float window = max(dot(n, normalize(vec3(0.55, 0.45, 0.7))), 0.0);
|
|
218
|
+
float bounce = max(-n.y, 0.0);
|
|
219
|
+
return vec3(0.06) + vec3(1.0, 0.97, 0.93) * key * 0.9 + vec3(0.42, 0.45, 0.52) * window * 0.5 +
|
|
220
|
+
vec3(0.3, 0.24, 0.18) * bounce * 0.6;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// The studio seen in brushed metal: the same lights as smooth lobes, with no
|
|
224
|
+
// hard-edged panes. A narrow band that mirrored the sharp window would show a
|
|
225
|
+
// clipped sliver of it at its edge, which reads as a seam rather than a light.
|
|
226
|
+
vec3 studioSoft(vec3 dir) {
|
|
227
|
+
vec3 c = mix(vec3(0.05, 0.052, 0.06), vec3(0.12, 0.125, 0.14), smoothstep(-0.2, 0.9, dir.y));
|
|
228
|
+
c += vec3(0.3, 0.24, 0.18) * smoothstep(-0.05, -0.75, dir.y);
|
|
229
|
+
c += vec3(1.6, 1.55, 1.45) * pow(max(dot(dir, normalize(KEY)), 0.0), 10.0);
|
|
230
|
+
c += vec3(0.7, 0.75, 0.85) * pow(max(dot(dir, normalize(vec3(0.55, 0.45, 0.7))), 0.0), 12.0);
|
|
231
|
+
c += vec3(0.55, 0.55, 0.55) * pow(max(dot(dir, normalize(vec3(-0.72, 0.2, -0.65))), 0.0), 6.0);
|
|
232
|
+
return c;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
float hash31(vec3 p) {
|
|
236
|
+
p = fract(p * vec3(0.1031, 0.1030, 0.0973) + uSeed * 0.013);
|
|
237
|
+
p += dot(p, p.yxz + 33.33);
|
|
238
|
+
return fract((p.x + p.y) * p.z);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
vec3 hash33(vec3 p) {
|
|
242
|
+
return vec3(hash31(p), hash31(p + 17.1), hash31(p + 41.7));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Premultiplied "over".
|
|
246
|
+
vec4 over(vec4 top, vec4 under) {
|
|
247
|
+
return top + under * (1.0 - top.a);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
vec3 metal(vec3 n, vec3 tone) {
|
|
251
|
+
vec3 r = reflect(-VIEW, n);
|
|
252
|
+
vec3 env = studio(r);
|
|
253
|
+
float spec = pow(max(dot(r, normalize(KEY)), 0.0), 60.0);
|
|
254
|
+
// Polished metal: the tone multiplies what it reflects, plus a floor of
|
|
255
|
+
// its own colour so a dark corner of the studio does not turn it brown.
|
|
256
|
+
float face = n.z;
|
|
257
|
+
return tone * (0.42 + 0.25 * face + env * 0.9) + vec3(spec * 1.4);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
vec3 shadeSphere(vec3 n, vec3 nw, vec3 obj, float px) {
|
|
261
|
+
vec3 r = reflect(-VIEW, nw);
|
|
262
|
+
float facing = n.z;
|
|
263
|
+
float fresnel = 0.05 + 0.95 * pow(1.0 - facing, 5.0);
|
|
264
|
+
vec3 L = normalize(KEY);
|
|
265
|
+
vec3 col;
|
|
266
|
+
|
|
267
|
+
if (uFinish < 0.5) {
|
|
268
|
+
// Gloss: coloured glass over a silvered inside. The body is the colour,
|
|
269
|
+
// deepened toward the core; the inside throws light back out on the far
|
|
270
|
+
// side, opposite the key; a clear coat reflects the studio on top.
|
|
271
|
+
vec3 deep = uColor * uColor;
|
|
272
|
+
vec3 body = mix(deep * 0.45, uColor * 0.85, pow(1.0 - facing, 1.6));
|
|
273
|
+
vec3 silvered = studio(r) * uColor * 0.55;
|
|
274
|
+
float inner = pow(max(dot(nw, normalize(vec3(0.5, -0.62, 0.6))), 0.0), 2.5);
|
|
275
|
+
vec3 glow = mix(uColor, vec3(1.0), 0.12) * inner * 0.72;
|
|
276
|
+
col = body + silvered + glow;
|
|
277
|
+
col = mix(col, studio(r), fresnel * 0.85);
|
|
278
|
+
col += vec3(1.0, 0.98, 0.95) * pow(max(dot(r, L), 0.0), 900.0) * 3.0;
|
|
279
|
+
} else if (uFinish < 1.5) {
|
|
280
|
+
// Matte: velvet. Soft light from the whole room, a sheen that grows toward
|
|
281
|
+
// the edge instead of a mirror, and a broad, faint highlight.
|
|
282
|
+
vec3 diffuse = studioDiffuse(nw);
|
|
283
|
+
float sheen = pow(1.0 - facing, 2.2);
|
|
284
|
+
col = uColor * diffuse * 0.82;
|
|
285
|
+
col += mix(uColor, vec3(1.0), 0.45) * sheen * 0.42;
|
|
286
|
+
col += vec3(1.0, 0.97, 0.92) * pow(max(dot(r, L), 0.0), 7.0) * 0.12;
|
|
287
|
+
col *= 0.85 + 0.15 * smoothstep(-0.9, 0.4, nw.y);
|
|
288
|
+
} else {
|
|
289
|
+
// Glitter: a dark lacquer under thousands of tiny tilted flakes. Each
|
|
290
|
+
// flake lives on the turning sphere, so as it turns, a different few line
|
|
291
|
+
// up between the lights and the viewer.
|
|
292
|
+
float cells = max(8.0, uRadius / (2.6 * px));
|
|
293
|
+
vec3 g = obj * cells;
|
|
294
|
+
vec3 cell = floor(g);
|
|
295
|
+
vec3 jitter = hash33(cell) * 2.0 - 1.0;
|
|
296
|
+
vec3 nf = normalize(nw + jitter * 0.6);
|
|
297
|
+
vec3 rf = reflect(-VIEW, nf);
|
|
298
|
+
float lit = hash31(cell + 7.0);
|
|
299
|
+
// Each flake is a round dot of random size somewhere in its cell, not the cell itself.
|
|
300
|
+
vec3 centre = 0.25 + 0.5 * hash33(cell + 3.0);
|
|
301
|
+
float flakeR = 0.18 + 0.22 * lit;
|
|
302
|
+
float soft = 0.5 * px * cells / uRadius + 0.04;
|
|
303
|
+
float dotMask = 1.0 - smoothstep(flakeR - soft, flakeR + soft, length(g - cell - centre));
|
|
304
|
+
float glint = smoothstep(0.86, 0.99, dot(rf, L)) * 3.0;
|
|
305
|
+
glint += smoothstep(0.86, 0.985, dot(rf, normalize(vec3(0.55, 0.45, 0.7)))) * 1.6;
|
|
306
|
+
glint += smoothstep(0.8, 0.97, dot(rf, normalize(vec3(-0.75, 0.25, -0.4)))) * 1.1;
|
|
307
|
+
vec3 flake = mix(uColor, vec3(1.0), 0.3) * (0.3 + 0.7 * lit) * studio(rf) * 0.9;
|
|
308
|
+
vec3 lacquer = uColor * uColor * 0.4 + uColor * studioDiffuse(nw) * 0.4;
|
|
309
|
+
col = lacquer + flake * (0.35 + 0.65 * dotMask);
|
|
310
|
+
col += mix(uColor, vec3(1.0), 0.65) * glint * dotMask * step(0.3, lit);
|
|
311
|
+
col = mix(col, studio(r) * 0.6, fresnel * 0.5);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (uBand > 0.5) {
|
|
315
|
+
// The band lives on the turning sphere: a metal stripe on the equator,
|
|
316
|
+
// or a ring of studs that shows the turn.
|
|
317
|
+
float half_ = 0.11;
|
|
318
|
+
float edge = 1.5 * px / uRadius;
|
|
319
|
+
float stripe = 1.0 - smoothstep(half_ - edge, half_ + edge, abs(obj.y));
|
|
320
|
+
float mask = stripe;
|
|
321
|
+
if (uBand > 1.5) {
|
|
322
|
+
float a = atan(obj.x, obj.z);
|
|
323
|
+
float slots = 18.0;
|
|
324
|
+
float s = (fract(a / 6.2831853 * slots) - 0.5) * 6.2831853 / slots;
|
|
325
|
+
vec2 d = vec2(s, obj.y / max(0.2, length(obj.xz)));
|
|
326
|
+
float rDot = 0.075;
|
|
327
|
+
mask = 1.0 - smoothstep(rDot - edge * 1.5, rDot + edge * 1.5, length(d));
|
|
328
|
+
} else {
|
|
329
|
+
float groove = 1.0 - smoothstep(0.0, edge * 1.2, abs(abs(obj.y) - half_ * 0.62));
|
|
330
|
+
mask *= 1.0 - 0.35 * groove;
|
|
331
|
+
}
|
|
332
|
+
vec3 rb = reflect(-VIEW, nw);
|
|
333
|
+
vec3 band = uMetal * (0.42 + 0.25 * facing + studioSoft(rb) * 0.9);
|
|
334
|
+
band += vec3(0.6) * pow(max(dot(rb, L), 0.0), 24.0);
|
|
335
|
+
band *= mix(0.85, 1.0, facing);
|
|
336
|
+
col = mix(col, band, mask * smoothstep(0.0, 0.08, facing));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return col;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
vec4 shade(vec2 frag) {
|
|
343
|
+
// Screen pixels with y down, then the pendulum's frame: pivot at the top
|
|
344
|
+
// centre, x across the thread, y down it.
|
|
345
|
+
vec2 p = vec2(frag.x, uRes.y - frag.y);
|
|
346
|
+
vec2 d = p - vec2(0.5 * uRes.x, 0.0);
|
|
347
|
+
float ca = cos(uAngle), sa = sin(uAngle);
|
|
348
|
+
vec2 l = vec2(d.x * ca - d.y * sa, d.x * sa + d.y * ca);
|
|
349
|
+
float px = uScale;
|
|
350
|
+
vec4 acc = vec4(0.0);
|
|
351
|
+
|
|
352
|
+
// Halo, behind everything.
|
|
353
|
+
vec2 fromCentre = l - vec2(0.0, uArm);
|
|
354
|
+
float dist = length(fromCentre) / uRadius;
|
|
355
|
+
if (uGlow > 0.0 && dist > 0.9) {
|
|
356
|
+
// Falls to exactly zero well inside the box, so no canvas edge ever shows.
|
|
357
|
+
float halo = exp(-(dist - 1.0) * 3.0) * smoothstep(0.9, 1.02, dist) * (1.0 - smoothstep(1.05, 1.6, dist));
|
|
358
|
+
// A whisper of noise breaks the 8-bit banding of a faint gradient on a light page.
|
|
359
|
+
float grain = fract(sin(dot(frag, vec2(12.9898, 78.233))) * 43758.5453) - 0.5;
|
|
360
|
+
float a = clamp(uGlow * 0.28 * halo + grain / 255.0, 0.0, 1.0);
|
|
361
|
+
vec3 tint = mix(uColor, vec3(1.0), 0.15);
|
|
362
|
+
acc = vec4(tint * a, a);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Thread, about one CSS pixel wide.
|
|
366
|
+
if (l.y > -px && l.y < uThread + px) {
|
|
367
|
+
float w = 0.5 * px;
|
|
368
|
+
float cover = 1.0 - smoothstep(w - 0.5, w + 0.5, abs(l.x));
|
|
369
|
+
cover *= smoothstep(-0.5, 0.5, l.y) * (1.0 - smoothstep(uThread - 0.5, uThread + 0.5, l.y));
|
|
370
|
+
float a = cover * 0.8;
|
|
371
|
+
acc = over(vec4(vec3(0.6, 0.6, 0.62) * a, a), acc);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// The loop the thread goes through.
|
|
375
|
+
vec2 lc = vec2(0.0, uThread + uLoopR);
|
|
376
|
+
float ring = abs(length(l - lc) - uLoopR + 0.5 * uLoopTube);
|
|
377
|
+
if (ring < uLoopTube + 1.0) {
|
|
378
|
+
float cover = 1.0 - smoothstep(0.5 * uLoopTube - 0.5, 0.5 * uLoopTube + 0.5, ring);
|
|
379
|
+
// Tube normal: across the tube in the plane, out of it at the crest.
|
|
380
|
+
vec2 rn = normalize(l - lc + 1e-4);
|
|
381
|
+
float across = clamp((length(l - lc) - (uLoopR - 0.5 * uLoopTube)) / max(0.5, 0.5 * uLoopTube), -1.0, 1.0);
|
|
382
|
+
vec2 up = vec2(rn.x, -rn.y) * across;
|
|
383
|
+
vec3 nw = vec3(up.x * ca - up.y * sa, up.x * sa + up.y * ca, sqrt(max(0.0, 1.0 - across * across)));
|
|
384
|
+
acc = over(vec4(metal(nw, uMetal) * cover, cover), acc);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// The sphere.
|
|
388
|
+
vec2 q = vec2(fromCentre.x, -fromCentre.y) / uRadius;
|
|
389
|
+
float r2 = dot(q, q);
|
|
390
|
+
float edge = px / uRadius;
|
|
391
|
+
if (r2 < (1.0 + edge) * (1.0 + edge)) {
|
|
392
|
+
float cover = 1.0 - smoothstep(1.0 - edge, 1.0 + edge * 0.5, sqrt(r2));
|
|
393
|
+
vec2 qc = q * min(1.0, 0.999 / max(sqrt(r2), 1e-4));
|
|
394
|
+
vec3 n = vec3(qc, sqrt(max(0.0, 1.0 - dot(qc, qc))));
|
|
395
|
+
// World normal: the local frame is turned by the swing.
|
|
396
|
+
vec3 nw = vec3(n.x * ca - n.y * sa, n.x * sa + n.y * ca, n.z);
|
|
397
|
+
// Object space: the sphere also turns around its thread.
|
|
398
|
+
float ct = cos(uTwist), st = sin(uTwist);
|
|
399
|
+
vec3 obj = vec3(n.x * ct - n.z * st, n.y, n.x * st + n.z * ct);
|
|
400
|
+
vec3 col = shadeSphere(n, nw, obj, px);
|
|
401
|
+
// The cap shades the very top of the sphere.
|
|
402
|
+
float neck = smoothstep(0.86, 1.0, n.y) * (1.0 - smoothstep(0.0, 0.45, abs(n.x)));
|
|
403
|
+
col *= 1.0 - 0.45 * neck;
|
|
404
|
+
acc = over(vec4(col * cover, cover), acc);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// The cap: a short fluted metal cylinder with a crown of prongs.
|
|
408
|
+
float halfW = 0.5 * uCapW;
|
|
409
|
+
if (abs(l.x) < halfW + 1.0 && l.y > uCapTop - 1.0 && l.y < uCapTop + uCapH * 1.4) {
|
|
410
|
+
float u = clamp(l.x / halfW, -1.0, 1.0);
|
|
411
|
+
float prong = 0.5 + 0.5 * cos(u * 3.14159 * 4.0);
|
|
412
|
+
float bottom = uCapTop + uCapH * (0.82 + 0.38 * prong * prong);
|
|
413
|
+
float cover = (1.0 - smoothstep(halfW - 0.5, halfW + 0.5, abs(l.x)));
|
|
414
|
+
cover *= smoothstep(uCapTop - 0.5, uCapTop + 0.5, l.y) * (1.0 - smoothstep(bottom - 0.6, bottom + 0.6, l.y));
|
|
415
|
+
vec3 n = vec3(u, 0.0, sqrt(max(0.0, 1.0 - u * u)));
|
|
416
|
+
vec3 nw = vec3(n.x * ca, n.x * sa, n.z);
|
|
417
|
+
vec3 col = metal(normalize(nw), uMetal);
|
|
418
|
+
float flute = 0.5 + 0.5 * cos(u * 3.14159 * 9.0);
|
|
419
|
+
col *= 0.78 + 0.22 * flute;
|
|
420
|
+
float rim = 1.0 - smoothstep(0.0, uCapH * 0.22, l.y - uCapTop);
|
|
421
|
+
col *= 1.0 - 0.3 * rim;
|
|
422
|
+
acc = over(vec4(col * cover, cover), acc);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return acc;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
void main() {
|
|
429
|
+
// Four rotated-grid taps: every edge is analytic, so this is the antialiasing.
|
|
430
|
+
vec4 acc = shade(gl_FragCoord.xy + vec2(-0.375, -0.125));
|
|
431
|
+
acc += shade(gl_FragCoord.xy + vec2(0.125, -0.375));
|
|
432
|
+
acc += shade(gl_FragCoord.xy + vec2(0.375, 0.125));
|
|
433
|
+
acc += shade(gl_FragCoord.xy + vec2(-0.125, 0.375));
|
|
434
|
+
acc *= 0.25;
|
|
435
|
+
// Gentle filmic shoulder so the soft boxes do not clip flat.
|
|
436
|
+
acc.rgb = acc.rgb / (1.0 + max(vec3(0.0), acc.rgb - acc.a) * 0.6);
|
|
437
|
+
gl_FragColor = vec4(min(acc.rgb, vec3(acc.a)), acc.a);
|
|
438
|
+
}
|
|
439
|
+
`;
|
|
440
|
+
const UNIFORMS = [
|
|
441
|
+
"uRes",
|
|
442
|
+
"uScale",
|
|
443
|
+
"uAngle",
|
|
444
|
+
"uTwist",
|
|
445
|
+
"uRadius",
|
|
446
|
+
"uArm",
|
|
447
|
+
"uThread",
|
|
448
|
+
"uLoopR",
|
|
449
|
+
"uLoopTube",
|
|
450
|
+
"uCapTop",
|
|
451
|
+
"uCapH",
|
|
452
|
+
"uCapW",
|
|
453
|
+
"uColor",
|
|
454
|
+
"uMetal",
|
|
455
|
+
"uFinish",
|
|
456
|
+
"uBand",
|
|
457
|
+
"uGlow",
|
|
458
|
+
"uSeed"
|
|
459
|
+
];
|
|
460
|
+
function compile(gl, type, source) {
|
|
461
|
+
const shader = gl.createShader(type);
|
|
462
|
+
if (!shader) return null;
|
|
463
|
+
gl.shaderSource(shader, source);
|
|
464
|
+
gl.compileShader(shader);
|
|
465
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
466
|
+
gl.deleteShader(shader);
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
return shader;
|
|
470
|
+
}
|
|
471
|
+
function buildResources(gl) {
|
|
472
|
+
const vertex = compile(gl, gl.VERTEX_SHADER, VERTEX_SHADER);
|
|
473
|
+
const fragment = compile(gl, gl.FRAGMENT_SHADER, FRAGMENT_SHADER);
|
|
474
|
+
const program = gl.createProgram();
|
|
475
|
+
if (!vertex || !fragment || !program) {
|
|
476
|
+
if (vertex) gl.deleteShader(vertex);
|
|
477
|
+
if (fragment) gl.deleteShader(fragment);
|
|
478
|
+
if (program) gl.deleteProgram(program);
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
gl.attachShader(program, vertex);
|
|
482
|
+
gl.attachShader(program, fragment);
|
|
483
|
+
gl.bindAttribLocation(program, 0, "aPos");
|
|
484
|
+
gl.linkProgram(program);
|
|
485
|
+
gl.deleteShader(vertex);
|
|
486
|
+
gl.deleteShader(fragment);
|
|
487
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
488
|
+
gl.deleteProgram(program);
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
gl.useProgram(program);
|
|
492
|
+
const buffer = gl.createBuffer();
|
|
493
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
494
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
|
|
495
|
+
gl.enableVertexAttribArray(0);
|
|
496
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
497
|
+
const uniforms = {};
|
|
498
|
+
for (const name of UNIFORMS) uniforms[name] = gl.getUniformLocation(program, name);
|
|
499
|
+
gl.disable(gl.DEPTH_TEST);
|
|
500
|
+
gl.clearColor(0, 0, 0, 0);
|
|
501
|
+
return { program, buffer, uniforms };
|
|
502
|
+
}
|
|
503
|
+
const FINISH_INDEX = { gloss: 0, matte: 1, glitter: 2 };
|
|
504
|
+
const BAND_INDEX = { none: 0, stripe: 1, dots: 2 };
|
|
505
|
+
const HOVER_GAIN = 9e-4;
|
|
506
|
+
const HOVER_MAX = 0.35;
|
|
507
|
+
const KEY_IMPULSE = 0.9;
|
|
508
|
+
function createOrnament(elements, options = {}) {
|
|
509
|
+
const { canvas } = elements;
|
|
510
|
+
const root = elements.root ?? null;
|
|
511
|
+
const releaseContext = options.releaseContext ?? true;
|
|
512
|
+
let gl = null;
|
|
513
|
+
try {
|
|
514
|
+
gl = canvas.getContext("webgl", {
|
|
515
|
+
alpha: true,
|
|
516
|
+
premultipliedAlpha: true,
|
|
517
|
+
antialias: false,
|
|
518
|
+
powerPreference: "low-power"
|
|
519
|
+
});
|
|
520
|
+
} catch {
|
|
521
|
+
gl = null;
|
|
522
|
+
}
|
|
523
|
+
if (!gl) return null;
|
|
524
|
+
const context = gl;
|
|
525
|
+
let resources = buildResources(context);
|
|
526
|
+
if (!resources) return null;
|
|
527
|
+
const live = {
|
|
528
|
+
color: options.color ?? ORNAMENT_DEFAULTS.color,
|
|
529
|
+
finish: options.finish ?? ORNAMENT_DEFAULTS.finish,
|
|
530
|
+
band: options.band ?? ORNAMENT_DEFAULTS.band,
|
|
531
|
+
size: options.size ?? ORNAMENT_DEFAULTS.size,
|
|
532
|
+
threadLength: options.threadLength ?? ORNAMENT_DEFAULTS.threadLength,
|
|
533
|
+
swing: options.swing ?? ORNAMENT_DEFAULTS.swing,
|
|
534
|
+
interactive: options.interactive ?? true,
|
|
535
|
+
glow: options.glow ?? ORNAMENT_DEFAULTS.glow,
|
|
536
|
+
reducedMotion: options.reducedMotion ?? false
|
|
537
|
+
};
|
|
538
|
+
const random = mulberry32(options.seed ?? ORNAMENT_DEFAULTS.seed);
|
|
539
|
+
const restTurn = random() * TAU;
|
|
540
|
+
const phase = random() * TAU;
|
|
541
|
+
const seedOffset = Math.floor(random() * 997);
|
|
542
|
+
let geometry = ornamentGeometry(live.size, live.threadLength);
|
|
543
|
+
let state = { ...PENDULUM_REST, time: random() * 10 };
|
|
544
|
+
let destroyed = false;
|
|
545
|
+
let contextLost = false;
|
|
546
|
+
let frame = 0;
|
|
547
|
+
let last = 0;
|
|
548
|
+
let inView = true;
|
|
549
|
+
let pageVisible = typeof document === "undefined" || !document.hidden;
|
|
550
|
+
let dragging = false;
|
|
551
|
+
let dragPointer = -1;
|
|
552
|
+
let grabOffset = 0;
|
|
553
|
+
let dragAngle = 0;
|
|
554
|
+
let dragTime = 0;
|
|
555
|
+
let hoverX = Number.NaN;
|
|
556
|
+
let hoverTime = 0;
|
|
557
|
+
function canMove() {
|
|
558
|
+
return live.interactive && !live.reducedMotion;
|
|
559
|
+
}
|
|
560
|
+
function swing() {
|
|
561
|
+
return live.reducedMotion ? 0 : Math.min(Math.max(finiteOr(live.swing, 0), 0), 1);
|
|
562
|
+
}
|
|
563
|
+
function resize() {
|
|
564
|
+
const ratio = Math.min(
|
|
565
|
+
typeof window !== "undefined" && window.devicePixelRatio || 1,
|
|
566
|
+
MAX_PIXEL_RATIO
|
|
567
|
+
);
|
|
568
|
+
const width = Math.max(1, Math.round(canvas.clientWidth * ratio));
|
|
569
|
+
const height = Math.max(1, Math.round(canvas.clientHeight * ratio));
|
|
570
|
+
if (canvas.width === width && canvas.height === height) return false;
|
|
571
|
+
canvas.width = width;
|
|
572
|
+
canvas.height = height;
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
function draw() {
|
|
576
|
+
if (destroyed || contextLost || !resources) return;
|
|
577
|
+
resize();
|
|
578
|
+
const u = resources.uniforms;
|
|
579
|
+
const scale = canvas.width / Math.max(1, canvas.clientWidth || geometry.width);
|
|
580
|
+
const colour = parseHexColor(
|
|
581
|
+
live.color,
|
|
582
|
+
parseHexColor(ORNAMENT_DEFAULTS.color, [0.7, 0.7, 0.7])
|
|
583
|
+
);
|
|
584
|
+
const tone = metalFor(colour);
|
|
585
|
+
const g = geometry;
|
|
586
|
+
context.viewport(0, 0, canvas.width, canvas.height);
|
|
587
|
+
context.clear(context.COLOR_BUFFER_BIT);
|
|
588
|
+
context.uniform2f(u.uRes, canvas.width, canvas.height);
|
|
589
|
+
context.uniform1f(u.uScale, scale);
|
|
590
|
+
context.uniform1f(u.uAngle, live.reducedMotion ? 0 : state.angle);
|
|
591
|
+
context.uniform1f(u.uTwist, restTurn + (live.reducedMotion ? 0 : state.twist));
|
|
592
|
+
context.uniform1f(u.uRadius, g.radius * scale);
|
|
593
|
+
context.uniform1f(u.uArm, g.arm * scale);
|
|
594
|
+
context.uniform1f(u.uThread, (g.capTop - g.loopRadius * 2 + g.loopTube) * scale);
|
|
595
|
+
context.uniform1f(u.uLoopR, g.loopRadius * scale);
|
|
596
|
+
context.uniform1f(u.uLoopTube, Math.max(1, g.loopTube * scale));
|
|
597
|
+
context.uniform1f(u.uCapTop, g.capTop * scale);
|
|
598
|
+
context.uniform1f(u.uCapH, g.capHeight * scale);
|
|
599
|
+
context.uniform1f(u.uCapW, g.capWidth * scale);
|
|
600
|
+
context.uniform3f(u.uColor, colour[0], colour[1], colour[2]);
|
|
601
|
+
context.uniform3f(u.uMetal, tone[0], tone[1], tone[2]);
|
|
602
|
+
context.uniform1f(u.uFinish, FINISH_INDEX[live.finish] ?? 0);
|
|
603
|
+
context.uniform1f(u.uBand, BAND_INDEX[live.band] ?? 0);
|
|
604
|
+
context.uniform1f(u.uGlow, Math.min(Math.max(finiteOr(live.glow, 0), 0), 1));
|
|
605
|
+
context.uniform1f(u.uSeed, seedOffset);
|
|
606
|
+
context.drawArrays(context.TRIANGLES, 0, 3);
|
|
607
|
+
}
|
|
608
|
+
function params() {
|
|
609
|
+
return {
|
|
610
|
+
arm: geometry.arm,
|
|
611
|
+
maxAngle: geometry.maxAngle,
|
|
612
|
+
swing: swing(),
|
|
613
|
+
phase,
|
|
614
|
+
dragging
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
function shouldRun() {
|
|
618
|
+
return !destroyed && !contextLost && !live.reducedMotion && inView && pageVisible && isMoving(state, swing(), dragging);
|
|
619
|
+
}
|
|
620
|
+
function tick(now) {
|
|
621
|
+
frame = 0;
|
|
622
|
+
if (destroyed) return;
|
|
623
|
+
const dt = last === 0 ? 0 : (now - last) / 1e3;
|
|
624
|
+
last = now;
|
|
625
|
+
state = stepPendulum(state, dt, params());
|
|
626
|
+
draw();
|
|
627
|
+
if (shouldRun()) frame = requestAnimationFrame(tick);
|
|
628
|
+
else last = 0;
|
|
629
|
+
}
|
|
630
|
+
function wake() {
|
|
631
|
+
if (destroyed) return;
|
|
632
|
+
if (frame !== 0) return;
|
|
633
|
+
if (shouldRun()) {
|
|
634
|
+
last = 0;
|
|
635
|
+
frame = requestAnimationFrame(tick);
|
|
636
|
+
} else {
|
|
637
|
+
draw();
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
function local(event) {
|
|
641
|
+
const rect = canvas.getBoundingClientRect();
|
|
642
|
+
const width = canvas.clientWidth || geometry.width;
|
|
643
|
+
const height = canvas.clientHeight || geometry.height;
|
|
644
|
+
const scaleX = rect.width > 0 ? width / rect.width : 1;
|
|
645
|
+
const scaleY = rect.height > 0 ? height / rect.height : 1;
|
|
646
|
+
return {
|
|
647
|
+
x: (event.clientX - rect.left) * scaleX - width / 2,
|
|
648
|
+
y: (event.clientY - rect.top) * scaleY
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
function reach(x, y) {
|
|
652
|
+
const cx = Math.sin(state.angle) * geometry.arm;
|
|
653
|
+
const cy = Math.cos(state.angle) * geometry.arm;
|
|
654
|
+
return Math.hypot(x - cx, y - cy) / geometry.radius;
|
|
655
|
+
}
|
|
656
|
+
function onPointerDown(event) {
|
|
657
|
+
if (!canMove() || dragging) return;
|
|
658
|
+
if (event.pointerType === "mouse" && event.button !== 0) return;
|
|
659
|
+
const { x, y } = local(event);
|
|
660
|
+
if (reach(x, y) > 1.15) return;
|
|
661
|
+
dragging = true;
|
|
662
|
+
dragPointer = event.pointerId;
|
|
663
|
+
grabOffset = Math.atan2(x, Math.max(1, y)) - state.angle;
|
|
664
|
+
dragAngle = state.angle;
|
|
665
|
+
dragTime = event.timeStamp;
|
|
666
|
+
state = { ...state, velocity: 0 };
|
|
667
|
+
try {
|
|
668
|
+
canvas.setPointerCapture(event.pointerId);
|
|
669
|
+
} catch {
|
|
670
|
+
}
|
|
671
|
+
wake();
|
|
672
|
+
}
|
|
673
|
+
function onPointerMove(event) {
|
|
674
|
+
if (!canMove()) return;
|
|
675
|
+
const { x, y } = local(event);
|
|
676
|
+
if (dragging) {
|
|
677
|
+
if (event.pointerId !== dragPointer) return;
|
|
678
|
+
const target = softLimit(Math.atan2(x, Math.max(1, y)) - grabOffset, geometry.maxAngle);
|
|
679
|
+
const dt = Math.max(1, event.timeStamp - dragTime) / 1e3;
|
|
680
|
+
const speed = state.velocity * 0.6 + (target - dragAngle) / dt * 0.4;
|
|
681
|
+
state = { ...state, angle: target, velocity: speed };
|
|
682
|
+
dragAngle = target;
|
|
683
|
+
dragTime = event.timeStamp;
|
|
684
|
+
wake();
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const near = reach(x, y) < 1.1;
|
|
688
|
+
if (near && Number.isFinite(hoverX)) {
|
|
689
|
+
const dt = Math.max(8, event.timeStamp - hoverTime) / 1e3;
|
|
690
|
+
const push = Math.max(-HOVER_MAX, Math.min(HOVER_MAX, (x - hoverX) / dt * HOVER_GAIN));
|
|
691
|
+
state = {
|
|
692
|
+
...state,
|
|
693
|
+
velocity: state.velocity + push,
|
|
694
|
+
twistVelocity: state.twistVelocity + push * 0.8
|
|
695
|
+
};
|
|
696
|
+
wake();
|
|
697
|
+
}
|
|
698
|
+
hoverX = near ? x : Number.NaN;
|
|
699
|
+
hoverTime = event.timeStamp;
|
|
700
|
+
}
|
|
701
|
+
function onPointerLeave() {
|
|
702
|
+
hoverX = Number.NaN;
|
|
703
|
+
}
|
|
704
|
+
function endDrag(event) {
|
|
705
|
+
if (!dragging || event.pointerId !== dragPointer) return;
|
|
706
|
+
dragging = false;
|
|
707
|
+
dragPointer = -1;
|
|
708
|
+
const limit = 4;
|
|
709
|
+
state = { ...state, velocity: Math.max(-limit, Math.min(limit, state.velocity)) };
|
|
710
|
+
try {
|
|
711
|
+
canvas.releasePointerCapture(event.pointerId);
|
|
712
|
+
} catch {
|
|
713
|
+
}
|
|
714
|
+
wake();
|
|
715
|
+
}
|
|
716
|
+
function onKeyDown(event) {
|
|
717
|
+
if (!canMove()) return;
|
|
718
|
+
let push = 0;
|
|
719
|
+
let turn = 0;
|
|
720
|
+
if (event.key === "ArrowLeft") push = -KEY_IMPULSE;
|
|
721
|
+
else if (event.key === "ArrowRight") push = KEY_IMPULSE;
|
|
722
|
+
else if (event.key === "ArrowUp") turn = -2.5;
|
|
723
|
+
else if (event.key === "ArrowDown") turn = 2.5;
|
|
724
|
+
else if (event.key === "Enter" || event.key === " ") {
|
|
725
|
+
push = state.angle > 0 ? -KEY_IMPULSE : KEY_IMPULSE;
|
|
726
|
+
turn = 1.5;
|
|
727
|
+
} else return;
|
|
728
|
+
event.preventDefault();
|
|
729
|
+
state = {
|
|
730
|
+
...state,
|
|
731
|
+
velocity: state.velocity + push,
|
|
732
|
+
twistVelocity: state.twistVelocity + turn
|
|
733
|
+
};
|
|
734
|
+
wake();
|
|
735
|
+
}
|
|
736
|
+
function onVisibility() {
|
|
737
|
+
pageVisible = !document.hidden;
|
|
738
|
+
wake();
|
|
739
|
+
}
|
|
740
|
+
function onContextLost(event) {
|
|
741
|
+
event.preventDefault();
|
|
742
|
+
contextLost = true;
|
|
743
|
+
resources = null;
|
|
744
|
+
if (frame !== 0) cancelAnimationFrame(frame);
|
|
745
|
+
frame = 0;
|
|
746
|
+
}
|
|
747
|
+
function onContextRestored() {
|
|
748
|
+
if (destroyed) return;
|
|
749
|
+
resources = buildResources(context);
|
|
750
|
+
contextLost = resources === null;
|
|
751
|
+
wake();
|
|
752
|
+
}
|
|
753
|
+
canvas.addEventListener("pointerdown", onPointerDown);
|
|
754
|
+
canvas.addEventListener("pointermove", onPointerMove);
|
|
755
|
+
canvas.addEventListener("pointerleave", onPointerLeave);
|
|
756
|
+
canvas.addEventListener("pointerup", endDrag);
|
|
757
|
+
canvas.addEventListener("pointercancel", endDrag);
|
|
758
|
+
canvas.addEventListener("webglcontextlost", onContextLost, false);
|
|
759
|
+
canvas.addEventListener("webglcontextrestored", onContextRestored, false);
|
|
760
|
+
root?.addEventListener("keydown", onKeyDown);
|
|
761
|
+
if (typeof document !== "undefined") {
|
|
762
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
763
|
+
}
|
|
764
|
+
let resizeObserver = null;
|
|
765
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
766
|
+
resizeObserver = new ResizeObserver(() => wake());
|
|
767
|
+
resizeObserver.observe(canvas);
|
|
768
|
+
}
|
|
769
|
+
let intersectionObserver = null;
|
|
770
|
+
if (typeof IntersectionObserver !== "undefined") {
|
|
771
|
+
intersectionObserver = new IntersectionObserver((entries) => {
|
|
772
|
+
for (const entry of entries) inView = entry.isIntersecting;
|
|
773
|
+
wake();
|
|
774
|
+
});
|
|
775
|
+
intersectionObserver.observe(canvas);
|
|
776
|
+
}
|
|
777
|
+
wake();
|
|
778
|
+
return {
|
|
779
|
+
setOptions(next) {
|
|
780
|
+
if (destroyed) return;
|
|
781
|
+
if (next.color !== void 0) live.color = next.color;
|
|
782
|
+
if (next.finish !== void 0) live.finish = next.finish;
|
|
783
|
+
if (next.band !== void 0) live.band = next.band;
|
|
784
|
+
if (next.swing !== void 0) live.swing = next.swing;
|
|
785
|
+
if (next.glow !== void 0) live.glow = next.glow;
|
|
786
|
+
if (next.interactive !== void 0) {
|
|
787
|
+
live.interactive = next.interactive;
|
|
788
|
+
if (!next.interactive) dragging = false;
|
|
789
|
+
}
|
|
790
|
+
if (next.size !== void 0 || next.threadLength !== void 0) {
|
|
791
|
+
if (next.size !== void 0) live.size = next.size;
|
|
792
|
+
if (next.threadLength !== void 0) live.threadLength = next.threadLength;
|
|
793
|
+
geometry = ornamentGeometry(live.size, live.threadLength);
|
|
794
|
+
}
|
|
795
|
+
if (next.reducedMotion !== void 0) {
|
|
796
|
+
live.reducedMotion = next.reducedMotion;
|
|
797
|
+
if (next.reducedMotion) {
|
|
798
|
+
dragging = false;
|
|
799
|
+
state = { ...PENDULUM_REST, time: state.time };
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
if (frame === 0) wake();
|
|
803
|
+
},
|
|
804
|
+
getAngle() {
|
|
805
|
+
return live.reducedMotion ? 0 : state.angle;
|
|
806
|
+
},
|
|
807
|
+
destroy() {
|
|
808
|
+
if (destroyed) return;
|
|
809
|
+
destroyed = true;
|
|
810
|
+
if (frame !== 0) cancelAnimationFrame(frame);
|
|
811
|
+
frame = 0;
|
|
812
|
+
canvas.removeEventListener("pointerdown", onPointerDown);
|
|
813
|
+
canvas.removeEventListener("pointermove", onPointerMove);
|
|
814
|
+
canvas.removeEventListener("pointerleave", onPointerLeave);
|
|
815
|
+
canvas.removeEventListener("pointerup", endDrag);
|
|
816
|
+
canvas.removeEventListener("pointercancel", endDrag);
|
|
817
|
+
canvas.removeEventListener("webglcontextlost", onContextLost, false);
|
|
818
|
+
canvas.removeEventListener("webglcontextrestored", onContextRestored, false);
|
|
819
|
+
root?.removeEventListener("keydown", onKeyDown);
|
|
820
|
+
if (typeof document !== "undefined") {
|
|
821
|
+
document.removeEventListener("visibilitychange", onVisibility);
|
|
822
|
+
}
|
|
823
|
+
resizeObserver?.disconnect();
|
|
824
|
+
intersectionObserver?.disconnect();
|
|
825
|
+
if (resources) {
|
|
826
|
+
context.deleteBuffer(resources.buffer);
|
|
827
|
+
context.deleteProgram(resources.program);
|
|
828
|
+
resources = null;
|
|
829
|
+
}
|
|
830
|
+
if (releaseContext) context.getExtension("WEBGL_lose_context")?.loseContext();
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
export {
|
|
835
|
+
FRAGMENT_SHADER,
|
|
836
|
+
GRAVITY,
|
|
837
|
+
IDLE_AMPLITUDE,
|
|
838
|
+
MAX_PIXEL_RATIO,
|
|
839
|
+
MAX_STEP,
|
|
840
|
+
ORNAMENT_DEFAULTS,
|
|
841
|
+
PENDULUM_REST,
|
|
842
|
+
REST_EPSILON,
|
|
843
|
+
SUB_STEP,
|
|
844
|
+
SWING_DAMPING,
|
|
845
|
+
TWIST_COUPLING,
|
|
846
|
+
TWIST_DAMPING,
|
|
847
|
+
TWIST_FREQUENCY,
|
|
848
|
+
VERTEX_SHADER,
|
|
849
|
+
breeze,
|
|
850
|
+
createOrnament,
|
|
851
|
+
fallbackSphere,
|
|
852
|
+
isMoving,
|
|
853
|
+
isSettled,
|
|
854
|
+
metalFor,
|
|
855
|
+
mulberry32,
|
|
856
|
+
ornamentGeometry,
|
|
857
|
+
parseHexColor,
|
|
858
|
+
softLimit,
|
|
859
|
+
stepPendulum,
|
|
860
|
+
swingFrequency
|
|
861
|
+
};
|