@arraypress/effects-astro 0.1.1 → 0.2.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/COMPONENTS.md CHANGED
@@ -414,6 +414,7 @@ Ink: ink in water over whatever is behind it. The pointer stirs it and trails in
414
414
  | `radius` | `number` | The size of a stroke's splats and of a drop, 1 as standard. |
415
415
  | `momentum` | `number` | How fast the motion dies away, per second (lower flows on longer). Default 0.25. |
416
416
  | `opacity` | `number` | The ink's strongest cover, 0–1. Default 1. |
417
+ | `idle` | `number` | Seconds between drops of ink on their own, so it moves before anyone touches it; 0 for none. They wait while it's off screen or the tab is hidden. Default 0. |
417
418
  | `ignore` | `string` | Clicks on these are theirs, not the ink's. Default: links, buttons, form fields and `[data-effect-ignore]`. |
418
419
  | `fixed` | `boolean` | Fill the viewport (position: fixed). Default false. |
419
420
  | `class` | `string` | Classes for the element. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arraypress/effects-astro",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "GPU effects for Astro sites, as components: liquid light with a real fluid under it, ink in water, and outline text a spotlight fills. Plain WebGL, no dependencies, with the fallbacks a theme needs (reduced motion, no WebGL, touch screens, off-screen pausing).",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -50,6 +50,8 @@ export interface StirOptions {
50
50
  */
51
51
  export function stir(fluid: Fluid, el: HTMLElement, options: StirOptions): {
52
52
  update(dt: number): void;
53
+ /** Drops ink at a point in the fluid's 0–1 space, as a click would. */
54
+ bloom(x: number, y: number): void;
53
55
  setColors(colors: Rgb[]): void;
54
56
  destroy(): void;
55
57
  };
@@ -423,9 +423,11 @@ const INK_PER_STEP = 0.3;
423
423
  * @param {Fluid} fluid The fluid.
424
424
  * @param {HTMLElement} el The element it covers.
425
425
  * @param {StirOptions} options How it is stirred.
426
- * @returns {{ update: (dt: number) => void, setColors: (colors: Rgb[]) => void, destroy: () => void }}
426
+ * @returns {{ update: (dt: number) => void, bloom: (x: number, y: number) => void, setColors: (colors: Rgb[]) => void, destroy: () => void }}
427
427
  * `update(dt)` lays the strokes since the last frame (call it before
428
- * `fluid.step`); `setColors` changes the ink's palette; `destroy` stops listening.
428
+ * `fluid.step`); `bloom(x, y)` drops ink at a point in the fluid's 0–1
429
+ * space, as a click would; `setColors` changes the ink's palette;
430
+ * `destroy` stops listening.
429
431
  */
430
432
  export function stir(fluid, el, options) {
431
433
  const {
@@ -455,12 +457,10 @@ export function stir(fluid, el, options) {
455
457
  * A drop of ink: a bloom of colour, and a ring of pushes outward with a
456
458
  * turn to them, so it bursts open and curls.
457
459
  *
458
- * @param {PointerEvent} e The press.
460
+ * @param {number} x Across, 0–1 in the fluid's space.
461
+ * @param {number} y Up, 0–1 in the fluid's space.
459
462
  */
460
- const drop = (e) => {
461
- const at = pressAt(el, e, ignore);
462
- if (!at) return;
463
- const { x, y } = at;
463
+ const bloom = (x, y) => {
464
464
  const box = el.getBoundingClientRect();
465
465
  const aspect = box.width / box.height;
466
466
  fluid.splat(x, y, 0, 0, ink(1.1), 0.01 * radius);
@@ -474,6 +474,15 @@ export function stir(fluid, el, options) {
474
474
  fluid.splat(x + (cx * 0.03 * radius) / aspect, y + cy * 0.03 * radius, vx / aspect, vy, [0, 0, 0], 0.003 * radius);
475
475
  }
476
476
  };
477
+ /**
478
+ * A click or tap: a bloom where it landed, unless it was on something of its own.
479
+ *
480
+ * @param {PointerEvent} e The press.
481
+ */
482
+ const drop = (e) => {
483
+ const at = pressAt(el, e, ignore);
484
+ if (at) bloom(at.x, at.y);
485
+ };
477
486
  if (drops) document.addEventListener('pointerdown', drop);
478
487
 
479
488
  return {
@@ -494,6 +503,7 @@ export function stir(fluid, el, options) {
494
503
  fluid.splat(p.x, p.y, p.dx * push, p.dy * push, dye, 0.0025 * radius);
495
504
  }
496
505
  },
506
+ bloom,
497
507
  setColors(next) {
498
508
  colors = next;
499
509
  },
@@ -23,6 +23,8 @@ export interface InkConfig {
23
23
  momentum?: number;
24
24
  /** The ink's strongest cover, 0–1. Default 1. */
25
25
  opacity?: number;
26
+ /** Seconds between drops of ink on their own, so it moves before anyone touches it; 0 for none. They wait while it's off screen or the tab is hidden. Default 0. */
27
+ idle?: number;
26
28
  /** Clicks on these are theirs, not the ink's. Default: links, buttons, form fields and `[data-effect-ignore]`. */
27
29
  ignore?: string;
28
30
  }
@@ -33,6 +33,7 @@ import { PALETTES, pickColors } from './palettes.js';
33
33
  * @property {number} [radius=1] The size of a stroke's splats and of a drop, 1 as standard.
34
34
  * @property {number} [momentum=0.25] How fast the motion dies away, per second (lower flows on longer).
35
35
  * @property {number} [opacity=1] The ink's strongest cover, 0–1.
36
+ * @property {number} [idle=0] Seconds between drops of ink on their own, so it moves before anyone touches it; 0 for none. They wait while the ink is off screen or the tab is hidden.
36
37
  * @property {string} [ignore] Clicks on these are theirs, not the ink's (default: links, buttons, form fields, `[data-effect-ignore]`).
37
38
  */
38
39
 
@@ -68,6 +69,9 @@ export function ink(el, config) {
68
69
  let fluid = null;
69
70
  /** @type {ReturnType<typeof stir> | null} */
70
71
  let stirrer = null;
72
+ const idle = Math.max(0, config.idle ?? 0);
73
+ // the first drop comes soon after it starts, then one every `idle` seconds of running
74
+ let untilDrop = Math.min(idle, 0.4);
71
75
 
72
76
  const { stage, stop } = createStage(el, {
73
77
  fragment: SHOW,
@@ -95,6 +99,11 @@ export function ink(el, config) {
95
99
  },
96
100
  update(_stage, _now, dt) {
97
101
  stirrer?.update(dt);
102
+ // counted in frames run, so it waits while the loop is paused (off screen, tab hidden)
103
+ if (idle > 0 && dt > 0 && (untilDrop -= dt) <= 0) {
104
+ untilDrop = idle;
105
+ stirrer?.bloom(0.15 + Math.random() * 0.7, 0.15 + Math.random() * 0.7);
106
+ }
98
107
  if (dt > 0) fluid?.step(dt);
99
108
  fluid?.bind(3, 4);
100
109
  },