@arraypress/effects-astro 0.0.0-stage → 0.1.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 +85 -0
- package/COMPONENTS.md +1264 -0
- package/LICENSE +139 -0
- package/README.md +197 -2
- package/package.json +60 -4
- package/src/components/AsciiRain.astro +36 -0
- package/src/components/BorderSweep.astro +39 -0
- package/src/components/Caustics.astro +39 -0
- package/src/components/ColorPanels.astro +62 -0
- package/src/components/Cursor.astro +97 -0
- package/src/components/DotGrid.astro +34 -0
- package/src/components/DotSurface.astro +35 -0
- package/src/components/EffectCanvas.astro +152 -0
- package/src/components/FitText.astro +35 -0
- package/src/components/FlutedGlass.astro +36 -0
- package/src/components/Glass.astro +69 -0
- package/src/components/GlassGlyph.astro +38 -0
- package/src/components/GlowField.astro +177 -0
- package/src/components/Grain.astro +98 -0
- package/src/components/HaloRing.astro +38 -0
- package/src/components/HighlightSweep.astro +74 -0
- package/src/components/ImageRipple.astro +56 -0
- package/src/components/Ink.astro +37 -0
- package/src/components/InkField.astro +45 -0
- package/src/components/IsoCity.astro +54 -0
- package/src/components/LightField.astro +40 -0
- package/src/components/LiquidMetal.astro +41 -0
- package/src/components/Magnetic.astro +52 -0
- package/src/components/Marquee.astro +216 -0
- package/src/components/MosaicReveal.astro +196 -0
- package/src/components/NebulaLogo.astro +38 -0
- package/src/components/NeonSwarm.astro +38 -0
- package/src/components/Ocean.astro +40 -0
- package/src/components/OutlineWordmark.astro +222 -0
- package/src/components/PaperBackground.astro +40 -0
- package/src/components/PaperShader.astro +55 -0
- package/src/components/Parallax.astro +49 -0
- package/src/components/ParticleLogo.astro +37 -0
- package/src/components/PixelGlitchText.astro +70 -0
- package/src/components/PointCloud.astro +37 -0
- package/src/components/ProductStack.astro +298 -0
- package/src/components/PulseMark.astro +189 -0
- package/src/components/RadiantGlass.astro +38 -0
- package/src/components/RainOnGlass.astro +71 -0
- package/src/components/Reveal.astro +115 -0
- package/src/components/ScrambleText.astro +52 -0
- package/src/components/ScrollReveal.astro +38 -0
- package/src/components/ShaderBackground.astro +55 -0
- package/src/components/SignalLines.astro +41 -0
- package/src/components/SilkRibbon.astro +37 -0
- package/src/components/SpotlightText.astro +131 -0
- package/src/components/Tilt.astro +79 -0
- package/src/components/Topography.astro +38 -0
- package/src/components/Typewriter.astro +66 -0
- package/src/components/VariableType.astro +59 -0
- package/src/components/VoxelLogo.astro +46 -0
- package/src/components/WaveBars.astro +41 -0
- package/src/components/WorldRoutes.astro +41 -0
- package/src/components/style.js +19 -0
- package/src/index.d.ts +143 -0
- package/src/index.js +105 -0
- package/src/runtime/ascii-rain.d.ts +53 -0
- package/src/runtime/ascii-rain.js +355 -0
- package/src/runtime/audio-meter.d.ts +65 -0
- package/src/runtime/audio-meter.js +282 -0
- package/src/runtime/audio.d.ts +26 -0
- package/src/runtime/audio.js +453 -0
- package/src/runtime/blur.d.ts +2 -0
- package/src/runtime/blur.js +56 -0
- package/src/runtime/canvas2d.d.ts +23 -0
- package/src/runtime/canvas2d.js +105 -0
- package/src/runtime/caustics.d.ts +40 -0
- package/src/runtime/caustics.js +286 -0
- package/src/runtime/color.d.ts +33 -0
- package/src/runtime/color.js +213 -0
- package/src/runtime/curl.d.ts +5 -0
- package/src/runtime/curl.js +81 -0
- package/src/runtime/cursor.d.ts +9 -0
- package/src/runtime/cursor.js +80 -0
- package/src/runtime/device.d.ts +14 -0
- package/src/runtime/device.js +73 -0
- package/src/runtime/dot-grid.d.ts +27 -0
- package/src/runtime/dot-grid.js +245 -0
- package/src/runtime/dot-surface.d.ts +46 -0
- package/src/runtime/dot-surface.js +429 -0
- package/src/runtime/feedback.d.ts +18 -0
- package/src/runtime/feedback.js +131 -0
- package/src/runtime/fit-text.d.ts +5 -0
- package/src/runtime/fit-text.js +60 -0
- package/src/runtime/fluid.d.ts +55 -0
- package/src/runtime/fluid.js +504 -0
- package/src/runtime/fluted-glass.d.ts +27 -0
- package/src/runtime/fluted-glass.js +157 -0
- package/src/runtime/gl.d.ts +19 -0
- package/src/runtime/gl.js +104 -0
- package/src/runtime/glass-glyph.d.ts +49 -0
- package/src/runtime/glass-glyph.js +355 -0
- package/src/runtime/glass-sheen.d.ts +2 -0
- package/src/runtime/glass-sheen.js +30 -0
- package/src/runtime/glsl.d.ts +17 -0
- package/src/runtime/glsl.js +78 -0
- package/src/runtime/glyphs.d.ts +22 -0
- package/src/runtime/glyphs.js +107 -0
- package/src/runtime/halo-ring.d.ts +47 -0
- package/src/runtime/halo-ring.js +334 -0
- package/src/runtime/height-field.d.ts +50 -0
- package/src/runtime/height-field.js +176 -0
- package/src/runtime/image-ripple.d.ts +25 -0
- package/src/runtime/image-ripple.js +353 -0
- package/src/runtime/image.d.ts +11 -0
- package/src/runtime/image.js +81 -0
- package/src/runtime/in-view.d.ts +6 -0
- package/src/runtime/in-view.js +36 -0
- package/src/runtime/ink-field/shaders.js +258 -0
- package/src/runtime/ink-field/sim.js +582 -0
- package/src/runtime/ink-field.d.ts +39 -0
- package/src/runtime/ink-field.js +311 -0
- package/src/runtime/ink.d.ts +31 -0
- package/src/runtime/ink.js +109 -0
- package/src/runtime/instancing.d.ts +12 -0
- package/src/runtime/instancing.js +37 -0
- package/src/runtime/iso-camera.d.ts +35 -0
- package/src/runtime/iso-camera.js +85 -0
- package/src/runtime/iso-city/figures.d.ts +30 -0
- package/src/runtime/iso-city/figures.js +136 -0
- package/src/runtime/iso-city/look.d.ts +34 -0
- package/src/runtime/iso-city/look.js +105 -0
- package/src/runtime/iso-city/plan.d.ts +59 -0
- package/src/runtime/iso-city/plan.js +463 -0
- package/src/runtime/iso-city/roads.d.ts +64 -0
- package/src/runtime/iso-city/roads.js +320 -0
- package/src/runtime/iso-city/scene.d.ts +27 -0
- package/src/runtime/iso-city/scene.js +432 -0
- package/src/runtime/iso-city/shaders.d.ts +6 -0
- package/src/runtime/iso-city/shaders.js +233 -0
- package/src/runtime/iso-city/shadows.d.ts +13 -0
- package/src/runtime/iso-city/shadows.js +148 -0
- package/src/runtime/iso-city/traffic.d.ts +80 -0
- package/src/runtime/iso-city/traffic.js +457 -0
- package/src/runtime/iso-city.d.ts +37 -0
- package/src/runtime/iso-city.js +458 -0
- package/src/runtime/lens-map.d.ts +11 -0
- package/src/runtime/lens-map.js +146 -0
- package/src/runtime/light-field.d.ts +29 -0
- package/src/runtime/light-field.js +292 -0
- package/src/runtime/liquid-glass.d.ts +48 -0
- package/src/runtime/liquid-glass.js +423 -0
- package/src/runtime/liquid-metal/beads.js +217 -0
- package/src/runtime/liquid-metal/field.js +88 -0
- package/src/runtime/liquid-metal/look.js +64 -0
- package/src/runtime/liquid-metal/shaders.js +255 -0
- package/src/runtime/liquid-metal/wave.js +53 -0
- package/src/runtime/liquid-metal.d.ts +46 -0
- package/src/runtime/liquid-metal.js +366 -0
- package/src/runtime/loop.d.ts +7 -0
- package/src/runtime/loop.js +112 -0
- package/src/runtime/magnetic.d.ts +9 -0
- package/src/runtime/magnetic.js +46 -0
- package/src/runtime/marquee.d.ts +2 -0
- package/src/runtime/marquee.js +27 -0
- package/src/runtime/mesh.d.ts +30 -0
- package/src/runtime/mesh.js +217 -0
- package/src/runtime/mosaic-reveal.d.ts +34 -0
- package/src/runtime/mosaic-reveal.js +504 -0
- package/src/runtime/motion.d.ts +2 -0
- package/src/runtime/motion.js +21 -0
- package/src/runtime/mount.d.ts +13 -0
- package/src/runtime/mount.js +120 -0
- package/src/runtime/nebula-logo.d.ts +31 -0
- package/src/runtime/nebula-logo.js +282 -0
- package/src/runtime/neon-swarm.d.ts +31 -0
- package/src/runtime/neon-swarm.js +258 -0
- package/src/runtime/ocean/presets.d.ts +54 -0
- package/src/runtime/ocean/presets.js +184 -0
- package/src/runtime/ocean/shader.d.ts +14 -0
- package/src/runtime/ocean/shader.js +389 -0
- package/src/runtime/ocean/view.d.ts +24 -0
- package/src/runtime/ocean/view.js +98 -0
- package/src/runtime/ocean/waves.d.ts +29 -0
- package/src/runtime/ocean/waves.js +119 -0
- package/src/runtime/ocean.d.ts +31 -0
- package/src/runtime/ocean.js +173 -0
- package/src/runtime/palettes.d.ts +37 -0
- package/src/runtime/palettes.js +75 -0
- package/src/runtime/paper-look.d.ts +42 -0
- package/src/runtime/paper-look.js +91 -0
- package/src/runtime/paper-looks.d.ts +17 -0
- package/src/runtime/paper-looks.js +21 -0
- package/src/runtime/paper.d.ts +37 -0
- package/src/runtime/paper.js +285 -0
- package/src/runtime/particle-logo.d.ts +31 -0
- package/src/runtime/particle-logo.js +196 -0
- package/src/runtime/ping-pong.d.ts +43 -0
- package/src/runtime/ping-pong.js +175 -0
- package/src/runtime/pixel-glitch.d.ts +17 -0
- package/src/runtime/pixel-glitch.js +287 -0
- package/src/runtime/point-cloud.d.ts +33 -0
- package/src/runtime/point-cloud.js +287 -0
- package/src/runtime/pointer.d.ts +27 -0
- package/src/runtime/pointer.js +128 -0
- package/src/runtime/points.d.ts +25 -0
- package/src/runtime/points.js +130 -0
- package/src/runtime/pools.d.ts +2 -0
- package/src/runtime/pools.js +25 -0
- package/src/runtime/product-stack.d.ts +24 -0
- package/src/runtime/product-stack.js +210 -0
- package/src/runtime/radiant-glass.d.ts +29 -0
- package/src/runtime/radiant-glass.js +282 -0
- package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
- package/src/runtime/rain-on-glass/backdrop.js +185 -0
- package/src/runtime/rain-on-glass/shader.d.ts +2 -0
- package/src/runtime/rain-on-glass/shader.js +168 -0
- package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
- package/src/runtime/rain-on-glass/wipe.js +65 -0
- package/src/runtime/rain-on-glass.d.ts +27 -0
- package/src/runtime/rain-on-glass.js +210 -0
- package/src/runtime/ramp.d.ts +2 -0
- package/src/runtime/ramp.js +30 -0
- package/src/runtime/random.d.ts +2 -0
- package/src/runtime/random.js +22 -0
- package/src/runtime/scroll-reveal.d.ts +22 -0
- package/src/runtime/scroll-reveal.js +73 -0
- package/src/runtime/shader-background.d.ts +42 -0
- package/src/runtime/shader-background.js +261 -0
- package/src/runtime/shader-backgrounds/band-lens.js +72 -0
- package/src/runtime/shader-backgrounds/common.js +53 -0
- package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
- package/src/runtime/shader-backgrounds/grid.js +33 -0
- package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
- package/src/runtime/shader-backgrounds/silk.js +57 -0
- package/src/runtime/shader-backgrounds/waves.js +31 -0
- package/src/runtime/shape.d.ts +42 -0
- package/src/runtime/shape.js +313 -0
- package/src/runtime/silk-ribbon.d.ts +23 -0
- package/src/runtime/silk-ribbon.js +206 -0
- package/src/runtime/slideshow.d.ts +27 -0
- package/src/runtime/slideshow.js +234 -0
- package/src/runtime/spotlight.d.ts +9 -0
- package/src/runtime/spotlight.js +97 -0
- package/src/runtime/stack-poses.d.ts +53 -0
- package/src/runtime/stack-poses.js +273 -0
- package/src/runtime/stage.d.ts +47 -0
- package/src/runtime/stage.js +187 -0
- package/src/runtime/swarm.d.ts +49 -0
- package/src/runtime/swarm.js +199 -0
- package/src/runtime/text-motion.d.ts +41 -0
- package/src/runtime/text-motion.js +165 -0
- package/src/runtime/text.d.ts +11 -0
- package/src/runtime/text.js +90 -0
- package/src/runtime/tilt.d.ts +19 -0
- package/src/runtime/tilt.js +116 -0
- package/src/runtime/topography.d.ts +71 -0
- package/src/runtime/topography.js +650 -0
- package/src/runtime/trackers.d.ts +21 -0
- package/src/runtime/trackers.js +76 -0
- package/src/runtime/variable-type.d.ts +43 -0
- package/src/runtime/variable-type.js +358 -0
- package/src/runtime/voxel-logo.d.ts +31 -0
- package/src/runtime/voxel-logo.js +237 -0
- package/src/runtime/waves.d.ts +70 -0
- package/src/runtime/waves.js +799 -0
- package/src/runtime/world-labels.d.ts +29 -0
- package/src/runtime/world-labels.js +59 -0
- package/src/runtime/world-land.d.ts +8 -0
- package/src/runtime/world-land.js +105 -0
- package/src/runtime/world-map.d.ts +69 -0
- package/src/runtime/world-map.js +308 -0
- package/src/runtime/world-plan.d.ts +101 -0
- package/src/runtime/world-plan.js +333 -0
- package/src/runtime/world-routes.d.ts +75 -0
- package/src/runtime/world-routes.js +673 -0
- package/src/styles/border-sweep.css +82 -0
- package/src/styles/glass.css +104 -0
- package/src/styles/motion.css +218 -0
- package/src/styles/reveal.css +51 -0
- package/src/vendor/paper/LICENSE +201 -0
- package/src/vendor/paper/NOTICE +5 -0
- package/src/vendor/paper/README.md +8 -0
- package/src/vendor/paper/get-shader-color-from-string.js +100 -0
- package/src/vendor/paper/get-shader-noise-texture.js +17 -0
- package/src/vendor/paper/presets.js +3678 -0
- package/src/vendor/paper/shader-sizing.js +37 -0
- package/src/vendor/paper/shader-utils.js +130 -0
- package/src/vendor/paper/shaders/color-panels.js +223 -0
- package/src/vendor/paper/shaders/dithering.js +270 -0
- package/src/vendor/paper/shaders/dot-grid.js +109 -0
- package/src/vendor/paper/shaders/dot-orbit.js +122 -0
- package/src/vendor/paper/shaders/fluted-glass.js +360 -0
- package/src/vendor/paper/shaders/gem-smoke.js +572 -0
- package/src/vendor/paper/shaders/god-rays.js +132 -0
- package/src/vendor/paper/shaders/grain-gradient.js +300 -0
- package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
- package/src/vendor/paper/shaders/halftone-dots.js +320 -0
- package/src/vendor/paper/shaders/heatmap.js +383 -0
- package/src/vendor/paper/shaders/image-dithering.js +198 -0
- package/src/vendor/paper/shaders/lens-distortion.js +275 -0
- package/src/vendor/paper/shaders/liquid-metal.js +632 -0
- package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/metaballs.js +110 -0
- package/src/vendor/paper/shaders/neuro-noise.js +76 -0
- package/src/vendor/paper/shaders/paper-texture.js +526 -0
- package/src/vendor/paper/shaders/perlin-noise.js +166 -0
- package/src/vendor/paper/shaders/pulsing-border.js +238 -0
- package/src/vendor/paper/shaders/simplex-noise.js +91 -0
- package/src/vendor/paper/shaders/smoke-ring.js +132 -0
- package/src/vendor/paper/shaders/spiral.js +82 -0
- package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
- package/src/vendor/paper/shaders/swirl.js +103 -0
- package/src/vendor/paper/shaders/voronoi.js +138 -0
- package/src/vendor/paper/shaders/warp.js +129 -0
- package/src/vendor/paper/shaders/water.js +121 -0
- package/src/vendor/paper/shaders/waves.js +65 -0
- package/src/vendor/paper/vertex-shader.js +159 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/stack-poses
|
|
3
|
+
*
|
|
4
|
+
* Where each cover of a `<ProductStack>` sits: pure layout, shared by the
|
|
5
|
+
* component (which writes the resting poses into the page, so the stack
|
|
6
|
+
* stands before any script runs) and the runtime (which moves between
|
|
7
|
+
* them as the pointer comes and goes).
|
|
8
|
+
*
|
|
9
|
+
* Lengths are in cover widths, so a pose holds at any size: the transform
|
|
10
|
+
* multiplies them by `--ps-w`, the cover's width in CSS. Three layouts:
|
|
11
|
+
*
|
|
12
|
+
* - `fan`: a hand of cards, the middle cover in front.
|
|
13
|
+
* - `stack`: a deck seen at an angle, the first cover in front and the
|
|
14
|
+
* rest stepping back up and to the right.
|
|
15
|
+
* - `arc`: a carousel arc, the middle cover facing the viewer and the
|
|
16
|
+
* rest turned in towards it either side, closer together further out.
|
|
17
|
+
*
|
|
18
|
+
* Open, the covers move further apart; a lifted cover comes forward,
|
|
19
|
+
* squares up to the viewer and pushes its neighbours aside.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** The layouts, the first the default. */
|
|
23
|
+
export const LAYOUTS = /** @type {const} */ (['fan', 'stack', 'arc']);
|
|
24
|
+
|
|
25
|
+
/** How much further apart the covers sit when the stack is open, per layout. */
|
|
26
|
+
const OPEN = { fan: 1.35, stack: 1.6, arc: 1.3 };
|
|
27
|
+
|
|
28
|
+
/** How far a lifted cover pushes its neighbours aside, in cover steps. */
|
|
29
|
+
const MAKE_ROOM = 0.4;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {'fan' | 'stack' | 'arc'} Layout
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {object} Pose
|
|
37
|
+
* @property {number} x Across, in cover widths; right is positive.
|
|
38
|
+
* @property {number} y Down, in cover widths.
|
|
39
|
+
* @property {number} z Towards the viewer, in cover widths.
|
|
40
|
+
* @property {number} rx Turn about the horizontal axis, degrees.
|
|
41
|
+
* @property {number} ry Turn about the vertical axis, degrees.
|
|
42
|
+
* @property {number} rz Turn in the plane, degrees.
|
|
43
|
+
* @property {number} s Scale.
|
|
44
|
+
* @property {number} blur How far out of focus, 0 sharp to 1 the furthest.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} PoseState
|
|
49
|
+
* @property {number} [spread=1] How far apart the covers sit, 0–2.
|
|
50
|
+
* @property {boolean} [open=false] Whether the stack is fanned apart (pointer over it, or a cover focused).
|
|
51
|
+
* @property {number} [lifted=-1] The cover that is lifted forward; -1 for none.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A known layout, or the default.
|
|
56
|
+
*
|
|
57
|
+
* @param {unknown} layout A layout's name.
|
|
58
|
+
* @returns {Layout} It, if known; else `fan`.
|
|
59
|
+
*/
|
|
60
|
+
export function layoutOf(layout) {
|
|
61
|
+
return LAYOUTS.includes(/** @type {Layout} */ (layout)) ? /** @type {Layout} */ (layout) : 'fan';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The cover in front when none is lifted: the middle one in a fan (between
|
|
66
|
+
* two when the count is even) or an arc (the left of the two), the first
|
|
67
|
+
* in a stack.
|
|
68
|
+
*
|
|
69
|
+
* @param {Layout} layout The layout.
|
|
70
|
+
* @param {number} n How many covers.
|
|
71
|
+
* @returns {number} Its index; in an even fan, half way between two.
|
|
72
|
+
*/
|
|
73
|
+
export function frontOf(layout, n) {
|
|
74
|
+
if (layout === 'stack') return 0;
|
|
75
|
+
return layout === 'arc' ? Math.floor((n - 1) / 2) : (n - 1) / 2;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* How much further back a cover right of the front sits than its twin on
|
|
80
|
+
* the left, so no two covers share a depth (their planes would cut through
|
|
81
|
+
* each other) and an even fan has one cover in front.
|
|
82
|
+
*
|
|
83
|
+
* @param {number} t Its place: steps from the front, signed.
|
|
84
|
+
* @returns {number} The extra depth, in cover widths.
|
|
85
|
+
*/
|
|
86
|
+
function behind(t) {
|
|
87
|
+
return t > 0 ? 0.06 : 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* One cover's pose, before centring.
|
|
92
|
+
*
|
|
93
|
+
* @param {Layout} layout The layout.
|
|
94
|
+
* @param {number} t Its place: steps from the front, signed; already pushed aside for a lifted neighbour.
|
|
95
|
+
* @param {number} k The spread, open or not.
|
|
96
|
+
* @param {boolean} lifted Whether it is the lifted cover.
|
|
97
|
+
* @returns {Omit<Pose, 'blur'>} Its pose.
|
|
98
|
+
*/
|
|
99
|
+
function place(layout, t, k, lifted) {
|
|
100
|
+
if (layout === 'stack') {
|
|
101
|
+
const pose = { x: t * 0.15 * k, y: -t * 0.06 * k, z: -t * 0.3 * k, rx: 5, ry: -28, rz: 0, s: 1 };
|
|
102
|
+
// Drawn up out of the deck to one height, and forward, in front of the covers that were in front of it.
|
|
103
|
+
return lifted ? { ...pose, x: pose.x * 0.6, y: -0.32, z: 0.5, rx: 2, ry: -12, s: 1.04 } : pose;
|
|
104
|
+
}
|
|
105
|
+
if (layout === 'arc') {
|
|
106
|
+
// The front cover faces the viewer; the rest turn in towards it and stand closer together the further out they go.
|
|
107
|
+
const near = Math.min(Math.abs(t), 1);
|
|
108
|
+
const far = Math.max(0, Math.abs(t) - 1);
|
|
109
|
+
const side = Math.sign(t);
|
|
110
|
+
const pose = { x: side * (near * 0.62 + far * 0.26) * k, y: 0, z: -(near * 0.5 + far * 0.16 * k) - behind(t), rx: 0, ry: -side * near * 48, rz: 0, s: 1 };
|
|
111
|
+
return lifted ? { ...pose, y: -0.04, z: pose.z + 0.6, ry: pose.ry * 0.25, s: 1.05 } : pose;
|
|
112
|
+
}
|
|
113
|
+
const pose = { x: t * 0.22 * k, y: t * t * 0.026 * k, z: -Math.abs(t) * 0.12 - behind(t), rx: 0, ry: 0, rz: t * 6.5 * k, s: 1 };
|
|
114
|
+
return lifted ? { ...pose, y: pose.y - 0.12, z: pose.z + 0.5, rz: pose.rz * 0.3, s: 1.06 } : pose;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Every cover's pose, before centring and focus.
|
|
119
|
+
*
|
|
120
|
+
* @param {Layout} layout The layout.
|
|
121
|
+
* @param {number} n How many covers.
|
|
122
|
+
* @param {PoseState} state The spread, and whether it is open and which cover is lifted.
|
|
123
|
+
* @returns {Array<Omit<Pose, 'blur'>>} The poses, in the covers' order.
|
|
124
|
+
*/
|
|
125
|
+
function placeAll(layout, n, { spread = 1, open = false, lifted = -1 }) {
|
|
126
|
+
const front = frontOf(layout, n);
|
|
127
|
+
const k = Math.max(0, spread) * (open ? OPEN[layout] : 1);
|
|
128
|
+
return Array.from({ length: n }, (_, i) => {
|
|
129
|
+
const aside = lifted < 0 || i === lifted ? 0 : Math.sign(i - lifted) * MAKE_ROOM;
|
|
130
|
+
return place(layout, i - front + aside, k, i === lifted);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The box round a set of covers' centres, in cover widths.
|
|
136
|
+
*
|
|
137
|
+
* @param {Array<{ x: number, y: number }>} poses The poses.
|
|
138
|
+
* @returns {{ left: number, right: number, top: number, bottom: number }} The box.
|
|
139
|
+
*/
|
|
140
|
+
function extent(poses) {
|
|
141
|
+
const xs = poses.map((p) => p.x);
|
|
142
|
+
const ys = poses.map((p) => p.y);
|
|
143
|
+
return { left: Math.min(...xs), right: Math.max(...xs), top: Math.min(...ys), bottom: Math.max(...ys) };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Where the resting stack's centre is, so every pose can be moved to put it
|
|
148
|
+
* in the middle (a stack steps off to one side; a fan dips at its ends).
|
|
149
|
+
*
|
|
150
|
+
* @param {Layout} layout The layout.
|
|
151
|
+
* @param {number} n How many covers.
|
|
152
|
+
* @param {number} spread The spread.
|
|
153
|
+
* @returns {{ x: number, y: number }} The centre, in cover widths.
|
|
154
|
+
*/
|
|
155
|
+
function centreOf(layout, n, spread) {
|
|
156
|
+
const box = extent(placeAll(layout, n, { spread }));
|
|
157
|
+
return { x: (box.left + box.right) / 2, y: (box.top + box.bottom) / 2 };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Every cover's pose: placed, centred, and given how far it is out of
|
|
162
|
+
* focus (by its distance from the lifted cover, or from the front).
|
|
163
|
+
*
|
|
164
|
+
* @param {Layout} layout The layout.
|
|
165
|
+
* @param {number} n How many covers.
|
|
166
|
+
* @param {PoseState} [state] The spread, and whether it is open and which cover is lifted.
|
|
167
|
+
* @returns {Pose[]} The poses, in the covers' order.
|
|
168
|
+
*/
|
|
169
|
+
export function stackPoses(layout, n, state = {}) {
|
|
170
|
+
if (n <= 0) return [];
|
|
171
|
+
const spread = state.spread ?? 1;
|
|
172
|
+
const lifted = Number.isInteger(state.lifted) && /** @type {number} */ (state.lifted) < n ? /** @type {number} */ (state.lifted) : -1;
|
|
173
|
+
const centre = centreOf(layout, n, spread);
|
|
174
|
+
const focus = lifted >= 0 ? lifted : frontOf(layout, n);
|
|
175
|
+
const furthest = Math.max(focus, n - 1 - focus, 1);
|
|
176
|
+
return placeAll(layout, n, { spread, open: state.open, lifted }).map((pose, i) => ({
|
|
177
|
+
...pose,
|
|
178
|
+
x: pose.x - centre.x,
|
|
179
|
+
y: pose.y - centre.y,
|
|
180
|
+
blur: Math.min(1, Math.max(0, Math.abs(i - focus) - 0.5) / Math.max(furthest - 0.5, 0.5)),
|
|
181
|
+
}));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* How far a set of covers reach from the centre each way, each cover's
|
|
186
|
+
* turned box included.
|
|
187
|
+
*
|
|
188
|
+
* @param {Pose[]} poses The poses.
|
|
189
|
+
* @param {number} ratio A cover's width over its height.
|
|
190
|
+
* @returns {{ x: number, y: number }} The furthest reach across and down, in cover widths.
|
|
191
|
+
*/
|
|
192
|
+
function reach(poses, ratio) {
|
|
193
|
+
let x = 0;
|
|
194
|
+
let y = 0;
|
|
195
|
+
for (const { x: px, y: py, rz, s } of poses) {
|
|
196
|
+
const turn = (rz * Math.PI) / 180;
|
|
197
|
+
const [cos, sin] = [Math.abs(Math.cos(turn)), Math.abs(Math.sin(turn))];
|
|
198
|
+
x = Math.max(x, Math.abs(px) + 0.5 * s * (cos + sin / ratio));
|
|
199
|
+
y = Math.max(y, Math.abs(py) + 0.5 * s * (sin + cos / ratio));
|
|
200
|
+
}
|
|
201
|
+
return { x, y };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The room the stack needs, in cover widths: across, at rest, open, and
|
|
206
|
+
* open with the front cover lifted (a cover lifted at either end pushes its
|
|
207
|
+
* neighbours a little past it); down, with any cover lifted. The component
|
|
208
|
+
* sizes its box to it.
|
|
209
|
+
*
|
|
210
|
+
* @param {Layout} layout The layout.
|
|
211
|
+
* @param {number} n How many covers.
|
|
212
|
+
* @param {number} [spread=1] The spread.
|
|
213
|
+
* @param {number} [ratio=1] A cover's width over its height.
|
|
214
|
+
* @returns {{ wide: number, tall: number }} The box's width and height, in cover widths.
|
|
215
|
+
*/
|
|
216
|
+
export function stackBounds(layout, n, spread = 1, ratio = 1) {
|
|
217
|
+
if (n <= 0) return { wide: 1, tall: 1 / ratio };
|
|
218
|
+
/**
|
|
219
|
+
* Every pose of the stack in some states.
|
|
220
|
+
*
|
|
221
|
+
* @param {PoseState[]} states The states.
|
|
222
|
+
* @returns {Pose[]} Their poses, all together.
|
|
223
|
+
*/
|
|
224
|
+
const posesIn = (states) => states.flatMap((state) => stackPoses(layout, n, state));
|
|
225
|
+
const settled = [{ spread }, { spread, open: true }, { spread, open: true, lifted: Math.floor(frontOf(layout, n)) }];
|
|
226
|
+
const lifts = Array.from({ length: n }, (_, lifted) => ({ spread, open: true, lifted }));
|
|
227
|
+
const x = reach(posesIn(settled), ratio).x;
|
|
228
|
+
const y = reach(posesIn([...settled, ...lifts]), ratio).y;
|
|
229
|
+
// A margin for what the box above leaves out: perspective, the lean and the float.
|
|
230
|
+
const margin = 1.05;
|
|
231
|
+
return { wide: +(x * 2 * margin).toFixed(3), tall: +(y * 2 * margin).toFixed(3) };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* A pose as a CSS transform, in `--ps-w` (the cover's width). The list of
|
|
236
|
+
* functions is the same for every pose, so the browser eases between any
|
|
237
|
+
* two.
|
|
238
|
+
*
|
|
239
|
+
* @param {Pose} pose The pose.
|
|
240
|
+
* @returns {string} The transform.
|
|
241
|
+
*/
|
|
242
|
+
export function poseTransform({ x, y, z, rx, ry, rz, s }) {
|
|
243
|
+
/**
|
|
244
|
+
* A length in cover widths.
|
|
245
|
+
*
|
|
246
|
+
* @param {number} v The length, in cover widths.
|
|
247
|
+
* @returns {string} It in CSS.
|
|
248
|
+
*/
|
|
249
|
+
const w = (v) => `calc(var(--ps-w) * ${+v.toFixed(4)})`;
|
|
250
|
+
/**
|
|
251
|
+
* An angle in degrees.
|
|
252
|
+
*
|
|
253
|
+
* @param {number} v The angle.
|
|
254
|
+
* @returns {string} It in CSS.
|
|
255
|
+
*/
|
|
256
|
+
const deg = (v) => `${+v.toFixed(2)}deg`;
|
|
257
|
+
return `translate3d(${w(x)}, ${w(y)}, ${w(z)}) rotateY(${deg(ry)}) rotateX(${deg(rx)}) rotateZ(${deg(rz)}) scale(${+s.toFixed(4)})`;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* A pose's depth of field as a CSS filter: covers out of focus dim and
|
|
262
|
+
* lose a little colour, and only the furthest blur, a little, so the cost
|
|
263
|
+
* stays small.
|
|
264
|
+
*
|
|
265
|
+
* @param {Pose} pose The pose.
|
|
266
|
+
* @param {number} depth How strong the depth of field is, 0–1.
|
|
267
|
+
* @returns {string} The filter; the same functions every time, so it eases.
|
|
268
|
+
*/
|
|
269
|
+
export function poseFilter({ blur }, depth) {
|
|
270
|
+
const d = Math.min(1, Math.max(0, depth)) * blur;
|
|
271
|
+
const soft = Math.max(0, d - 0.35) * 3;
|
|
272
|
+
return `brightness(${+(1 - d * 0.55).toFixed(3)}) saturate(${+(1 - d * 0.35).toFixed(3)}) blur(${+soft.toFixed(2)}px)`;
|
|
273
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { GL } from './gl.js';
|
|
2
|
+
|
|
3
|
+
/** A running full-canvas effect: its context, canvas and program. */
|
|
4
|
+
export interface Stage {
|
|
5
|
+
gl: GL;
|
|
6
|
+
canvas: HTMLCanvasElement;
|
|
7
|
+
program: WebGLProgram;
|
|
8
|
+
uniform(name: string): WebGLUniformLocation | null;
|
|
9
|
+
/** True with prefers-reduced-motion or the `still` option: one frame, redrawn on changes. */
|
|
10
|
+
still: boolean;
|
|
11
|
+
/** The pixel ratio the canvas is drawn at. */
|
|
12
|
+
ratio: number;
|
|
13
|
+
/** Draws a frame now. */
|
|
14
|
+
redraw(): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface StageOptions {
|
|
18
|
+
/** The fragment shader; `uRes` (canvas size in pixels) is set for it. */
|
|
19
|
+
fragment: string;
|
|
20
|
+
/** A vertex shader of its own (position attribute at location 0). Default passes the triangle through. */
|
|
21
|
+
vertex?: string;
|
|
22
|
+
/** Transparent (premultiplied) or opaque canvas. Default true. */
|
|
23
|
+
alpha?: boolean;
|
|
24
|
+
/** Share of the pixel ratio to draw at. Default 1. */
|
|
25
|
+
scale?: number;
|
|
26
|
+
/** Lower the resolution while frames run slow (a weak GPU), down to half; a step that does not help is taken back and adjusting stops. Default true. */
|
|
27
|
+
adaptive?: boolean;
|
|
28
|
+
/** Accept WebGL 1 without WebGL 2. Default true. */
|
|
29
|
+
webgl1?: boolean;
|
|
30
|
+
/** Smooth the edges of geometry drawn into it (multisampling): for effects that draw meshes. Default false. */
|
|
31
|
+
antialias?: boolean;
|
|
32
|
+
/** One frame (redrawn after resizes and redraws), never the loop, as with reduced motion. Default false. */
|
|
33
|
+
still?: boolean;
|
|
34
|
+
/** Once, after the context is made; returns a clean-up, or false when the effect cannot run here (the canvas is removed). */
|
|
35
|
+
setup?(stage: Stage): (() => void) | false | void;
|
|
36
|
+
/** After each resize. */
|
|
37
|
+
resize?(stage: Stage): void;
|
|
38
|
+
/** Each frame, before the program is bound (free to run other passes). */
|
|
39
|
+
update?(stage: Stage, now: number, dt: number): void;
|
|
40
|
+
/** Each frame, with the program bound. */
|
|
41
|
+
uniforms?(stage: Stage, now: number, dt: number): void;
|
|
42
|
+
/** Each frame, after the effect's pass: draw on top of it. */
|
|
43
|
+
after?(stage: Stage, now: number, dt: number): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Starts a full-canvas effect in `el`'s canvas. `stage` is null without WebGL (the canvas is removed). */
|
|
47
|
+
export function createStage(el: HTMLElement, options: StageOptions): { stage: Stage | null; stop(): void };
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/stage
|
|
3
|
+
*
|
|
4
|
+
* The set-up every full-canvas effect shares, so each effect is only its
|
|
5
|
+
* shader and what feeds it: a WebGL context on the element's canvas, the
|
|
6
|
+
* effect's program on a full-screen triangle, the canvas kept the size of
|
|
7
|
+
* the element at a capped pixel ratio, frames from the page's one loop
|
|
8
|
+
* (or one still frame with prefers-reduced-motion or `still`), and everything torn
|
|
9
|
+
* down when the effect stops.
|
|
10
|
+
*
|
|
11
|
+
* Each frame: `update` (free to use the context for other passes), then
|
|
12
|
+
* the effect's program is bound with `uRes` set, `uniforms` sets the
|
|
13
|
+
* rest, the triangle is drawn, and `after` draws anything on top.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { getContext, createProgram, bindTriangle } from './gl.js';
|
|
17
|
+
import { pixelRatio, prefersReducedMotion } from './device.js';
|
|
18
|
+
import { animate } from './loop.js';
|
|
19
|
+
|
|
20
|
+
/** Frames between looks at the frame rate, the smoothed frame time counted slow (about 45 fps), each step down, and the lowest share of the pixel ratio a step may reach. */
|
|
21
|
+
const ADAPT_WINDOW = 60;
|
|
22
|
+
const ADAPT_SLOW = 1 / 45;
|
|
23
|
+
const ADAPT_STEP = 0.8;
|
|
24
|
+
const ADAPT_FLOOR = 0.5;
|
|
25
|
+
|
|
26
|
+
/** The vertex shader for a full-screen pass that works in `gl_FragCoord`. */
|
|
27
|
+
const CLIP = `
|
|
28
|
+
attribute vec2 p;
|
|
29
|
+
void main() { gl_Position = vec4(p, 0.0, 1.0); }
|
|
30
|
+
`;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {object} Stage
|
|
34
|
+
* @property {WebGL2RenderingContext | WebGLRenderingContext} gl The context.
|
|
35
|
+
* @property {HTMLCanvasElement} canvas The canvas.
|
|
36
|
+
* @property {WebGLProgram} program The effect's program.
|
|
37
|
+
* @property {(name: string) => WebGLUniformLocation | null} uniform A uniform of the effect's program.
|
|
38
|
+
* @property {boolean} still True with prefers-reduced-motion or the `still` option: one frame, redrawn on changes.
|
|
39
|
+
* @property {number} ratio The pixel ratio the canvas is drawn at.
|
|
40
|
+
* @property {() => void} redraw Draws a frame now (for still stages after a change).
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @typedef {object} StageOptions
|
|
45
|
+
* @property {string} fragment The effect's fragment shader (`uRes`, the canvas size in pixels, is set for it).
|
|
46
|
+
* @property {string} [vertex] A vertex shader of its own (its position attribute at location 0); the default passes the triangle through.
|
|
47
|
+
* @property {boolean} [alpha=true] A transparent canvas (premultiplied), or an opaque one.
|
|
48
|
+
* @property {number} [scale=1] Share of the pixel ratio to draw at.
|
|
49
|
+
* @property {boolean} [adaptive=true] Lower the resolution while frames run slow (a weak GPU), down to half; a step that does not help is taken back and adjusting stops.
|
|
50
|
+
* @property {boolean} [webgl1=true] Accept WebGL 1 where there is no WebGL 2.
|
|
51
|
+
* @property {boolean} [antialias=false] Smooth the edges of geometry drawn into it (multisampling): for effects that draw meshes.
|
|
52
|
+
* @property {boolean} [still=false] Draw one frame (again after each resize and
|
|
53
|
+
* redraw) and never join the loop, as with prefers-reduced-motion: for an
|
|
54
|
+
* effect used as wallpaper.
|
|
55
|
+
* @property {(stage: Stage) => (() => void) | false | void} [setup] Once, after the context is made;
|
|
56
|
+
* returns a clean-up, or false when the effect cannot run here after all (the canvas is removed).
|
|
57
|
+
* @property {(stage: Stage) => void} [resize] After the canvas is resized.
|
|
58
|
+
* @property {(stage: Stage, now: number, dt: number) => void} [update] Each frame, before the effect's program is bound.
|
|
59
|
+
* @property {(stage: Stage, now: number, dt: number) => void} [uniforms] Each frame, with the program bound: set its uniforms.
|
|
60
|
+
* @property {(stage: Stage, now: number, dt: number) => void} [after] Each frame, after the effect's pass: draw on top of it.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Starts a full-canvas effect on an element (its first canvas is drawn into).
|
|
65
|
+
*
|
|
66
|
+
* @param {HTMLElement} el The element.
|
|
67
|
+
* @param {StageOptions} options The effect.
|
|
68
|
+
* @returns {{ stage: Stage | null, stop: () => void }} The stage (null without
|
|
69
|
+
* WebGL, when the canvas is removed), and a function that stops it.
|
|
70
|
+
*/
|
|
71
|
+
export function createStage(el, options) {
|
|
72
|
+
const { fragment, alpha = true, scale = 1, webgl1 = true, adaptive = true, antialias = false } = options;
|
|
73
|
+
const canvas = el.querySelector('canvas');
|
|
74
|
+
const gl = canvas && getContext(canvas, { alpha, premultipliedAlpha: true, antialias }, webgl1);
|
|
75
|
+
const compiled = gl && createProgram(gl, options.vertex ?? CLIP, fragment);
|
|
76
|
+
if (!canvas || !gl || !compiled) {
|
|
77
|
+
canvas?.remove();
|
|
78
|
+
gl?.getExtension('WEBGL_lose_context')?.loseContext();
|
|
79
|
+
return { stage: null, stop: () => {} };
|
|
80
|
+
}
|
|
81
|
+
const triangle = bindTriangle(gl);
|
|
82
|
+
/**
|
|
83
|
+
* How the frame rate is going: the share of the pixel ratio drawn at
|
|
84
|
+
* now, the smoothed frame time, frames since the last look, the frame
|
|
85
|
+
* time before the last step down (0 when there was none), and whether
|
|
86
|
+
* it has stopped adjusting.
|
|
87
|
+
*/
|
|
88
|
+
const quality = { share: 1, ema: 0, frames: 0, before: 0, settled: false };
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Lowers the resolution while frames run slow, a step at a time. A step
|
|
92
|
+
* that buys nothing means the slowness is not this canvas's (a 30 Hz
|
|
93
|
+
* screen, a busy page): it is taken back, and adjusting stops.
|
|
94
|
+
*
|
|
95
|
+
* @param {number} dt Seconds since the last frame.
|
|
96
|
+
*/
|
|
97
|
+
const adapt = (dt) => {
|
|
98
|
+
quality.ema = quality.ema ? quality.ema * 0.9 + dt * 0.1 : dt;
|
|
99
|
+
if (quality.settled || ++quality.frames < ADAPT_WINDOW) return;
|
|
100
|
+
quality.frames = 0;
|
|
101
|
+
if (quality.before) {
|
|
102
|
+
const helped = quality.ema < quality.before * 0.9;
|
|
103
|
+
quality.before = 0;
|
|
104
|
+
if (!helped) {
|
|
105
|
+
quality.share = Math.min(1, quality.share / ADAPT_STEP);
|
|
106
|
+
quality.settled = true;
|
|
107
|
+
size();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (quality.ema > ADAPT_SLOW && quality.share > ADAPT_FLOOR) {
|
|
112
|
+
quality.before = quality.ema;
|
|
113
|
+
quality.share = Math.max(ADAPT_FLOOR, quality.share * ADAPT_STEP);
|
|
114
|
+
size();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Draws one frame.
|
|
120
|
+
*
|
|
121
|
+
* @param {number} now The time, ms.
|
|
122
|
+
* @param {number} dt Seconds since the last frame.
|
|
123
|
+
*/
|
|
124
|
+
const render = (now, dt) => {
|
|
125
|
+
options.update?.(stage, now, dt);
|
|
126
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
127
|
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
128
|
+
bindTriangle(gl, triangle);
|
|
129
|
+
gl.useProgram(compiled.program);
|
|
130
|
+
gl.uniform2f(compiled.uniform('uRes'), canvas.width, canvas.height);
|
|
131
|
+
options.uniforms?.(stage, now, dt);
|
|
132
|
+
if (alpha) {
|
|
133
|
+
gl.clearColor(0, 0, 0, 0);
|
|
134
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
135
|
+
}
|
|
136
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
137
|
+
options.after?.(stage, now, dt);
|
|
138
|
+
if (adaptive && dt > 0) adapt(dt);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** @type {Stage} */
|
|
142
|
+
const stage = {
|
|
143
|
+
gl,
|
|
144
|
+
canvas,
|
|
145
|
+
program: compiled.program,
|
|
146
|
+
uniform: compiled.uniform,
|
|
147
|
+
still: options.still === true || prefersReducedMotion(),
|
|
148
|
+
ratio: pixelRatio(scale),
|
|
149
|
+
redraw: () => render(performance.now(), 0),
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
gl.useProgram(compiled.program);
|
|
153
|
+
const cleanup = options.setup?.(stage);
|
|
154
|
+
if (cleanup === false) {
|
|
155
|
+
// It cannot run here: the canvas goes, and its GPU context with it.
|
|
156
|
+
canvas.remove();
|
|
157
|
+
gl.getExtension('WEBGL_lose_context')?.loseContext();
|
|
158
|
+
return { stage: null, stop: () => {} };
|
|
159
|
+
}
|
|
160
|
+
gl.useProgram(compiled.program);
|
|
161
|
+
|
|
162
|
+
/** Sizes the canvas to the element, at the capped pixel ratio (lowered while frames run slow). */
|
|
163
|
+
const size = () => {
|
|
164
|
+
stage.ratio = pixelRatio(scale) * quality.share;
|
|
165
|
+
canvas.width = Math.max(1, Math.round(el.clientWidth * stage.ratio));
|
|
166
|
+
canvas.height = Math.max(1, Math.round(el.clientHeight * stage.ratio));
|
|
167
|
+
gl.useProgram(compiled.program);
|
|
168
|
+
options.resize?.(stage);
|
|
169
|
+
if (stage.still) stage.redraw();
|
|
170
|
+
};
|
|
171
|
+
const resizer = new ResizeObserver(size);
|
|
172
|
+
resizer.observe(el);
|
|
173
|
+
size();
|
|
174
|
+
|
|
175
|
+
const stopLoop = stage.still ? () => {} : animate(el, render);
|
|
176
|
+
return {
|
|
177
|
+
stage,
|
|
178
|
+
stop: () => {
|
|
179
|
+
stopLoop();
|
|
180
|
+
resizer.disconnect();
|
|
181
|
+
cleanup?.();
|
|
182
|
+
// Gone from the page: give the GPU context back now rather than
|
|
183
|
+
// whenever it is collected, as a browser keeps only so many.
|
|
184
|
+
if (!el.isConnected) gl.getExtension('WEBGL_lose_context')?.loseContext();
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** A flock of particles, each held to a home by a spring. */
|
|
2
|
+
export interface Swarm {
|
|
3
|
+
count: number;
|
|
4
|
+
/** Positions, x, y pairs. */
|
|
5
|
+
pos: Float32Array;
|
|
6
|
+
/** Velocities, x, y pairs, units a second. */
|
|
7
|
+
vel: Float32Array;
|
|
8
|
+
/** Homes, x, y pairs. */
|
|
9
|
+
home: Float32Array;
|
|
10
|
+
/** How tightly each is held to its home, 0–1; a few are loose. */
|
|
11
|
+
grip: Float32Array;
|
|
12
|
+
/** Seconds stepped since it was last dispersed. */
|
|
13
|
+
time: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** What moves a swarm on a step. */
|
|
17
|
+
export interface SwarmForces {
|
|
18
|
+
/** The springs' stiffness at full grip, a second squared. Default 40. */
|
|
19
|
+
pull?: number;
|
|
20
|
+
/** How hard the curl-noise flow pushes, units a second squared. Default 0.5. */
|
|
21
|
+
flow?: number;
|
|
22
|
+
/** The flow's swirls a unit. Default 6. */
|
|
23
|
+
scale?: number;
|
|
24
|
+
/** How fast a particle loses speed, a share a second. Default 4. */
|
|
25
|
+
damping?: number;
|
|
26
|
+
/** Each particle's chance a second of trading homes with a neighbour. Default 0.05. */
|
|
27
|
+
wander?: number;
|
|
28
|
+
/** Seconds the springs take to come on after `disperse`. Default 2.2. */
|
|
29
|
+
arrive?: number;
|
|
30
|
+
/** A pointer to flee: where, how far it reaches, how hard it pushes. */
|
|
31
|
+
pointer?: { x: number; y: number; radius: number; force: number } | null;
|
|
32
|
+
/** The random source. Default Math.random. */
|
|
33
|
+
random?: () => number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Makes a swarm of `count` particles at the origin. */
|
|
37
|
+
export function createSwarm(count: number, random?: () => number): Swarm;
|
|
38
|
+
|
|
39
|
+
/** Throws the swarm into a loose cloud round (x, y), moving every which way, and restarts its clock. */
|
|
40
|
+
export function disperse(swarm: Swarm, x: number, y: number, spread: number, random?: () => number): void;
|
|
41
|
+
|
|
42
|
+
/** Gives the particles their homes (x, y pairs), ordered so particles close in the order have homes close on the shape. */
|
|
43
|
+
export function setHomes(swarm: Swarm, homes: Float32Array): void;
|
|
44
|
+
|
|
45
|
+
/** Puts every particle at its home, at rest, for a still frame. */
|
|
46
|
+
export function settle(swarm: Swarm): void;
|
|
47
|
+
|
|
48
|
+
/** Moves the swarm on by `dt` seconds: springs, flow, the pointer, damping. */
|
|
49
|
+
export function stepSwarm(swarm: Swarm, dt: number, forces?: SwarmForces): void;
|