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

Architektura jednej kartki

Inne

Podgląd

Ładowanie podglądu…

Papierowa harmonijka zmienia kształt przy regulacji kąta zagięcia.

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>Architektura jednej kartki</h1><p>Zmień kąt zagięcia kartki.</p></div></header><section class="scene" aria-label="Wizualizacja"><div class="paper-model" role="img" aria-label="Przestrzenny model kartki złożonej w harmonijkę"></div><span class="paper-caption">Papier / przekrój przestrzenny</span></section><div class="toolbar"><button id="motion" type="button" aria-pressed="false">Uruchom</button><label for="fold">Zagięcie <input id="fold" type="range" min="5" max="70" value="40"><output id="fold-value">40°</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:#e8e4f0;--ink:#3d3156;--accent:#755e9b}h1{font-family:Georgia,serif;font-weight:normal}.scene{perspective:700px;background:#c9bfd9}.paper-model{position:relative;width:270px;height:175px;transform-style:preserve-3d;transform:rotateX(-17deg) rotateY(-24deg)}.panel{position:absolute;top:0;left:0;width:32px;height:175px;transform-origin:0 50%;background:#eee8f7;border:1px solid #71617f;backface-visibility:visible}.panel:nth-child(even){background:#a998c2}.panel:nth-child(3n){background:#d3c3e4}.paper-caption{position:absolute;right:15px;bottom:14px;font-size:13px}@media(max-width:400px){.paper-model{scale:.86}}
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 model=document.querySelector('.paper-model'),slider=document.querySelector('#fold'),panels=[];for(let i=0;i<10;i++){const panel=document.createElement('div');panel.className='panel';model.append(panel);panels.push(panel);}slider.addEventListener('input',()=>{document.querySelector('#fold-value').value=slider.value+'°';if(paint)paint(elapsed);});run(time=>{const fold=Number(slider.value)+Math.sin(time*.55)*4,angle=fold*Math.PI/180,total=10*32*Math.cos(angle);panels.forEach((panel,i)=>{const signed=i%2?-fold:fold,x=(270-total)/2+i*32*Math.cos(angle),z=i%2?-32*Math.sin(angle):0;panel.style.transform=`translate3d(${x}px,0,${z}px) rotateY(${signed}deg)`;});});
})();

Edytuj swój pen

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

Link jest ważny przez 30 minut.