@jsb188/gfx 1.1.11 → 1.1.12
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 +248 -54
- package/elements/Shapes.helpers.ts +204 -0
- package/elements/Shapes.hooks.ts +171 -0
- package/elements/Shapes.tsx +505 -0
- package/elements/Shapes.types.ts +81 -0
- package/elements/Stack.helpers.ts +42 -0
- package/elements/Stack.tsx +143 -0
- package/elements/Text.helpers.ts +45 -0
- package/elements/Text.hooks.ts +197 -0
- package/elements/Text.tsx +98 -0
- package/libs/dotGrid.helpers.ts +53 -0
- package/libs/dotGrid.ts +480 -77
- package/libs/effects/dotEffects.ts +136 -0
- package/libs/effects/effectQuad.ts +8 -2
- package/libs/pointer.ts +6 -0
- package/libs/runtime.ts +6 -4
- package/libs/types.ts +8 -1
- package/main.ts +2 -0
- package/modules/Carousel.helpers.ts +234 -0
- package/modules/Carousel.hooks.ts +520 -0
- package/modules/Carousel.tsx +135 -29
- package/modules/Dots.tsx +269 -0
- package/modules/DotsBoard.helpers.ts +388 -0
- package/modules/DotsBoard.hooks.ts +144 -0
- package/modules/DotsBoard.strings.ts +649 -0
- package/modules/DotsBoard.types.ts +240 -0
- package/modules/DotsBoardCanvas.hooks.ts +49 -0
- package/modules/DotsBoardCanvas.tsx +190 -0
- package/modules/DotsBoardElements.tsx +330 -0
- package/modules/Gallery.helpers.ts +32 -0
- package/modules/Gallery.hooks.ts +149 -0
- package/modules/Gallery.tsx +194 -0
- package/modules/GfxCanvas.tsx +10 -2
- package/modules/KineticHeader.helpers.ts +44 -0
- package/modules/KineticHeader.hooks.ts +206 -0
- package/modules/KineticHeader.tsx +305 -0
- package/modules/KineticHeader.types.ts +56 -0
- package/modules/ScrollSteps.hooks.ts +138 -0
- package/modules/ScrollSteps.tsx +7 -52
- package/package.json +15 -5
- package/scenes/Carousel.ts +99 -18
- package/scenes/DotsBoard.helpers.ts +12 -0
- package/scenes/DotsBoard.ts +245 -0
- package/tsconfig.json +1 -1
- package/utils/align.ts +18 -0
- package/utils/color.ts +64 -2
- package/utils/dotEffectSpecs.ts +79 -0
- package/utils/dotGridSpecs.ts +304 -46
- package/utils/motion.ts +88 -0
- package/utils/random.ts +22 -0
- package/utils/strings.ts +116 -0
- package/modules/DotsArea.tsx +0 -120
- package/modules/VisionBoard.helpers.ts +0 -561
- package/modules/VisionBoard.tsx +0 -358
- package/modules/VisionBoard.types.ts +0 -88
- package/scenes/DotsArea.ts +0 -208
package/libs/dotGrid.ts
CHANGED
|
@@ -3,16 +3,22 @@ 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,
|
|
6
8
|
DOT_SHAPES,
|
|
9
|
+
getDotGridAnchor,
|
|
7
10
|
getDotGridCoverSize,
|
|
8
11
|
isDotGridInteractive,
|
|
9
12
|
resolveDotGridOptions,
|
|
10
13
|
} from '../utils/dotGridSpecs.ts';
|
|
11
14
|
import type { DotGridOptions } from '../utils/dotGridSpecs.ts';
|
|
15
|
+
import { getDotEffectId } from '../utils/dotEffectSpecs.ts';
|
|
12
16
|
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';
|
|
13
19
|
import { GLSL_CONSTANTS, GLSL_COVER_UV, GLSL_HASH2, GLSL_HEADER, fragmentShader } from './glsl.ts';
|
|
14
20
|
import type { ImageTexture } from './texture.ts';
|
|
15
|
-
import type { SceneSize } from './types.ts';
|
|
21
|
+
import type { RGB, SceneSize } from './types.ts';
|
|
16
22
|
import { setVec2 } from './uniforms.ts';
|
|
17
23
|
|
|
18
24
|
/**
|
|
@@ -25,16 +31,18 @@ import { setVec2 } from './uniforms.ts';
|
|
|
25
31
|
* replace the flat color: each dot shows the part of the cover-fit image under it, so bigger
|
|
26
32
|
* dots reveal more of it. A reveal amount grows the dots to full coverage, so at 0 the grid
|
|
27
33
|
* reads as a solid image or color, and a pulse makes a random few dots swell and shrink each
|
|
28
|
-
* cycle. A bottom fade turns
|
|
29
|
-
* toward the bottom edge (
|
|
30
|
-
*
|
|
31
|
-
* transparent canvas.
|
|
34
|
+
* cycle. A bottom fade turns the dots over the last so many pixels transparent, or into one
|
|
35
|
+
* flat color, toward the bottom edge (thrown or pulsing or not, and the image through them);
|
|
36
|
+
* the filled reveal state has its own bottom fade, which a fade handoff amount crosses over to
|
|
37
|
+
* the dots' once they have shrunk. Output is premultiplied for a transparent canvas.
|
|
32
38
|
*/
|
|
33
39
|
|
|
34
40
|
// Per texel: xy = offset from home (CSS px), zw = velocity (px/s). Each frame the cursor
|
|
35
41
|
// kicks dots inside `uRadius` of it away from itself in proportion to its speed (with a
|
|
36
42
|
// per-dot random factor for a scattered look), then a damped spring (ω, ζ) pulls them home.
|
|
37
|
-
//
|
|
43
|
+
// Inside the bottom fade the kick eases off with the dot's depth into it (none on the bottom
|
|
44
|
+
// edge), so the fading dots stay put. Semi-implicit Euler stays stable while ω·dt < 2, so
|
|
45
|
+
// large frames are split into substeps.
|
|
38
46
|
const SPRING_FRAGMENT = fragmentShader(
|
|
39
47
|
`
|
|
40
48
|
uniform vec2 uOrigin;
|
|
@@ -43,10 +51,26 @@ uniform vec2 uPointer;
|
|
|
43
51
|
uniform vec2 uPointerVelocity;
|
|
44
52
|
uniform float uRadius;
|
|
45
53
|
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;
|
|
46
60
|
uniform float uOmega;
|
|
47
61
|
uniform float uZeta;
|
|
48
62
|
uniform float uDt;
|
|
49
63
|
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
|
+
}
|
|
50
74
|
void main() {
|
|
51
75
|
ivec2 cell = ivec2(gl_FragCoord.xy);
|
|
52
76
|
vec4 state = texelFetch(tInput, cell, 0);
|
|
@@ -54,8 +78,25 @@ void main() {
|
|
|
54
78
|
vec2 velocity = state.zw;
|
|
55
79
|
vec2 home = uOrigin + vec2(cell) * uSpacing;
|
|
56
80
|
float speed = length(uPointerVelocity);
|
|
57
|
-
|
|
81
|
+
// How far from the bottom fade the dot's home is (1 above it, 0 on the bottom edge).
|
|
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
|
+
}
|
|
58
97
|
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;
|
|
59
100
|
for (int i = 0; i < 8; i++) {
|
|
60
101
|
if (i >= uSteps) break;
|
|
61
102
|
vec2 away = home + offset - uPointer;
|
|
@@ -63,6 +104,7 @@ void main() {
|
|
|
63
104
|
if (speed > 0.0 && dist < uRadius) {
|
|
64
105
|
float falloff = 1.0 - dist / uRadius;
|
|
65
106
|
vec2 direction = dist > 0.001 ? away / dist : vec2(1.0, 0.0);
|
|
107
|
+
direction = scatterDotKick(direction, hoverVariation, uRandomness);
|
|
66
108
|
velocity += direction * (uStrength * speed * 0.5 * uOmega * falloff * falloff * random) * dt;
|
|
67
109
|
}
|
|
68
110
|
velocity += (-uOmega * uOmega * offset - 2.0 * uZeta * uOmega * velocity) * dt;
|
|
@@ -81,19 +123,30 @@ void main() {
|
|
|
81
123
|
`);
|
|
82
124
|
|
|
83
125
|
// The resting diameter eases from the cover size to `uDotSize` with `uReveal`; the thrown and
|
|
84
|
-
// pulse scales multiply on top, and the sprite is capped at what the driver allows.
|
|
126
|
+
// pulse scales multiply on top, and the sprite is capped at what the driver allows. Inside
|
|
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
|
|
85
130
|
// cycle is `floor(time / interval)`: hashing the cell with the cycle picks about `chance` of
|
|
86
131
|
// the dots (never the same dot twice in a row) and gives each a random start inside the cycle,
|
|
87
132
|
// so no per-dot state is needed.
|
|
88
133
|
const DOT_GRID_VERTEX = `${GLSL_HEADER}${GLSL_CONSTANTS}
|
|
89
134
|
uniform vec2 uResolution;
|
|
135
|
+
uniform vec2 uViewSize;
|
|
136
|
+
uniform vec2 uViewOrigin;
|
|
90
137
|
uniform float uPixelRatio;
|
|
138
|
+
uniform vec2 uHoverPointer;
|
|
139
|
+
uniform float uHoverRadius;
|
|
140
|
+
uniform float uHoverAmount;
|
|
141
|
+
uniform float uHoverFade;
|
|
91
142
|
uniform float uColumns;
|
|
92
143
|
uniform vec2 uOrigin;
|
|
93
144
|
uniform float uSpacing;
|
|
94
145
|
uniform float uDotSize;
|
|
95
146
|
uniform float uCoverSize;
|
|
147
|
+
uniform float uRevealSize;
|
|
96
148
|
uniform float uReveal;
|
|
149
|
+
uniform float uFadeBottom;
|
|
97
150
|
uniform sampler2D tState;
|
|
98
151
|
uniform float uOmega;
|
|
99
152
|
uniform float uThrowScale;
|
|
@@ -105,14 +158,25 @@ uniform float uTime;
|
|
|
105
158
|
uniform float uPulseStart;
|
|
106
159
|
uniform float uPulseChance;
|
|
107
160
|
uniform float uPulseInterval;
|
|
108
|
-
uniform float
|
|
109
|
-
uniform float
|
|
110
|
-
uniform float
|
|
161
|
+
uniform float uRestOn;
|
|
162
|
+
uniform float uRestInterval;
|
|
163
|
+
uniform float uRestRandomOrigin;
|
|
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;
|
|
111
171
|
out float vWeight;
|
|
112
172
|
out float vPulse;
|
|
173
|
+
out float vRest;
|
|
174
|
+
out vec4 vEffectTint;
|
|
175
|
+
out float vClick;
|
|
113
176
|
out float vOpacity;
|
|
114
177
|
out float vRadius;
|
|
115
178
|
out float vPointSize;
|
|
179
|
+
flat out ivec2 vCell;
|
|
116
180
|
|
|
117
181
|
// Integer bit-mixing hash: exact on every GPU, unlike sin-based float hashes whose results drift
|
|
118
182
|
// (and correlate between cycles) once the inputs grow large.
|
|
@@ -134,38 +198,105 @@ bool pulsesIn(ivec2 cell, int cycle) {
|
|
|
134
198
|
return cellRandom(cell, cycle, 1u) < uPulseChance;
|
|
135
199
|
}
|
|
136
200
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
201
|
+
${GLSL_DOT_EFFECTS}
|
|
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;
|
|
140
209
|
float time = uTime - uPulseStart;
|
|
141
|
-
if (uPulseChance <= 0.0 || uPulseStart < 0.0 || time < 0.0) return
|
|
210
|
+
if (uMotionEnabled < 0.5 || uPulseChance <= 0.0 || uPulseStart < 0.0 || time < 0.0) return noDotEffect();
|
|
142
211
|
int cycle = int(floor(time / uPulseInterval));
|
|
143
|
-
if (!pulsesIn(cell, cycle) || (cycle > 0 && pulsesIn(cell, cycle - 1))) return
|
|
212
|
+
if (!pulsesIn(cell, cycle) || (cycle > 0 && pulsesIn(cell, cycle - 1))) return noDotEffect();
|
|
144
213
|
float start = cellRandom(cell, cycle, 2u) * (uPulseInterval - uPulseDuration);
|
|
145
|
-
float
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
return
|
|
214
|
+
float phase = (time - float(cycle) * uPulseInterval - start) / uPulseDuration;
|
|
215
|
+
seed = cellRandom(cell, cycle, 3u);
|
|
216
|
+
int effect = selectDotEffect(uPulseEffect, cellRandom(cell, cycle, 4u));
|
|
217
|
+
vec2 away = effect == 0 ? vec2(0.0) : vec2(cos(seed * PI * 2.0), sin(seed * PI * 2.0)) * uPulseRadius * 0.35;
|
|
218
|
+
return sampleDotEffect(effect, away, phase, seed, uPulseRadius, uPulseWidth, uPulseStrength);
|
|
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;
|
|
150
259
|
}
|
|
151
260
|
|
|
152
261
|
void main() {
|
|
153
|
-
float
|
|
154
|
-
|
|
262
|
+
// Integer division, exact on every GPU (a float mod/divide can land a row's first dot at the end of the row before).
|
|
263
|
+
int columns = int(uColumns + 0.5);
|
|
264
|
+
ivec2 cell = ivec2(gl_VertexID % columns, gl_VertexID / columns);
|
|
265
|
+
vCell = cell;
|
|
155
266
|
vec4 state = texelFetch(tState, cell, 0);
|
|
156
267
|
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);
|
|
159
268
|
// How far the dot has been thrown: the spring's swing amplitude, which decays smoothly
|
|
160
269
|
// instead of dipping every time the dot passes through home.
|
|
161
270
|
float amplitude = length(vec2(length(state.xy), length(state.zw) / uOmega));
|
|
162
271
|
float weight = clamp(amplitude * uThrowScale, 0.0, 1.0);
|
|
163
272
|
vWeight = weight;
|
|
164
|
-
float
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
273
|
+
float restSeed;
|
|
274
|
+
float pulseSeed;
|
|
275
|
+
float clickSeed;
|
|
276
|
+
DotEffectSample resting = restEffect(cell, position, restSeed);
|
|
277
|
+
DotEffectSample pulsing = pulseEffect(cell, pulseSeed);
|
|
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);
|
|
169
300
|
vPointSize = min(size * uPixelRatio + 2.0, uMaxPointSize);
|
|
170
301
|
vRadius = (vPointSize - 2.0) * 0.5;
|
|
171
302
|
gl_PointSize = vPointSize;
|
|
@@ -177,14 +308,34 @@ void main() {
|
|
|
177
308
|
// a signed distance from the sprite center (device pixels, negative inside), which gives every
|
|
178
309
|
// shape the same one-pixel anti-aliased rim and lets a pulsing dot morph between its resting
|
|
179
310
|
// shape and the pulse shape by blending the two distances. The bottom fade is also by screen
|
|
180
|
-
// position, so a dot thrown into the fade zone fades with it
|
|
311
|
+
// position, so a dot thrown into the fade zone fades with it, to transparent or to the fade
|
|
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.
|
|
181
316
|
const DOT_GRID_FRAGMENT = `${GLSL_HEADER}
|
|
182
317
|
uniform vec2 uResolution;
|
|
318
|
+
uniform vec2 uViewSize;
|
|
319
|
+
uniform vec2 uViewOrigin;
|
|
183
320
|
uniform float uPixelRatio;
|
|
321
|
+
uniform vec2 uOrigin;
|
|
322
|
+
uniform float uSpacing;
|
|
184
323
|
uniform float uReveal;
|
|
324
|
+
uniform float uFadeHandoff;
|
|
185
325
|
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;
|
|
186
335
|
uniform int uShape;
|
|
187
336
|
uniform int uPulseShape;
|
|
337
|
+
uniform int uRestShape;
|
|
338
|
+
uniform int uClickShape;
|
|
188
339
|
uniform float uCorner;
|
|
189
340
|
uniform vec3 uColor;
|
|
190
341
|
uniform float uThrownColorOn;
|
|
@@ -193,39 +344,103 @@ uniform sampler2D tOverlay;
|
|
|
193
344
|
uniform vec2 uOverlaySize;
|
|
194
345
|
uniform float uOverlayOn;
|
|
195
346
|
uniform float uOverlayZoom;
|
|
347
|
+
uniform vec2 uOverlayRegion;
|
|
348
|
+
uniform vec2 uOverlayOffset;
|
|
349
|
+
uniform float uOverlayFade;
|
|
350
|
+
uniform vec3 uHoverColor;
|
|
351
|
+
in float vHover;
|
|
196
352
|
in float vWeight;
|
|
197
353
|
in float vPulse;
|
|
354
|
+
in float vRest;
|
|
355
|
+
in vec4 vEffectTint;
|
|
356
|
+
in float vClick;
|
|
198
357
|
in float vOpacity;
|
|
199
358
|
in float vRadius;
|
|
200
359
|
in float vPointSize;
|
|
360
|
+
flat in ivec2 vCell;
|
|
201
361
|
out vec4 fragColor;
|
|
202
362
|
${GLSL_COVER_UV}
|
|
203
363
|
|
|
204
364
|
float shapeDistance(int shape, vec2 p, float r) {
|
|
205
|
-
if (shape == 1) {
|
|
365
|
+
if (shape == 1 || shape == 3) {
|
|
206
366
|
float corner = r * uCorner;
|
|
207
367
|
vec2 q = abs(p) - (r - corner);
|
|
208
|
-
|
|
368
|
+
float outer = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - corner;
|
|
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));
|
|
209
380
|
}
|
|
210
381
|
return length(p) - r;
|
|
211
382
|
}
|
|
212
383
|
|
|
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
|
+
|
|
213
392
|
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;
|
|
214
396
|
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
|
+
}
|
|
215
402
|
float dist = shapeDistance(uShape, p, vRadius);
|
|
216
|
-
if (
|
|
403
|
+
if (vRest > 0.0) dist = mix(dist, shapeDistance(uRestShape, p, vRadius), vRest);
|
|
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);
|
|
217
406
|
float alpha = clamp(-dist + 0.5, 0.0, 1.0) * vOpacity;
|
|
218
|
-
//
|
|
219
|
-
//
|
|
220
|
-
//
|
|
221
|
-
float
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
407
|
+
// Two linear fades along the logical layer's bottom edge, each 0 above its zone
|
|
408
|
+
// and 1 on the edge: the filled state's over uImageFade CSS pixels while the handoff is 0,
|
|
409
|
+
// and the dots' over uFadeBottom once it is 1, crossing over in between.
|
|
410
|
+
float faded = uFadeBottom > 0.0 ? (1.0 - clamp((uResolution.y - fragment.y) / uFadeBottom, 0.0, 1.0)) * uFadeHandoff : 0.0;
|
|
411
|
+
float imageFaded = uImageFade > 0.0 ? (1.0 - clamp((uResolution.y - fragment.y) / uImageFade, 0.0, 1.0)) * (1.0 - uFadeHandoff) : 0.0;
|
|
412
|
+
// To transparent, unless a fade color takes over the color instead.
|
|
413
|
+
alpha *= 1.0 - faded * (1.0 - uFadeColorOn);
|
|
414
|
+
alpha *= 1.0 - imageFaded * (1.0 - uImageFadeColorOn);
|
|
415
|
+
// Sample the board's top image region, independently of the layer and viewport heights.
|
|
416
|
+
vec2 region = uOverlayRegion.y > 0.0 ? uOverlayRegion : uResolution;
|
|
417
|
+
vec2 imagePoint = fragment + uOverlayOffset;
|
|
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
|
+
}
|
|
229
444
|
}
|
|
230
445
|
`;
|
|
231
446
|
|
|
@@ -243,9 +458,10 @@ const SPRING = {
|
|
|
243
458
|
|
|
244
459
|
/*
|
|
245
460
|
* Build the simulation pass, the point program, an attribute-free geometry (WebGL2 draws
|
|
246
|
-
* from `gl_VertexID`), and the helpers that lay the grid out and drive it. The grid
|
|
247
|
-
* dot
|
|
248
|
-
* past every edge. Without half-float render targets the
|
|
461
|
+
* from `gl_VertexID`), and the helpers that lay the grid out and drive it. The grid anchors a
|
|
462
|
+
* dot per `dots.alignX` / `alignY` (centered by default, like `mask-position: center` does for
|
|
463
|
+
* a CSS tile) and extends one spacing past every edge. Without half-float render targets the
|
|
464
|
+
* cursor kicks are disabled; the other effects still work.
|
|
249
465
|
*/
|
|
250
466
|
export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridOptions) {
|
|
251
467
|
let options = resolveDotGridOptions(initialOptions);
|
|
@@ -255,6 +471,7 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
255
471
|
let dotCount = 0;
|
|
256
472
|
let lastKickAt = Number.NEGATIVE_INFINITY;
|
|
257
473
|
let simulating = false;
|
|
474
|
+
const clicks = createDotClickEffects();
|
|
258
475
|
// Sprites larger than the driver allows would silently clamp, so the dot size is capped.
|
|
259
476
|
const [, maxPointSize] = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE) as Float32Array;
|
|
260
477
|
|
|
@@ -266,10 +483,18 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
266
483
|
uPointerVelocity: { value: [0, 0] },
|
|
267
484
|
uRadius: { value: 0 },
|
|
268
485
|
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 },
|
|
269
492
|
uOmega: { value: 1 },
|
|
270
493
|
uZeta: { value: 1 },
|
|
271
494
|
uDt: { value: 0 },
|
|
272
495
|
uSteps: { value: 1 },
|
|
496
|
+
uHeight: { value: 1 },
|
|
497
|
+
uFadeBottom: { value: 0 },
|
|
273
498
|
});
|
|
274
499
|
const restPass = createPassProgram(gl, REST_FRAGMENT);
|
|
275
500
|
const program = new Program(gl, {
|
|
@@ -277,17 +502,35 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
277
502
|
fragment: DOT_GRID_FRAGMENT,
|
|
278
503
|
uniforms: {
|
|
279
504
|
uResolution: { value: [1, 1] },
|
|
505
|
+
uViewSize: { value: [1, 1] },
|
|
506
|
+
uViewOrigin: { value: [0, 0] },
|
|
280
507
|
uPixelRatio: { value: 1 },
|
|
281
508
|
uShape: { value: 0 },
|
|
282
|
-
|
|
509
|
+
|
|
283
510
|
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] },
|
|
284
516
|
uColumns: { value: 1 },
|
|
285
517
|
uOrigin: { value: [0, 0] },
|
|
286
518
|
uSpacing: { value: 1 },
|
|
287
519
|
uDotSize: { value: 1 },
|
|
288
520
|
uCoverSize: { value: 1 },
|
|
521
|
+
uRevealSize: { value: -1 },
|
|
289
522
|
uReveal: { value: 1 },
|
|
523
|
+
uFadeHandoff: { value: 1 },
|
|
290
524
|
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] },
|
|
291
534
|
tState: { value: chain.output },
|
|
292
535
|
uOmega: { value: 1 },
|
|
293
536
|
uThrowScale: { value: 0 },
|
|
@@ -302,14 +545,23 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
302
545
|
uPulseStart: { value: -1 },
|
|
303
546
|
uPulseChance: { value: 0 },
|
|
304
547
|
uPulseInterval: { value: 1 },
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
548
|
+
...createDotEffectUniforms('uPulse'),
|
|
549
|
+
...createDotEffectUniforms('uClick'),
|
|
550
|
+
...createDotEffectUniforms('uRest'),
|
|
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 },
|
|
308
557
|
// OGL dereferences every sampler on draw, so the slot holds the state texture until an overlay arrives.
|
|
309
558
|
tOverlay: { value: chain.output },
|
|
310
559
|
uOverlaySize: { value: [1, 1] },
|
|
311
560
|
uOverlayOn: { value: 0 },
|
|
312
561
|
uOverlayZoom: { value: 1 },
|
|
562
|
+
uOverlayRegion: { value: [0, 0] },
|
|
563
|
+
uOverlayOffset: { value: [0, 0] },
|
|
564
|
+
uOverlayFade: { value: 0 },
|
|
313
565
|
},
|
|
314
566
|
transparent: true,
|
|
315
567
|
depthTest: false,
|
|
@@ -329,20 +581,24 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
329
581
|
}
|
|
330
582
|
|
|
331
583
|
/*
|
|
332
|
-
* Position the first dot
|
|
333
|
-
*
|
|
584
|
+
* Position the first dot (the last one at or before the canvas start on each axis, walking
|
|
585
|
+
* back from the alignment anchor), count how many cover the canvas plus a one-spacing
|
|
586
|
+
* margin, and give the simulation one texel per dot (which also rests every dot at home).
|
|
334
587
|
*/
|
|
335
588
|
function layout() {
|
|
336
589
|
const { width, height, pixelRatio } = size;
|
|
337
|
-
const { spacing, size: dotSize } = options.dots;
|
|
338
|
-
const
|
|
339
|
-
const
|
|
590
|
+
const { spacing, size: dotSize, alignX, alignY } = options.dots;
|
|
591
|
+
const anchorX = getDotGridAnchor(alignX, dotSize, width);
|
|
592
|
+
const anchorY = getDotGridAnchor(alignY, dotSize, height);
|
|
593
|
+
const firstX = anchorX - Math.ceil(anchorX / spacing) * spacing;
|
|
594
|
+
const firstY = anchorY - Math.ceil(anchorY / spacing) * spacing;
|
|
340
595
|
const columns = Math.ceil((width - firstX) / spacing) + 1;
|
|
341
596
|
const rows = Math.ceil((height - firstY) / spacing) + 1;
|
|
342
597
|
const { uniforms } = program;
|
|
343
598
|
setVec2(uniforms.uResolution, width, height);
|
|
344
599
|
setVec2(uniforms.uOrigin, firstX, firstY);
|
|
345
600
|
setVec2(spring.uniforms.uOrigin, firstX, firstY);
|
|
601
|
+
spring.uniforms.uHeight.value = height;
|
|
346
602
|
uniforms.uPixelRatio.value = pixelRatio;
|
|
347
603
|
uniforms.uColumns.value = columns;
|
|
348
604
|
uniforms.uSpacing.value = spacing;
|
|
@@ -364,9 +620,7 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
364
620
|
const { uniforms } = program;
|
|
365
621
|
uniforms.uPulseChance.value = pulse && dotCount > 0 ? Math.min(1, pulse.count / dotCount) : 0;
|
|
366
622
|
uniforms.uPulseInterval.value = (pulse?.interval ?? 1000) / 1000;
|
|
367
|
-
uniforms
|
|
368
|
-
uniforms.uPulseScale.value = pulse?.scale ?? 1;
|
|
369
|
-
uniforms.uPulseOpacity.value = pulse?.opacity ?? options.dots.opacity;
|
|
623
|
+
setDotEffectUniforms(uniforms, 'uPulse', pulse);
|
|
370
624
|
}
|
|
371
625
|
|
|
372
626
|
/*
|
|
@@ -375,22 +629,36 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
375
629
|
* The pulse uniforms depend on the dot count, so `layout` writes those.
|
|
376
630
|
*/
|
|
377
631
|
function applyOptions() {
|
|
378
|
-
const { dots, kick, thrown } = options;
|
|
632
|
+
const { dots, kick, thrown, click } = options;
|
|
379
633
|
const { uniforms } = program;
|
|
380
|
-
const
|
|
381
|
-
|
|
382
|
-
|
|
634
|
+
const bounce = kick ?? DOT_GRID_DEFAULTS.kick;
|
|
635
|
+
const zeta = SPRING.zetaRange[0] + (SPRING.zetaRange[1] - SPRING.zetaRange[0]) * bounce.bounciness;
|
|
636
|
+
omega = 4 / (zeta * (bounce.bounceDuration / 1000));
|
|
637
|
+
settleSeconds = (bounce.bounceDuration / 1000) * SPRING.settleFactor;
|
|
383
638
|
spring.uniforms.uRadius.value = kick?.radius ?? 0;
|
|
384
639
|
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;
|
|
385
644
|
spring.uniforms.uOmega.value = omega;
|
|
386
645
|
spring.uniforms.uZeta.value = zeta;
|
|
387
646
|
uniforms.uOmega.value = omega;
|
|
388
647
|
// A dot counts as fully thrown at a quarter of the biggest throw a swipe can give
|
|
389
648
|
// (`radius × strength`); the kick falls off quadratically from the cursor, so most dots
|
|
390
649
|
// swing far less than that and this keeps the thrown look visible across the kicked area.
|
|
391
|
-
|
|
650
|
+
const throwDistance = Math.max(
|
|
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;
|
|
392
655
|
uniforms.uShape.value = DOT_SHAPES.indexOf(dots.shape);
|
|
393
|
-
uniforms
|
|
656
|
+
setDotEffectUniforms(uniforms, 'uClick', click);
|
|
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);
|
|
394
662
|
uniforms.uColor.value = hexToRgb(dots.color, [1, 1, 1]);
|
|
395
663
|
uniforms.uOpacity.value = dots.opacity;
|
|
396
664
|
uniforms.uThrownColorOn.value = thrown?.color ? 1 : 0;
|
|
@@ -403,10 +671,30 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
403
671
|
* Re-lay the grid for a new canvas size.
|
|
404
672
|
*/
|
|
405
673
|
function resize(nextSize: SceneSize) {
|
|
674
|
+
if (size.width === nextSize.width && size.height === nextSize.height && size.pixelRatio === nextSize.pixelRatio) return;
|
|
406
675
|
size = nextSize;
|
|
676
|
+
setViewport(nextSize.width, nextSize.height, 0);
|
|
407
677
|
layout();
|
|
408
678
|
}
|
|
409
679
|
|
|
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
|
+
|
|
410
698
|
/*
|
|
411
699
|
* Apply new options in place; spacing and size change the layout, the rest are uniforms.
|
|
412
700
|
*/
|
|
@@ -429,14 +717,69 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
429
717
|
}
|
|
430
718
|
|
|
431
719
|
/*
|
|
432
|
-
*
|
|
720
|
+
* Configure the hover tint independently of spring movement and image sampling.
|
|
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.
|
|
433
730
|
*/
|
|
434
|
-
function
|
|
731
|
+
function setHoverPointer(x: number, y: number, amount: number) {
|
|
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) {
|
|
435
740
|
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);
|
|
436
779
|
}
|
|
437
780
|
|
|
438
781
|
/*
|
|
439
|
-
* When (on the same clock as `setTime`)
|
|
782
|
+
* When (on the same clock as `setTime`) ambient effects may begin, so the first cycle starts then;
|
|
440
783
|
* null holds the dots still, e.g. until a reveal has finished.
|
|
441
784
|
*/
|
|
442
785
|
function setPulseStart(seconds: number | null) {
|
|
@@ -447,7 +790,14 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
447
790
|
* Whether dots pulse on their own, which needs continuous frames.
|
|
448
791
|
*/
|
|
449
792
|
function isPulsing() {
|
|
450
|
-
return options.pulse !== null && options.pulse.count > 0
|
|
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;
|
|
451
801
|
}
|
|
452
802
|
|
|
453
803
|
/*
|
|
@@ -459,18 +809,61 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
459
809
|
|
|
460
810
|
/*
|
|
461
811
|
* How far the dots have shrunk from full coverage to their size: 0 fills the area, 1 is
|
|
462
|
-
* the resting grid. Pass an already eased value.
|
|
812
|
+
* the resting grid. Pass an already eased value. An optional diameter overrides the size
|
|
813
|
+
* for an appear animation; use amount 1 to keep holes open and avoid clipping to cells.
|
|
463
814
|
*/
|
|
464
|
-
function setReveal(amount: number) {
|
|
815
|
+
function setReveal(amount: number, diameter?: number) {
|
|
465
816
|
program.uniforms.uReveal.value = Math.min(1, Math.max(0, amount));
|
|
817
|
+
program.uniforms.uRevealSize.value = diameter === undefined ? -1 : Math.max(0, diameter);
|
|
466
818
|
}
|
|
467
819
|
|
|
468
820
|
/*
|
|
469
|
-
* Height in CSS pixels of the fade
|
|
470
|
-
*
|
|
821
|
+
* Height in CSS pixels of the fade along the bottom edge; 0 for none. The dots fade to
|
|
822
|
+
* transparent, or to the hex `color` when given (the image through them included), and the
|
|
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`.
|
|
471
825
|
*/
|
|
472
|
-
function setFadeBottom(height: number) {
|
|
826
|
+
function setFadeBottom(height: number, color: string | null = null) {
|
|
473
827
|
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));
|
|
474
867
|
}
|
|
475
868
|
|
|
476
869
|
/*
|
|
@@ -479,10 +872,10 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
479
872
|
* no reveal is running). Returns whether the dots are still moving and need another frame.
|
|
480
873
|
*/
|
|
481
874
|
function step(pointer: [number, number], velocity: [number, number], active: boolean, dt: number, now: number) {
|
|
482
|
-
if (!
|
|
875
|
+
if (!chain.halfFloat) return false;
|
|
483
876
|
// A zero-length frame (the first after a pause) has nothing to integrate; keep asking for frames.
|
|
484
877
|
if (dt <= 0) return simulating;
|
|
485
|
-
const moving = active && (velocity[0] !== 0 || velocity[1] !== 0);
|
|
878
|
+
const moving = active && isInteractive() && (velocity[0] !== 0 || velocity[1] !== 0);
|
|
486
879
|
if (moving) {
|
|
487
880
|
lastKickAt = now;
|
|
488
881
|
simulating = true;
|
|
@@ -535,14 +928,24 @@ export function createDotGrid(gl: OGLRenderingContext, initialOptions?: DotGridO
|
|
|
535
928
|
resize,
|
|
536
929
|
setOptions,
|
|
537
930
|
setOverlay,
|
|
931
|
+
setOverlayRegion,
|
|
932
|
+
setViewport,
|
|
538
933
|
setOverlayZoom,
|
|
539
934
|
setReveal,
|
|
540
935
|
setFadeBottom,
|
|
936
|
+
setBottomTint,
|
|
937
|
+
setImageFade,
|
|
938
|
+
setFadeHandoff,
|
|
541
939
|
setTime,
|
|
940
|
+
setHover,
|
|
941
|
+
setHoverPointer,
|
|
942
|
+
clickAt,
|
|
943
|
+
stepClicks,
|
|
542
944
|
setPulseStart,
|
|
543
945
|
step,
|
|
544
946
|
isInteractive,
|
|
545
947
|
isPulsing,
|
|
948
|
+
isAnimating,
|
|
546
949
|
draw,
|
|
547
950
|
dispose,
|
|
548
951
|
};
|