@arraypress/effects-astro 0.0.0-stage → 0.1.1
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 +303 -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,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/liquid-metal
|
|
3
|
+
*
|
|
4
|
+
* A logo made of liquid chrome. At rest it is a still, polished slab of
|
|
5
|
+
* metal with rounded edges, reflecting a studio that turns slowly round
|
|
6
|
+
* it, so its reflections glide. Touch it and it behaves like liquid
|
|
7
|
+
* metal: the pointer dents it and ripples run out from the dent; a stroke
|
|
8
|
+
* through it parts it, and it flows back behind; a click splashes it, a
|
|
9
|
+
* crater with a ring wave and droplets thrown out that fall and find their
|
|
10
|
+
* way back in. After any of it, surface tension pulls it back into the
|
|
11
|
+
* logo's exact shape, with a last small wobble.
|
|
12
|
+
*
|
|
13
|
+
* - The shape is a distance field (src/runtime/shape.js) over the logo's
|
|
14
|
+
* box: its bevel, and the rest the metal returns to.
|
|
15
|
+
* - The surface is a wave simulation on the GPU (liquid-metal/wave.js, on
|
|
16
|
+
* height-field.js): heights and speeds over the box, a damped wave
|
|
17
|
+
* equation with a spring back to rest. The waves tilt the surface and
|
|
18
|
+
* move its outline.
|
|
19
|
+
* - The droplets are worked out on the CPU (liquid-metal/beads.js).
|
|
20
|
+
* - The chrome is a reflection of a studio made in the shader
|
|
21
|
+
* (liquid-metal/shaders.js): sky, horizon, floor, softboxes and a key
|
|
22
|
+
* light, with Fresnel, the metal's colour (`tint`) and a palette's tint.
|
|
23
|
+
*
|
|
24
|
+
* Transparent round the logo, with a soft shadow under it. The simulation
|
|
25
|
+
* sleeps once the metal has settled, and only the render runs. With
|
|
26
|
+
* prefers-reduced-motion it draws one still frame; without float render
|
|
27
|
+
* targets it is the still, turning chrome with nothing to push. Without
|
|
28
|
+
* WebGL it removes its canvas.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { createStage } from './stage.js';
|
|
32
|
+
import { drawShape, distanceField, blurField } from './shape.js';
|
|
33
|
+
import { canHover, isMobileGpu } from './device.js';
|
|
34
|
+
import { pointerIn, trail, pressAt } from './pointer.js';
|
|
35
|
+
import { watchColors } from './mount.js';
|
|
36
|
+
import { pickColors } from './palettes.js';
|
|
37
|
+
import { RENDER, MAX_BEADS } from './liquid-metal/shaders.js';
|
|
38
|
+
import { MAX_PUSHES } from './height-field.js';
|
|
39
|
+
import { createWave } from './liquid-metal/wave.js';
|
|
40
|
+
import { createField } from './liquid-metal/field.js';
|
|
41
|
+
import { fieldSampler, splash, stepBeads, packBeads } from './liquid-metal/beads.js';
|
|
42
|
+
import { metalColor, studioTints } from './liquid-metal/look.js';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @typedef {object} LiquidMetalConfig
|
|
46
|
+
* @property {string} [text] A word to make of metal.
|
|
47
|
+
* @property {string} [font] Its CSS font, without the size (default: the page's, bold).
|
|
48
|
+
* @property {string} [src] An image (an SVG logo) to make of metal instead.
|
|
49
|
+
* @property {{ x: number, y: number, width: number, height: number }} [area]
|
|
50
|
+
* Where the logo sits, each 0–1 from the top left (default: the right half).
|
|
51
|
+
* @property {number} [size=1] How much of its area the logo fills: 1 fits it, less shrinks it about the area's middle.
|
|
52
|
+
* @property {string} [tint='chrome'] The metal: `chrome`, `gold`, `rose-gold`, `copper`, `black-chrome`, or a CSS hex colour.
|
|
53
|
+
* @property {string[]} [colors] The studio's colours, reflected in the metal: the sky, the horizon, the floor (default a neutral studio).
|
|
54
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`: it tints the studio.
|
|
55
|
+
* @property {number} [bevel=1] How wide its rounded edges are.
|
|
56
|
+
* @property {number} [viscosity=1] How thick the metal is: more settles sooner, with smaller ripples.
|
|
57
|
+
* @property {number} [tension=1] How hard it pulls back into shape: more reforms sooner, with a quicker wobble.
|
|
58
|
+
* @property {number} [ripple=1] How hard the pointer pushes into it.
|
|
59
|
+
* @property {boolean} [interactive=true] Whether it reacts to the pointer at all.
|
|
60
|
+
* @property {number} [shadow=1] The soft shadow under it; 0 for none.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
/** Where the logo sits by default: the right half, so copy can sit on the left. */
|
|
64
|
+
const AREA = { x: 0.46, y: 0.08, width: 0.5, height: 0.84 };
|
|
65
|
+
/** The margin round the logo the simulation covers (room for swells and droplets), a share of its smaller side. */
|
|
66
|
+
const MARGIN = 0.14;
|
|
67
|
+
/** The rounded edge's width, a share of the logo's height. */
|
|
68
|
+
const BEVEL = 0.07;
|
|
69
|
+
/** The distance field's width at most, pixels: it is smooth, so the GPU's filtering does the rest. */
|
|
70
|
+
const FIELD = 1024;
|
|
71
|
+
/** Simulation cells along the box's longer side. */
|
|
72
|
+
const CELLS = 256;
|
|
73
|
+
const CELLS_MOBILE = 160;
|
|
74
|
+
/** Simulation steps a second, and the most in one frame. */
|
|
75
|
+
const RATE = 180;
|
|
76
|
+
const MAX_STEPS = 6;
|
|
77
|
+
/** How long the surface runs after the last touch before it sleeps, ms. */
|
|
78
|
+
const SETTLE = 7000;
|
|
79
|
+
/** The pointer's reach, box heights. */
|
|
80
|
+
const REACH = 0.035;
|
|
81
|
+
/** Pushes, per frame at 60 fps: a hovering pointer, a pressed one, a stroke (per box height travelled), a click, a landing droplet (per box height of its radius). */
|
|
82
|
+
const HOVER = 0.0035;
|
|
83
|
+
const PRESS = 0.016;
|
|
84
|
+
const STROKE = 4;
|
|
85
|
+
const CLICK = 0.42;
|
|
86
|
+
const LAND = 7;
|
|
87
|
+
/** Droplets a click throws (fewer on phones). */
|
|
88
|
+
const DROPLETS = 14;
|
|
89
|
+
/** How fast the studio turns, radians a second. */
|
|
90
|
+
const SPIN = 0.05;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The logo's box: its area (scaled by `size` about its middle), grown by a
|
|
94
|
+
* margin for the swells and droplets, and kept on the canvas.
|
|
95
|
+
*
|
|
96
|
+
* @param {{ x: number, y: number, width: number, height: number }} area Where the logo sits, 0–1 from the top left.
|
|
97
|
+
* @param {number} size How much of its area it fills.
|
|
98
|
+
* @param {number} width The canvas's width, pixels.
|
|
99
|
+
* @param {number} height The canvas's height, pixels.
|
|
100
|
+
* @returns {{ left: number, top: number, width: number, height: number, shape: { x: number, y: number, width: number, height: number } }}
|
|
101
|
+
* The box in canvas pixels from the top left, and the logo's area as a share of the box.
|
|
102
|
+
*/
|
|
103
|
+
export function logoBox(area, size, width, height) {
|
|
104
|
+
const cx = (area.x + area.width / 2) * width;
|
|
105
|
+
const cy = (area.y + area.height / 2) * height;
|
|
106
|
+
const w = area.width * width * size;
|
|
107
|
+
const h = area.height * height * size;
|
|
108
|
+
const margin = Math.min(w, h) * MARGIN;
|
|
109
|
+
const left = Math.max(0, Math.floor(cx - w / 2 - margin));
|
|
110
|
+
const top = Math.max(0, Math.floor(cy - h / 2 - margin));
|
|
111
|
+
const boxWidth = Math.max(1, Math.min(width, Math.ceil(cx + w / 2 + margin)) - left);
|
|
112
|
+
const boxHeight = Math.max(1, Math.min(height, Math.ceil(cy + h / 2 + margin)) - top);
|
|
113
|
+
return {
|
|
114
|
+
left,
|
|
115
|
+
top,
|
|
116
|
+
width: boxWidth,
|
|
117
|
+
height: boxHeight,
|
|
118
|
+
shape: { x: (cx - w / 2 - left) / boxWidth, y: (cy - h / 2 - top) / boxHeight, width: w / boxWidth, height: h / boxHeight },
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Starts liquid metal on an element: a transparent canvas inside it, filling it.
|
|
124
|
+
*
|
|
125
|
+
* @param {HTMLElement} el The element (its first canvas is drawn into).
|
|
126
|
+
* @param {LiquidMetalConfig} config The settings.
|
|
127
|
+
* @returns {() => void} Stops it.
|
|
128
|
+
*/
|
|
129
|
+
export function liquidMetal(el, config) {
|
|
130
|
+
let tints = studioTints(pickColors(config, []));
|
|
131
|
+
const ripple = config.ripple ?? 1;
|
|
132
|
+
const viscosity = config.viscosity ?? 1;
|
|
133
|
+
const motion = {
|
|
134
|
+
speed: 0.3,
|
|
135
|
+
spring: 0.0045 * (config.tension ?? 1),
|
|
136
|
+
keep: 1 - 0.01 * viscosity,
|
|
137
|
+
viscosity: Math.min(0.2, 0.05 * viscosity),
|
|
138
|
+
};
|
|
139
|
+
const start = performance.now();
|
|
140
|
+
/** @type {import('./liquid-metal/wave.js').Wave | null} */
|
|
141
|
+
let wave = null;
|
|
142
|
+
/** @type {import('./liquid-metal/field.js').Field | null} */
|
|
143
|
+
let field = null;
|
|
144
|
+
/** @type {import('./liquid-metal/beads.js').FieldSampler | null} */
|
|
145
|
+
let sample = null;
|
|
146
|
+
/** @type {import('./liquid-metal/beads.js').Bead[]} */
|
|
147
|
+
let beads = [];
|
|
148
|
+
const packed = new Float32Array(MAX_BEADS * 4);
|
|
149
|
+
const pushes = new Float32Array(MAX_PUSHES * 4);
|
|
150
|
+
let pushCount = 0;
|
|
151
|
+
/** The box, as a share of the canvas (from the bottom left), and its size in pixels. */
|
|
152
|
+
const box = { x: 0, y: 0, w: 1, h: 1, px: 1, aspect: 1 };
|
|
153
|
+
let pressed = false;
|
|
154
|
+
let awake = false;
|
|
155
|
+
let lastStir = 0;
|
|
156
|
+
let backlog = 0;
|
|
157
|
+
/** Bumped on each new layout, so a slow shape drawn for an old one is dropped. */
|
|
158
|
+
let generation = 0;
|
|
159
|
+
/** The layout the shape was last drawn for, and the field's width then. */
|
|
160
|
+
const built = { layout: '', width: 0 };
|
|
161
|
+
const path = trail(el, { step: 0.008 });
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Queues a push for the next step.
|
|
165
|
+
*
|
|
166
|
+
* @param {number} x Across, box heights.
|
|
167
|
+
* @param {number} y Up, box heights.
|
|
168
|
+
* @param {number} radius Its reach, box heights.
|
|
169
|
+
* @param {number} strength How hard.
|
|
170
|
+
*/
|
|
171
|
+
const push = (x, y, radius, strength) => {
|
|
172
|
+
if (pushCount >= MAX_PUSHES) return;
|
|
173
|
+
pushes.set([x, y, radius, strength], pushCount * 4);
|
|
174
|
+
pushCount++;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* A point on the element in box heights.
|
|
179
|
+
*
|
|
180
|
+
* @param {number} x Across the element, 0–1.
|
|
181
|
+
* @param {number} y Up the element, 0–1.
|
|
182
|
+
* @returns {{ x: number, y: number } | null} The point, or null outside the box.
|
|
183
|
+
*/
|
|
184
|
+
const toBox = (x, y) => {
|
|
185
|
+
const u = (x - box.x) / box.w;
|
|
186
|
+
const v = (y - box.y) / box.h;
|
|
187
|
+
return u < 0 || v < 0 || u > 1 || v > 1 ? null : { x: u * box.aspect, y: v };
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A press: a splash where it lands on the metal, and the metal held
|
|
192
|
+
* down while the button is.
|
|
193
|
+
*
|
|
194
|
+
* @param {PointerEvent} e The press.
|
|
195
|
+
*/
|
|
196
|
+
const down = (e) => {
|
|
197
|
+
const at = pressAt(el, e);
|
|
198
|
+
const point = at && toBox(at.x, at.y);
|
|
199
|
+
if (!point || !sample || sample(point.x, point.y).d < -0.02) return;
|
|
200
|
+
pressed = true;
|
|
201
|
+
push(point.x, point.y, REACH * 1.7, CLICK * ripple);
|
|
202
|
+
const room = MAX_BEADS - beads.length;
|
|
203
|
+
beads.push(...splash(point.x, point.y, Math.min(room, isMobileGpu() ? DROPLETS / 2 : DROPLETS), Math.min(1.5, ripple)));
|
|
204
|
+
};
|
|
205
|
+
/** The button let go. */
|
|
206
|
+
const up = () => {
|
|
207
|
+
pressed = false;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const { stage, stop } = createStage(el, {
|
|
211
|
+
fragment: RENDER,
|
|
212
|
+
alpha: true,
|
|
213
|
+
setup({ gl, uniform, still }) {
|
|
214
|
+
field = createField(gl);
|
|
215
|
+
// A flat surface, for when there is no simulation.
|
|
216
|
+
const flat = gl.createTexture();
|
|
217
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
218
|
+
gl.bindTexture(gl.TEXTURE_2D, flat);
|
|
219
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
220
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(4));
|
|
221
|
+
|
|
222
|
+
gl.uniform1i(uniform('uSdf'), 1);
|
|
223
|
+
gl.uniform1i(uniform('uState'), 2);
|
|
224
|
+
gl.uniform1f(uniform('uSpread'), 0.1);
|
|
225
|
+
gl.uniform3fv(uniform('uMetal'), metalColor(config.tint));
|
|
226
|
+
gl.uniform1f(uniform('uShadow'), config.shadow ?? 1);
|
|
227
|
+
gl.uniform1f(uniform('uWave'), 0.5);
|
|
228
|
+
|
|
229
|
+
const interactive = config.interactive !== false && !still;
|
|
230
|
+
if (interactive) {
|
|
231
|
+
wave = createWave(gl);
|
|
232
|
+
if (wave) {
|
|
233
|
+
document.addEventListener('pointerdown', down, { passive: true });
|
|
234
|
+
document.addEventListener('pointerup', up, { passive: true });
|
|
235
|
+
document.addEventListener('pointercancel', up, { passive: true });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return () => {
|
|
239
|
+
document.removeEventListener('pointerdown', down);
|
|
240
|
+
document.removeEventListener('pointerup', up);
|
|
241
|
+
document.removeEventListener('pointercancel', up);
|
|
242
|
+
wave?.dispose();
|
|
243
|
+
wave = null;
|
|
244
|
+
field?.dispose();
|
|
245
|
+
gl.deleteTexture(flat);
|
|
246
|
+
};
|
|
247
|
+
},
|
|
248
|
+
resize({ gl, uniform, canvas, program }) {
|
|
249
|
+
const area = config.area ?? AREA;
|
|
250
|
+
const fit = logoBox(area, config.size ?? 1, canvas.width, canvas.height);
|
|
251
|
+
const aspect = fit.width / fit.height;
|
|
252
|
+
Object.assign(box, {
|
|
253
|
+
x: fit.left / canvas.width,
|
|
254
|
+
y: 1 - (fit.top + fit.height) / canvas.height,
|
|
255
|
+
w: fit.width / canvas.width,
|
|
256
|
+
h: fit.height / canvas.height,
|
|
257
|
+
px: fit.height,
|
|
258
|
+
aspect,
|
|
259
|
+
});
|
|
260
|
+
gl.uniform4f(uniform('uBox'), box.x, box.y, box.w, box.h);
|
|
261
|
+
gl.uniform1f(uniform('uAspect'), aspect);
|
|
262
|
+
gl.uniform1f(uniform('uPx'), 1 / fit.height);
|
|
263
|
+
// The horizon crosses a little above the logo's middle, and the
|
|
264
|
+
// face's reflection sweeps about a third of the way to the zenith.
|
|
265
|
+
gl.uniform2f(uniform('uCentre'), (fit.shape.x + fit.shape.width / 2) * aspect, 1 - (fit.shape.y + fit.shape.height * 0.45));
|
|
266
|
+
gl.uniform1f(uniform('uLens'), 0.6 / fit.shape.height);
|
|
267
|
+
const width = Math.min(isMobileGpu() ? FIELD / 2 : FIELD, fit.width);
|
|
268
|
+
const height = Math.max(1, Math.round((width * fit.height) / fit.width));
|
|
269
|
+
// Only the pixel density changed (the stage easing its resolution on a
|
|
270
|
+
// slow GPU): the shape, the surface and the droplets all live in the
|
|
271
|
+
// box, so they stay as they are, unless the field is now too coarse.
|
|
272
|
+
const layout = [box.x, box.y, box.w, box.h].map((v) => v.toFixed(3)).join();
|
|
273
|
+
if (layout === built.layout && width <= built.width * 1.25) return;
|
|
274
|
+
built.layout = layout;
|
|
275
|
+
built.width = width;
|
|
276
|
+
const bevel = BEVEL * fit.shape.height * (config.bevel ?? 1);
|
|
277
|
+
const spread = Math.max(bevel * 3, 0.06);
|
|
278
|
+
wave?.resize(isMobileGpu() ? CELLS_MOBILE : CELLS, aspect);
|
|
279
|
+
awake = false;
|
|
280
|
+
beads = [];
|
|
281
|
+
sample = null;
|
|
282
|
+
const mine = ++generation;
|
|
283
|
+
drawShape({ text: config.text, font: config.font, src: config.src, area: fit.shape, padding: 0.06 }, width, height)
|
|
284
|
+
.then((mask) => {
|
|
285
|
+
if (mine !== generation) return;
|
|
286
|
+
const sdf = blurField(distanceField(mask, Math.max(2, Math.round(spread * height))), width, height, width / 400);
|
|
287
|
+
field?.upload(sdf, width, height);
|
|
288
|
+
sample = fieldSampler(sdf, width, height, spread, aspect);
|
|
289
|
+
wave?.shape({ spread, bevel, bulge: bevel * 1.6 });
|
|
290
|
+
gl.useProgram(program);
|
|
291
|
+
gl.uniform2f(uniform('uSdfTexel'), 1 / width, 1 / height);
|
|
292
|
+
gl.uniform1f(uniform('uSpread'), spread);
|
|
293
|
+
gl.uniform1f(uniform('uBevel'), bevel);
|
|
294
|
+
gl.uniform1f(uniform('uBulge'), bevel * 1.6);
|
|
295
|
+
if (stage?.still) stage.redraw();
|
|
296
|
+
})
|
|
297
|
+
.catch((error) => console.warn('[effects-astro]', error));
|
|
298
|
+
},
|
|
299
|
+
update({ canvas }, now, dt) {
|
|
300
|
+
if (!wave || !sample) return;
|
|
301
|
+
const time = dt || 1 / 60;
|
|
302
|
+
const frame = time * 60;
|
|
303
|
+
const shape = sample;
|
|
304
|
+
// Strokes: dents along the path, by how far and how fast the pointer went.
|
|
305
|
+
const points = path.read(time);
|
|
306
|
+
for (const p of points) {
|
|
307
|
+
const at = toBox(p.x, p.y);
|
|
308
|
+
if (!at) continue;
|
|
309
|
+
const speed = Math.hypot(p.dx * canvas.width, p.dy * canvas.height) / box.px;
|
|
310
|
+
push(at.x, at.y, REACH, (ripple * STROKE * Math.min(speed, 4) * time) / points.length);
|
|
311
|
+
}
|
|
312
|
+
// The pointer resting on it presses a dimple; held down, a deep one.
|
|
313
|
+
const p = pointerIn(el);
|
|
314
|
+
const at = p.inside && (pressed || canHover()) ? toBox(p.x, p.y) : null;
|
|
315
|
+
if (at && shape(at.x, at.y).d > -REACH) push(at.x, at.y, REACH * (pressed ? 1.2 : 1), ripple * (pressed ? PRESS : HOVER) * frame);
|
|
316
|
+
// Droplets in the air or crawling home; each one landing ripples where it lands.
|
|
317
|
+
beads = stepBeads(beads, time, shape, box.aspect, (x, y, size) => push(x, y, size * 2.4, ripple * LAND * size));
|
|
318
|
+
|
|
319
|
+
if (pushCount || beads.length) {
|
|
320
|
+
awake = true;
|
|
321
|
+
lastStir = now;
|
|
322
|
+
} else if (awake && now - lastStir > SETTLE) {
|
|
323
|
+
// Settled: flatten what is left (far below a pixel) and let the simulation sleep.
|
|
324
|
+
awake = false;
|
|
325
|
+
wave.clear();
|
|
326
|
+
}
|
|
327
|
+
if (!awake) {
|
|
328
|
+
backlog = 0;
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
backlog += time;
|
|
332
|
+
let steps = 0;
|
|
333
|
+
while (backlog >= 1 / RATE && steps < MAX_STEPS) {
|
|
334
|
+
wave.step(motion, pushes, steps === 0 ? pushCount : 0);
|
|
335
|
+
backlog -= 1 / RATE;
|
|
336
|
+
steps++;
|
|
337
|
+
}
|
|
338
|
+
if (steps === MAX_STEPS) backlog = 0;
|
|
339
|
+
if (steps) pushCount = 0;
|
|
340
|
+
},
|
|
341
|
+
uniforms({ gl, uniform, still }, now) {
|
|
342
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
343
|
+
if (wave && awake) gl.bindTexture(gl.TEXTURE_2D, wave.texture());
|
|
344
|
+
gl.uniform1f(uniform('uWaves'), wave && awake ? 1 : 0);
|
|
345
|
+
if (wave) gl.uniform2fv(uniform('uStateTexel'), wave.texel());
|
|
346
|
+
// A still frame is taken where a softbox lies across the face.
|
|
347
|
+
gl.uniform1f(uniform('uSpin'), still ? 0.35 : ((now - start) / 1000) * SPIN);
|
|
348
|
+
gl.uniform3fv(uniform('uSky'), tints[0]);
|
|
349
|
+
gl.uniform3fv(uniform('uBand'), tints[1]);
|
|
350
|
+
gl.uniform3fv(uniform('uFloor'), tints[2]);
|
|
351
|
+
const count = packBeads(beads, packed);
|
|
352
|
+
gl.uniform1i(uniform('uBeadCount'), count);
|
|
353
|
+
if (count) gl.uniform4fv(uniform('uBeads'), packed);
|
|
354
|
+
},
|
|
355
|
+
});
|
|
356
|
+
if (!stage) return stop;
|
|
357
|
+
|
|
358
|
+
const unwatch = watchColors(el, (next) => {
|
|
359
|
+
tints = studioTints(next.colors);
|
|
360
|
+
if (stage.still) stage.redraw();
|
|
361
|
+
});
|
|
362
|
+
return () => {
|
|
363
|
+
stop();
|
|
364
|
+
unwatch();
|
|
365
|
+
};
|
|
366
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registers a frame callback on the page's one animation loop, called while
|
|
3
|
+
* `el` is within 128px of the viewport and the tab is showing. `dt` is the
|
|
4
|
+
* seconds since the last frame (0 after a pause, at most 1/20). Returns a
|
|
5
|
+
* function that unregisters it.
|
|
6
|
+
*/
|
|
7
|
+
export function animate(el: Element, frame: (now: number, dt: number) => void): () => void;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/loop
|
|
3
|
+
*
|
|
4
|
+
* One animation loop for every effect on the page. Each effect registers a
|
|
5
|
+
* frame callback against its element; the loop calls it only while that
|
|
6
|
+
* element is near the screen and the tab is showing, so a page full of
|
|
7
|
+
* effects costs only what is in view.
|
|
8
|
+
*
|
|
9
|
+
* "Near" is within 128px of the viewport: an effect starts a moment before
|
|
10
|
+
* it scrolls in, so it never appears frozen. The loop itself stops when
|
|
11
|
+
* nothing needs it and starts again when something does.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** How far outside the viewport an effect still runs, so it is moving when it arrives. */
|
|
15
|
+
const MARGIN = '128px';
|
|
16
|
+
/** The longest step a frame reports, in seconds: after a stall, effects resume rather than jump. */
|
|
17
|
+
const MAX_STEP = 1 / 20;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {object} Entry
|
|
21
|
+
* @property {Element} el The element whose visibility gates it.
|
|
22
|
+
* @property {(now: number, dt: number) => void} frame The callback.
|
|
23
|
+
* @property {boolean} near Whether the element is near the screen.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** @type {Set<Entry>} */
|
|
27
|
+
const entries = new Set();
|
|
28
|
+
/** @type {IntersectionObserver | null} */
|
|
29
|
+
let observer = null;
|
|
30
|
+
let raf = 0;
|
|
31
|
+
let last = 0;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The shared IntersectionObserver, made on first use (there is none on the
|
|
35
|
+
* server).
|
|
36
|
+
*
|
|
37
|
+
* @returns {IntersectionObserver} The observer.
|
|
38
|
+
*/
|
|
39
|
+
function watcher() {
|
|
40
|
+
observer ??= new IntersectionObserver(
|
|
41
|
+
(changes) => {
|
|
42
|
+
for (const change of changes) {
|
|
43
|
+
for (const entry of entries) if (entry.el === change.target) entry.near = change.isIntersecting;
|
|
44
|
+
}
|
|
45
|
+
wake();
|
|
46
|
+
},
|
|
47
|
+
{ rootMargin: MARGIN },
|
|
48
|
+
);
|
|
49
|
+
return observer;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Is any effect due a frame?
|
|
54
|
+
*
|
|
55
|
+
* @returns {boolean} True when some registered element is near the screen
|
|
56
|
+
* and the tab is showing.
|
|
57
|
+
*/
|
|
58
|
+
function due() {
|
|
59
|
+
if (document.hidden) return false;
|
|
60
|
+
for (const entry of entries) if (entry.near) return true;
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* One turn of the loop: every effect near the screen draws, then the loop
|
|
66
|
+
* books the next turn if anything still needs it.
|
|
67
|
+
*
|
|
68
|
+
* @param {number} now The frame's timestamp.
|
|
69
|
+
*/
|
|
70
|
+
function tick(now) {
|
|
71
|
+
const dt = last ? Math.min((now - last) / 1000, MAX_STEP) : 0;
|
|
72
|
+
last = now;
|
|
73
|
+
for (const entry of entries) if (entry.near) entry.frame(now, dt);
|
|
74
|
+
raf = due() ? requestAnimationFrame(tick) : 0;
|
|
75
|
+
if (!raf) last = 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Starts the loop if it is stopped and something needs it. */
|
|
79
|
+
function wake() {
|
|
80
|
+
if (!raf && due()) raf = requestAnimationFrame(tick);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Registers a frame callback, called while `el` is near the screen.
|
|
85
|
+
*
|
|
86
|
+
* @param {Element} el The element whose visibility gates it.
|
|
87
|
+
* @param {(now: number, dt: number) => void} frame Draws one frame; `dt` is
|
|
88
|
+
* the seconds since the last, 0 on the first after a pause.
|
|
89
|
+
* @returns {() => void} Unregisters it.
|
|
90
|
+
*/
|
|
91
|
+
export function animate(el, frame) {
|
|
92
|
+
/** @type {Entry} */
|
|
93
|
+
const entry = { el, frame, near: false };
|
|
94
|
+
entries.add(entry);
|
|
95
|
+
watcher().observe(el);
|
|
96
|
+
return () => {
|
|
97
|
+
entries.delete(entry);
|
|
98
|
+
if (![...entries].some((e) => e.el === el)) watcher().unobserve(el);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (typeof document !== 'undefined') {
|
|
103
|
+
document.addEventListener('visibilitychange', () => {
|
|
104
|
+
if (document.hidden) {
|
|
105
|
+
cancelAnimationFrame(raf);
|
|
106
|
+
raf = 0;
|
|
107
|
+
last = 0;
|
|
108
|
+
} else {
|
|
109
|
+
wake();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface MagneticConfig {
|
|
2
|
+
/** How far it follows the pointer, a share of the pointer's distance from its centre. Default 0.3. */
|
|
3
|
+
strength?: number;
|
|
4
|
+
/** How far outside the element the pull starts, CSS px. Default 40. */
|
|
5
|
+
reach?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Makes an element lean towards a nearby pointer (`--magnet-x`, `--magnet-y`, and half that for its contents). Returns a function that stops it. */
|
|
9
|
+
export function magnetic(el: HTMLElement, config: MagneticConfig): () => void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/magnetic
|
|
3
|
+
*
|
|
4
|
+
* Elements the pointer pulls on: as the pointer comes near, the element
|
|
5
|
+
* leans a little towards it (`--magnet-x` / `--magnet-y`, px), and its
|
|
6
|
+
* contents a little further (`--magnet-inner-x` / `--magnet-inner-y`), so
|
|
7
|
+
* a button reads as a soft object; it springs back when the pointer
|
|
8
|
+
* leaves. Off with prefers-reduced-motion and without a hovering pointer.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { canHover, prefersReducedMotion } from './device.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {object} MagneticConfig
|
|
15
|
+
* @property {number} [strength=0.3] How far it follows the pointer, as a share of the pointer's distance from its centre.
|
|
16
|
+
* @property {number} [reach=40] How far outside the element the pull starts, CSS px.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Makes an element magnetic.
|
|
21
|
+
*
|
|
22
|
+
* @param {HTMLElement} el The element.
|
|
23
|
+
* @param {MagneticConfig} config The settings.
|
|
24
|
+
* @returns {() => void} Stops it.
|
|
25
|
+
*/
|
|
26
|
+
export function magnetic(el, config) {
|
|
27
|
+
if (prefersReducedMotion() || !canHover()) return () => {};
|
|
28
|
+
const strength = config.strength ?? 0.3;
|
|
29
|
+
const reach = config.reach ?? 40;
|
|
30
|
+
|
|
31
|
+
/** @param {PointerEvent} e The pointer's move anywhere on the page. */
|
|
32
|
+
const move = (e) => {
|
|
33
|
+
const box = el.getBoundingClientRect();
|
|
34
|
+
const near =
|
|
35
|
+
e.clientX > box.left - reach && e.clientX < box.right + reach && e.clientY > box.top - reach && e.clientY < box.bottom + reach;
|
|
36
|
+
const dx = near ? (e.clientX - (box.left + box.width / 2)) * strength : 0;
|
|
37
|
+
const dy = near ? (e.clientY - (box.top + box.height / 2)) * strength : 0;
|
|
38
|
+
el.style.setProperty('--magnet-x', `${dx.toFixed(1)}px`);
|
|
39
|
+
el.style.setProperty('--magnet-y', `${dy.toFixed(1)}px`);
|
|
40
|
+
el.style.setProperty('--magnet-inner-x', `${(dx * 0.5).toFixed(1)}px`);
|
|
41
|
+
el.style.setProperty('--magnet-inner-y', `${(dy * 0.5).toFixed(1)}px`);
|
|
42
|
+
el.classList.toggle('is-pulled', near);
|
|
43
|
+
};
|
|
44
|
+
document.addEventListener('pointermove', move, { passive: true });
|
|
45
|
+
return () => document.removeEventListener('pointermove', move);
|
|
46
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/marquee
|
|
3
|
+
*
|
|
4
|
+
* The pause button on a `<Marquee>`: moving content that lasts more than
|
|
5
|
+
* five seconds needs a way to stop it (WCAG 2.2.2). Toggles `is-paused` on
|
|
6
|
+
* the marquee, which freezes the scroll, and keeps the button's pressed
|
|
7
|
+
* state and label in step for assistive technology.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wires one marquee's pause button.
|
|
12
|
+
*
|
|
13
|
+
* @param {HTMLElement} el The marquee.
|
|
14
|
+
* @returns {() => void} Unwires it.
|
|
15
|
+
*/
|
|
16
|
+
export function marquee(el) {
|
|
17
|
+
const button = el.querySelector('[data-marquee-toggle]');
|
|
18
|
+
if (!(button instanceof HTMLButtonElement)) return () => {};
|
|
19
|
+
/** Pauses or resumes, and says so. */
|
|
20
|
+
const toggle = () => {
|
|
21
|
+
const paused = el.classList.toggle('is-paused');
|
|
22
|
+
button.setAttribute('aria-pressed', String(paused));
|
|
23
|
+
button.setAttribute('aria-label', paused ? button.dataset.resume ?? 'Resume scrolling' : button.dataset.pause ?? 'Pause scrolling');
|
|
24
|
+
};
|
|
25
|
+
button.addEventListener('click', toggle);
|
|
26
|
+
return () => button.removeEventListener('click', toggle);
|
|
27
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type Vec3 = [number, number, number];
|
|
2
|
+
export type Vec2 = [number, number];
|
|
3
|
+
|
|
4
|
+
/** Floats a vertex: position 3, normal 3, colour 3, uv 2, tag 2. */
|
|
5
|
+
export const STRIDE: number;
|
|
6
|
+
|
|
7
|
+
/** A flat-shaded mesh being built: every triangle its own vertices, with its face's normal. */
|
|
8
|
+
export interface Mesh {
|
|
9
|
+
/** Adds a triangle (anticlockwise seen from its front; with `outside`, turned to face away from that point). Degenerate ones are dropped. */
|
|
10
|
+
tri(a: Vec3, b: Vec3, c: Vec3, color: Vec3, tag?: Vec2, uvs?: [Vec2, Vec2, Vec2], outside?: Vec3): void;
|
|
11
|
+
/** Adds a quad, a b c d anticlockwise, as two triangles. */
|
|
12
|
+
quad(a: Vec3, b: Vec3, c: Vec3, d: Vec3, color: Vec3, tag?: Vec2, uvs?: [Vec2, Vec2, Vec2, Vec2], outside?: Vec3): void;
|
|
13
|
+
/** Adds a convex solid between two rings of as many points: a quad between each pair of edges (uv: distance round ring `a`, and up the side), the rings filled as caps. Default caps: both. */
|
|
14
|
+
loft(a: Vec3[], b: Vec3[], color: Vec3, tag?: Vec2, options?: { caps?: 'both' | 'start' | 'end' | 'none' }): Mesh;
|
|
15
|
+
/** Adds an axis-aligned box between two corners; no bottom face unless `bottom`. */
|
|
16
|
+
box(min: Vec3, max: Vec3, color: Vec3, tag?: Vec2, options?: { bottom?: boolean }): Mesh;
|
|
17
|
+
/** Adds a convex polygon (x, z) raised from y0 to y1; `top` scales the top ring about its centre (0 makes a pyramid). */
|
|
18
|
+
prism(polygon: Vec2[], y0: number, y1: number, color: Vec3, tag?: Vec2, options?: { bottom?: boolean; top?: number }): Mesh;
|
|
19
|
+
/** Adds a convex profile (x, y) pushed from z0 to z1. */
|
|
20
|
+
extrude(profile: Vec2[], z0: number, z1: number, color: Vec3, tag?: Vec2): Mesh;
|
|
21
|
+
/** Adds a faceted sphere (default 8 segments, 5 rings); `squash` scales it vertically. */
|
|
22
|
+
sphere(centre: Vec3, radius: number, color: Vec3, tag?: Vec2, segments?: number, rings?: number, squash?: number): Mesh;
|
|
23
|
+
/** Vertices so far. */
|
|
24
|
+
count(): number;
|
|
25
|
+
/** The vertices, STRIDE floats each. */
|
|
26
|
+
build(): Float32Array;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Makes an empty mesh to build into. */
|
|
30
|
+
export function createMesh(): Mesh;
|