176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
feat: move 2.5D / Pseudo-3D to the guided-steps page
matinbddfe8d628cd
diff --git a/animations/06-3d-advanced/2-5d-pseudo-3d/README.md b/animations/06-3d-advanced/2-5d-pseudo-3d/README.mdindex ec3dbd7b..ea890923 100644--- a/animations/06-3d-advanced/2-5d-pseudo-3d/README.md+++ b/animations/06-3d-advanced/2-5d-pseudo-3d/README.md@@ -1,7 +1,7 @@# 2.5D / Pseudo-3D## What it is-2.5D (pseudo-3D) arranges flat 2D layers at different virtual depths, then moves a virtual "camera" to create a parallax depth illusion. Layers closest to the camera move the most in response to mouse or scroll; distant layers barely move. The technique was used in classic video games (Doom, old platformers) and animated films (Disney's multiplane camera, 1937), and translates directly to CSS with no additional libraries.+2.5D fakes depth with flat layers. The picture is split into layers at different distances, and when the camera moves, near layers slide further than far ones, as the view from a train window does. The same trick gave old cartoons and video games their sense of depth.## When to use it- Hero sections with illustrated scenes that should feel dimensional@@ -21,9 +21,10 @@ const LAYERS = [{ el: document.getElementById('ground'), depth: 1.0 },];-stage.addEventListener('mousemove', e => {- const normX = (e.clientX / stage.clientWidth) - 0.5; // -0.5 to 0.5- const normY = (e.clientY / stage.clientHeight) - 0.5;+stage.addEventListener('pointermove', e => {+ const r = stage.getBoundingClientRect();+ const normX = (e.clientX - r.left) / r.width - 0.5; // -0.5 to 0.5+ const normY = (e.clientY - r.top) / r.height - 0.5;LAYERS.forEach(({ el, depth }) => {el.style.transform = `translate(${-normX * STRENGTH * depth}px,@@ -33,35 +34,38 @@ stage.addEventListener('mousemove', e => {});```-For an **auto-pan** camera, drive the norm values with sine:+The demo has seven layers, from the sky (depth 0.05) to the ground (depth 1.0). The camera stays where the pointer leaves it. It also listens for `pointerdown`, so a tap moves the camera and a finger dragged sideways carries it along, the same way the mouse does.++For **Show me**, the demo drives the same layers with a three-second sweep instead of the pointer, ending in the middle:```js-function autoLoop(t) {- const normX = Math.sin(t * 0.0003) * 0.5;- const normY = Math.sin(t * 0.0002) * 0.3;+function sweep(now) { // start = performance.now() when Show me is pressed+ const p = Math.min(1, (now - start) / 3000);+ const normX = 0.45 * Math.sin(2 * Math.PI * p);+ const normY = 0.2 * Math.sin(Math.PI * p);LAYERS.forEach(({ el, depth }) => {el.style.transform = `translate(${-normX * STRENGTH * depth}px, ${-normY * STRENGTH * depth * 0.4}px)`;});- requestAnimationFrame(autoLoop);+ if (p < 1) requestAnimationFrame(sweep);}```+The demo also blends in the camera's starting position, so a second press, or a run that a real pointer stopped, carries on from where the camera is instead of jumping. Reset puts the camera back in the middle.+## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| Strength | 40px | Maximum displacement of the nearest (depth 1) layer |-| Layer count | 7 | More layers = smoother depth gradient; fewer = staircase effect |-| Depth values | 0.05–1.0 | The distribution determines how pronounced the depth gap feels |-| Y multiplier | 0.4 | Vertical parallax is usually less than horizontal for landscape scenes |+| Camera movement | Medium | How far the nearest layer travels as the pointer crosses the scene from one side to the other: small is 20px, medium 40px and large 70px (half that each way from the middle); the farthest layer moves 5% of that |+| Layer labels | off | Labels each layer with the share of the camera movement it follows, from 5% for the sky to 100% for the ground |## Production notes- **Each layer must be wider than the stage**: when the camera pans, layers must extend beyond the visible frame so empty edges don't show. Add 10–20% horizontal overflow per layer.- **CSS `perspective` vs `translateZ`**: CSS 3D perspective gives the same result with less JavaScript — put `perspective: 800px` on the container and use `translateZ(depth)` on each layer. Mouse rotation of the entire container then creates the parallax effect with a single transform.-- **Scroll-driven variant**: replace `mousemove` with `scroll` and map scroll position to the camera offset. Combine with sticky positioning to pin the scene while the user scrolls through it.+- **Scroll-driven variant**: replace `pointermove` with `scroll` and map scroll position to the camera offset. Combine with sticky positioning to pin the scene while the user scrolls through it.- **Game engines**: this is the native technique in 2D game engines (Phaser, PixiJS, Godot) with "parallax scrolling" built in as a first-class feature. Each layer specifies a scroll factor (0 to 1).- **Optimization**: layers that extend beyond the viewport trigger paint. Use `overflow: hidden` on the parent and `transform: translateZ(0)` on each layer to promote them to GPU compositing layers.## See also-- [Parallax Depth-of-Field](../../01-scroll-based/parallax-depth-of-field/) — the same layered parallax concept for scroll-based scenes-- [Parallax 3D Tilt](../parallax-3d-tilt/) — mouse-driven 3D tilt of a single card-- [Scroll-Driven 3D Rotation](../scroll-driven-3d-rotation/) — scroll as the camera driver+- [Parallax Depth-of-Field](../../01-scroll-based/parallax-depth-of-field/) — the same layered depth, driven by scrolling+- [Parallax 3D Tilt](../parallax-3d-tilt/) — one card leans toward the pointer+- [Scroll-Driven 3D Rotation](../scroll-driven-3d-rotation/) — scrolling moves a 3D objectdiff --git a/animations/06-3d-advanced/2-5d-pseudo-3d/index.html b/animations/06-3d-advanced/2-5d-pseudo-3d/index.htmlindex fad3bff2..ae1d4b9b 100644--- a/animations/06-3d-advanced/2-5d-pseudo-3d/index.html+++ b/animations/06-3d-advanced/2-5d-pseudo-3d/index.html@@ -4,7 +4,7 @@<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>2.5D / Pseudo-3D — Animation Handbook</title>- <meta name="description" content="Flat 2D layers at seven virtual depths — mouse movement parallaxes each at a different rate.">+ <meta name="description" content="Flat layers slide by different amounts, faking depth. Best for hero scenes."><link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/2-5d-pseudo-3d/"><link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg">@@ -12,27 +12,18 @@<meta property="og:type" content="article"><meta property="og:site_name" content="Animation Handbook"><meta property="og:title" content="2.5D / Pseudo-3D — Animation Handbook">- <meta property="og:description" content="Flat 2D layers at seven virtual depths — mouse movement parallaxes each at a different rate.">+ <meta property="og:description" content="Flat layers slide by different amounts, faking depth. Best for hero scenes."><meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/2-5d-pseudo-3d/"><meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:title" content="2.5D / Pseudo-3D — Animation Handbook">- <meta name="twitter:description" content="Flat 2D layers at seven virtual depths — mouse movement parallaxes each at a different rate.">+ <meta name="twitter:description" content="Flat layers slide by different amounts, faking depth. Best for hero scenes."><meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">- <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "2.5D / Pseudo-3D", "name": "2.5D / Pseudo-3D", "description": "Flat 2D layers at seven virtual depths — mouse movement parallaxes each at a different rate.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/2-5d-pseudo-3d/", "inLanguage": "en", "articleSection": "3D & Advanced", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, 3D & Advanced, 2.5D / Pseudo-3D"}</script>+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "2.5D / Pseudo-3D", "name": "2.5D / Pseudo-3D", "description": "Flat layers slide by different amounts, faking depth. Best for hero scenes.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/2-5d-pseudo-3d/", "inLanguage": "en", "articleSection": "3D & Advanced", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, 3D & Advanced, 2.5D / Pseudo-3D"}</script><style>- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-700.woff2') format('woff2');font-weight:700;font-display:swap}- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-800.woff2') format('woff2');font-weight:800;font-display:swap}- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-400.woff2') format('woff2');font-weight:400;font-display:swap}- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-500.woff2') format('woff2');font-weight:500;font-display:swap}- :root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#3fd6c4;--ui-text:#f4f4f2;--ui-muted:#77777e;--disp:'Bricolage','Arial Narrow',sans-serif;--mono:'PlexMono',ui-monospace,Menlo,Consolas,monospace;--stage-h:620px}+ :root{--ui-accent:#3fd6c4}*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}- body{background:var(--bg);color:var(--ui-text);font-family:var(--mono);-webkit-font-smoothing:antialiased;padding:clamp(12px,3vw,24px)}- header{margin-bottom:20px}- header h1{font-family:var(--disp);font-weight:700;font-size:clamp(11px,1.8vw,15px);letter-spacing:-.01em}- header p{font-size:clamp(10px,1.2vw,12px);color:var(--ui-muted);margin-top:4px}- .layout{display:flex;gap:clamp(12px,2vw,24px);align-items:flex-start}- .stage{flex:1;min-width:0;height:var(--stage-h);border:1px solid var(--ui-border);border-radius:8px;overflow:hidden;position:relative;cursor:none;touch-action:pan-y}+ .stage{position:relative;cursor:none;touch-action:pan-y}/* 3D scene container */.scene{width:100%;height:100%;position:relative;perspective:800px;perspective-origin:50% 50%;overflow:hidden}.scene-inner{width:100%;height:100%;position:relative;transform-style:preserve-3d;transform:translateZ(0);transition:transform 0ms linear}@@ -47,7 +38,7 @@.mtns-far{display:flex;align-items:flex-end;justify-content:center;padding-bottom:0}.mtn{position:absolute;bottom:35%;border-style:solid;border-color:transparent;border-bottom-color:#0d2040}/* Hills */- .hills-wrap{position:absolute;bottom:30%;left:-40px;right:-40px;height:180px;overflow:hidden}+ .hills-wrap{position:absolute;bottom:30%;left:-40px;right:-40px;height:108px;overflow:hidden}.hills{width:120%;height:100%;border-radius:50% 50% 0 0;margin-left:-10%}/* Ground */.ground{position:absolute;bottom:-24px;left:-20%;right:-20%;height:calc(30% + 24px)}@@ -57,205 +48,255 @@.tree-top{border-style:solid;border-color:transparent}.tree-trunk{background:#3a2510}/* Fog */- .fog{position:absolute;bottom:28%;left:-60px;right:-60px;height:80px;background:linear-gradient(transparent,rgba(10,30,60,.4) 60%,rgba(10,30,60,.6))}+ .fog{position:absolute;bottom:28%;left:-60px;right:-60px;height:56px;background:linear-gradient(transparent,rgba(10,30,60,.4) 60%,rgba(10,30,60,.6))}/* Moon */- .moon{position:absolute;top:15%;right:20%;width:50px;height:50px;border-radius:50%;background:#d8e8f8;box-shadow:0 0 20px rgba(200,220,255,.4)}- /* Cursor */- #cursor{position:absolute;width:20px;height:20px;border-radius:50%;border:2px solid var(--ui-accent);pointer-events:none;transform:translate(-50%,-50%);z-index:100;display:none}- /* Depth label */- .depth-label{position:absolute;top:4px;right:4px;font-size:8px;letter-spacing:1px;text-transform:uppercase;color:rgba(255,255,255,.5);pointer-events:none;z-index:10}- /* Panel */- aside{width:clamp(200px,22vw,240px);flex-shrink:0;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:16px;display:flex;flex-direction:column;gap:10px}- .note{font-size:10px;color:var(--ui-muted);line-height:1.6;border-left:2px solid var(--ui-accent);padding-left:8px}- .lbl{font-size:10px;text-transform:uppercase;letter-spacing:1px;color:var(--ui-accent)}- .kv{display:flex;justify-content:space-between;font-size:10px;color:var(--ui-muted)}- .kv .v{color:var(--ui-text)}- .div{height:1px;background:var(--ui-border)}- .sr{display:flex;align-items:center;gap:6px;font-size:10px}- .sr input[type=range]{flex:1;accent-color:var(--ui-accent);min-height:44px}- .sv{width:40px;text-align:right;color:var(--ui-text)}- select{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;font-size:10px;padding:8px 6px;border-radius:4px;width:100%;min-height:44px}- .tog{display:flex;align-items:center;gap:8px;font-size:10px;cursor:pointer;min-height:44px}- .tog input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink:0}- button.rst{background:transparent;border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;font-size:10px;padding:14px 8px;border-radius:4px;cursor:pointer;width:100%;min-height:44px}- @media(hover:hover){button.rst:hover{border-color:var(--ui-accent);color:var(--ui-accent)}}- @media(max-width:600px){.layout{flex-direction:column;align-items:stretch}aside{width:100%}:root{--stage-h:420px}.stage{flex:none}}+ .moon{position:absolute;top:15%;right:20%;width:36px;height:36px;border-radius:50%;background:#d8e8f8;box-shadow:0 0 20px rgba(200,220,255,.4)}+ /* Cursor ring: moved with a transform, from the stage's top left corner */+ #cursor{position:absolute;left:0;top:0;width:20px;height:20px;border-radius:50%;border:2px solid var(--ui-accent);pointer-events:none;z-index:100;display:none}+ /* Depth labels: shown by the Layer labels switch, one line each in the top left corner, placed by the script.+ The dark backing keeps a star behind a letter from washing it out. */+ .depth-label{position:absolute;top:4px;left:4px;padding:0 5px;border-radius:4px;background:rgba(2,8,18,.75);font-size:11px;font-weight:600;letter-spacing:1px;text-transform:uppercase;color:rgba(255,255,255,.7);pointer-events:none;z-index:10;display:none}@media(prefers-reduced-motion:reduce){.scene-inner{transition:none!important}}</style>-<style id="ah-inject">-:root{--ah-bg:rgba(17,17,20,.82);--ah-line:rgba(255,255,255,.1);--ah-ink:#f4f4f2;--ah-muted:#9a9aa2;--ah-accent:#6ea8ff}-body{padding-top:46px}-.ah-bar{position:fixed;top:0;left:0;right:0;height:46px;z-index:2147483000;display:flex;align-items:center;justify-content:space-between;gap:10px;padding:0 clamp(10px,3vw,18px);background:var(--ah-bg);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);border-bottom:1px solid var(--ah-line);font-family:ui-monospace,Menlo,Consolas,monospace;font-size:12px;box-sizing:border-box}-.ah-bar a,.ah-bar button{color:var(--ah-muted);text-decoration:none;background:none;border:0;font:inherit;cursor:pointer;display:inline-flex;align-items:center;gap:6px;min-height:34px;padding:0 7px;border-radius:6px;transition:color .18s,background .18s;white-space:nowrap}-.ah-bar a:hover,.ah-bar button:hover{color:var(--ah-ink)}-.ah-home{color:var(--ah-ink);font-weight:600}-.ah-actions{display:flex;align-items:center;gap:2px}-.ah-sep{width:1px;height:16px;background:var(--ah-line);margin:0 4px}-.ah-copied{color:var(--ah-accent)!important}-@media(max-width:600px){.ah-bar .ah-hideSm{display:none}}-@media print{.ah-bar{display:none}body{padding-top:0}}-</style>+ <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=4">+ <script src="../../../assets/js/demo-page.js?v=4" defer></script></head>-<body>-<div class="ah-bar" role="navigation" aria-label="Animation Handbook"><a class="ah-home" href="../../../">← Animation Handbook</a><span class="ah-actions"><a class="ah-hideSm" href="../chromatic-aberration/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../ray-marching-sdf/" rel="next" title="Next">Next ›</a><span class="ah-sep ah-hideSm"></span><button type="button" id="ah-copy" class="ah-hideSm">Copy source</button><a class="ah-hideSm" href="https://github.com/matinmonshizadeh/animation-handbook/blob/main/animations/06-3d-advanced/2-5d-pseudo-3d/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>-<header>- <h1>2.5D / Pseudo-3D</h1>- <p>Flat 2D layers at seven virtual depths — mouse movement parallaxes each at a different rate.</p>-</header>-<div class="layout">- <div class="stage" id="stage">- <div class="scene" id="scene">- <div class="scene-inner" id="scene-inner">- <!-- Layer 0: sky (slowest) -->- <div class="layer sky" id="L0"><div class="depth-label" id="dl0">depth 0.05</div></div>- <!-- Stars -->- <div class="layer stars" id="L1"><div class="depth-label" id="dl1">depth 0.1</div><div class="moon"></div></div>- <!-- Far mountains -->- <div class="layer" id="L2" style="background:transparent">- <div class="depth-label" id="dl2">depth 0.2</div>- <div class="mtns-far" style="position:absolute;bottom:30%;left:0;right:0;height:200px">- <div class="mtn" style="border-width:0 100px 160px;left:5%"></div>- <div class="mtn" style="border-width:0 80px 120px;left:18%"></div>- <div class="mtn" style="border-width:0 120px 180px;left:30%"></div>- <div class="mtn" style="border-width:0 90px 140px;left:55%"></div>- <div class="mtn" style="border-width:0 110px 160px;left:70%"></div>- <div class="mtn" style="border-width:0 70px 100px;left:85%"></div>+<body class="hb" data-hb-kind="do" data-hb-autoplay>+<nav class="hb-bar" aria-label="Animation Handbook">+ <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>+ <span class="hb-pager"><a href="../chromatic-aberration/" rel="prev" aria-label="Previous: Chromatic Aberration"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Chromatic Aberration</span></a><a href="../ray-marching-sdf/" rel="next" aria-label="Next: Ray Marching / SDF Scene"><span><span class="hb-dir">Next: </span>Ray Marching / SDF Scene</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>+</nav>+<main class="hb-page">+ <header class="hb-head">+ <p class="hb-cat">06.11 · 3D & Advanced</p>+ <h1>2.5D / Pseudo-3D</h1>+ <p class="hb-lede">Flat layers slide by different amounts, faking depth. Best for hero scenes.</p>+ </header>++ <section class="hb-step hb-watch" aria-labelledby="hb-watch-title">+ <div class="hb-step-head">+ <span class="hb-num" aria-hidden="true">1</span>+ <div><h2 id="hb-watch-title">Hover it</h2><p>Move your pointer across the scene, drag a finger sideways, or press Show me.</p></div>+ </div>+ <div class="stage" id="stage">+ <div class="scene" id="scene" role="img" aria-label="A night landscape of seven flat layers, from the sky to the ground, that slide by different amounts as the camera moves">+ <div class="scene-inner" id="scene-inner">+ <!-- Layer 0: sky (slowest) -->+ <div class="layer sky" id="L0"><div class="depth-label" id="dl0">moves 5%</div></div>+ <!-- Stars -->+ <div class="layer stars" id="L1"><div class="depth-label" id="dl1">moves 10%</div><div class="moon"></div></div>+ <!-- Far mountains -->+ <div class="layer" id="L2" style="background:transparent">+ <div class="depth-label" id="dl2">moves 20%</div>+ <div class="mtns-far" style="position:absolute;bottom:30%;left:0;right:0;height:140px">+ <div class="mtn" style="border-width:0 70px 112px;left:5%"></div>+ <div class="mtn" style="border-width:0 56px 84px;left:18%"></div>+ <div class="mtn" style="border-width:0 84px 126px;left:30%"></div>+ <div class="mtn" style="border-width:0 63px 98px;left:55%"></div>+ <div class="mtn" style="border-width:0 77px 112px;left:70%"></div>+ <div class="mtn" style="border-width:0 49px 70px;left:85%"></div>+ </div>+ </div>+ <!-- Mid hills -->+ <div class="layer" id="L3" style="background:transparent">+ <div class="depth-label" id="dl3">moves 40%</div>+ <div class="hills-wrap"><div class="hills" style="background:#0a2035;width:110%;margin-left:-5%"></div></div>+ <div class="hills-wrap" style="bottom:32%;height:78px"><div class="hills" style="background:#091c30"></div></div>+ </div>+ <!-- Trees far -->+ <div class="layer" id="L4" style="background:transparent">+ <div class="depth-label" id="dl4">moves 60%</div>+ <div class="trees-wrap" id="trees-far"></div>+ </div>+ <!-- Trees near -->+ <div class="layer" id="L5" style="background:transparent">+ <div class="depth-label" id="dl5">moves 85%</div>+ <div class="trees-wrap" id="trees-near" style="bottom:28%"></div>+ <div class="fog"></div>+ </div>+ <!-- Ground (fastest) -->+ <div class="layer" id="L6" style="background:transparent">+ <div class="depth-label" id="dl6">moves 100%</div>+ <div class="ground" style="background:linear-gradient(180deg,#050f1a,#030c15);border-top:1px solid rgba(88,166,255,.1)"></div></div></div>- <!-- Mid hills -->- <div class="layer" id="L3" style="background:transparent">- <div class="depth-label" id="dl3">depth 0.4</div>- <div class="hills-wrap"><div class="hills" style="background:#0a2035;width:110%;margin-left:-5%"></div></div>- <div class="hills-wrap" style="bottom:32%;height:130px"><div class="hills" style="background:#091c30"></div></div>- </div>- <!-- Trees far -->- <div class="layer" id="L4" style="background:transparent">- <div class="depth-label" id="dl4">depth 0.6</div>- <div class="trees-wrap" id="trees-far"></div>- </div>- <!-- Trees near -->- <div class="layer" id="L5" style="background:transparent">- <div class="depth-label" id="dl5">depth 0.85</div>- <div class="trees-wrap" id="trees-near" style="bottom:28%"></div>- <div class="fog"></div>- </div>- <!-- Ground (fastest) -->- <div class="layer" id="L6" style="background:transparent">- <div class="depth-label" id="dl6">depth 1.0</div>- <div class="ground" style="background:linear-gradient(180deg,#050f1a,#030c15);border-top:1px solid rgba(88,166,255,.1)"></div>+ </div>+ <div id="cursor"></div>+ </div>+ <div class="hb-player">+ <button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</button>+ <button class="hb-play" type="button" id="btn-reset" data-hb-reset><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>Reset</button>+ </div>+ </section>++ <section class="hb-step hb-try" aria-labelledby="hb-try-title">+ <div class="hb-step-head">+ <span class="hb-num" aria-hidden="true">2</span>+ <div><h2 id="hb-try-title">Try it</h2><p>Change a setting, then try it again or press Show me.</p></div>+ </div>+ <div class="hb-settings">+ <div class="hb-setting">+ <p class="hb-setting-name" id="move-lbl">Camera movement</p>+ <div class="seg" id="move-seg" role="group" aria-labelledby="move-lbl">+ <button type="button" data-px="20" aria-pressed="false">Small</button>+ <button type="button" data-px="40" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-px="70" aria-pressed="false">Large</button></div>+ <p class="hb-hint">How far the nearest layer moves; far ones move less.</p>+ </div>+ <div class="hb-setting">+ <label class="hb-switch-row"><span>Layer labels</span><input class="hb-switch" type="checkbox" role="switch" id="labels-tog" autocomplete="off"></label>+ <p class="hb-hint">Shows how much each layer moves.</p></div></div>- <div id="cursor"></div>- </div>- <aside>- <p class="note">Each layer gets a depth multiplier (0–1). Mouse displacement × depth = translateX offset. Layers at depth 0 don't move; depth 1 moves the most.</p>- <div class="lbl">Mode</div>- <select id="mode-sel">- <option value="mouse" selected>Mouse-driven</option>- <option value="auto">Auto-pan</option>- </select>- <div class="lbl">Camera Strength</div>- <div class="sr"><input type="range" id="str-sl" min="10" max="100" value="40" step="5"><span class="sv" id="str-v">40px</span></div>- <label class="tog"><input type="checkbox" id="labels-tog"><span>Show depth labels</span></label>- <div class="div"></div>- <div class="kv"><span>Mouse X</span><span class="v" id="mx-v">—</span></div>- <div class="kv"><span>Layers</span><span class="v">7</span></div>- <button class="rst" id="reset-btn">↺ Reset camera</button>- </aside>-</div>+ </section>++ <section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">+ <div class="hb-step-head">+ <span class="hb-num" aria-hidden="true">3</span>+ <div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>+ </div>+ <div class="hb-card">+ <p class="hb-prompt">Add a 2.5D parallax scene to [the illustration or hero you want to give depth]. Split the picture into flat layers, from a far sky to the near ground, and when the pointer moves, shift every layer the opposite way by an amount that grows with how near it is: the sky barely moves and the ground moves the most, so the flat picture seems deep. Make each layer a little wider than the frame so no edge shows. On touch screens, dragging a finger should move the scene the same way. If the visitor has reduced motion turned on, keep the layers still. Match the settings listed below.</p>+ <div class="hb-chips-box" hidden>+ <p class="hb-chips-title">Your settings</p>+ <ul class="hb-chips"></ul>+ </div>+ <div class="hb-copy-row">+ <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>+ <p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>+ </div>+ </div>+ </section>++ <section class="hb-about" aria-labelledby="hb-what-title">+ <div class="hb-what">+ <h2 id="hb-what-title">What it is</h2>+ <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>+ </div>+ <div class="hb-fit">+ <h2>Good for</h2>+ <ul class="hb-tags hb-good"><li>Hero scenes</li><li>Illustrations</li><li>Game sites</li><li>Landing pages</li></ul>+ <h3>Avoid on</h3>+ <ul class="hb-tags hb-avoid"><li>Photos</li><li>Content-heavy pages</li></ul>+ </div>+ </section>++ <section class="hb-related" aria-labelledby="hb-related-title" hidden>+ <h2 id="hb-related-title">Similar animations</h2>+ <div class="hb-rel-list"></div>+ </section>+</main>+<footer class="hb-foot"><span>Animation Handbook</span><a href="#top">Back to top ↑</a></footer><script>-const stage=document.getElementById('stage');-const cursor=document.getElementById('cursor');-const mxV=document.getElementById('mx-v');-const LAYERS=[- {id:'L0',depth:.05},{id:'L1',depth:.1},{id:'L2',depth:.2},- {id:'L3',depth:.4},{id:'L4',depth:.6},{id:'L5',depth:.85},{id:'L6',depth:1}-];-let mx=0,my=0,strength=40,mode='mouse',raf=null,autoT=0,autoT0=0;-const motionOk=!window.matchMedia('(prefers-reduced-motion:reduce)').matches;+ const stage=document.getElementById('stage');+ const cursor=document.getElementById('cursor');+ const LAYERS=[+ {id:'L0',depth:.05},{id:'L1',depth:.1},{id:'L2',depth:.2},+ {id:'L3',depth:.4},{id:'L4',depth:.6},{id:'L5',depth:.85},{id:'L6',depth:1}+ ].map(l=>({...l,el:document.getElementById(l.id)}));+ let mx=0,my=0,strength=40; // where the camera is (-0.5 to 0.5 across the stage) and how far the nearest layer moves+ let raf=0; // the frame of a Show me run that is under way, 0 when there is none++ // Add stars+ const starLayer=document.getElementById('L1');+ for(let i=0;i<80;i++){+ const s=document.createElement('div');s.className='star';+ s.style.cssText=`width:${Math.random()*2+1}px;height:${Math.random()*2+1}px;top:${Math.random()*60}%;left:${Math.random()*100}%;opacity:${.3+Math.random()*.7}`;+ starLayer.appendChild(s);+ }-// Add stars-const starLayer=document.getElementById('L1');-for(let i=0;i<80;i++){- const s=document.createElement('div');s.className='star';- s.style.cssText=`width:${Math.random()*2+1}px;height:${Math.random()*2+1}px;top:${Math.random()*60}%;left:${Math.random()*100}%;opacity:${.3+Math.random()*.7}`;- starLayer.appendChild(s);-}+ // Add trees+ function addTrees(containerId,count,size,color){+ const wrap=document.getElementById(containerId);+ for(let i=0;i<count;i++){+ const x=5+i*(95/count)+Math.random()*5;+ const h=size*(0.7+Math.random()*.6);const w=h*.55;+ const el=document.createElement('div');el.className='tree-el';+ el.style.left=x+'%';+ el.innerHTML=`<div class="tree-top" style="border-width:0 ${w/2}px ${h*.7}px;border-bottom-color:${color}"></div><div class="tree-trunk" style="width:${w*.12}px;height:${h*.3}px"></div>`;+ wrap.appendChild(el);+ }+ }+ addTrees('trees-far',14,25,'#061520');+ addTrees('trees-near',8,39,'#040e18');-// Add trees-function addTrees(containerId,count,size,color){- const wrap=document.getElementById(containerId);- for(let i=0;i<count;i++){- const x=5+i*(95/count)+Math.random()*5;- const h=size*(0.7+Math.random()*.6);const w=h*.55;- const el=document.createElement('div');el.className='tree-el';- el.style.left=x+'%';- el.innerHTML=`<div class="tree-top" style="border-width:0 ${w/2}px ${h*.7}px;border-bottom-color:${color}"></div><div class="tree-trunk" style="width:${w*.12}px;height:${h*.3}px"></div>`;- wrap.appendChild(el);+ // Every layer slides the opposite way from the camera, by its depth times the camera movement+ function applyParallax(normX,normY){+ LAYERS.forEach(l=>{+ const tx=(-normX*strength*l.depth).toFixed(1);+ const ty=(-normY*strength*l.depth*.4).toFixed(1);+ l.el.style.transform=`translate(${tx}px,${ty}px)`;+ });}-}-addTrees('trees-far',14,36,'#061520');-addTrees('trees-near',8,56,'#040e18');-function applyParallax(normX,normY){- LAYERS.forEach(l=>{- const el=document.getElementById(l.id);- if(!el)return;- const tx=(-normX*strength*l.depth).toFixed(1);- const ty=(-normY*strength*l.depth*.4).toFixed(1);- el.style.transform=`translate(${tx}px,${ty}px)`;- });-}+ // The pointer and a finger put the camera where they are on the stage; a mouse also shows the ring.+ // A finger dragged off the stage keeps sending its moves to the stage, so the camera is held to the stage's edges.+ const edge=v=>Math.max(-.5,Math.min(.5,v));+ function aim(e){+ const r=stage.getBoundingClientRect();+ mx=edge((e.clientX-r.left)/r.width-.5);+ my=edge((e.clientY-r.top)/r.height-.5);+ if(e.pointerType==='mouse'){+ cursor.style.display='block';+ cursor.style.transform=`translate(${e.clientX-r.left-10}px,${e.clientY-r.top-10}px)`;+ }+ applyParallax(mx,my);+ }-stage.addEventListener('pointermove',e=>{- const r=stage.getBoundingClientRect();- mx=(e.clientX-r.left)/r.width-.5;- my=(e.clientY-r.top)/r.height-.5;- if(e.pointerType==='mouse'){cursor.style.display='block';cursor.style.left=(e.clientX-r.left)+'px';cursor.style.top=(e.clientY-r.top)+'px';}- mxV.textContent=(mx*100).toFixed(0)+'%';- if(mode==='mouse')applyParallax(mx,my);-});-stage.addEventListener('pointerleave',()=>{cursor.style.display='none'});+ // Show me: in three seconds the camera sweeps right, back through the middle, left and back, dipping a little, and rests+ // in the middle. It starts from where the camera is, and mx and my always hold where it is, so a second press or a+ // run that was stopped carries on from there.+ function showMe(){+ cancelAnimationFrame(raf);+ const x0=mx,y0=my;+ let t0=0;+ function step(now){+ if(!t0)t0=now;+ const p=Math.min(1,(now-t0)/3000);+ mx=x0*(1-p)+.45*Math.sin(2*Math.PI*p);+ my=y0*(1-p)+.2*Math.sin(Math.PI*p);+ if(p<1){applyParallax(mx,my);raf=requestAnimationFrame(step)}+ else{raf=0;mx=my=0;applyParallax(0,0)}+ }+ raf=requestAnimationFrame(step);+ }+ function stopRun(){cancelAnimationFrame(raf);raf=0}+ document.getElementById('btn-demo').addEventListener('click',showMe);+ document.getElementById('btn-reset').addEventListener('click',()=>{stopRun();mx=my=0;applyParallax(0,0)});+ // The visitor pressed, typed, scrolled or touched inside the stage: a run under way stops and the camera stays where it is.+ // Sent more than once for one press, so it does nothing when no run is under way.+ document.addEventListener('hb:input',()=>{if(raf)stopRun()});-function autoLoop(t){- raf=requestAnimationFrame(autoLoop);- autoT=(t-autoT0)*.0003;- applyParallax(Math.sin(autoT)*.5,Math.sin(autoT*.7)*.3);-}+ // Hovering sends no hb:input, so the visitor's own pointer ends a run here, then takes the camera. The camera stays+ // where the pointer leaves it. A tap sets it too: pointerdown, since a touch that lifts again sends no move.+ stage.addEventListener('pointermove',e=>{if(e.isTrusted&&raf)stopRun();aim(e)});+ stage.addEventListener('pointerdown',aim);+ stage.addEventListener('pointerleave',()=>{cursor.style.display='none'});-document.getElementById('mode-sel').addEventListener('change',e=>{- mode=e.target.value;- if(raf){cancelAnimationFrame(raf);raf=null;}- if(mode==='auto'){- autoT0=performance.now();- if(motionOk)raf=requestAnimationFrame(autoLoop);- else applyParallax(.25,.15); // reduced motion: hold a static camera offset instead of panning- }else applyParallax(mx,my); // leave the auto-pan offset behind, snap to the last pointer position-});-document.getElementById('str-sl').addEventListener('input',e=>{- strength=+e.target.value;document.getElementById('str-v').textContent=strength+'px';- // A running auto-pan repaints itself next frame; every other state has to be redrawn here- // or the new strength wouldn't show until the pointer moved again.- if(!raf)applyParallax(mode==='auto'?.25:mx,mode==='auto'?.15:my);-});-document.getElementById('labels-tog').addEventListener('change',e=>{- document.querySelectorAll('.depth-label').forEach(d=>d.style.display=e.target.checked?'block':'none');-});-document.getElementById('reset-btn').addEventListener('click',()=>{mx=0;my=0;autoT0=performance.now();mxV.textContent='—';applyParallax(0,0)});+ function choices(id,pick){+ const buttons=document.querySelectorAll('#'+id+' button');+ buttons.forEach(b=>b.addEventListener('click',()=>{+ buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});+ pick(b.dataset);+ }));+ }+ // A run repaints itself on its next frame; every other state has to be drawn here or the new movement would not show+ // until the camera moved again.+ choices('move-seg',d=>{strength=+d.px;if(!raf)applyParallax(mx,my)});+ document.getElementById('labels-tog').addEventListener('change',e=>{+ document.querySelectorAll('.depth-label').forEach(d=>d.style.display=e.target.checked?'block':'none');+ });-// Hide labels by default; stagger them vertically so the seven layers' labels don't stack on one-// spot, and inset each by its own maximum camera travel so a panned layer never carries its label-// out past the clipped edge of the scene.-const MAX_PAN=+document.getElementById('str-sl').max*.5;-const SKY_INSET=6; // .sky is inset -6px, so its label starts outside the scene box-document.querySelectorAll('.depth-label').forEach((d,i)=>{- const pad=i===0?SKY_INSET:0;- d.style.display='none';- d.style.top=(4+i*12+pad)+'px';- d.style.right=(6+pad+MAX_PAN*LAYERS[i].depth)+'px';-});+ // Stack the seven labels down the top left corner, one line each, and inset each by its own largest camera travel+ // (half the largest Camera movement, times its depth) so a panned layer never carries its label past the clipped edge.+ const MAX_PAN=35;+ const SKY_INSET=6; // .sky is inset -6px, so its label starts outside the scene box+ document.querySelectorAll('.depth-label').forEach((d,i)=>{+ const pad=i===0?SKY_INSET:0;+ d.style.top=(4+i*16+pad)+'px';+ d.style.left=(6+pad+MAX_PAN*LAYERS[i].depth)+'px';+ });</script>-<script>(function(){var b=document.getElementById("ah-copy");if(!b)return;b.addEventListener("click",function(){fetch(location.href).then(function(r){return r.text();}).catch(function(){return document.documentElement.outerHTML;}).then(function(t){return navigator.clipboard.writeText(t);}).then(function(){b.textContent="Copied!";b.classList.add("ah-copied");setTimeout(function(){b.textContent="Copy source";b.classList.remove("ah-copied");},1500);}).catch(function(){b.textContent="Copy failed";setTimeout(function(){b.textContent="Copy source";},1500);});});})();</script></body></html>diff --git a/index.html b/index.htmlindex 998b7a9d..046e2146 100644--- a/index.html+++ b/index.html@@ -1018,7 +1018,7 @@ const CATS=[['noise-based-motion','Noise-Based Motion','Smooth noise makes dots sway and a blob ripple. Best for calm backgrounds.'],['svg-path-animation','SVG Path Animation','A line drawing draws itself, stroke by stroke. Best for icons and logos.'],['chromatic-aberration','Chromatic Aberration','A word splits into red, green and blue fringes. Best for bold titles.'],- ['2-5d-pseudo-3d','2.5D / Pseudo-3D','Seven depth layers parallax at different rates — mouse-driven or auto-panning camera.'],+ ['2-5d-pseudo-3d','2.5D / Pseudo-3D','Flat layers slide by different amounts, faking depth. Best for hero scenes.'],['ray-marching-sdf','Ray Marching / SDF','A 3D scene drawn only from formulas, circled by a camera. Best for art pages.'],['gpgpu-particle-system','GPGPU Particle System','Tens of thousands of particles flow on the graphics chip. Best for hero effects.'],['image-distortion-hover','Image Distortion on Hover','Fragment shader displaces UV coordinates around the cursor — ripple, push, liquid, pixelate.'],