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

Planetarium stołowe

Inne

Podgląd

Ładowanie podglądu…

Model planetarium w CSS 3D z czterema planetami krążącymi po nachylonych orbitach.

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>Planetarium stołowe</h1><p>Cztery planety na umownych orbitach.</p></div></header><section class="scene" aria-label="Wizualizacja"><div class="universe" role="img" aria-label="Przestrzenny model słońca i czterech planet"><div class="sun"></div><div class="orbit o1"><i class="planet"></i></div><div class="orbit o2"><i class="planet"></i></div><div class="orbit o3"><i class="planet"></i></div><div class="orbit o4"><i class="planet"></i></div></div><p class="legend">Słońce · Merkury · Wenus · Ziemia · Mars</p></section><div class="toolbar"><button id="motion" type="button" aria-pressed="false">Uruchom</button><label for="orbit-speed">Tempo <input id="orbit-speed" type="range" min="1" max="8" value="3"><output id="orbit-value">3</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:#171b33;--ink:#f3ede1;--accent:#ffd477}h1{font-family:Georgia,serif;font-weight:normal}.scene{perspective:750px;background:#10152a}.universe{position:relative;width:310px;height:310px;transform-style:preserve-3d;transform:rotateX(57deg) rotateZ(-18deg)}.sun{position:absolute;width:58px;height:58px;left:126px;top:126px;background:#ffd477;border-radius:50%;border:5px solid #e7ac48;transform:rotateX(-57deg)}.orbit{position:absolute;border:1px solid #6a7894;border-radius:50%;transform-style:preserve-3d}.o1{inset:98px}.o2{inset:72px}.o3{inset:40px}.o4{inset:7px}.planet{position:absolute;left:50%;top:-8px;width:16px;height:16px;border-radius:50%;background:#bebcb5;border:2px solid #ffffff70;transform:rotateX(-57deg);display:block}.o2 .planet{background:#e6b976;width:22px;height:22px}.o3 .planet{background:#6eadd5;width:24px;height:24px}.o4 .planet{background:#db7859;width:18px;height:18px}.legend{position:absolute;bottom:8px;left:12px;right:12px;text-align:center;font-size:13px;color:#c6ccdb}@media(max-width:400px){.universe{transform:scale(.86) rotateX(57deg) rotateZ(-18deg)}}
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 orbits=[...document.querySelectorAll('.orbit')],slider=document.querySelector('#orbit-speed');slider.addEventListener('input',()=>document.querySelector('#orbit-value').value=slider.value);run(time=>orbits.forEach((orbit,i)=>{orbit.style.transform=`rotateZ(${time*Number(slider.value)*17/(i+1)+i*67}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.