use-scroll-animate 5.4.0 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [5.5.0] - 2026-10-08
11
+
12
+ ### Added
13
+ - **Generative backgrounds pack** (`use-scroll-animate/components/effects`, `registerGenerativeEffects()`, `GENERATIVE_FX`, kind `background`): `flow-field`, `voronoi`, `mesh-gradient`, `starfield`, `metaballs`, `contours` — Canvas 2D, options `colors`, `background`, `speed`, `quality` plus per-effect knobs (`count`, `seeds`, `blobs`, `stars`, `balls`, `levels`, `cell`…).
14
+ - `canvasBackground(el, ctx, { init, draw }, options)` — the shared runner, exported for custom generative effects: an `aria-hidden`, pointer-transparent canvas behind the content (`isolation: isolate`), renders only while visible (IntersectionObserver) and the tab is shown, resizes with the element, and adapts quality (render scale 0.35–1 drops on sustained slow frames, recovers on fast ones). Helpers `noise2()`, `hexRgb()`.
15
+ - Showcase: **Generative backgrounds** card (switch between all six).
16
+
17
+ ### Accessibility
18
+ - Under reduced motion each background draws one static frame and never loops; canvases are `aria-hidden` and never take pointer events.
19
+
10
20
  ## [5.4.0] - 2026-10-08
11
21
 
12
22
  ### Added
@@ -829,6 +829,292 @@ function defineStory(tag = 'usa-story') {
829
829
  }, { id: 'usa-story', text: CSS });
830
830
  }
831
831
 
