@jsb188/gfx 1.1.2 → 1.1.11
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 +54 -248
- package/libs/dotGrid.ts +77 -480
- package/libs/effects/effectQuad.ts +2 -8
- package/libs/pointer.ts +0 -6
- package/libs/runtime.ts +4 -6
- package/libs/types.ts +1 -8
- package/main.ts +0 -2
- package/modules/Carousel.helpers.ts +0 -234
- package/modules/Carousel.tsx +29 -135
- package/modules/DotsArea.tsx +120 -0
- package/modules/GfxCanvas.tsx +2 -10
- package/modules/ScrollSteps.tsx +52 -7
- package/modules/VisionBoard.helpers.ts +561 -0
- package/modules/VisionBoard.tsx +358 -0
- package/modules/VisionBoard.types.ts +88 -0
- package/package.json +5 -15
- package/scenes/Carousel.ts +18 -99
- package/scenes/DotsArea.ts +208 -0
- package/tsconfig.json +1 -1
- package/utils/color.ts +2 -64
- package/utils/dotGridSpecs.ts +46 -304
- package/elements/Shapes.helpers.ts +0 -204
- package/elements/Shapes.hooks.ts +0 -171
- package/elements/Shapes.tsx +0 -505
- package/elements/Shapes.types.ts +0 -81
- package/elements/Stack.helpers.ts +0 -42
- package/elements/Stack.tsx +0 -143
- package/elements/Text.helpers.ts +0 -45
- package/elements/Text.hooks.ts +0 -197
- package/elements/Text.tsx +0 -98
- package/libs/dotGrid.helpers.ts +0 -53
- package/libs/effects/dotEffects.ts +0 -136
- package/modules/Carousel.hooks.ts +0 -520
- package/modules/Dots.tsx +0 -269
- package/modules/DotsBoard.helpers.ts +0 -388
- package/modules/DotsBoard.hooks.ts +0 -144
- package/modules/DotsBoard.strings.ts +0 -649
- package/modules/DotsBoard.types.ts +0 -240
- package/modules/DotsBoardCanvas.hooks.ts +0 -49
- package/modules/DotsBoardCanvas.tsx +0 -190
- package/modules/DotsBoardElements.tsx +0 -330
- package/modules/Gallery.helpers.ts +0 -32
- package/modules/Gallery.hooks.ts +0 -149
- package/modules/Gallery.tsx +0 -194
- package/modules/KineticHeader.helpers.ts +0 -44
- package/modules/KineticHeader.hooks.ts +0 -206
- package/modules/KineticHeader.tsx +0 -305
- package/modules/KineticHeader.types.ts +0 -56
- package/modules/ScrollSteps.hooks.ts +0 -138
- package/scenes/DotsBoard.helpers.ts +0 -12
- package/scenes/DotsBoard.ts +0 -245
- package/utils/align.ts +0 -18
- package/utils/dotEffectSpecs.ts +0 -79
- package/utils/motion.ts +0 -88
- package/utils/random.ts +0 -22
- package/utils/strings.ts +0 -116
package/libs/dotGrid.ts
CHANGED
|
@@ -3,22 +3,16 @@ import type { OGLRenderingContext } from 'ogl';
|
|
|
3
3
|
import { hexToRgb } from '../utils/color.ts';
|
|
4
4
|
import {
|
|
5
5
|
DOT_ROUNDED_SQUARE_CORNER,
|
|
6
|
-
DOT_RING_INNER_RADIUS,
|
|
7
|
-
DOT_GRID_DEFAULTS,
|
|
8
6
|
DOT_SHAPES,
|
|
9
|
-
getDotGridAnchor,
|
|
10
7
|
getDotGridCoverSize,
|
|
11
8
|
isDotGridInteractive,
|
|
12
9
|
resolveDotGridOptions,
|
|
13
10
|
} from '../utils/dotGridSpecs.ts';
|
|
14
11
|
import type { DotGridOptions } from '../utils/dotGridSpecs.ts';
|
|
15
|
-
import { getDotEffectId } from '../utils/dotEffectSpecs.ts';
|
|
16
12
|
import { PassChain, createPassProgram } from './chain.ts';
|
|
17
|
-
import { DOT_CLICK_MAX_EFFECTS, createDotClickEffects } from './dotGrid.helpers.ts';
|
|
18
|
-
import { GLSL_DOT_EFFECTS, createDotEffectUniforms, dotEffectDeclarations, setDotEffectUniforms } from './effects/dotEffects.ts';
|
|
19
13
|
import { GLSL_CONSTANTS, GLSL_COVER_UV, GLSL_HASH2, GLSL_HEADER, fragmentShader } from './glsl.ts';
|
|
20
14
|
import type { ImageTexture } from './texture.ts';
|
|
21
|
-
import type {
|
|
15
|
+
import type { SceneSize } from './types.ts';
|
|
22
16
|
import { setVec2 } from './uniforms.ts';
|
|
23
17
|
|
|
24
18
|
/**
|
|
@@ -31,18 +25,16 @@ import { setVec2 } from './uniforms.ts';
|
|
|
31
25
|
* replace the flat color: each dot shows the part of the cover-fit image under it, so bigger
|
|
32
26
|
* dots reveal more of it. A reveal amount grows the dots to full coverage, so at 0 the grid
|
|
33
27
|
* reads as a solid image or color, and a pulse makes a random few dots swell and shrink each
|
|
34
|
-
* cycle. A bottom fade turns
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
28
|
+
* cycle. A bottom fade turns whatever is drawn over the last so many pixels transparent
|
|
29
|
+
* toward the bottom edge (dots, thrown or pulsing or not), except that the filled reveal state
|
|
30
|
+
* stays solid and the fade comes in as the dots shrink. Output is premultiplied for a
|
|
31
|
+
* transparent canvas.
|
|
38
32
|
*/
|
|
39
33
|
|
|
40
34
|
// Per texel: xy = offset from home (CSS px), zw = velocity (px/s). Each frame the cursor
|
|
41
35
|
// kicks dots inside `uRadius` of it away from itself in proportion to its speed (with a
|
|
42
36
|
// per-dot random factor for a scattered look), then a damped spring (ω, ζ) pulls them home.
|
|
43
|
-
//
|
|
44
|
-
// edge), so the fading dots stay put. Semi-implicit Euler stays stable while ω·dt < 2, so
|
|
45
|
-
// large frames are split into substeps.
|
|
37
|
+
// Semi-implicit Euler stays stable while ω·dt < 2, so large frames are split into substeps.
|
|
46
38
|
const SPRING_FRAGMENT = fragmentShader(
|
|
47
39
|
`
|
|
48
40
|
uniform vec2 uOrigin;
|
|
@@ -51,26 +43,10 @@ uniform vec2 uPointer;
|
|
|
51
43
|
uniform vec2 uPointerVelocity;
|
|
52
44
|
uniform float uRadius;
|
|
53
45
|
uniform float uStrength;
|
|
54
|
-
uniform float uRandomness;
|
|
55
|
-
uniform float uImpulse;
|
|
56
|
-
uniform float uImpulseRadius;
|
|
57
|
-
uniform float uImpulseStrength;
|
|
58
|
-
uniform float uImpulseRandomness;
|
|
59
|
-
uniform float uImpulseSeed;
|
|
60
46
|
uniform float uOmega;
|
|
61
47
|
uniform float uZeta;
|
|
62
48
|
uniform float uDt;
|
|
63
49
|
uniform int uSteps;
|
|
64
|
-
uniform float uHeight;
|
|
65
|
-
uniform float uFadeBottom;
|
|
66
|
-
/*
|
|
67
|
-
* Vary a kick's strength and angle while preserving its outward (or inward) direction.
|
|
68
|
-
*/
|
|
69
|
-
vec2 scatterDotKick(vec2 direction, vec2 variation, float randomness) {
|
|
70
|
-
float angle = variation.y * randomness * 0.8;
|
|
71
|
-
vec2 scattered = direction * cos(angle) + vec2(-direction.y, direction.x) * sin(angle);
|
|
72
|
-
return scattered * (1.0 + variation.x * randomness);
|
|
73
|
-
}
|
|
74
50
|
void main() {
|
|
75
51
|
ivec2 cell = ivec2(gl_FragCoord.xy);
|
|
76
52
|
vec4 state = texelFetch(tInput, cell, 0);
|
|
@@ -78,25 +54,8 @@ void main() {
|
|
|
78
54
|
vec2 velocity = state.zw;
|
|
79
55
|
vec2 home = uOrigin + vec2(cell) * uSpacing;
|
|
80
56
|
float speed = length(uPointerVelocity);
|
|
81
|
-
|
|
82
|
-
float fade = uFadeBottom > 0.0 ? clamp((uHeight - home.y) / uFadeBottom, 0.0, 1.0) : 1.0;
|
|
83
|
-
float random = (0.6 + 0.8 * hash2(vec2(cell)).x) * fade;
|
|
84
|
-
// A click adds one radial impulse; subsequent frames use the same spring as cursor kicks.
|
|
85
|
-
if (uImpulse > 0.0) {
|
|
86
|
-
vec2 away = home + offset - uPointer;
|
|
87
|
-
float dist = length(away);
|
|
88
|
-
if (dist < uImpulseRadius) {
|
|
89
|
-
float falloff = 1.0 - dist / uImpulseRadius;
|
|
90
|
-
vec2 direction = dist > 0.001 ? away / dist : vec2(1.0, 0.0);
|
|
91
|
-
// Seed once per click so the kick varies without adding jitter during the return.
|
|
92
|
-
vec2 variation = hash2(vec2(cell) + uImpulseSeed) * 2.0 - 1.0;
|
|
93
|
-
direction = scatterDotKick(direction, variation, uImpulseRandomness);
|
|
94
|
-
velocity += direction * uImpulseStrength * uImpulseRadius * uOmega * falloff * falloff * random;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
57
|
+
float random = 0.6 + 0.8 * hash2(vec2(cell)).x;
|
|
97
58
|
float dt = uDt / float(uSteps);
|
|
98
|
-
// Keep hover variation fixed per dot so continuous cursor movement never rerolls it frame to frame.
|
|
99
|
-
vec2 hoverVariation = hash2(vec2(cell) + 19.0) * 2.0 - 1.0;
|
|
100
59
|
for (int i = 0; i < 8; i++) {
|
|
101
60
|
if (i >= uSteps) break;
|
|
102
61
|
vec2 away = home + offset - uPointer;
|
|
@@ -104,7 +63,6 @@ void main() {
|
|
|
104
63
|
if (speed > 0.0 && dist < uRadius) {
|
|
105
64
|
float falloff = 1.0 - dist / uRadius;
|
|
106
65
|
vec2 direction = dist > 0.001 ? away / dist : vec2(1.0, 0.0);
|
|
107
|
-
direction = scatterDotKick(direction, hoverVariation, uRandomness);
|
|
108
66
|
velocity += direction * (uStrength * speed * 0.5 * uOmega * falloff * falloff * random) * dt;
|
|
109
67
|
}
|
|
110
68
|
velocity += (-uOmega * uOmega * offset - 2.0 * uZeta * uOmega * velocity) * dt;
|
|
@@ -123,30 +81,19 @@ void main() {
|
|
|
123
81
|
`);
|
|
124
82
|
|
|
125
83
|
// The resting diameter eases from the cover size to `uDotSize` with `uReveal`; the thrown and
|
|
126
|
-
// pulse scales multiply on top, and the sprite is capped at what the driver allows.
|
|
127
|
-
// the bottom fade the thrown scale is eased off with the dot's depth into it, so the dots
|
|
128
|
-
// that are fading out (or to the fade color) do not swell under the cursor as the others do;
|
|
129
|
-
// the throw itself is unchanged. A pulse
|
|
84
|
+
// pulse scales multiply on top, and the sprite is capped at what the driver allows. A pulse
|
|
130
85
|
// cycle is `floor(time / interval)`: hashing the cell with the cycle picks about `chance` of
|
|
131
86
|
// the dots (never the same dot twice in a row) and gives each a random start inside the cycle,
|
|
132
87
|
// so no per-dot state is needed.
|
|
133
88
|
const DOT_GRID_VERTEX = `${GLSL_HEADER}${GLSL_CONSTANTS}
|
|
134
89
|
uniform vec2 uResolution;
|
|
135
|
-
uniform vec2 uViewSize;
|
|
136
|
-
uniform vec2 uViewOrigin;
|
|
137
90
|
uniform float uPixelRatio;
|
|
138
|
-
uniform vec2 uHoverPointer;
|
|
139
|
-
uniform float uHoverRadius;
|
|
140
|
-
uniform float uHoverAmount;
|
|
141
|
-
uniform float uHoverFade;
|
|
142
91
|
uniform float uColumns;
|
|
143
92
|
uniform vec2 uOrigin;
|
|
144
93
|
uniform float uSpacing;
|
|
145
94
|
uniform float uDotSize;
|
|
146
95
|
uniform float uCoverSize;
|
|
147
|
-
uniform float uRevealSize;
|
|
148
96
|
uniform float uReveal;
|
|
149
|
-
uniform float uFadeBottom;
|
|
150
97
|
uniform sampler2D tState;
|
|
151
98
|
uniform float uOmega;
|
|
152
99
|
uniform float uThrowScale;
|
|
@@ -158,25 +105,14 @@ uniform float uTime;
|
|
|
158
105
|
uniform float uPulseStart;
|
|
159
106
|
uniform float uPulseChance;
|
|
160
107
|
uniform float uPulseInterval;
|
|
161
|
-
uniform float
|
|
162
|
-
uniform float
|
|
163
|
-
uniform float
|
|
164
|
-
uniform float uMotionEnabled;
|
|
165
|
-
uniform vec4 uClicks[${DOT_CLICK_MAX_EFFECTS}];
|
|
166
|
-
uniform float uClickEffects[${DOT_CLICK_MAX_EFFECTS}];
|
|
167
|
-
${dotEffectDeclarations('uPulse')}
|
|
168
|
-
${dotEffectDeclarations('uClick')}
|
|
169
|
-
${dotEffectDeclarations('uRest')}
|
|
170
|
-
out float vHover;
|
|
108
|
+
uniform float uPulseDuration;
|
|
109
|
+
uniform float uPulseScale;
|
|
110
|
+
uniform float uPulseOpacity;
|
|
171
111
|
out float vWeight;
|
|
172
112
|
out float vPulse;
|
|
173
|
-
out float vRest;
|
|
174
|
-
out vec4 vEffectTint;
|
|
175
|
-
out float vClick;
|
|
176
113
|
out float vOpacity;
|
|
177
114
|
out float vRadius;
|
|
178
115
|
out float vPointSize;
|
|
179
|
-
flat out ivec2 vCell;
|
|
180
116
|
|
|
181
117
|
// Integer bit-mixing hash: exact on every GPU, unlike sin-based float hashes whose results drift
|
|
182
118
|
// (and correlate between cycles) once the inputs grow large.
|
|
@@ -198,105 +134,38 @@ bool pulsesIn(ivec2 cell, int cycle) {
|
|
|
198
134
|
return cellRandom(cell, cycle, 1u) < uPulseChance;
|
|
199
135
|
}
|
|
200
136
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
* Sample a selected dot's pulse. A stable local origin gives motion effects a small orbit
|
|
205
|
-
* around home; the default swell preserves the original pulse's timing and size exactly.
|
|
206
|
-
*/
|
|
207
|
-
DotEffectSample pulseEffect(ivec2 cell, out float seed) {
|
|
208
|
-
seed = 0.0;
|
|
137
|
+
// 0..1 how far into its swell this dot is; 0 when it is not pulsing (or pulsing has not started).
|
|
138
|
+
// A dot never pulses in two consecutive cycles, so each pulse stands alone.
|
|
139
|
+
float pulseBump(ivec2 cell) {
|
|
209
140
|
float time = uTime - uPulseStart;
|
|
210
|
-
if (
|
|
141
|
+
if (uPulseChance <= 0.0 || uPulseStart < 0.0 || time < 0.0) return 0.0;
|
|
211
142
|
int cycle = int(floor(time / uPulseInterval));
|
|
212
|
-
if (!pulsesIn(cell, cycle) || (cycle > 0 && pulsesIn(cell, cycle - 1))) return
|
|
143
|
+
if (!pulsesIn(cell, cycle) || (cycle > 0 && pulsesIn(cell, cycle - 1))) return 0.0;
|
|
213
144
|
float start = cellRandom(cell, cycle, 2u) * (uPulseInterval - uPulseDuration);
|
|
214
|
-
float
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/*
|
|
222
|
-
* Animate the resting grid from a fixed or randomly chosen origin. Origin and effect remain
|
|
223
|
-
* stable for the whole cycle, while sparkle timing can differ between individual dots.
|
|
224
|
-
*/
|
|
225
|
-
DotEffectSample restEffect(ivec2 cell, vec2 position, out float seed) {
|
|
226
|
-
seed = 0.0;
|
|
227
|
-
float time = uTime - uPulseStart;
|
|
228
|
-
if (uMotionEnabled < 0.5 || uRestOn < 0.5 || uPulseStart < 0.0 || time < 0.0) return noDotEffect();
|
|
229
|
-
int cycle = int(floor(time / uRestInterval));
|
|
230
|
-
float phase = (time - float(cycle) * uRestInterval) / uRestDuration;
|
|
231
|
-
seed = cellRandom(ivec2(0), cycle, 50u);
|
|
232
|
-
vec2 origin = uResolution * 0.5;
|
|
233
|
-
if (uRestRandomOrigin > 0.5) {
|
|
234
|
-
origin = uResolution * (0.15 + 0.7 * vec2(cellRandom(ivec2(0), cycle, 51u), cellRandom(ivec2(0), cycle, 52u)));
|
|
235
|
-
}
|
|
236
|
-
return sampleDotEffect(selectDotEffect(uRestEffect, seed), position - origin, phase, cellRandom(cell, cycle, 53u), uRestRadius, uRestWidth, uRestStrength);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/*
|
|
240
|
-
* Sample overlapping clicks; each dot follows the strongest effect so repeated clicks do
|
|
241
|
-
* not stack unbounded movement, scale, or opacity. Effect IDs are chosen only at click time.
|
|
242
|
-
*/
|
|
243
|
-
DotEffectSample clickEffect(ivec2 cell, vec2 position, out float seed) {
|
|
244
|
-
seed = 0.0;
|
|
245
|
-
DotEffectSample result = noDotEffect();
|
|
246
|
-
if (uMotionEnabled < 0.5) return result;
|
|
247
|
-
for (int i = 0; i < ${DOT_CLICK_MAX_EFFECTS}; i++) {
|
|
248
|
-
vec4 click = uClicks[i];
|
|
249
|
-
if (click.w < 0.5) continue;
|
|
250
|
-
float phase = (uTime - click.z) / uClickDuration;
|
|
251
|
-
int cycle = int(click.z * 1000.0) + i;
|
|
252
|
-
DotEffectSample candidate = sampleDotEffect(int(uClickEffects[i]), position - click.xy, phase, cellRandom(cell, cycle, 31u), uClickRadius, uClickWidth, uClickStrength);
|
|
253
|
-
if (candidate.amount > result.amount) {
|
|
254
|
-
result = candidate;
|
|
255
|
-
seed = cellRandom(ivec2(i), cycle, 32u);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return result;
|
|
145
|
+
float t = (time - float(cycle) * uPulseInterval - start) / uPulseDuration;
|
|
146
|
+
if (t <= 0.0 || t >= 1.0) return 0.0;
|
|
147
|
+
// sin² rises and falls with zero slope at both ends, so a pulse starts and stops gently.
|
|
148
|
+
float bump = sin(PI * t);
|
|
149
|
+
return bump * bump;
|
|
259
150
|
}
|
|
260
151
|
|
|
261
152
|
void main() {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
ivec2 cell = ivec2(gl_VertexID % columns, gl_VertexID / columns);
|
|
265
|
-
vCell = cell;
|
|
153
|
+
float id = float(gl_VertexID);
|
|
154
|
+
ivec2 cell = ivec2(int(mod(id, uColumns)), int(floor(id / uColumns)));
|
|
266
155
|
vec4 state = texelFetch(tState, cell, 0);
|
|
267
156
|
vec2 position = uOrigin + vec2(cell) * uSpacing + state.xy;
|
|
157
|
+
vec2 uv = position / uResolution;
|
|
158
|
+
gl_Position = vec4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 0.0, 1.0);
|
|
268
159
|
// How far the dot has been thrown: the spring's swing amplitude, which decays smoothly
|
|
269
160
|
// instead of dipping every time the dot passes through home.
|
|
270
161
|
float amplitude = length(vec2(length(state.xy), length(state.zw) / uOmega));
|
|
271
162
|
float weight = clamp(amplitude * uThrowScale, 0.0, 1.0);
|
|
272
163
|
vWeight = weight;
|
|
273
|
-
float
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
DotEffectSample clicking = clickEffect(cell, position, clickSeed);
|
|
279
|
-
vRest = resting.amount;
|
|
280
|
-
vPulse = pulsing.amount;
|
|
281
|
-
vClick = clicking.amount;
|
|
282
|
-
vEffectTint = vec4(0.0);
|
|
283
|
-
addDotEffectTint(vEffectTint, resting, uRestColor, uRestColorOn, restSeed);
|
|
284
|
-
addDotEffectTint(vEffectTint, pulsing, uPulseColor, uPulseColorOn, pulseSeed);
|
|
285
|
-
addDotEffectTint(vEffectTint, clicking, uClickColor, uClickColorOn, clickSeed);
|
|
286
|
-
float opacity = mix(mix(uOpacity, uRestOpacity, vRest), uPulseOpacity, vPulse);
|
|
287
|
-
vOpacity = mix(mix(opacity, uThrownOpacity, weight), uClickOpacity, vClick);
|
|
288
|
-
// New movement fades near the bottom along with the cursor's existing size response.
|
|
289
|
-
float fade = uFadeBottom > 0.0 ? clamp((uResolution.y - position.y) / uFadeBottom, 0.0, 1.0) : 1.0;
|
|
290
|
-
position += (resting.offset + pulsing.offset + clicking.offset) * fade;
|
|
291
|
-
float hoverDistance = length(position - uHoverPointer) / max(1.0, uHoverRadius);
|
|
292
|
-
vHover = uHoverAmount * (uHoverFade > 0.5 ? max(0.0, 1.0 - hoverDistance) : (hoverDistance < 1.0 ? 1.0 : 0.0));
|
|
293
|
-
vec2 uv = (position - uViewOrigin) / uViewSize;
|
|
294
|
-
gl_Position = vec4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 0.0, 1.0);
|
|
295
|
-
// One device pixel on each side keeps the anti-aliased rim inside the sprite.
|
|
296
|
-
float size = uRevealSize >= 0.0 ? uRevealSize : mix(uCoverSize, uDotSize, uReveal);
|
|
297
|
-
size *= mix(1.0, uThrownScale, weight * fade);
|
|
298
|
-
size *= mix(1.0, uRestScale, vRest) * mix(1.0, uPulseScale, vPulse);
|
|
299
|
-
size = mix(size, uDotSize * uClickScale, vClick);
|
|
164
|
+
float pulse = pulseBump(cell);
|
|
165
|
+
vPulse = pulse;
|
|
166
|
+
vOpacity = mix(mix(uOpacity, uPulseOpacity, pulse), uThrownOpacity, weight);
|
|
167
|
+
// One device pixel of margin on each side keeps the anti-aliased rim inside the sprite.
|
|
168
|
+
float size = mix(uCoverSize, uDotSize, uReveal) * mix(1.0, uThrownScale, weight) * mix(1.0, uPulseScale, pulse);
|
|
300
169
|
vPointSize = min(size * uPixelRatio + 2.0, uMaxPointSize);
|
|
301
170
|
vRadius = (vPointSize - 2.0) * 0.5;
|
|
302
171
|
gl_PointSize = vPointSize;
|
|
@@ -308,34 +177,14 @@ void main() {
|
|
|
308
177
|
// a signed distance from the sprite center (device pixels, negative inside), which gives every
|
|
309
178
|
// shape the same one-pixel anti-aliased rim and lets a pulsing dot morph between its resting
|
|
310
179
|
// shape and the pulse shape by blending the two distances. The bottom fade is also by screen
|
|
311
|
-
// position, so a dot thrown into the fade zone fades with it
|
|
312
|
-
// color when one is on. While a reveal runs, each dot is clipped to its own grid cell: at cover
|
|
313
|
-
// size the dots overlap along every cell edge, and where a fade makes them translucent the
|
|
314
|
-
// overlaps would blend to a higher alpha than either dot and draw as a grid of seams. Cells
|
|
315
|
-
// tile the area exactly, so the fill stays seamless; the clip is off once kicks can move dots.
|
|
180
|
+
// position, so a dot thrown into the fade zone fades with it.
|
|
316
181
|
const DOT_GRID_FRAGMENT = `${GLSL_HEADER}
|
|
317
182
|
uniform vec2 uResolution;
|
|
318
|
-
uniform vec2 uViewSize;
|
|
319
|
-
uniform vec2 uViewOrigin;
|
|
320
183
|
uniform float uPixelRatio;
|
|
321
|
-
uniform vec2 uOrigin;
|
|
322
|
-
uniform float uSpacing;
|
|
323
184
|
uniform float uReveal;
|
|
324
|
-
uniform float uFadeHandoff;
|
|
325
185
|
uniform float uFadeBottom;
|
|
326
|
-
uniform float uFadeColorOn;
|
|
327
|
-
uniform vec3 uFadeColor;
|
|
328
|
-
uniform float uBottomTintOn;
|
|
329
|
-
uniform vec2 uBottomTintRange;
|
|
330
|
-
uniform vec4 uBottomTint;
|
|
331
|
-
uniform int uBottomTintMode;
|
|
332
|
-
uniform float uImageFade;
|
|
333
|
-
uniform float uImageFadeColorOn;
|
|
334
|
-
uniform vec3 uImageFadeColor;
|
|
335
186
|
uniform int uShape;
|
|
336
187
|
uniform int uPulseShape;
|
|
337
|
-
uniform int uRestShape;
|
|
338
|
-
uniform int uClickShape;
|
|
339
188
|
uniform float uCorner;
|
|
340
189
|
uniform vec3 uColor;
|
|
341
190
|
uniform float uThrownColorOn;
|
|
@@ -344,103 +193,39 @@ uniform sampler2D tOverlay;
|
|
|
344
193
|
uniform vec2 uOverlaySize;
|
|
345
194
|
uniform float uOverlayOn;
|
|
346
195
|
uniform float uOverlayZoom;
|
|
347
|
-
uniform vec2 uOverlayRegion;
|
|
348
|
-
uniform vec2 uOverlayOffset;
|
|
349
|
-
uniform float uOverlayFade;
|
|
350
|
-
uniform vec3 uHoverColor;
|
|
351
|
-
in float vHover;
|
|
352
196
|
in float vWeight;
|
|
353
197
|
in float vPulse;
|
|
354
|
-
in float vRest;
|
|
355
|
-
in vec4 vEffectTint;
|
|
356
|
-
in float vClick;
|
|
357
198
|
in float vOpacity;
|
|
358
199
|
in float vRadius;
|
|
359
200
|
in float vPointSize;
|
|
360
|
-
flat in ivec2 vCell;
|
|
361
201
|
out vec4 fragColor;
|
|
362
202
|
${GLSL_COVER_UV}
|
|
363
203
|
|
|
364
204
|
float shapeDistance(int shape, vec2 p, float r) {
|
|
365
|
-
if (shape == 1
|
|
205
|
+
if (shape == 1) {
|
|
366
206
|
float corner = r * uCorner;
|
|
367
207
|
vec2 q = abs(p) - (r - corner);
|
|
368
|
-
|
|
369
|
-
if (shape == 3) {
|
|
370
|
-
// Inset the same outline for a uniform stroke; keep its hole closed at reveal start.
|
|
371
|
-
float inner = -outer - r * (1.0 - ${DOT_RING_INNER_RADIUS});
|
|
372
|
-
return max(outer, mix(-r - 1.0, inner, uReveal));
|
|
373
|
-
}
|
|
374
|
-
return outer;
|
|
375
|
-
}
|
|
376
|
-
if (shape == 2) {
|
|
377
|
-
// A ring's hole opens with the reveal so its filled intro still covers every pixel.
|
|
378
|
-
float inner = mix(-r - 1.0, r * ${DOT_RING_INNER_RADIUS}, uReveal);
|
|
379
|
-
return max(length(p) - r, inner - length(p));
|
|
208
|
+
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - corner;
|
|
380
209
|
}
|
|
381
210
|
return length(p) - r;
|
|
382
211
|
}
|
|
383
212
|
|
|
384
|
-
/*
|
|
385
|
-
* Apply thrown and triggered effect tints equally to the base color and the image sample.
|
|
386
|
-
*/
|
|
387
|
-
vec3 applyDotTints(vec3 base) {
|
|
388
|
-
vec3 color = mix(base, uThrownColor, vWeight * uThrownColorOn);
|
|
389
|
-
return color * (1.0 - vEffectTint.a) + vEffectTint.rgb;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
213
|
void main() {
|
|
393
|
-
vec2 fragment = vec2(gl_FragCoord.x / uPixelRatio, uViewSize.y - gl_FragCoord.y / uPixelRatio) + uViewOrigin;
|
|
394
|
-
// A zero-sized dot must stay completely invisible, including its anti-aliased rim.
|
|
395
|
-
if (vRadius <= 0.0) discard;
|
|
396
214
|
vec2 p = (gl_PointCoord - 0.5) * vPointSize;
|
|
397
|
-
// The pixel's cell from its own screen position (CSS px, y down like the grid), the same
|
|
398
|
-
// arithmetic for every dot, so exactly one dot claims each pixel however gl_PointCoord rounds.
|
|
399
|
-
if (uReveal < 1.0) {
|
|
400
|
-
if (ivec2(floor((fragment - uOrigin) / uSpacing + 0.5)) != vCell) discard;
|
|
401
|
-
}
|
|
402
215
|
float dist = shapeDistance(uShape, p, vRadius);
|
|
403
|
-
if (
|
|
404
|
-
if (vPulse > 0.0) dist = mix(dist, shapeDistance(uPulseShape, p, vRadius), vPulse);
|
|
405
|
-
if (vClick > 0.0) dist = mix(dist, shapeDistance(uClickShape, p, vRadius), vClick);
|
|
216
|
+
if (uPulseShape != uShape && vPulse > 0.0) dist = mix(dist, shapeDistance(uPulseShape, p, vRadius), vPulse);
|
|
406
217
|
float alpha = clamp(-dist + 0.5, 0.0, 1.0) * vOpacity;
|
|
407
|
-
//
|
|
408
|
-
//
|
|
409
|
-
//
|
|
410
|
-
float
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
vec2 imageUv = vec2(imagePoint.x / region.x, 1.0 - imagePoint.y / region.y);
|
|
419
|
-
vec2 overlayUv = (coverUv(imageUv, region, uOverlaySize) - 0.5) / uOverlayZoom + 0.5;
|
|
420
|
-
float imageWeight = imagePoint.y >= 0.0 && imagePoint.y < region.y ? 1.0 : 0.0;
|
|
421
|
-
if (uOverlayFade > 0.0) imageWeight *= clamp((region.y - imagePoint.y) / uOverlayFade, 0.0, 1.0);
|
|
422
|
-
// Fade the image into the active dot color: hover tint under the pointer, base color elsewhere.
|
|
423
|
-
// Full-strength image pixels keep priority, and the fade has no gray-to-hover seam.
|
|
424
|
-
vec3 color = mix(applyDotTints(uColor), uHoverColor, vHover);
|
|
425
|
-
if (uOverlayOn > 0.5 && imageWeight > 0.0) {
|
|
426
|
-
color = mix(color, applyDotTints(texture(tOverlay, overlayUv).rgb), imageWeight);
|
|
427
|
-
}
|
|
428
|
-
color = mix(color, uFadeColor, faded * uFadeColorOn);
|
|
429
|
-
color = mix(color, uImageFadeColor, imageFaded * uImageFadeColorOn);
|
|
430
|
-
// This final color band stays in logical board coordinates as the canvas window scrolls.
|
|
431
|
-
// Premultiplied interpolation preserves RGBA alpha without dimming the footer underneath.
|
|
432
|
-
float bottomDistance = uResolution.y - fragment.y;
|
|
433
|
-
float span = uBottomTintRange.x - uBottomTintRange.y;
|
|
434
|
-
float blend = span > 0.0 ? clamp((uBottomTintRange.x - bottomDistance) / span, 0.0, 1.0) : step(bottomDistance, uBottomTintRange.y);
|
|
435
|
-
vec4 tint = vec4(uBottomTint.rgb * uBottomTint.a, uBottomTint.a);
|
|
436
|
-
if (uBottomTintMode == 2) {
|
|
437
|
-
// Source-colored dots blend with the DOM backdrop through CSS. Effects still shape/move dots.
|
|
438
|
-
fragColor = tint * blend * alpha;
|
|
439
|
-
} else if (uBottomTintMode == 1) {
|
|
440
|
-
fragColor = vec4(color, 1.0) * (1.0 - blend) * alpha;
|
|
441
|
-
} else {
|
|
442
|
-
fragColor = mix(vec4(color, 1.0), tint, blend * uBottomTintOn) * alpha;
|
|
443
|
-
}
|
|
218
|
+
// Linear fade to transparent over the bottom uFadeBottom CSS pixels (gl_FragCoord.y is 0 at
|
|
219
|
+
// the bottom edge), weighted by the reveal so the filled state stays solid and the fade
|
|
220
|
+
// arrives as the dots shrink.
|
|
221
|
+
float fade = uFadeBottom > 0.0 ? clamp(gl_FragCoord.y / (uFadeBottom * uPixelRatio), 0.0, 1.0) : 1.0;
|
|
222
|
+
alpha *= mix(1.0, fade, uReveal);
|
|
223
|
+
vec2 canvas = uResolution * uPixelRatio;
|
|
224
|
+
// The overlay zoom scales the cover-fit image about the area's center.
|
|
225
|
+
vec2 overlayUv = (coverUv(gl_FragCoord.xy / canvas, canvas, uOverlaySize) - 0.5) / uOverlayZoom + 0.5;
|
|
226
|
+
vec3 base = uOverlayOn > 0.5 ? texture(tOverlay, overlayUv).rgb : uColor;
|
|
227
|
+
vec3 color = mix(base, uThrownColor, vWeight * uThrownColorOn);
|
|
228
|
+
fragColor = vec4(color * alpha, alpha);
|
|
444
229
|
}
|
|
445
230
|
`;
|
|
446
231
|
|
|
@@ -458,10 +243,9 @@ const SPRING = {
|
|
|
458
243
|
|
|
459
244
|
/*
|
|
460
245
|
* Build the simulation pass, the point program, an attribute-free geometry (WebGL2 draws
|
|
461
|
-
* from `gl_VertexID`), and the helpers that lay the grid out and drive it. The grid
|
|
462
|
-
* dot
|
|
463
|
-
*
|
|
464
|
-
* cursor kicks are disabled; the other effects still work.
|
|
246
|
+
* from `gl_VertexID`), and the helpers that lay the grid out and drive it. The grid centers a
|
|
247
|
+
* dot on the canvas, like `mask-position: center` does for a CSS tile, and extends one spacing
|
|
248
|
+
* past every edge. Without half-float render targets the grid is static.
|
|
465
249
|
*/
|
|
466
250
|
export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridOptions) {
|
|
467
251
|
let options = resolveDotGridOptions(initialOptions);
|
|
@@ -471,7 +255,6 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
471
255
|
let dotCount = 0;
|
|
472
256
|
let lastKickAt = Number.NEGATIVE_INFINITY;
|
|
473
257
|
let simulating = false;
|
|
474
|
-
const clicks = createDotClickEffects();
|
|
475
258
|
// Sprites larger than the driver allows would silently clamp, so the dot size is capped.
|
|
476
259
|
const [, maxPointSize] = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE) as Float32Array;
|
|
477
260
|
|
|
@@ -483,18 +266,10 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
483
266
|
uPointerVelocity: { value: [0, 0] },
|
|
484
267
|
uRadius: { value: 0 },
|
|
485
268
|
uStrength: { value: 0 },
|
|
486
|
-
uRandomness: { value: 0 },
|
|
487
|
-
uImpulse: { value: 0 },
|
|
488
|
-
uImpulseRadius: { value: 0 },
|
|
489
|
-
uImpulseStrength: { value: 0 },
|
|
490
|
-
uImpulseRandomness: { value: 0 },
|
|
491
|
-
uImpulseSeed: { value: 0 },
|
|
492
269
|
uOmega: { value: 1 },
|
|
493
270
|
uZeta: { value: 1 },
|
|
494
271
|
uDt: { value: 0 },
|
|
495
272
|
uSteps: { value: 1 },
|
|
496
|
-
uHeight: { value: 1 },
|
|
497
|
-
uFadeBottom: { value: 0 },
|
|
498
273
|
});
|
|
499
274
|
const restPass = createPassProgram(gl, REST_FRAGMENT);
|
|
500
275
|
const program = new Program(gl, {
|
|
@@ -502,35 +277,17 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
502
277
|
fragment: DOT_GRID_FRAGMENT,
|
|
503
278
|
uniforms: {
|
|
504
279
|
uResolution: { value: [1, 1] },
|
|
505
|
-
uViewSize: { value: [1, 1] },
|
|
506
|
-
uViewOrigin: { value: [0, 0] },
|
|
507
280
|
uPixelRatio: { value: 1 },
|
|
508
281
|
uShape: { value: 0 },
|
|
509
|
-
|
|
282
|
+
uPulseShape: { value: 0 },
|
|
510
283
|
uCorner: { value: DOT_ROUNDED_SQUARE_CORNER },
|
|
511
|
-
uHoverPointer: { value: [-1e6, -1e6] },
|
|
512
|
-
uHoverRadius: { value: 0 },
|
|
513
|
-
uHoverAmount: { value: 0 },
|
|
514
|
-
uHoverFade: { value: 1 },
|
|
515
|
-
uHoverColor: { value: [0, 0, 0] },
|
|
516
284
|
uColumns: { value: 1 },
|
|
517
285
|
uOrigin: { value: [0, 0] },
|
|
518
286
|
uSpacing: { value: 1 },
|
|
519
287
|
uDotSize: { value: 1 },
|
|
520
288
|
uCoverSize: { value: 1 },
|
|
521
|
-
uRevealSize: { value: -1 },
|
|
522
289
|
uReveal: { value: 1 },
|
|
523
|
-
uFadeHandoff: { value: 1 },
|
|
524
290
|
uFadeBottom: { value: 0 },
|
|
525
|
-
uFadeColorOn: { value: 0 },
|
|
526
|
-
uFadeColor: { value: [1, 1, 1] },
|
|
527
|
-
uBottomTintOn: { value: 0 },
|
|
528
|
-
uBottomTintMode: { value: 0 },
|
|
529
|
-
uBottomTintRange: { value: [0, 0] },
|
|
530
|
-
uBottomTint: { value: [0, 0, 0, 0] },
|
|
531
|
-
uImageFade: { value: 0 },
|
|
532
|
-
uImageFadeColorOn: { value: 0 },
|
|
533
|
-
uImageFadeColor: { value: [1, 1, 1] },
|
|
534
291
|
tState: { value: chain.output },
|
|
535
292
|
uOmega: { value: 1 },
|
|
536
293
|
uThrowScale: { value: 0 },
|
|
@@ -545,23 +302,14 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
545
302
|
uPulseStart: { value: -1 },
|
|
546
303
|
uPulseChance: { value: 0 },
|
|
547
304
|
uPulseInterval: { value: 1 },
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
uRestOn: { value: 0 },
|
|
552
|
-
uRestInterval: { value: 1 },
|
|
553
|
-
uRestRandomOrigin: { value: 0 },
|
|
554
|
-
uMotionEnabled: { value: 1 },
|
|
555
|
-
uClicks: { value: clicks.data },
|
|
556
|
-
uClickEffects: { value: clicks.effects },
|
|
305
|
+
uPulseDuration: { value: 1 },
|
|
306
|
+
uPulseScale: { value: 1 },
|
|
307
|
+
uPulseOpacity: { value: 1 },
|
|
557
308
|
// OGL dereferences every sampler on draw, so the slot holds the state texture until an overlay arrives.
|
|
558
309
|
tOverlay: { value: chain.output },
|
|
559
310
|
uOverlaySize: { value: [1, 1] },
|
|
560
311
|
uOverlayOn: { value: 0 },
|
|
561
312
|
uOverlayZoom: { value: 1 },
|
|
562
|
-
uOverlayRegion: { value: [0, 0] },
|
|
563
|
-
uOverlayOffset: { value: [0, 0] },
|
|
564
|
-
uOverlayFade: { value: 0 },
|
|
565
313
|
},
|
|
566
314
|
transparent: true,
|
|
567
315
|
depthTest: false,
|
|
@@ -581,24 +329,20 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
581
329
|
}
|
|
582
330
|
|
|
583
331
|
/*
|
|
584
|
-
* Position the first dot
|
|
585
|
-
*
|
|
586
|
-
* margin, and give the simulation one texel per dot (which also rests every dot at home).
|
|
332
|
+
* Position the first dot, count how many cover the canvas plus a one-spacing margin, and
|
|
333
|
+
* give the simulation one texel per dot (which also rests every dot at home).
|
|
587
334
|
*/
|
|
588
335
|
function layout() {
|
|
589
336
|
const { width, height, pixelRatio } = size;
|
|
590
|
-
const { spacing, size: dotSize
|
|
591
|
-
const
|
|
592
|
-
const
|
|
593
|
-
const firstX = anchorX - Math.ceil(anchorX / spacing) * spacing;
|
|
594
|
-
const firstY = anchorY - Math.ceil(anchorY / spacing) * spacing;
|
|
337
|
+
const { spacing, size: dotSize } = options.dots;
|
|
338
|
+
const firstX = width / 2 - Math.ceil(width / 2 / spacing) * spacing;
|
|
339
|
+
const firstY = height / 2 - Math.ceil(height / 2 / spacing) * spacing;
|
|
595
340
|
const columns = Math.ceil((width - firstX) / spacing) + 1;
|
|
596
341
|
const rows = Math.ceil((height - firstY) / spacing) + 1;
|
|
597
342
|
const { uniforms } = program;
|
|
598
343
|
setVec2(uniforms.uResolution, width, height);
|
|
599
344
|
setVec2(uniforms.uOrigin, firstX, firstY);
|
|
600
345
|
setVec2(spring.uniforms.uOrigin, firstX, firstY);
|
|
601
|
-
spring.uniforms.uHeight.value = height;
|
|
602
346
|
uniforms.uPixelRatio.value = pixelRatio;
|
|
603
347
|
uniforms.uColumns.value = columns;
|
|
604
348
|
uniforms.uSpacing.value = spacing;
|
|
@@ -620,7 +364,9 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
620
364
|
const { uniforms } = program;
|
|
621
365
|
uniforms.uPulseChance.value = pulse && dotCount > 0 ? Math.min(1, pulse.count / dotCount) : 0;
|
|
622
366
|
uniforms.uPulseInterval.value = (pulse?.interval ?? 1000) / 1000;
|
|
623
|
-
|
|
367
|
+
uniforms.uPulseDuration.value = (pulse?.duration ?? 1000) / 1000;
|
|
368
|
+
uniforms.uPulseScale.value = pulse?.scale ?? 1;
|
|
369
|
+
uniforms.uPulseOpacity.value = pulse?.opacity ?? options.dots.opacity;
|
|
624
370
|
}
|
|
625
371
|
|
|
626
372
|
/*
|
|
@@ -629,36 +375,22 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
629
375
|
* The pulse uniforms depend on the dot count, so `layout` writes those.
|
|
630
376
|
*/
|
|
631
377
|
function applyOptions() {
|
|
632
|
-
const { dots, kick, thrown
|
|
378
|
+
const { dots, kick, thrown } = options;
|
|
633
379
|
const { uniforms } = program;
|
|
634
|
-
const
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
settleSeconds = (bounce.bounceDuration / 1000) * SPRING.settleFactor;
|
|
380
|
+
const zeta = SPRING.zetaRange[0] + (SPRING.zetaRange[1] - SPRING.zetaRange[0]) * (kick?.bounciness ?? 0);
|
|
381
|
+
omega = kick ? 4 / (zeta * (kick.bounceDuration / 1000)) : 1;
|
|
382
|
+
settleSeconds = kick ? (kick.bounceDuration / 1000) * SPRING.settleFactor : 0;
|
|
638
383
|
spring.uniforms.uRadius.value = kick?.radius ?? 0;
|
|
639
384
|
spring.uniforms.uStrength.value = kick?.strength ?? 0;
|
|
640
|
-
spring.uniforms.uRandomness.value = kick?.randomness ?? 0;
|
|
641
|
-
spring.uniforms.uImpulseRadius.value = click?.kickRadius ?? 0;
|
|
642
|
-
spring.uniforms.uImpulseStrength.value = click?.kickStrength ?? 0;
|
|
643
|
-
spring.uniforms.uImpulseRandomness.value = click?.kickRandomness ?? 0;
|
|
644
385
|
spring.uniforms.uOmega.value = omega;
|
|
645
386
|
spring.uniforms.uZeta.value = zeta;
|
|
646
387
|
uniforms.uOmega.value = omega;
|
|
647
388
|
// A dot counts as fully thrown at a quarter of the biggest throw a swipe can give
|
|
648
389
|
// (`radius × strength`); the kick falls off quadratically from the cursor, so most dots
|
|
649
390
|
// swing far less than that and this keeps the thrown look visible across the kicked area.
|
|
650
|
-
|
|
651
|
-
kick ? kick.radius * Math.abs(kick.strength) : 0,
|
|
652
|
-
click && (click.effect === 'ripple' || click.effect === 'random') ? click.kickRadius * Math.abs(click.kickStrength) : 0,
|
|
653
|
-
);
|
|
654
|
-
uniforms.uThrowScale.value = throwDistance > 0 ? 4 / throwDistance : 0;
|
|
391
|
+
uniforms.uThrowScale.value = kick ? 4 / (kick.radius * Math.abs(kick.strength)) : 0;
|
|
655
392
|
uniforms.uShape.value = DOT_SHAPES.indexOf(dots.shape);
|
|
656
|
-
|
|
657
|
-
setDotEffectUniforms(uniforms, 'uRest', options.rest);
|
|
658
|
-
uniforms.uRestOn.value = options.rest ? 1 : 0;
|
|
659
|
-
uniforms.uRestInterval.value = (options.rest?.interval ?? 1000) / 1000;
|
|
660
|
-
uniforms.uRestRandomOrigin.value = options.rest?.origin === 'random' ? 1 : 0;
|
|
661
|
-
if (!click) clicks.update(0, 0);
|
|
393
|
+
uniforms.uPulseShape.value = DOT_SHAPES.indexOf(options.pulse?.shape ?? dots.shape);
|
|
662
394
|
uniforms.uColor.value = hexToRgb(dots.color, [1, 1, 1]);
|
|
663
395
|
uniforms.uOpacity.value = dots.opacity;
|
|
664
396
|
uniforms.uThrownColorOn.value = thrown?.color ? 1 : 0;
|
|
@@ -671,30 +403,10 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
671
403
|
* Re-lay the grid for a new canvas size.
|
|
672
404
|
*/
|
|
673
405
|
function resize(nextSize: SceneSize) {
|
|
674
|
-
if (size.width === nextSize.width && size.height === nextSize.height && size.pixelRatio === nextSize.pixelRatio) return;
|
|
675
406
|
size = nextSize;
|
|
676
|
-
setViewport(nextSize.width, nextSize.height, 0);
|
|
677
407
|
layout();
|
|
678
408
|
}
|
|
679
409
|
|
|
680
|
-
/*
|
|
681
|
-
* Draw a window of the logical grid without reallocating or resetting its simulation.
|
|
682
|
-
*/
|
|
683
|
-
function setViewport(width: number, height: number, top: number) {
|
|
684
|
-
setVec2(program.uniforms.uViewSize, Math.max(1, width), Math.max(1, height));
|
|
685
|
-
setVec2(program.uniforms.uViewOrigin, 0, top);
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
/*
|
|
689
|
-
* Cover-fit the image to a board region, with the layer's offset in that board. The region's
|
|
690
|
-
* bottom blends only the base image color; kicks and click effects retain their strength.
|
|
691
|
-
*/
|
|
692
|
-
function setOverlayRegion(width: number, height: number, left: number, top: number, fade: number) {
|
|
693
|
-
setVec2(program.uniforms.uOverlayRegion, Math.max(1, width), Math.max(0, height));
|
|
694
|
-
setVec2(program.uniforms.uOverlayOffset, left, top);
|
|
695
|
-
program.uniforms.uOverlayFade.value = Math.max(0, Math.min(height, fade));
|
|
696
|
-
}
|
|
697
|
-
|
|
698
410
|
/*
|
|
699
411
|
* Apply new options in place; spacing and size change the layout, the rest are uniforms.
|
|
700
412
|
*/
|
|
@@ -717,69 +429,14 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
717
429
|
}
|
|
718
430
|
|
|
719
431
|
/*
|
|
720
|
-
*
|
|
721
|
-
*/
|
|
722
|
-
function setHover(color: RGB | null, radius = 175, fade = true) {
|
|
723
|
-
program.uniforms.uHoverColor.value = color ?? [0, 0, 0];
|
|
724
|
-
program.uniforms.uHoverRadius.value = color ? Math.max(1, radius) : 0;
|
|
725
|
-
program.uniforms.uHoverFade.value = fade ? 1 : 0;
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
/*
|
|
729
|
-
* Move the hover center in logical layer pixels; zero amount hides the tint.
|
|
432
|
+
* Clock for the pulse, in seconds; only needed while `isPulsing()`.
|
|
730
433
|
*/
|
|
731
|
-
function
|
|
732
|
-
setVec2(program.uniforms.uHoverPointer, x, y);
|
|
733
|
-
program.uniforms.uHoverAmount.value = program.uniforms.uHoverRadius.value > 0 ? amount : 0;
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
/*
|
|
737
|
-
* Clock and reduced-motion state for pulse, resting, and click effects, in seconds.
|
|
738
|
-
*/
|
|
739
|
-
function setTime(seconds: number, reducedMotion = false) {
|
|
434
|
+
function setTime(seconds: number) {
|
|
740
435
|
program.uniforms.uTime.value = seconds;
|
|
741
|
-
program.uniforms.uMotionEnabled.value = reducedMotion ? 0 : 1;
|
|
742
|
-
if (reducedMotion && simulating) rest();
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
/*
|
|
746
|
-
* Start a click effect; ripple impulses use the click's own radius and strength.
|
|
747
|
-
*/
|
|
748
|
-
function clickAt(x: number, y: number, seconds: number) {
|
|
749
|
-
if (!options.click) return false;
|
|
750
|
-
const effect = clicks.add(x, y, seconds, options.click.effect);
|
|
751
|
-
if (
|
|
752
|
-
effect === getDotEffectId('ripple') &&
|
|
753
|
-
options.click.kickStrength !== 0 &&
|
|
754
|
-
options.click.kickRadius > 0 &&
|
|
755
|
-
chain.halfFloat &&
|
|
756
|
-
program.uniforms.uMotionEnabled.value > 0
|
|
757
|
-
) {
|
|
758
|
-
const { uniforms } = spring;
|
|
759
|
-
setVec2(uniforms.uPointer, x, y);
|
|
760
|
-
setVec2(uniforms.uPointerVelocity, 0, 0);
|
|
761
|
-
uniforms.uDt.value = 0;
|
|
762
|
-
uniforms.uSteps.value = 1;
|
|
763
|
-
uniforms.uImpulse.value = 1;
|
|
764
|
-
// The shared click timestamp keeps board layers in sync; bound it for shader precision.
|
|
765
|
-
uniforms.uImpulseSeed.value = Math.round(seconds * 1000) % 4096;
|
|
766
|
-
program.uniforms.tState.value = chain.render([spring]);
|
|
767
|
-
uniforms.uImpulse.value = 0;
|
|
768
|
-
lastKickAt = seconds;
|
|
769
|
-
simulating = true;
|
|
770
|
-
}
|
|
771
|
-
return true;
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
/*
|
|
775
|
-
* Expire click effects or clear them for reduced motion; true requests another frame.
|
|
776
|
-
*/
|
|
777
|
-
function stepClicks(seconds: number, reducedMotion: boolean) {
|
|
778
|
-
return clicks.update(seconds, reducedMotion ? 0 : (options.click?.duration ?? 0) / 1000);
|
|
779
436
|
}
|
|
780
437
|
|
|
781
438
|
/*
|
|
782
|
-
* When (on the same clock as `setTime`)
|
|
439
|
+
* When (on the same clock as `setTime`) pulsing may begin, so the first cycle starts then;
|
|
783
440
|
* null holds the dots still, e.g. until a reveal has finished.
|
|
784
441
|
*/
|
|
785
442
|
function setPulseStart(seconds: number | null) {
|
|
@@ -790,14 +447,7 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
790
447
|
* Whether dots pulse on their own, which needs continuous frames.
|
|
791
448
|
*/
|
|
792
449
|
function isPulsing() {
|
|
793
|
-
return options.pulse !== null && options.pulse.count > 0;
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
/*
|
|
797
|
-
* Whether pulse or resting effects need an ambient animation loop.
|
|
798
|
-
*/
|
|
799
|
-
function isAnimating() {
|
|
800
|
-
return isPulsing() || options.rest !== null;
|
|
450
|
+
return options.pulse !== null && options.pulse.count > 0 && options.pulse.scale !== 1;
|
|
801
451
|
}
|
|
802
452
|
|
|
803
453
|
/*
|
|
@@ -809,61 +459,18 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
809
459
|
|
|
810
460
|
/*
|
|
811
461
|
* How far the dots have shrunk from full coverage to their size: 0 fills the area, 1 is
|
|
812
|
-
* the resting grid. Pass an already eased value.
|
|
813
|
-
* for an appear animation; use amount 1 to keep holes open and avoid clipping to cells.
|
|
462
|
+
* the resting grid. Pass an already eased value.
|
|
814
463
|
*/
|
|
815
|
-
function setReveal(amount: number
|
|
464
|
+
function setReveal(amount: number) {
|
|
816
465
|
program.uniforms.uReveal.value = Math.min(1, Math.max(0, amount));
|
|
817
|
-
program.uniforms.uRevealSize.value = diameter === undefined ? -1 : Math.max(0, diameter);
|
|
818
466
|
}
|
|
819
467
|
|
|
820
468
|
/*
|
|
821
|
-
* Height in CSS pixels of the fade along the bottom edge; 0 for none. The
|
|
822
|
-
*
|
|
823
|
-
* cursor kicks and swells them less the deeper into the fade they sit. The filled reveal
|
|
824
|
-
* state is not this fade's: `setFadeHandoff` brings it in, taking over from `setImageFade`.
|
|
469
|
+
* Height in CSS pixels of the fade to transparent along the bottom edge; 0 for none. The
|
|
470
|
+
* filled reveal state is never faded: the fade comes in with `setReveal`.
|
|
825
471
|
*/
|
|
826
|
-
function setFadeBottom(height: number
|
|
472
|
+
function setFadeBottom(height: number) {
|
|
827
473
|
program.uniforms.uFadeBottom.value = Math.max(0, height);
|
|
828
|
-
spring.uniforms.uFadeBottom.value = Math.max(0, height);
|
|
829
|
-
program.uniforms.uFadeColorOn.value = color ? 1 : 0;
|
|
830
|
-
if (color) program.uniforms.uFadeColor.value = hexToRgb(color, [1, 1, 1]);
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
/*
|
|
834
|
-
* Blend the final dot color to RGBA between bottom-relative distances, retaining it below
|
|
835
|
-
* the end stop. Unlike reveal fades this never weakens cursor movement or dot scaling.
|
|
836
|
-
*/
|
|
837
|
-
function setBottomTint(
|
|
838
|
-
start: number,
|
|
839
|
-
end: number,
|
|
840
|
-
color: [number, number, number, number] | null,
|
|
841
|
-
mode: 'color' | 'normal' | 'blend' = 'color',
|
|
842
|
-
) {
|
|
843
|
-
program.uniforms.uBottomTintOn.value = color ? 1 : 0;
|
|
844
|
-
program.uniforms.uBottomTintMode.value = color ? (mode === 'blend' ? 2 : mode === 'normal' ? 1 : 0) : 0;
|
|
845
|
-
setVec2(program.uniforms.uBottomTintRange, start, end);
|
|
846
|
-
if (color) program.uniforms.uBottomTint.value = color;
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
/*
|
|
850
|
-
* Height in CSS pixels of the fade along the bottom edge of the filled reveal state (the
|
|
851
|
-
* solid image or color before the dots shrink); 0 for none. To transparent, or to the hex
|
|
852
|
-
* `color` when given. It goes out with `setFadeHandoff` as `setFadeBottom` comes in.
|
|
853
|
-
*/
|
|
854
|
-
function setImageFade(height: number, color: string | null = null) {
|
|
855
|
-
program.uniforms.uImageFade.value = Math.max(0, height);
|
|
856
|
-
program.uniforms.uImageFadeColorOn.value = color ? 1 : 0;
|
|
857
|
-
if (color) program.uniforms.uImageFadeColor.value = hexToRgb(color, [1, 1, 1]);
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
/*
|
|
861
|
-
* Which bottom fade is drawn, 0..1: at 0 the filled reveal state's (`setImageFade`) at full
|
|
862
|
-
* strength and none of the dots', at 1 the dots' (`setFadeBottom`) alone, blending in
|
|
863
|
-
* between. Independent of `setReveal`, so a fade can outlast the shrink.
|
|
864
|
-
*/
|
|
865
|
-
function setFadeHandoff(amount: number) {
|
|
866
|
-
program.uniforms.uFadeHandoff.value = Math.min(1, Math.max(0, amount));
|
|
867
474
|
}
|
|
868
475
|
|
|
869
476
|
/*
|
|
@@ -872,10 +479,10 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
872
479
|
* no reveal is running). Returns whether the dots are still moving and need another frame.
|
|
873
480
|
*/
|
|
874
481
|
function step(pointer: [number, number], velocity: [number, number], active: boolean, dt: number, now: number) {
|
|
875
|
-
if (!
|
|
482
|
+
if (!isInteractive()) return false;
|
|
876
483
|
// A zero-length frame (the first after a pause) has nothing to integrate; keep asking for frames.
|
|
877
484
|
if (dt <= 0) return simulating;
|
|
878
|
-
const moving = active &&
|
|
485
|
+
const moving = active && (velocity[0] !== 0 || velocity[1] !== 0);
|
|
879
486
|
if (moving) {
|
|
880
487
|
lastKickAt = now;
|
|
881
488
|
simulating = true;
|
|
@@ -928,24 +535,14 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
928
535
|
resize,
|
|
929
536
|
setOptions,
|
|
930
537
|
setOverlay,
|
|
931
|
-
setOverlayRegion,
|
|
932
|
-
setViewport,
|
|
933
538
|
setOverlayZoom,
|
|
934
539
|
setReveal,
|
|
935
540
|
setFadeBottom,
|
|
936
|
-
setBottomTint,
|
|
937
|
-
setImageFade,
|
|
938
|
-
setFadeHandoff,
|
|
939
541
|
setTime,
|
|
940
|
-
setHover,
|
|
941
|
-
setHoverPointer,
|
|
942
|
-
clickAt,
|
|
943
|
-
stepClicks,
|
|
944
542
|
setPulseStart,
|
|
945
543
|
step,
|
|
946
544
|
isInteractive,
|
|
947
545
|
isPulsing,
|
|
948
|
-
isAnimating,
|
|
949
546
|
draw,
|
|
950
547
|
dispose,
|
|
951
548
|
};
|