No code or notes on this page.
Code
HTML
<div id="grass-back" class="grass"></div>
<div id="flower">
<svg class="stem" viewBox="0 0 20 100" preserveAspectRatio="none" aria-hidden="true"><path d="M10 0 C 13 35, 6 70, 10 100" vector-effect="non-scaling-stroke"/></svg>
<div id="head"></div>
</div>
<div id="grass-front" class="grass"></div>
<style>
html
{
overflow-x: clip;
}
#meadow
{
--head: min(300px, 47cqi);
--ink: light-dark(#7a8290, #eef2f8);
--achene: light-dark(#5b4a33, #c9b48a);
--stem: light-dark(#6f8a3e, #6d8f58);
position: relative;
aspect-ratio: 8 / 7;
background:
radial-gradient(circle at 80% 24%, light-dark(#fff8e4, #f4f2e8) 0 3.2cqi, light-dark(#ffe8b066, #dfe8ff33) 3.4cqi, transparent 24cqi),
linear-gradient(light-dark(#f0cf98, #0a1230), light-dark(#f7ead2, #1c2a4c) 60%, light-dark(#d8dfb4, #17281d) 80%, light-dark(#a7b975, #0f1f10));
}
#flower
{
position: absolute;
left: 50%;
top: 8%;
bottom: 4%;
width: var(--head);
translate: -50% 0;
transform-origin: 50% 100%;
}
.stem
{
position: absolute;
left: calc(50% - 10px);
top: calc(var(--head) / 2);
width: 20px;
height: calc(100% - var(--head) / 2);
fill: none;
stroke: var(--stem);
stroke-width: 3.5;
stroke-linecap: round;
}
#head
{
position: relative;
width: 100%;
aspect-ratio: 1;
container-type: inline-size;
perspective: min(800px, 100cqi * 800 / 640);
}
#head::before
{
content: '';
position: absolute;
inset: 44.5cqi;
border-radius: 50%;
background:
radial-gradient(circle, #0004 30%, transparent 36%) 0 0 / 1.6cqi 1.6cqi,
radial-gradient(circle at 40% 35%, light-dark(#c4ae76, #a99666), light-dark(#7a6a3e, #5a4d2e));
}
.seed
{
--puff: calc(11cqi + 4cqi * var(--z));
position: absolute;
left: calc(50% + 46cqi * var(--r) * sin(var(--a)) - var(--puff) / 2);
top: calc(50% - 46cqi * var(--r) * cos(var(--a)) - var(--puff) / 2);
width: var(--puff);
aspect-ratio: 1;
pointer-events: none;
will-change: transform, filter;
}
.seed[data-band="1"] { z-index: 1; }
.seed[data-band="2"] { z-index: 2; }
.fluff
{
position: absolute;
inset: 0;
rotate: var(--a);
}
.fluff::before
{
content: '';
position: absolute;
inset: 0;
border-radius: 50%;
background: repeating-conic-gradient(var(--ink) 0 2deg, transparent 2deg 11deg);
mask: radial-gradient(closest-side, #000 5%, #0009 40%, #0003 75%, transparent);
}
.fluff::after
{
content: '';
position: absolute;
left: calc(50% - 2px);
top: 50%;
width: 4px;
height: max(0px, 46cqi * var(--r) - 5cqi);
background:
radial-gradient(closest-side, var(--achene) 70%, transparent) bottom / 0.85cqi 2cqi no-repeat,
linear-gradient(color-mix(in srgb, var(--ink) 45%, transparent) 0 0) top / 1px calc(100% - 5px) no-repeat;
}
.grass
{
position: absolute;
inset: auto 0 0;
height: 20%;
pointer-events: none;
}
.blade
{
position: absolute;
bottom: -1px;
left: calc(var(--x) * 1% - 0.5cqi);
width: 1cqi;
height: calc(var(--h) * 100%);
transform-origin: 50% 100%;
clip-path: polygon(0 100%, var(--tip-x) 0, 100% 100%);
background: linear-gradient(to top, var(--root), var(--tip));
}
#grass-back .blade
{
--root: light-dark(#8ea35c, #1d3322);
--tip: light-dark(#b9c789, #355a3c);
height: calc(var(--h) * 70%);
}
#grass-front .blade
{
--root: light-dark(#4f6a28, #0c1a0d);
--tip: light-dark(#7d9a45, #2a4a2c);
}
<script type="module">
import ProximityEffect from 'proximity-effect';
import { onFrameResize } from '../../assets/js/frame-resize.mjs';
const meadow = document.querySelector('#meadow'),
flower = document.querySelector('#flower'),
head = document.querySelector('#head');
const SEEDS = 150,
GOLDEN = Math.PI * (3 - Math.sqrt(5)),
bands = [[], [], []];
for (let i = 0; i < SEEDS; i++)
{
const z = 1 - (i + 0.5) / SEEDS * 0.92,
seed = document.createElement('div'),
fluff = document.createElement('div'),
band = Math.random() < 0.3 ? 2 : Math.random() < 0.5 ? 1 : 0;
seed.className = 'seed';
fluff.className = 'fluff';
seed.style.setProperty('--a', `${i * GOLDEN}rad`);
seed.style.setProperty('--r', Math.sqrt(1 - z * z).toFixed(3));
seed.style.setProperty('--z', z.toFixed(3));
seed.dataset.band = band;
seed.append(fluff);
head.append(seed);
bands[band].push(seed);
}
const BANDS = [
{ z: { value: -180, scatter: 160 }, blur: 1.2, opacity: 90, attack: 0.06 },
{ z: { value: 240, scatter: 160 }, blur: 0.5, opacity: 100, attack: 0.07 },
{ z: { value: 520, scatter: 120 }, blur: 4, opacity: 75, attack: 0.09 }
];
const shared = {
pointerLeave: 'hold',
touchDrag: meadow
};
const flights = BANDS.map((band, i) =>
{
const fx = new ProximityEffect(bands[i], {
...shared,
threshold: 30,
runoff: 90,
jitter: 30,
attack: band.attack,
decay: 0.006,
tolerance: 0.002
});
fx.addStyle('translateX', [{ value: 0, scatter: 260 }, 0]);
fx.addStyle('translateY', [{ value: -100, scatter: 280 }, 0]);
fx.addStyle('translateZ', [band.z, 0]);
fx.addStyle('rotateZ', [{ value: 0, scatter: 320 }, 0]);
fx.addStyle('rotateX', [{ value: 0, scatter: 140 }, 0]);
fx.addStyle('blur', [{ value: band.blur, scatter: band.blur }, 0]);
fx.addStyle('opacity', [band.opacity, 100]);
return fx;
});
const blades = [];
for (const [row, count] of [['#grass-back', 70], ['#grass-front', 56]])
{
const holder = document.querySelector(row);
for (let i = 0; i < count; i++)
{
const blade = document.createElement('div');
blade.className = 'blade';
blade.style.setProperty('--x', ((i + Math.random()) / count * 100).toFixed(1));
blade.style.setProperty('--h', (0.35 + Math.random() * 0.65).toFixed(2));
blade.style.setProperty('--tip-x', `${(Math.random() * 240 - 70).toFixed(0)}%`);
holder.append(blade);
blades.push(blade);
}
}
const grass = new ProximityEffect(blades, {
...shared,
threshold: 0,
runoff: 180,
jitter: 40,
attack: 0.12,
decay: 0.03,
tolerance: 0.002
});
grass.addStyle('rotate', [{ value: 0, scatter: 16 }, { value: 0, scatter: 10 }]);
const lean = new ProximityEffect([flower], {
...shared,
threshold: 0,
runoff: 320,
attack: 0.04,
decay: 0.015
});
lean.addStyle('rotate', [0, 0]);
const effects = [...flights, grass, lean];
onFrameResize(meadow, width =>
{
for (const fx of effects)
{
fx.scaleEffect = Math.min(1, width / 640);
}
});
const wind = { x: 0, y: 0 },
push = { x: 0, y: 0 };
let lastMove = null;
const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
addEventListener('pointermove', e =>
{
if (lastMove)
{
const dt = Math.max(8, e.timeStamp - lastMove.t);
push.x = clamp((e.clientX - lastMove.x) / dt * 300, -360, 360);
push.y = clamp((e.clientY - lastMove.y) / dt * 200, -240, 120);
}
lastMove = { x: e.clientX, y: e.clientY, t: e.timeStamp };
});
const stirred = fx => fx.nodes.some((node, i) => (fx.getNodeIndexData(i, 'lastDelta') ?? 1) < 1);
function weather(now)
{
push.x *= 0.94;
push.y *= 0.94;
wind.x += (60 + 40 * Math.sin(now / 1300) + push.x - wind.x) * 0.06;
wind.y += (25 * Math.sin(now / 1700) + push.y - wind.y) * 0.06;
for (const fx of flights)
{
if (stirred(fx))
{
fx.updateStyle('translateX', [{ value: wind.x, scatter: 260 + Math.abs(wind.x) * 0.5 }, 0]);
fx.updateStyle('translateY', [{ value: wind.y - 100, scatter: 280 }, 0]);
}
}
if (stirred(grass))
{
grass.updateStyle('rotate', [{ value: wind.x * 0.05, scatter: 16 }, { value: 0, scatter: 10 }]);
}
if (stirred(lean))
{
lean.updateStyle('rotate', [wind.x * 0.012, 0]);
}
requestAnimationFrame(weather);
}
requestAnimationFrame(weather);
document.querySelector('#blow').addEventListener('click', () =>
{
const box = head.getBoundingClientRect(),
from = box.left - box.width * 0.4,
to = box.right + box.width * 0.4,
start = performance.now(),
time = 1200;
function sweep(now)
{
const k = Math.min(1, (now - start) / time),
x = from + (to - from) * k,
y = box.top + box.height * (0.5 - 0.15 * Math.sin(k * Math.PI * 2));
for (const fx of effects)
{
fx.setCoords(x, y);
}
push.x = 300;
if (k < 1)
{
requestAnimationFrame(sweep);
}
else
{
for (const fx of effects)
{
fx.clearCoords();
}
}
}
requestAnimationFrame(sweep);
});