Esempi
Esempi concreti. Demo live.
Scegli un esempio e aprilo a schermo intero: anteprima live e sorgente, fianco a fianco.
Scegli un esempio e aprilo a schermo intero: anteprima live e sorgente, fianco a fianco.
Un filtro blur e una matrice di colori fondono le bolle tra loro, mentre una scia di cerchi insegue il puntatore.
import 'nuclo' const W = 320 const H = 200 const SIZES = [24, 21, 18, 16, 14, 12, 10, 9, 8] const trail = SIZES.map(() => ({ x: W / 2, y: H / 2 })) let target: { x: number; y: number } | null = null let blur = 10 let goo = true let t = 0 let last = performance.now() // Each blob eases toward the one before it. The first follows // the pointer, or a figure eight while there is none. function step(dt: number) { t += 0.016 * dt const lead = target ?? { x: W / 2 + Math.sin(t * 1.3) * 110, y: H / 2 + Math.sin(t * 2.6) * 55, } const ease = 1 - 0.7 ** dt trail.forEach((p, i) => { const to = i ? trail[i - 1] : lead p.x += (to.x - p.x) * ease p.y += (to.y - p.y) * ease }) } // The pointer in viewBox units: the svg is scaled to its box. function follow(e: PointerEvent) { const box = (e.currentTarget as Element).getBoundingClientRect() target = { x: ((e.clientX - box.left) / box.width) * W, y: ((e.clientY - box.top) / box.height) * H, } } // The filter blurs the blobs, then a color matrix multiplies the // alpha by 20 and subtracts 8. Soft edges snap back to hard ones, // and blobs whose blurs overlap melt into one shape. export const Goo = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px' } }, scope('goo'), svgSvg( { viewBox: `0 0 ${W} ${H}`, width: '100%', role: 'img', 'aria-label': 'Gooey blobs', style: { maxWidth: '380px', touchAction: 'none', overflow: 'visible' }, // redraw every frame, and only this scope onMount: () => { let id = requestAnimationFrame(function frame(now: number) { step(Math.max(0, Math.min(2, (now - last) / (1000 / 60)))) last = now update('goo') id = requestAnimationFrame(frame) }) return () => cancelAnimationFrame(id) }, onPointerDown: follow, onPointerMove: follow, onPointerLeave: () => { target = null }, }, defsSvg( filterSvg( { id: 'goo', filterUnits: 'userSpaceOnUse', x: 0, y: 0, width: W, height: H }, feGaussianBlurSvg({ in: 'SourceGraphic', stdDeviation: () => blur }), feColorMatrixSvg({ mode: 'matrix', values: '1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 20 -8', }), ), ), rectSvg({ x: 0.5, y: 0.5, width: W - 1, height: H - 1, rx: 12, fill: 'none', stroke: '#ddd', }), gSvg( { fill: '#ff3f00', filter: () => (goo ? 'url(#goo)' : 'none') }, circleSvg({ cx: 64, cy: () => 100 + Math.sin(t * 2) * 14, r: 28, fill: '#2563eb' }), circleSvg({ cx: 256, cy: () => 100 + Math.cos(t * 2) * 14, r: 28, fill: '#ff8a00' }), ...trail.map((p, i) => circleSvg({ r: SIZES[i], cx: () => p.x, cy: () => p.y }), ), ), ), label( { style: { display: 'flex', alignItems: 'center', gap: '10px', fontSize: '0.8125rem' } }, span('Blur'), input({ type: 'range', min: '0', max: '16', step: '1', value: () => String(blur), onInput: (e) => { blur = Number(e.currentTarget.value) }, }), span(() => String(blur)), ), button({ 'aria-pressed': () => String(goo) }, () => (goo ? 'Goo on' : 'Goo off'), { onClick: () => { goo = !goo }, }), div({ style: { fontSize: '0.75rem', color: '#8b949e' } }, 'move the pointer over it'), )