832
+ /** Deterministic smooth pseudo-noise in [-1, 1] (sum of sines — cheap, no tables). */
833
+ function noise2(x, y, t = 0) {
834
+ return (Math.sin(x * 1.7 + t) * Math.cos(y * 1.3 - t * 0.7) + Math.sin((x + y) * 0.9 + t * 0.5) * 0.5 + Math.cos(x * 0.6 - y * 1.1 - t * 0.3) * 0.5) / 2;
835
+ }
836
+ /** Hex `#rrggbb` → [r, g, b]. */
837
+ function hexRgb(hex) {
838
+ const m = /^#?([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i.exec(hex.trim());
839
+ return m ? [parseInt(m[1], 16), parseInt(m[2], 16), parseInt(m[3], 16)] : [124, 92, 255];
840
+ }
841
+ /**
842
+ * Mount a generative canvas behind `el` and run `spec` on it. Returns the
843
+ * cleanup. Exposed for custom generative effects.
844
+ */
845
+ function canvasBackground(el, fx, spec, o) {
846
+ const canvas = document.createElement('canvas');
847
+ canvas.setAttribute('aria-hidden', 'true');
848
+ canvas.setAttribute('data-usa-fx-canvas', '');
849
+ canvas.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;pointer-events:none;z-index:-1;border-radius:inherit';
850
+ const restore = [];
851
+ const set = (k, v) => {
852
+ restore.push([k, el.style[k]]);
853
+ el.style[k] = v;
854
+ };
855
+ if (getComputedStyle(el).position === 'static')
856
+ set('position', 'relative');
857
+ set('isolation', 'isolate');
858
+ el.prepend(canvas);
859
+ const ctx = canvas.getContext('2d');
860
+ let raf = 0;
861
+ let visible = true;
862
+ let quality = Math.min(1, Math.max(0.35, Number(o.quality) || 1));
863
+ let w = 0;
864
+ let h = 0;
865
+ let state = null;
866
+ const t0 = performance.now();
867
+ let last = t0;
868
+ let slow = 0;
869
+ let fast = 0;
870
+ const resize = () => {
871
+ const r = el.getBoundingClientRect();
872
+ w = Math.max(1, Math.round(r.width));
873
+ h = Math.max(1, Math.round(r.height));
874
+ const dpr = Math.min(2, (typeof devicePixelRatio === 'number' ? devicePixelRatio : 1) || 1) * quality;
875
+ canvas.width = Math.max(1, Math.round(w * dpr));
876
+ canvas.height = Math.max(1, Math.round(h * dpr));
877
+ if (ctx)
878
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
879
+ state = spec.init ? spec.init(w, h, o) : {};
880
+ };
881
+ const frame = (now) => {
882
+ raf = 0;
883
+ if (!ctx)
884
+ return;
885
+ const dt = now - last;
886
+ last = now;
887
+ // adaptive quality: sustained slow frames lower the render scale, fast ones raise it
888
+ if (dt > 28)
889
+ slow++;
890
+ else if (dt < 18)
891
+ fast++;
892
+ if (slow > 20 && quality > 0.35) {
893
+ quality = Math.max(0.35, quality - 0.15);
894
+ slow = fast = 0;
895
+ resize();
896
+ }
897
+ else if (fast > 240 && quality < 1) {
898
+ quality = Math.min(1, quality + 0.15);
899
+ slow = fast = 0;
900
+ resize();
901
+ }
902
+ spec.draw({ ctx, w, h, t: (now - t0) / 1000, quality, state, o });
903
+ if (visible && !document.hidden)
904
+ raf = requestAnimationFrame(frame);
905
+ };
906
+ const start = () => {
907
+ if (!raf && ctx && !fx.reduced) {
908
+ last = performance.now();
909
+ raf = requestAnimationFrame(frame);
910
+ }
911
+ };
912
+ resize();
913
+ if (ctx)
914
+ spec.draw({ ctx, w, h, t: 0, quality, state, o }); // first / static frame
915
+ const io = typeof IntersectionObserver === 'function' ? new IntersectionObserver((es) => {
916
+ visible = es.some((e) => e.isIntersecting);
917
+ if (visible)
918
+ start();
919
+ }) : null;
920
+ io?.observe(el);
921
+ const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(() => {
922
+ resize();
923
+ if (ctx && fx.reduced)
924
+ spec.draw({ ctx, w, h, t: 0, quality, state, o });
925
+ }) : null;
926
+ ro?.observe(el);
927
+ const vis = () => !document.hidden && visible && start();
928
+ document.addEventListener('visibilitychange', vis);
929
+ start();
930
+ return () => {
931
+ cancelAnimationFrame(raf);
932
+ raf = 0;
933
+ io?.disconnect();
934
+ ro?.disconnect();
935
+ document.removeEventListener('visibilitychange', vis);
936
+ canvas.remove();
937
+ for (const [k, v] of restore)
938
+ el.style[k] = v;
939
+ };
940
+ }
941
+ const gen = (name, description, defaults, spec) => ({
942
+ name,
943
+ kind: 'background',
944
+ description,
945
+ reduced: 'run',
946
+ defaults: { colors: PALETTE, background: '#0b0d12', speed: 1, quality: 1, ...defaults },
947
+ run: (el, o, ctx) => canvasBackground(el, ctx, spec, o),
948
+ });
949
+ /** Low-resolution scalar field → ImageData, drawn scaled up (used by voronoi / metaballs / contours). */
950
+ function field(f, cell, px) {
951
+ const step = Math.max(2, Math.round(cell / f.quality));
952
+ const cw = Math.max(1, Math.ceil(f.w / step));
953
+ const ch = Math.max(1, Math.ceil(f.h / step));
954
+ const st = f.state;
955
+ if (!st.buf || st.buf.width !== cw || st.buf.height !== ch) {
956
+ st.buf = document.createElement('canvas');
957
+ st.buf.width = cw;
958
+ st.buf.height = ch;
959
+ st.bctx = st.buf.getContext('2d');
960
+ st.img = st.bctx?.createImageData(cw, ch);
961
+ }
962
+ if (!st.bctx || !st.img)
963
+ return;
964
+ const d = st.img.data;
965
+ for (let y = 0; y < ch; y++)
966
+ for (let x = 0; x < cw; x++)
967
+ px(x * step, y * step, d, (y * cw + x) * 4);
968
+ st.bctx.putImageData(st.img, 0, 0);
969
+ f.ctx.imageSmoothingEnabled = true;
970
+ f.ctx.drawImage(st.buf, 0, 0, f.w, f.h);
971
+ }
972
+ const GENERATIVE_FX = [
973
+ gen('flow-field', 'Particles drift along a slowly changing flow field, leaving silky trails.', { count: 600, fade: 0.06 }, {
974
+ init: (w, h, o) => ({ p: Array.from({ length: o.count }, () => [Math.random() * w, Math.random() * h]) }),
975
+ draw: ({ ctx, w, h, t, state, o, quality }) => {
976
+ ctx.fillStyle = o.background;
977
+ ctx.globalAlpha = t === 0 ? 1 : o.fade;
978
+ ctx.fillRect(0, 0, w, h);
979
+ ctx.globalAlpha = 0.8;
980
+ const n = Math.round(state.p.length * quality);
981
+ const steps = t === 0 ? 40 : 1;
982
+ for (let i = 0; i < n; i++) {
983
+ const q = state.p[i];
984
+ ctx.fillStyle = o.colors[i % o.colors.length];
985
+ for (let s = 0; s < steps; s++) {
986
+ const a = noise2(q[0] * 0.004, q[1] * 0.004, t * 0.15 * o.speed) * Math.PI * 2;
987
+ q[0] += Math.cos(a) * 1.2 * o.speed;
988
+ q[1] += Math.sin(a) * 1.2 * o.speed;
989
+ if (q[0] < 0 || q[0] > w || q[1] < 0 || q[1] > h)
990
+ (q[0] = Math.random() * w), (q[1] = Math.random() * h);
991
+ ctx.fillRect(q[0], q[1], 1.4, 1.4);
992
+ }
993
+ }
994
+ ctx.globalAlpha = 1;
995
+ },
996
+ }),
997
+ gen('voronoi', 'Drifting Voronoi cells in the palette, with darker borders.', { seeds: 14, cell: 6 }, {
998
+ init: (w, h, o) => ({ s: Array.from({ length: o.seeds }, (_, i) => ({ x: Math.random() * w, y: Math.random() * h, vx: Math.random() - 0.5, vy: Math.random() - 0.5, c: hexRgb(o.colors[i % o.colors.length]) })) }),
999
+ draw: (f) => {
1000
+ const { w, h, state, o } = f;
1001
+ for (const s of state.s) {
1002
+ s.x += s.vx * o.speed;
1003
+ s.y += s.vy * o.speed;
1004
+ if (s.x < 0 || s.x > w)
1005
+ s.vx *= -1;
1006
+ if (s.y < 0 || s.y > h)
1007
+ s.vy *= -1;
1008
+ }
1009
+ field(f, o.cell, (x, y, d, i) => {
1010
+ let b = Infinity;
1011
+ let b2 = Infinity;
1012
+ let c = state.s[0].c;
1013
+ for (const s of state.s) {
1014
+ const dd = (s.x - x) ** 2 + (s.y - y) ** 2;
1015
+ if (dd < b)
1016
+ (b2 = b), (b = dd), (c = s.c);
1017
+ else if (dd < b2)
1018
+ b2 = dd;
1019
+ }
1020
+ const edge = Math.sqrt(b2) - Math.sqrt(b) < 6 ? 0.55 : 1;
1021
+ d[i] = c[0] * edge;
1022
+ d[i + 1] = c[1] * edge;
1023
+ d[i + 2] = c[2] * edge;
1024
+ d[i + 3] = 255;
1025
+ });
1026
+ },
1027
+ }),
1028
+ gen('mesh-gradient', 'Soft blobs of colour orbit and blend into a living mesh gradient.', { blobs: 4, blur: 0.65 }, {
1029
+ draw: ({ ctx, w, h, t, o }) => {
1030
+ ctx.fillStyle = o.background;
1031
+ ctx.fillRect(0, 0, w, h);
1032
+ const r = Math.max(w, h) * o.blur;
1033
+ for (let i = 0; i < o.blobs; i++) {
1034
+ const a = t * 0.25 * o.speed + (i * Math.PI * 2) / o.blobs;
1035
+ const x = w / 2 + Math.cos(a * (1 + i * 0.13)) * w * 0.32;
1036
+ const y = h / 2 + Math.sin(a * (0.8 + i * 0.11)) * h * 0.32;
1037
+ const [cr, cg, cb] = hexRgb(o.colors[i % o.colors.length]);
1038
+ const g = ctx.createRadialGradient(x, y, 0, x, y, r);
1039
+ g.addColorStop(0, `rgba(${cr},${cg},${cb},0.7)`);
1040
+ g.addColorStop(1, `rgba(${cr},${cg},${cb},0)`);
1041
+ ctx.fillStyle = g;
1042
+ ctx.fillRect(0, 0, w, h);
1043
+ }
1044
+ },
1045
+ }),
1046
+ gen('starfield', 'Stars stream toward the viewer (warp speed with `speed: 4`).', { stars: 400, color: '#ffffff' }, {
1047
+ init: (_w, _h, o) => ({ s: Array.from({ length: o.stars }, () => [Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random()]) }),
1048
+ draw: ({ ctx, w, h, t, state, o, quality }) => {
1049
+ ctx.fillStyle = o.background;
1050
+ ctx.fillRect(0, 0, w, h);
1051
+ ctx.fillStyle = o.color;
1052
+ const n = Math.round(state.s.length * quality);
1053
+ for (let i = 0; i < n; i++) {
1054
+ const s = state.s[i];
1055
+ if (t > 0)
1056
+ s[2] -= 0.004 * o.speed;
1057
+ if (s[2] <= 0.01)
1058
+ (s[0] = Math.random() * 2 - 1), (s[1] = Math.random() * 2 - 1), (s[2] = 1);
1059
+ const x = w / 2 + (s[0] / s[2]) * w * 0.5;
1060
+ const y = h / 2 + (s[1] / s[2]) * h * 0.5;
1061
+ if (x < 0 || x > w || y < 0 || y > h)
1062
+ continue;
1063
+ const size = (1 - s[2]) * 2.6;
1064
+ ctx.globalAlpha = Math.min(1, (1 - s[2]) * 1.6);
1065
+ ctx.fillRect(x, y, size, size);
1066
+ }
1067
+ ctx.globalAlpha = 1;
1068
+ },
1069
+ }),
1070
+ gen('metaballs', 'Gooey blobs that merge and split (low-resolution field, scaled up).', { balls: 6, cell: 5, threshold: 1 }, {
1071
+ init: (w, h, o) => ({ b: Array.from({ length: o.balls }, () => ({ x: Math.random() * w, y: Math.random() * h, vx: (Math.random() - 0.5) * 1.6, vy: (Math.random() - 0.5) * 1.6, r: Math.min(w, h) * (0.08 + Math.random() * 0.08) })) }),
1072
+ draw: (f) => {
1073
+ const { w, h, state, o } = f;
1074
+ for (const b of state.b) {
1075
+ b.x += b.vx * o.speed;
1076
+ b.y += b.vy * o.speed;
1077
+ if (b.x < 0 || b.x > w)
1078
+ b.vx *= -1;
1079
+ if (b.y < 0 || b.y > h)
1080
+ b.vy *= -1;
1081
+ }
1082
+ const [ar, ag, ab] = hexRgb(o.colors[0]);
1083
+ const [br, bg, bb] = hexRgb(o.colors[1] || o.colors[0]);
1084
+ const [kr, kg, kb] = hexRgb(o.background);
1085
+ field(f, o.cell, (x, y, d, i) => {
1086
+ let v = 0;
1087
+ for (const b of state.b)
1088
+ v += (b.r * b.r) / ((b.x - x) ** 2 + (b.y - y) ** 2 + 1);
1089
+ const inside = v >= o.threshold;
1090
+ const k = Math.min(1, (v - o.threshold) * 0.8);
1091
+ d[i] = inside ? ar + (br - ar) * k : kr;
1092
+ d[i + 1] = inside ? ag + (bg - ag) * k : kg;
1093
+ d[i + 2] = inside ? ab + (bb - ab) * k : kb;
1094
+ d[i + 3] = 255;
1095
+ });
1096
+ },
1097
+ }),
1098
+ gen('contours', 'Topographic contour lines over a slowly shifting height field.', { levels: 9, cell: 4, color: '#7c5cff' }, {
1099
+ draw: (f) => {
1100
+ const { t, o } = f;
1101
+ const [lr, lg, lb] = hexRgb(o.color);
1102
+ const [kr, kg, kb] = hexRgb(o.background);
1103
+ const z = (x, y) => (noise2(x * 0.006, y * 0.006, t * 0.2 * o.speed) + 1) / 2;
1104
+ const step = Math.max(2, Math.round(o.cell / f.quality));
1105
+ field(f, o.cell, (x, y, d, i) => {
1106
+ const a = Math.floor(z(x, y) * o.levels);
1107
+ const line = a !== Math.floor(z(x + step, y) * o.levels) || a !== Math.floor(z(x, y + step) * o.levels);
1108
+ const shade = 0.12 + (a / o.levels) * 0.18;
1109
+ d[i] = line ? lr : kr + (lr - kr) * shade;
1110
+ d[i + 1] = line ? lg : kg + (lg - kg) * shade;
1111
+ d[i + 2] = line ? lb : kb + (lb - kb) * shade;
1112
+ d[i + 3] = 255;
1113
+ });
1114
+ },
1115
+ }),
1116
+ ];
1117
+
832
1118
  /**
833
1119
  * use-scroll-animate/components/effects — the 5.x effect packs, all
834
1120
  * registered through `registerEffect()` (5.0) and playable with
@@ -846,6 +1132,7 @@ const EFFECT_PACKS = {
846
1132
  'cards-click': [...CARD_FX, ...CLICK_FX],
847
1133
  physics: PHYSICS_FX,
848
1134
  page: PAGE_FX,
1135
+ generative: GENERATIVE_FX,
849
1136
  };
850
1137
  /** 5.1: card & click effects 2.0. */
