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

Teren z trójkątów

Inne

Podgląd

Ładowanie podglądu…

Animowany teren z trójkątów z regulowaną wysokością wzniesień.

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>Teren z trójkątów</h1><p>Siatka zmienia się w pasmo gór. Ustaw wysokość terenu.</p></div></header><section class="scene" aria-label="Wizualizacja"><canvas id="terrain" role="img" aria-label="Trójwymiarowy teren z kolorowych trójkątów"></canvas></section><div class="toolbar"><button id="motion" type="button" aria-pressed="false">Uruchom</button><label for="altitude">Wysokość <input id="altitude" type="range" min="5" max="60" value="30"><output id="altitude-value">30</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:#edf4ed;--ink:#244839;--accent:#1c7855}h1{font-family:Georgia,serif;font-weight:normal}.scene{background:#d4e7d3;border-bottom:3px solid #527b5a}
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 surface=canvasSurface('#terrain');if(!surface)return;const ctx=surface.context,slider=document.querySelector('#altitude');slider.addEventListener('input',()=>{document.querySelector('#altitude-value').value=slider.value;if(paint)paint(elapsed);});
function project(x,y,z){const yaw=-.62,px=x*Math.cos(yaw)-z*Math.sin(yaw),pz=x*Math.sin(yaw)+z*Math.cos(yaw),py=y*.84-pz*.55,depth=y*.55+pz*.84,scale=Math.min(surface.width,surface.height)*1.1/(7+depth);return{x:surface.width/2+px*scale,y:surface.height*.5-py*scale,z:depth,height:y};}
run(time=>{const {width:w,height:h}=surface;ctx.clearRect(0,0,w,h);const grid=[],size=18;for(let row=0;row<=size;row++){grid[row]=[];for(let col=0;col<=size;col++){const x=(col-size/2)/4,z=(row-size/2)/4,y=(Math.sin(x*1.8+time*.3)*Math.cos(z*1.1-time*.17)+Math.sin(x+z)*.35)*Number(slider.value)/60;grid[row][col]=project(x,y,z);}}const faces=[];for(let r=0;r<size;r++)for(let c=0;c<size;c++){faces.push([grid[r][c],grid[r+1][c],grid[r][c+1]],[grid[r+1][c],grid[r+1][c+1],grid[r][c+1]]);}faces.sort((a,b)=>b.reduce((n,p)=>n+p.z,0)-a.reduce((n,p)=>n+p.z,0));faces.forEach(face=>{const altitude=face.reduce((n,p)=>n+p.height,0)/3;ctx.fillStyle=`hsl(${125+altitude*18} 32% ${47+altitude*17}%)`;ctx.strokeStyle='#24483980';ctx.lineWidth=.6;ctx.beginPath();face.forEach((p,i)=>i?ctx.lineTo(p.x,p.y):ctx.moveTo(p.x,p.y));ctx.closePath();ctx.fill();ctx.stroke();});});
})();

Edytuj swój pen

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

Link jest ważny przez 30 minut.