@arraypress/effects-astro 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +1 -1
- package/COMPONENTS.md +128 -0
- package/LICENSE +2 -2
- package/package.json +1 -1
- package/src/components/CascadeGrid.astro +41 -0
- package/src/components/DitherField.astro +41 -0
- package/src/components/FlowField.astro +38 -0
- package/src/components/KineticGrid.astro +38 -0
- package/src/components/LightLeak.astro +39 -0
- package/src/components/VoronoiMesh.astro +39 -0
- package/src/index.d.ts +13 -0
- package/src/index.js +7 -0
- package/src/runtime/ascii-noise.js +6 -107
- package/src/runtime/cascade-grid.d.ts +112 -0
- package/src/runtime/cascade-grid.js +423 -0
- package/src/runtime/dither-field.d.ts +74 -0
- package/src/runtime/dither-field.js +442 -0
- package/src/runtime/flow-field.d.ts +117 -0
- package/src/runtime/flow-field.js +516 -0
- package/src/runtime/hash-noise.d.ts +14 -0
- package/src/runtime/hash-noise.js +169 -0
- package/src/runtime/kinetic-grid.d.ts +88 -0
- package/src/runtime/kinetic-grid.js +487 -0
- package/src/runtime/light-leak.d.ts +72 -0
- package/src/runtime/light-leak.js +408 -0
- package/src/runtime/voronoi-mesh.d.ts +101 -0
- package/src/runtime/voronoi-mesh.js +610 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/flow-field
|
|
3
|
+
*
|
|
4
|
+
* A Perlin-style flow field with hundreds of short, fine trails riding
|
|
5
|
+
* it: each trail is born somewhere at random, follows the field (its
|
|
6
|
+
* heading is the noise there, turned up to two whole turns, so trails
|
|
7
|
+
* gather into streams and swing round in loops), fades in, lives a few
|
|
8
|
+
* seconds and fades out, its tail thinning as it goes. The field itself
|
|
9
|
+
* evolves slowly, and two slow signals drift the look: one how many
|
|
10
|
+
* trails there are, how fast new ones travel and how fast the field
|
|
11
|
+
* changes; the other how heavy the lines are.
|
|
12
|
+
*
|
|
13
|
+
* The simulation (`createFlow`) is plain JavaScript and steps at a fixed
|
|
14
|
+
* 60 ticks a second whatever the frame rate, with every clock in seconds;
|
|
15
|
+
* it is seeded (`seed`), so two cards differ and the same card is the
|
|
16
|
+
* same on every load. Trail counts follow the element's area (`density`
|
|
17
|
+
* is trails per 100 × 100 CSS px) and the field's features follow its
|
|
18
|
+
* size (`scale` across √(width × height)), so a card and a hero look
|
|
19
|
+
* alike. It starts settled: the trails are already full, so the first
|
|
20
|
+
* frame (and the still frame with prefers-reduced-motion or `speed: 0`)
|
|
21
|
+
* is a field in motion, not an empty one.
|
|
22
|
+
*
|
|
23
|
+
* Canvas 2D, opaque: each frame fills the `background` and draws every
|
|
24
|
+
* trail's tail over it, segment by segment, each fainter than the last,
|
|
25
|
+
* so trails fade into the ground colour exactly (no smear). Segments are
|
|
26
|
+
* grouped by colour and alpha so a frame is a few dozen strokes, not
|
|
27
|
+
* thousands. Colours blend in Oklab, along each trail's life or by its
|
|
28
|
+
* speed. A `quiet` area spawns fewer trails and dims those passing
|
|
29
|
+
* through, behind a centred lockup. Canvas 2D rather than WebGL: fine
|
|
30
|
+
* antialiased hairlines come free, and a wall of cards takes no WebGL
|
|
31
|
+
* contexts from the effects beside it.
|
|
32
|
+
*
|
|
33
|
+
* After `perlin_field_bg`, a Processing sketch shared with the owner
|
|
34
|
+
* (2026-11-20): the heading as noise × 4π, the tail of thirty steps drawn
|
|
35
|
+
* every third, fading from the head, and the two slow drives are its;
|
|
36
|
+
* here the field is sampled smoothly rather than on a 40px grid, trails
|
|
37
|
+
* live and die rather than run on, and everything is by the second.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import { createCanvas2D } from './canvas2d.js';
|
|
41
|
+
import { mixOklab, parseColor } from './color.js';
|
|
42
|
+
import { noiseGrad } from './curl.js';
|
|
43
|
+
import { quietUniform } from './glsl.js';
|
|
44
|
+
import { watchColors } from './mount.js';
|
|
45
|
+
import { pickColors } from './palettes.js';
|
|
46
|
+
import { seededRandom } from './random.js';
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @typedef {object} FlowFieldConfig
|
|
50
|
+
* @property {string[]} [colors] The trails' colours, CSS hex, blended in Oklab along each trail's life (or by its speed). Default white.
|
|
51
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
|
|
52
|
+
* @property {string} [background='#000000'] The ground, CSS hex: filled every frame, so the canvas is opaque.
|
|
53
|
+
* @property {'life' | 'speed' | 'random'} [colorBy='life'] What picks a trail's colour along `colors`: its age, how fast it travels, or one colour each at random.
|
|
54
|
+
* @property {number} [density=12] Trails per 100 × 100 CSS px, on average.
|
|
55
|
+
* @property {number} [length=0.5] How much of its path a trail shows behind it, seconds.
|
|
56
|
+
* @property {number} [weight=0.7] Line width, CSS px.
|
|
57
|
+
* @property {number} [opacity=0.75] A trail head's opacity, 0–1; the tail fades from it.
|
|
58
|
+
* @property {number} [pace=110] How fast trails travel, CSS px a second, on average.
|
|
59
|
+
* @property {number} [scale=2] How many noise features span the element (across √(width × height)); higher is busier.
|
|
60
|
+
* @property {number} [evolve=0.15] How fast the field changes, noise units a second.
|
|
61
|
+
* @property {number} [life=4] A trail's average life, seconds (each lives half to one and a half times it).
|
|
62
|
+
* @property {number} [vary=0.7] How far the slow drives swing density, pace, weight and evolution, 0–1; 0 holds them.
|
|
63
|
+
* @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
|
|
64
|
+
* @property {number} [seed=1] Which field and which trails: the same seed draws the same.
|
|
65
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
66
|
+
* An area with fewer, fainter trails: centre (0–1 from the top left), radius (share of the height), depth (0–1).
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/** The settings' defaults. */
|
|
70
|
+
export const FLOW_DEFAULTS = Object.freeze({
|
|
71
|
+
background: '#000000',
|
|
72
|
+
colorBy: /** @type {'life' | 'speed' | 'random'} */ ('life'),
|
|
73
|
+
density: 12,
|
|
74
|
+
length: 0.5,
|
|
75
|
+
weight: 0.7,
|
|
76
|
+
opacity: 0.75,
|
|
77
|
+
pace: 110,
|
|
78
|
+
scale: 2,
|
|
79
|
+
evolve: 0.15,
|
|
80
|
+
life: 4,
|
|
81
|
+
vary: 0.7,
|
|
82
|
+
speed: 1,
|
|
83
|
+
seed: 1,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/** The default trail colour: white, as the sketch drew. */
|
|
87
|
+
const WHITE = ['#ffffff'];
|
|
88
|
+
|
|
89
|
+
/** Simulation ticks a second: the sketch's frame rate, now fixed. */
|
|
90
|
+
export const TICKS = 60;
|
|
91
|
+
|
|
92
|
+
/** One tick, seconds. */
|
|
93
|
+
const TICK = 1 / TICKS;
|
|
94
|
+
|
|
95
|
+
/** How many segments a tail is drawn in (the sketch: thirty steps, every third). */
|
|
96
|
+
const SEGMENTS = 10;
|
|
97
|
+
|
|
98
|
+
/** Alpha steps segments are grouped by. */
|
|
99
|
+
const ALPHA_LEVELS = 10;
|
|
100
|
+
|
|
101
|
+
/** Colour steps along `colors` segments are grouped by (one colour needs one). */
|
|
102
|
+
const COLOR_LEVELS = 10;
|
|
103
|
+
|
|
104
|
+
/** Seconds a trail takes to fade in, and to fade out. */
|
|
105
|
+
const FADE_IN = 0.4;
|
|
106
|
+
const FADE_OUT = 0.7;
|
|
107
|
+
|
|
108
|
+
/** How fast the two slow drives wander, noise units a second (the sketch's `driveSpeed`). */
|
|
109
|
+
const DRIVE_RATE = 0.12;
|
|
110
|
+
|
|
111
|
+
/** How long a new canvas is run before its first frame, seconds, so it starts settled. */
|
|
112
|
+
const WARM = 2.5;
|
|
113
|
+
|
|
114
|
+
/** Scratch for one noise sample. */
|
|
115
|
+
const sample = new Float64Array(3);
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The field's noise in three dimensions: value noise across, blended
|
|
119
|
+
* smoothly between independent slices in time, its spread kept even
|
|
120
|
+
* between slices (a plain blend of two would go flat halfway).
|
|
121
|
+
*
|
|
122
|
+
* @param {number} x Across, noise units.
|
|
123
|
+
* @param {number} y Down, noise units.
|
|
124
|
+
* @param {number} z Time, noise units.
|
|
125
|
+
* @returns {number} Around 0–1, centred on 0.5.
|
|
126
|
+
*/
|
|
127
|
+
export function flowNoise(x, y, z) {
|
|
128
|
+
const iz = Math.floor(z);
|
|
129
|
+
const f = z - iz;
|
|
130
|
+
const u = f * f * (3 - 2 * f);
|
|
131
|
+
const a = noiseGrad(x + iz * 31.7, y + iz * 17.3, sample)[0];
|
|
132
|
+
const b = noiseGrad(x + (iz + 1) * 31.7, y + (iz + 1) * 17.3, sample)[0];
|
|
133
|
+
const spread = Math.sqrt((1 - u) * (1 - u) + u * u);
|
|
134
|
+
return 0.5 + (a + (b - a) * u - 0.5) / spread;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The field's heading at a point, as the sketch turned it: the noise
|
|
139
|
+
* times two whole turns.
|
|
140
|
+
*
|
|
141
|
+
* @param {number} x Across, noise units.
|
|
142
|
+
* @param {number} y Down, noise units.
|
|
143
|
+
* @param {number} z Time, noise units.
|
|
144
|
+
* @returns {number} Radians.
|
|
145
|
+
*/
|
|
146
|
+
export function flowAngle(x, y, z) {
|
|
147
|
+
return flowNoise(x, y, z) * Math.PI * 4;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* One of the slow drives: a signal that wanders 0–1 over tens of
|
|
152
|
+
* seconds, stretched (as the sketch stretched its noise) so it reaches
|
|
153
|
+
* both ends.
|
|
154
|
+
*
|
|
155
|
+
* @param {number} t Seconds.
|
|
156
|
+
* @param {number} channel Which drive (each is independent).
|
|
157
|
+
* @returns {number} 0–1.
|
|
158
|
+
*/
|
|
159
|
+
export function driveAt(t, channel) {
|
|
160
|
+
const v = noiseGrad(t * DRIVE_RATE + channel * 57.31, channel * 13.7 + 0.5, sample)[0];
|
|
161
|
+
return Math.min(1, Math.max(0, (v - 0.2) / 0.6));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* How much a quiet area lets through at a point: the GLSL quiet's
|
|
166
|
+
* falloff (`quietOpen` in glsl.js), in CSS px.
|
|
167
|
+
*
|
|
168
|
+
* @param {number} x Across, CSS px from the left.
|
|
169
|
+
* @param {number} y Down, CSS px from the top.
|
|
170
|
+
* @param {number} width The element's width, CSS px.
|
|
171
|
+
* @param {number} height Its height, CSS px.
|
|
172
|
+
* @param {FlowFieldConfig['quiet']} quiet The area, or none.
|
|
173
|
+
* @returns {number} 1 − depth at the area's centre, rising to 1 at its edge and beyond; 1 without one.
|
|
174
|
+
*/
|
|
175
|
+
export function quietFactor(x, y, width, height, quiet) {
|
|
176
|
+
const [qx, qy, radius, depth] = quietUniform(quiet, width / height);
|
|
177
|
+
if (radius <= 0) return 1;
|
|
178
|
+
const d = Math.hypot((x - width / 2) / height - qx, (height / 2 - y) / height - qy);
|
|
179
|
+
const t = Math.min(1, Math.max(0, (d - radius * 0.06) / (radius * 0.94)));
|
|
180
|
+
return 1 - depth + depth * t * t * (3 - 2 * t);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* How many trails an element holds on average.
|
|
185
|
+
*
|
|
186
|
+
* @param {number} width CSS px.
|
|
187
|
+
* @param {number} height CSS px.
|
|
188
|
+
* @param {number} [density=12] Trails per 100 × 100 CSS px.
|
|
189
|
+
* @returns {number} A whole number, at least 1 for any area.
|
|
190
|
+
*/
|
|
191
|
+
export function trailCount(width, height, density = FLOW_DEFAULTS.density) {
|
|
192
|
+
return Math.max(width > 0 && height > 0 && density > 0 ? 1 : 0, Math.round(((width * height) / 10000) * Math.max(0, density)));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The colours as strokes take them: steps along `colors`, blended in
|
|
197
|
+
* Oklab from the first to the last.
|
|
198
|
+
*
|
|
199
|
+
* @param {string[]} colors CSS hex colours, at least one.
|
|
200
|
+
* @param {number} [steps=10] How many (one colour gives one).
|
|
201
|
+
* @returns {string[]} `rgb(…)` strings.
|
|
202
|
+
* @throws {TypeError} When a colour is not hex.
|
|
203
|
+
*/
|
|
204
|
+
export function flowRamp(colors, steps = COLOR_LEVELS) {
|
|
205
|
+
const cs = colors.map(parseColor);
|
|
206
|
+
const css = (/** @type {number[]} */ c) => `rgb(${c.map((v) => Math.round(Math.min(1, Math.max(0, v)) * 255)).join(' ')})`;
|
|
207
|
+
if (cs.length < 2) return [css(cs[0] ?? [1, 1, 1])];
|
|
208
|
+
return Array.from({ length: steps }, (_, s) => {
|
|
209
|
+
const k = (s / (steps - 1)) * (cs.length - 1);
|
|
210
|
+
const i = Math.min(cs.length - 2, Math.floor(k));
|
|
211
|
+
return css(mixOklab(cs[i], cs[i + 1], k - i));
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A trail's opacity from its age: fading in at birth, out at the end.
|
|
217
|
+
*
|
|
218
|
+
* @param {number} age Seconds since it was born.
|
|
219
|
+
* @param {number} life Seconds it lives.
|
|
220
|
+
* @returns {number} 0–1.
|
|
221
|
+
*/
|
|
222
|
+
export function lifeAlpha(age, life) {
|
|
223
|
+
return Math.max(0, Math.min(1, age / FADE_IN, (life - age) / FADE_OUT));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @typedef {object} Trail
|
|
228
|
+
* @property {boolean} alive Whether it is drawn and moving.
|
|
229
|
+
* @property {number} x Its head, CSS px.
|
|
230
|
+
* @property {number} y Its head, CSS px.
|
|
231
|
+
* @property {Float32Array} path Its last positions, a ring of x, y pairs.
|
|
232
|
+
* @property {number} head The ring's newest pair.
|
|
233
|
+
* @property {number} age Seconds since birth.
|
|
234
|
+
* @property {number} life Seconds it lives.
|
|
235
|
+
* @property {number} pace Its speed, CSS px a second, set at birth.
|
|
236
|
+
* @property {number} tone Where it sits along the colours for `colorBy: 'speed'` or `'random'`, 0–1.
|
|
237
|
+
*/
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* @typedef {object} Flow
|
|
241
|
+
* @property {Trail[]} trails The pool, every trail it may hold.
|
|
242
|
+
* @property {number} width CSS px.
|
|
243
|
+
* @property {number} height CSS px.
|
|
244
|
+
* @property {number} steps Positions a trail keeps.
|
|
245
|
+
* @property {number} time Seconds simulated.
|
|
246
|
+
* @property {number} target How many trails it wants now.
|
|
247
|
+
* @property {number} weight The line width now, CSS px.
|
|
248
|
+
* @property {(seconds: number) => void} advance Steps the simulation on by some seconds (at fixed ticks; a remainder carries over).
|
|
249
|
+
* @property {(buckets: number[][]) => number} segments Fills `buckets[colour × ALPHA_LEVELS + alpha − 1]` with each segment's points; returns how many segments.
|
|
250
|
+
*/
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The simulation, without a canvas: a pool of trails following the
|
|
254
|
+
* field over an area, stepped at fixed ticks and run on until settled.
|
|
255
|
+
*
|
|
256
|
+
* @param {FlowFieldConfig} config The settings.
|
|
257
|
+
* @param {number} width The area's width, CSS px.
|
|
258
|
+
* @param {number} height Its height, CSS px.
|
|
259
|
+
* @param {{ warm?: number }} [options] `warm`: seconds to run before the first frame (default 2.5).
|
|
260
|
+
* @returns {Flow} The simulation.
|
|
261
|
+
*/
|
|
262
|
+
export function createFlow(config, width, height, options = {}) {
|
|
263
|
+
const o = { ...FLOW_DEFAULTS, ...config };
|
|
264
|
+
const rnd = seededRandom(o.seed);
|
|
265
|
+
const vary = Math.min(1, Math.max(0, o.vary));
|
|
266
|
+
const steps = Math.max(2, Math.round(o.length * TICKS) + 1);
|
|
267
|
+
const per = (steps - 1) / Math.min(SEGMENTS, steps - 1);
|
|
268
|
+
const unit = o.scale / Math.sqrt(Math.max(1, width * height));
|
|
269
|
+
const ox = rnd() * 4096;
|
|
270
|
+
const oy = rnd() * 4096;
|
|
271
|
+
const drift = rnd() * 512;
|
|
272
|
+
const base = trailCount(width, height, o.density);
|
|
273
|
+
const margin = 0.06 * Math.sqrt(width * height);
|
|
274
|
+
const paceLo = o.pace * (1 - 0.8 * vary);
|
|
275
|
+
const paceHi = o.pace * (1 + 0.8 * vary);
|
|
276
|
+
let z = rnd() * 256;
|
|
277
|
+
let carry = 0;
|
|
278
|
+
|
|
279
|
+
/** @type {Trail[]} */
|
|
280
|
+
const trails = Array.from({ length: Math.ceil(base * (1 + 0.7 * vary)) }, () => ({
|
|
281
|
+
alive: false,
|
|
282
|
+
x: 0,
|
|
283
|
+
y: 0,
|
|
284
|
+
path: new Float32Array(steps * 2),
|
|
285
|
+
head: 0,
|
|
286
|
+
age: 0,
|
|
287
|
+
life: 1,
|
|
288
|
+
pace: o.pace,
|
|
289
|
+
tone: 0,
|
|
290
|
+
}));
|
|
291
|
+
|
|
292
|
+
/** @type {Flow} */
|
|
293
|
+
const flow = { trails, width, height, steps, time: 0, target: base, weight: o.weight, advance, segments };
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* The drives now, as factors around 1.
|
|
297
|
+
*
|
|
298
|
+
* @returns {{ density: number, pace: number, evolve: number, weight: number }} The factors.
|
|
299
|
+
*/
|
|
300
|
+
function drives() {
|
|
301
|
+
const wide = driveAt(flow.time + drift, 0) - 0.5;
|
|
302
|
+
const low = driveAt(flow.time + drift, 1) - 0.5;
|
|
303
|
+
return {
|
|
304
|
+
density: 1 + vary * wide * 1.4,
|
|
305
|
+
pace: 1 + vary * wide * 1.6,
|
|
306
|
+
evolve: 1 + vary * wide * 1.6,
|
|
307
|
+
weight: 1 + vary * low * 1.2,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Gives a trail a new life at a random place, fewer in a quiet area.
|
|
313
|
+
*
|
|
314
|
+
* @param {Trail} p The trail.
|
|
315
|
+
* @param {number} pace Its speed, CSS px a second.
|
|
316
|
+
*/
|
|
317
|
+
function spawn(p, pace) {
|
|
318
|
+
let x = 0;
|
|
319
|
+
let y = 0;
|
|
320
|
+
for (let tries = 0; tries < 6; tries++) {
|
|
321
|
+
x = -margin + rnd() * (width + 2 * margin);
|
|
322
|
+
y = -margin + rnd() * (height + 2 * margin);
|
|
323
|
+
if (rnd() < quietFactor(x, y, width, height, o.quiet)) break;
|
|
324
|
+
}
|
|
325
|
+
p.alive = true;
|
|
326
|
+
p.x = x;
|
|
327
|
+
p.y = y;
|
|
328
|
+
p.path.fill(0);
|
|
329
|
+
for (let i = 0; i < steps; i++) {
|
|
330
|
+
p.path[i * 2] = x;
|
|
331
|
+
p.path[i * 2 + 1] = y;
|
|
332
|
+
}
|
|
333
|
+
p.head = 0;
|
|
334
|
+
p.age = 0;
|
|
335
|
+
p.life = o.life * (0.5 + rnd());
|
|
336
|
+
p.pace = pace * (0.85 + rnd() * 0.3);
|
|
337
|
+
p.tone = o.colorBy === 'speed' ? Math.min(1, Math.max(0, (p.pace - paceLo) / Math.max(1e-6, paceHi - paceLo))) : rnd();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** One tick: the drives move, trails are born and retired toward the count wanted, each trail steps along the field. */
|
|
341
|
+
function tick() {
|
|
342
|
+
flow.time += TICK;
|
|
343
|
+
const d = drives();
|
|
344
|
+
z += o.evolve * d.evolve * TICK;
|
|
345
|
+
flow.target = Math.min(trails.length, Math.round(base * d.density));
|
|
346
|
+
flow.weight = o.weight * d.weight;
|
|
347
|
+
// Births refill the count wanted over about half a second, not all at once.
|
|
348
|
+
let births = Math.max(1, Math.ceil(trails.length * TICK * 2));
|
|
349
|
+
for (let i = 0; i < trails.length; i++) {
|
|
350
|
+
const p = trails[i];
|
|
351
|
+
if (!p.alive) {
|
|
352
|
+
if (i < flow.target && births > 0) {
|
|
353
|
+
spawn(p, o.pace * d.pace);
|
|
354
|
+
births--;
|
|
355
|
+
}
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
p.age += TICK;
|
|
359
|
+
if (p.age >= p.life) {
|
|
360
|
+
p.alive = false;
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
// Past the count wanted, or off the edge: fade out early (the sketch's death fade).
|
|
364
|
+
const out = p.x < -margin || p.y < -margin || p.x > width + margin || p.y > height + margin;
|
|
365
|
+
if ((i >= flow.target || out) && p.life - p.age > FADE_OUT) p.life = p.age + FADE_OUT;
|
|
366
|
+
const a = flowAngle(p.x * unit + ox, p.y * unit + oy, z);
|
|
367
|
+
p.x += Math.cos(a) * p.pace * TICK;
|
|
368
|
+
p.y += Math.sin(a) * p.pace * TICK;
|
|
369
|
+
p.head = (p.head + 1) % steps;
|
|
370
|
+
p.path[p.head * 2] = p.x;
|
|
371
|
+
p.path[p.head * 2 + 1] = p.y;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Steps the simulation on.
|
|
377
|
+
*
|
|
378
|
+
* @param {number} seconds How long, seconds (a remainder under a tick carries over).
|
|
379
|
+
*/
|
|
380
|
+
function advance(seconds) {
|
|
381
|
+
carry += Math.max(0, seconds);
|
|
382
|
+
while (carry >= TICK) {
|
|
383
|
+
carry -= TICK;
|
|
384
|
+
tick();
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Lays every visible segment into buckets by colour and alpha, for a
|
|
390
|
+
* stroke each: a tail is `SEGMENTS` pieces from the head back, each
|
|
391
|
+
* fainter (the sketch's fade), times the trail's life fade and the
|
|
392
|
+
* quiet area at the piece's middle.
|
|
393
|
+
*
|
|
394
|
+
* @param {number[][]} buckets One list of points a bucket (`COLOR_LEVELS × ALPHA_LEVELS`), emptied first; each segment is its point count, then x, y pairs.
|
|
395
|
+
* @returns {number} How many segments were laid.
|
|
396
|
+
*/
|
|
397
|
+
function segments(buckets) {
|
|
398
|
+
for (const b of buckets) b.length = 0;
|
|
399
|
+
const tones = buckets.length / ALPHA_LEVELS;
|
|
400
|
+
const pieces = Math.round((steps - 1) / per);
|
|
401
|
+
let laid = 0;
|
|
402
|
+
for (const p of trails) {
|
|
403
|
+
if (!p.alive) continue;
|
|
404
|
+
const fade = lifeAlpha(p.age, p.life);
|
|
405
|
+
if (fade <= 0) continue;
|
|
406
|
+
const tone = o.colorBy === 'life' ? p.age / p.life : p.tone;
|
|
407
|
+
const ci = tones > 1 ? Math.min(tones - 1, Math.floor(tone * tones)) : 0;
|
|
408
|
+
for (let k = 0; k < pieces; k++) {
|
|
409
|
+
const from = Math.round(k * per);
|
|
410
|
+
const to = Math.round((k + 1) * per);
|
|
411
|
+
const mid = (p.head - ((from + to) >> 1) + steps) % steps;
|
|
412
|
+
const q = o.quiet ? quietFactor(p.path[mid * 2], p.path[mid * 2 + 1], width, height, o.quiet) : 1;
|
|
413
|
+
const level = Math.round((1 - from / (steps - 1)) * fade * q * ALPHA_LEVELS);
|
|
414
|
+
if (level <= 0) continue;
|
|
415
|
+
const out = buckets[ci * ALPHA_LEVELS + Math.min(ALPHA_LEVELS, level) - 1];
|
|
416
|
+
out.push(to - from + 1);
|
|
417
|
+
for (let s = from; s <= to; s++) {
|
|
418
|
+
const at = (p.head - s + steps) % steps;
|
|
419
|
+
out.push(p.path[at * 2], p.path[at * 2 + 1]);
|
|
420
|
+
}
|
|
421
|
+
laid++;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return laid;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Born at random ages and run on, so the first frame is already a field in motion.
|
|
428
|
+
const start = drives();
|
|
429
|
+
for (let i = 0; i < Math.min(trails.length, base); i++) {
|
|
430
|
+
spawn(trails[i], o.pace * start.pace);
|
|
431
|
+
trails[i].age = rnd() * trails[i].life * 0.8;
|
|
432
|
+
}
|
|
433
|
+
advance(options.warm ?? WARM);
|
|
434
|
+
return flow;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Starts the flow field on an element (its first canvas is drawn into,
|
|
439
|
+
* or a 2D one in its place when the host made it for WebGL).
|
|
440
|
+
*
|
|
441
|
+
* @param {HTMLElement} el The element.
|
|
442
|
+
* @param {FlowFieldConfig} config The settings.
|
|
443
|
+
* @returns {() => void} Stops it.
|
|
444
|
+
*/
|
|
445
|
+
export function flowField(el, config) {
|
|
446
|
+
const o = { ...FLOW_DEFAULTS, ...config };
|
|
447
|
+
const speed = Math.max(0, o.speed);
|
|
448
|
+
const opacity = Math.min(1, Math.max(0, o.opacity));
|
|
449
|
+
let ramp = flowRamp(pickColors(config, WHITE));
|
|
450
|
+
/** @type {number[][]} */
|
|
451
|
+
let buckets = [];
|
|
452
|
+
/** @type {Flow | null} */
|
|
453
|
+
let flow = null;
|
|
454
|
+
let fresh = true;
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* A new simulation for the new size (the same seed, so the same field).
|
|
458
|
+
*
|
|
459
|
+
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
460
|
+
*/
|
|
461
|
+
const resize = (c) => {
|
|
462
|
+
flow = createFlow(config, c.width, c.height);
|
|
463
|
+
buckets = Array.from({ length: ramp.length * ALPHA_LEVELS }, () => []);
|
|
464
|
+
fresh = true;
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* One frame: the simulation stepped on, the ground filled, the tails
|
|
469
|
+
* stroked a bucket at a time. Held after one frame when `speed` is 0.
|
|
470
|
+
*
|
|
471
|
+
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
472
|
+
* @param {number} _now The time, ms.
|
|
473
|
+
* @param {number} dt Seconds since the last frame.
|
|
474
|
+
*/
|
|
475
|
+
const draw = (c, _now, dt) => {
|
|
476
|
+
if (!flow || (!fresh && (c.still || speed === 0))) return;
|
|
477
|
+
fresh = false;
|
|
478
|
+
if (!c.still) flow.advance(dt * speed);
|
|
479
|
+
const { ctx } = c;
|
|
480
|
+
ctx.globalAlpha = 1;
|
|
481
|
+
ctx.fillStyle = o.background;
|
|
482
|
+
ctx.fillRect(0, 0, c.width, c.height);
|
|
483
|
+
flow.segments(buckets);
|
|
484
|
+
ctx.lineWidth = flow.weight;
|
|
485
|
+
ctx.lineCap = 'butt';
|
|
486
|
+
ctx.lineJoin = 'round';
|
|
487
|
+
for (let b = 0; b < buckets.length; b++) {
|
|
488
|
+
const pts = buckets[b];
|
|
489
|
+
if (!pts.length) continue;
|
|
490
|
+
ctx.strokeStyle = ramp[Math.floor(b / ALPHA_LEVELS)];
|
|
491
|
+
ctx.globalAlpha = (((b % ALPHA_LEVELS) + 1) / ALPHA_LEVELS) * opacity;
|
|
492
|
+
ctx.beginPath();
|
|
493
|
+
for (let i = 0; i < pts.length; ) {
|
|
494
|
+
const n = pts[i++];
|
|
495
|
+
ctx.moveTo(pts[i], pts[i + 1]);
|
|
496
|
+
for (let s = 1; s < n; s++) ctx.lineTo(pts[i + s * 2], pts[i + s * 2 + 1]);
|
|
497
|
+
i += n * 2;
|
|
498
|
+
}
|
|
499
|
+
ctx.stroke();
|
|
500
|
+
}
|
|
501
|
+
ctx.globalAlpha = 1;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const { c, stop } = createCanvas2D(el, { draw, resize });
|
|
505
|
+
if (!c) return stop;
|
|
506
|
+
const unwatch = watchColors(el, (next) => {
|
|
507
|
+
ramp = flowRamp(next.colors);
|
|
508
|
+
buckets = Array.from({ length: ramp.length * ALPHA_LEVELS }, () => []);
|
|
509
|
+
fresh = true;
|
|
510
|
+
draw(c, performance.now(), 0);
|
|
511
|
+
});
|
|
512
|
+
return () => {
|
|
513
|
+
stop();
|
|
514
|
+
unwatch();
|
|
515
|
+
};
|
|
516
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** GLSL: `vec3 hash33(vec3 p3)`, three values 0–1 from three inputs (Hash without Sine). */
|
|
2
|
+
export declare const GLSL_HASH33: string;
|
|
3
|
+
/** Hash without Sine, three in and three out (the shader's `hash33`); each value 0–1. */
|
|
4
|
+
export declare function hash33(x: number, y: number, z: number): [number, number, number];
|
|
5
|
+
/** GLSL: `vec2 grad(vec2 p)` and `float gnoise(vec2 p)`, gradient noise with a quintic fade, 0–1. */
|
|
6
|
+
export declare const GLSL_GRADIENT_NOISE: string;
|
|
7
|
+
/** A lattice point's gradient (the shader's `grad`), each part −1 to 1. */
|
|
8
|
+
export declare function grad(x: number, y: number): [number, number];
|
|
9
|
+
/** Gradient noise with a quintic fade (the shader's `gnoise`), 0–1. */
|
|
10
|
+
export declare function gnoise(x: number, y: number): number;
|
|
11
|
+
/** GLSL: `float noiseField(vec2 p, float evo, float wt)`, the warped five-octave field, 0–1 (needs GLSL_GRADIENT_NOISE). */
|
|
12
|
+
export declare const GLSL_NOISE_FIELD: string;
|
|
13
|
+
/** The warped field (the shader's `noiseField`): five octaves of gradient noise, each warped by the sum so far; 0–1. */
|
|
14
|
+
export declare function noiseField(x: number, y: number, evo: number, wt: number): number;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/hash-noise
|
|
3
|
+
*
|
|
4
|
+
* Hashing and noise that the GPU and JavaScript compute alike, so an
|
|
5
|
+
* effect's shader and its JS twin (a text still, a test, a frame worked
|
|
6
|
+
* out on the CPU) agree: Dave Hoskins' Hash without Sine, which brings its
|
|
7
|
+
* input under 1 before it multiplies (32-bit and 64-bit floats then stay in
|
|
8
|
+
* step), gradient noise with a quintic fade on it, and the warped
|
|
9
|
+
* five-octave field the friend's sketches share (ASCII noise, the dither
|
|
10
|
+
* field). Each comes as GLSL (ES 1.00) and as a JS function of the same
|
|
11
|
+
* name.
|
|
12
|
+
*
|
|
13
|
+
* Hash without Sine is MIT, © 2014 David Hoskins
|
|
14
|
+
* (https://www.shadertoy.com/view/4djSRW).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The fractional part.
|
|
19
|
+
*
|
|
20
|
+
* @param {number} n A number.
|
|
21
|
+
* @returns {number} `n − floor(n)`, 0–1.
|
|
22
|
+
*/
|
|
23
|
+
const fract = (n) => n - Math.floor(n);
|
|
24
|
+
|
|
25
|
+
/** GLSL: `vec3 hash33(vec3 p3)`, three values 0–1 from three inputs. */
|
|
26
|
+
export const GLSL_HASH33 = `
|
|
27
|
+
vec3 hash33(vec3 p3) {
|
|
28
|
+
p3 = fract(p3 * vec3(0.1031, 0.1030, 0.0973));
|
|
29
|
+
p3 += dot(p3, p3.yxz + 33.33);
|
|
30
|
+
return fract((p3.xxy + p3.yxx) * p3.zyx);
|
|
31
|
+
}
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Hash without Sine, three in and three out (the shader's `hash33`).
|
|
36
|
+
*
|
|
37
|
+
* @param {number} x The first input.
|
|
38
|
+
* @param {number} y The second.
|
|
39
|
+
* @param {number} z The third.
|
|
40
|
+
* @returns {[number, number, number]} Three values, each 0–1.
|
|
41
|
+
*/
|
|
42
|
+
export function hash33(x, y, z) {
|
|
43
|
+
let a = fract(x * 0.1031);
|
|
44
|
+
let b = fract(y * 0.103);
|
|
45
|
+
let c = fract(z * 0.0973);
|
|
46
|
+
const d = a * (b + 33.33) + b * (a + 33.33) + c * (c + 33.33);
|
|
47
|
+
a += d;
|
|
48
|
+
b += d;
|
|
49
|
+
c += d;
|
|
50
|
+
return [fract((a + b) * c), fract((a + a) * b), fract((b + a) * a)];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* GLSL: `vec2 grad(vec2 p)`, a lattice point's gradient (Hash without
|
|
55
|
+
* Sine, −1 to 1), and `float gnoise(vec2 p)`, gradient noise with a
|
|
56
|
+
* quintic fade, 0–1.
|
|
57
|
+
*/
|
|
58
|
+
export const GLSL_GRADIENT_NOISE = `
|
|
59
|
+
vec2 grad(vec2 p) {
|
|
60
|
+
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
|
|
61
|
+
p3 += dot(p3, p3.yzx + 33.33);
|
|
62
|
+
return -1.0 + 2.0 * fract((p3.xx + p3.yz) * p3.zy);
|
|
63
|
+
}
|
|
64
|
+
float gnoise(vec2 p) {
|
|
65
|
+
vec2 i = floor(p);
|
|
66
|
+
vec2 f = p - i;
|
|
67
|
+
vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
|
|
68
|
+
float a = dot(grad(i), f);
|
|
69
|
+
float b = dot(grad(i + vec2(1.0, 0.0)), f - vec2(1.0, 0.0));
|
|
70
|
+
float c = dot(grad(i + vec2(0.0, 1.0)), f - vec2(0.0, 1.0));
|
|
71
|
+
float d = dot(grad(i + vec2(1.0, 1.0)), f - vec2(1.0, 1.0));
|
|
72
|
+
return 0.5 + 0.5 * mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A lattice point's gradient (the shader's `grad`).
|
|
78
|
+
*
|
|
79
|
+
* @param {number} x The point's x.
|
|
80
|
+
* @param {number} y The point's y.
|
|
81
|
+
* @returns {[number, number]} A gradient, each part −1 to 1.
|
|
82
|
+
*/
|
|
83
|
+
export function grad(x, y) {
|
|
84
|
+
let a = fract(x * 0.1031);
|
|
85
|
+
let b = fract(y * 0.103);
|
|
86
|
+
let c = fract(x * 0.0973);
|
|
87
|
+
const d = a * (b + 33.33) + b * (c + 33.33) + c * (a + 33.33);
|
|
88
|
+
a += d;
|
|
89
|
+
b += d;
|
|
90
|
+
c += d;
|
|
91
|
+
return [-1 + 2 * fract((a + b) * c), -1 + 2 * fract((a + c) * b)];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Gradient noise with a quintic fade (the shader's `gnoise`).
|
|
96
|
+
*
|
|
97
|
+
* @param {number} x Where, x.
|
|
98
|
+
* @param {number} y Where, y.
|
|
99
|
+
* @returns {number} 0–1.
|
|
100
|
+
*/
|
|
101
|
+
export function gnoise(x, y) {
|
|
102
|
+
const ix = Math.floor(x);
|
|
103
|
+
const iy = Math.floor(y);
|
|
104
|
+
const fx = x - ix;
|
|
105
|
+
const fy = y - iy;
|
|
106
|
+
const ux = fx * fx * fx * (fx * (fx * 6 - 15) + 10);
|
|
107
|
+
const uy = fy * fy * fy * (fy * (fy * 6 - 15) + 10);
|
|
108
|
+
/**
|
|
109
|
+
* A corner's gradient dotted with the offset to it.
|
|
110
|
+
*
|
|
111
|
+
* @param {[number, number]} g The gradient.
|
|
112
|
+
* @param {number} dx Offset x.
|
|
113
|
+
* @param {number} dy Offset y.
|
|
114
|
+
* @returns {number} The dot product.
|
|
115
|
+
*/
|
|
116
|
+
const dot = (g, dx, dy) => g[0] * dx + g[1] * dy;
|
|
117
|
+
const a = dot(grad(ix, iy), fx, fy);
|
|
118
|
+
const b = dot(grad(ix + 1, iy), fx - 1, fy);
|
|
119
|
+
const c = dot(grad(ix, iy + 1), fx, fy - 1);
|
|
120
|
+
const d = dot(grad(ix + 1, iy + 1), fx - 1, fy - 1);
|
|
121
|
+
const ab = a + (b - a) * ux;
|
|
122
|
+
const cd = c + (d - c) * ux;
|
|
123
|
+
return 0.5 + 0.5 * (ab + (cd - ab) * uy);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* GLSL: `float noiseField(vec2 p, float evo, float wt)`, five octaves of
|
|
128
|
+
* gradient noise each warped by the sum so far, 0–1. Needs
|
|
129
|
+
* GLSL_GRADIENT_NOISE before it.
|
|
130
|
+
*/
|
|
131
|
+
export const GLSL_NOISE_FIELD = `
|
|
132
|
+
float noiseField(vec2 p, float evo, float wt) {
|
|
133
|
+
float warp = gnoise(p + wt * 0.04);
|
|
134
|
+
float v = 0.0;
|
|
135
|
+
float amp = 0.5;
|
|
136
|
+
float freq = 1.0;
|
|
137
|
+
for (int i = 0; i < 5; i++) {
|
|
138
|
+
v += amp * gnoise(p * freq + vec2(warp * 0.3, warp * 0.25) + evo * 0.06);
|
|
139
|
+
amp *= 0.5;
|
|
140
|
+
freq *= 2.07;
|
|
141
|
+
warp = v;
|
|
142
|
+
}
|
|
143
|
+
return v;
|
|
144
|
+
}
|
|
145
|
+
`;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The warped field (the shader's `noiseField`): five octaves of gradient
|
|
149
|
+
* noise, each warped by the sum so far.
|
|
150
|
+
*
|
|
151
|
+
* @param {number} x Where, x.
|
|
152
|
+
* @param {number} y Where, y.
|
|
153
|
+
* @param {number} evo The morphing clock.
|
|
154
|
+
* @param {number} wt The warp's clock (seconds).
|
|
155
|
+
* @returns {number} 0–1, near 0.5 on average.
|
|
156
|
+
*/
|
|
157
|
+
export function noiseField(x, y, evo, wt) {
|
|
158
|
+
let warp = gnoise(x + wt * 0.04, y + wt * 0.04);
|
|
159
|
+
let v = 0;
|
|
160
|
+
let amp = 0.5;
|
|
161
|
+
let freq = 1;
|
|
162
|
+
for (let i = 0; i < 5; i++) {
|
|
163
|
+
v += amp * gnoise(x * freq + warp * 0.3 + evo * 0.06, y * freq + warp * 0.25 + evo * 0.06);
|
|
164
|
+
amp *= 0.5;
|
|
165
|
+
freq *= 2.07;
|
|
166
|
+
warp = v;
|
|
167
|
+
}
|
|
168
|
+
return v;
|
|
169
|
+
}
|