Przejdź do treści
<code>pen.pl

Papierowy rozkwit

Animacje

Podgląd

Ładowanie podglądu…

Dwanaście papierowych płatków rozwija się w przestrzenny kwiat sterowany suwakiem.

0,0 / 5 Brak ocen

Kod źródłowy

Licencja MIT — także do użytku komercyjnego. Zachowaj informacje o autorze i licencji.

HTML
<main class="pen" data-motion="paused"><header><div><h1>Papierowy rozkwit</h1><p>Ustaw stopień otwarcia kwiatu.</p></div></header><section class="scene" aria-label="Wizualizacja"><div class="flower" aria-hidden="true"></div><p class="flower-label">Papier / 12 płatków</p></section><div class="toolbar"><button id="motion" type="button" aria-pressed="false">Uruchom</button><label for="spread">Otwarcie <input id="spread" type="range" min="30" max="85" value="62"><output id="spread-value">62°</output></label></div></main>
CSS

* { box-sizing: border-box; }
html, body { margin: 0; min-width: 280px; }
body { background: var(--paper); color: var(--ink); font: 16px/1.5 Arial, sans-serif; }
.pen { min-height: 100vh; padding: clamp(16px, 3vw, 30px); display: flex; flex-direction: column; gap: 16px; max-width: 1120px; margin: auto; }
header { display: flex; align-items: baseline; justify-content: space-between; gap: 20px; flex-wrap: wrap; }
h1 { font-size: clamp(26px, 4.1vw, 42px); line-height: 1.08; margin: 0 0 8px; letter-spacing: -.04em; }
p { margin: 0; max-width: 64ch; }
.scene { position: relative; min-height: 230px; flex: 1; display: grid; place-items: center; overflow: hidden; }
.scene canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.toolbar { display: flex; align-items: center; flex-wrap: wrap; gap: 12px 20px; }
.toolbar label { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; min-height: 44px; }
button, select { font: inherit; color: var(--ink); background: var(--paper); border: 1px solid currentColor; border-radius: 0; min-height: 44px; padding: 8px 15px; cursor: pointer; }
button:hover { background: var(--ink); color: var(--paper); }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 3px solid var(--accent); outline-offset: 4px; }
input[type=range] { width: 145px; max-width: 42vw; accent-color: var(--accent); min-height: 36px; cursor: pointer; }
output, .hint { font-size: 14px; }
[data-motion="paused"] .animated { animation-play-state: paused !important; }
@media (max-width: 500px) { header { gap: 6px; } .scene { min-height: 280px; } .toolbar { gap: 10px; } .toolbar label { font-size: 14px; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition-duration: 0s !important; scroll-behavior: auto !important; } }
body{--paper:#fae7e4;--ink:#722b36;--accent:#a72142}.scene{perspective:800px;background:#f7d3d4}.flower{width:220px;height:220px;transform-style:preserve-3d;transform:rotateX(44deg) rotateZ(-12deg)}.petal{position:absolute;left:70px;top:-35px;width:80px;height:160px;transform-origin:40px 145px;transform-style:preserve-3d}.leaf{display:block;width:100%;height:100%;background:#d2495f;clip-path:polygon(50% 0,100% 45%,70% 100%,30% 100%,0 45%);transform-origin:50% 95%;animation:bloom 5s ease-in-out infinite;animation-delay:var(--delay);border-top:2px solid #fff}.petal:nth-child(3n) .leaf{background:#9c2642}.petal:nth-child(3n+1) .leaf{background:#f77a87}.flower-label{position:absolute;bottom:15px;right:18px;font-size:14px}@keyframes bloom{0%,100%{transform:rotateX(calc(15deg - var(--open,62deg)))}50%{transform:rotateX(calc(0deg - var(--open,62deg)))}}
JavaScript
(() => {

const root = document.querySelector('.pen');
const preference = matchMedia('(prefers-reduced-motion: reduce)');
const motionButton = document.querySelector('#motion');
let playing = !preference.matches, elapsed = 0, paint = null;
function syncMotion() {
  root.dataset.motion = playing ? 'running' : 'paused';
  motionButton.textContent = playing ? 'Wstrzymaj' : 'Uruchom';
  motionButton.setAttribute('aria-pressed', String(playing));
}
motionButton.addEventListener('click', () => { playing = !playing; syncMotion(); });
preference.addEventListener('change', event => { playing = !event.matches; syncMotion(); });
syncMotion();
function run(draw) {
  paint = draw;
  draw(0);
  let last = performance.now(), lastPaint = 0;
  function frame(now) {
    if (playing && !document.hidden) { elapsed += Math.min(now - last, 48) / 1000; if (now - lastPaint >= 30) { draw(elapsed); lastPaint = now; } }
    last = now;
    requestAnimationFrame(frame);
  }
  requestAnimationFrame(frame);
}
function canvasSurface(selector) {
  const canvas = document.querySelector(selector), context = canvas.getContext('2d');
  if (!context) { canvas.replaceWith(document.createTextNode('Ta wizualizacja wymaga obsługi Canvas 2D.')); return null; }
  const surface = { canvas, context, width: 0, height: 0 };
  function resize() {
    const box = canvas.parentElement.getBoundingClientRect(), ratio = Math.min(devicePixelRatio || 1, 2);
    surface.width = box.width; surface.height = box.height;
    canvas.width = Math.max(1, Math.round(box.width * ratio)); canvas.height = Math.max(1, Math.round(box.height * ratio));
    context.setTransform(ratio, 0, 0, ratio, 0, 0);
    if (paint) paint(elapsed);
  }
  resize(); new ResizeObserver(resize).observe(canvas.parentElement);
  return surface;
}
const flower=document.querySelector('.flower');for(let i=0;i<12;i++){const petal=document.createElement('div');petal.className='petal';petal.style.transform=`rotateZ(${i*30}deg)`;petal.innerHTML='<i class="leaf animated"></i>';petal.style.setProperty('--delay',(-i*.2)+'s');flower.append(petal);}
const spread=document.querySelector('#spread');spread.addEventListener('input',()=>{root.style.setProperty('--open',spread.value+'deg');document.querySelector('#spread-value').value=spread.value+'°';});
})();

Edytuj swój pen

Podaj adres e-mail użyty przy zapisie. Wyślemy link do edycji.

Link jest ważny przez 30 minut.