851
1138
  function registerCardClickEffects() {
@@ -859,6 +1146,10 @@ function registerPhysicsEffects() {
859
1146
  function registerPageEffects() {
860
1147
  components_fx.registerEffects(EFFECT_PACKS.page);
861
1148
  }
1149
+ /** 5.5: generative Canvas 2D backgrounds. */
1150
+ function registerGenerativeEffects() {
1151
+ components_fx.registerEffects(EFFECT_PACKS.generative);
1152
+ }
862
1153
  /** Define the 5.x elements of this entry (`<usa-story>`, …) under their default tags. */
863
1154
  function defineEffectElements() {
864
1155
  defineStory();
@@ -873,16 +1164,21 @@ function registerAllEffects() {
873
1164
  exports.CARD_FX = CARD_FX;
874
1165
  exports.CLICK_FX = CLICK_FX;
875
1166
  exports.EFFECT_PACKS = EFFECT_PACKS;
1167
+ exports.GENERATIVE_FX = GENERATIVE_FX;
876
1168
  exports.PAGE_FX = PAGE_FX;
877
1169
  exports.PHYSICS_FX = PHYSICS_FX;
878
1170
  exports.STORY_TEMPLATES = STORY_TEMPLATES;
879
1171
  exports.bounceKeyframes = bounceKeyframes;
1172
+ exports.canvasBackground = canvasBackground;
880
1173
  exports.defineEffectElements = defineEffectElements;
881
1174
  exports.defineStory = defineStory;
882
1175
  exports.formatCount = formatCount;
883
1176
  exports.fxLayer = fxLayer;
1177
+ exports.hexRgb = hexRgb;
1178
+ exports.noise2 = noise2;
884
1179
  exports.registerAllEffects = registerAllEffects;
885
1180
  exports.registerCardClickEffects = registerCardClickEffects;
1181
+ exports.registerGenerativeEffects = registerGenerativeEffects;
886
1182
  exports.registerPageEffects = registerPageEffects;
887
1183
  exports.registerPhysicsEffects = registerPhysicsEffects;
888
1184
  exports.solveSpring = solveSpring;