Beispiele
Praxisnahe Beispiele. Live-Demos.
Öffne ein Beispiel im Vollbild: Live-Vorschau und Quellcode liegen nebeneinander.
Öffne ein Beispiel im Vollbild: Live-Vorschau und Quellcode liegen nebeneinander.
Ein Rad rollt in einem Ring, und ein Stift zeichnet die Bahn nach. Jeder Frame verlängert einen Strich und aktualisiert nur dessen scope().
import 'nuclo' const R = 96 // the fixed ring const STEPS = 180 // points per lap of the wheel around the ring let r = 60 // the rolling wheel let d = 52 // the pen's distance from the wheel's center let pts: number[][] = [] let lens: number[] = [] // the curve's length up to each point let line = '' let s = 1 // the scale that fits the drawing in the box let k = 0 // how many points are drawn let playing = true const gcd = (a: number, b: number): number => (b ? gcd(b, a % b) : a) const rand = (min: number, max: number) => Math.round(min + Math.random() * (max - min)) // A hypotrochoid. The pen is back at its start after r / gcd(R, r) laps. function trace() { const n = (r / gcd(R, r)) * STEPS s = 88 / Math.max(R, R - r + d) pts = [] lens = [0] for (let i = 0; i <= n; i++) { const t = (i / STEPS) * 2 * Math.PI const p = [ 100 + s * ((R - r) * Math.cos(t) + d * Math.cos(((R - r) / r) * t)), 100 + s * ((R - r) * Math.sin(t) - d * Math.sin(((R - r) / r) * t)), ].map((v) => Math.round(v * 10) / 10) if (i) lens.push(lens[i - 1] + Math.hypot(p[0] - pts[i - 1][0], p[1] - pts[i - 1][1])) pts.push(p) } line = pts.join(' ') k = 0 playing = true } trace() const last = () => pts.length - 1 const done = () => k >= last() const pen = () => pts[Math.floor(k)] const wheel = () => { const t = (Math.floor(k) / STEPS) * 2 * Math.PI return [100 + s * (R - r) * Math.cos(t), 100 + s * (R - r) * Math.sin(t)] } // A labeled range input with its value beside it. const slider = (name: string, min: number, max: number, step: number, get: () => number, set: (v: number) => void) => label( { style: { display: 'flex', alignItems: 'center', gap: '10px', fontSize: '0.8125rem' } }, span(name), input({ type: 'range', min: String(min), max: String(max), step: String(step), value: () => String(get()), onInput: (e) => set(Number(e.currentTarget.value)), }), span(() => String(get())), ) // The points are computed once per shape. Drawing it is only a // longer dash on every frame, and update('spiro') refreshes the // scoped element alone instead of the whole page. export const Spirograph = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px' } }, scope('spiro'), svgSvg( { viewBox: '0 0 200 200', width: '200', height: '200', style: { color: '#ff3f00', overflow: 'visible' }, onMount: () => { let id = requestAnimationFrame(function frame() { if (playing && !done()) { k = Math.min(last(), k + Math.max(3, last() / 300)) update('spiro') } id = requestAnimationFrame(frame) }) return () => cancelAnimationFrame(id) }, }, // the whole curve, faint polylineSvg({ points: () => line, fill: 'none', stroke: 'currentColor', 'stroke-opacity': 0.14 }), // the part drawn so far: one dash that grows polylineSvg({ points: () => line, fill: 'none', stroke: 'currentColor', 'stroke-width': 1.4, 'stroke-linejoin': 'round', 'stroke-dasharray': () => `${lens[Math.floor(k)]} ${lens[last()]}`, }), // the ring, the rolling wheel, the pen arm and the pen; they fade out when done gSvg( { opacity: () => (done() ? 0 : 1), style: { transition: 'opacity 0.4s ease' } }, circleSvg({ cx: 100, cy: 100, r: () => R * s, fill: 'none', stroke: '#2563eb', 'stroke-opacity': 0.4 }), circleSvg({ cx: () => wheel()[0], cy: () => wheel()[1], r: () => r * s, fill: 'none', stroke: '#2563eb' }), lineSvg({ x1: () => wheel()[0], y1: () => wheel()[1], x2: () => pen()[0], y2: () => pen()[1], stroke: '#2563eb' }), circleSvg({ cx: () => pen()[0], cy: () => pen()[1], r: 3, fill: '#2563eb' }), ), ), slider('Wheel', 24, 84, 4, () => r, (v) => { r = v; trace() }), slider('Pen', 10, 90, 2, () => d, (v) => { d = v; trace() }), div( { style: { display: 'flex', gap: '8px' } }, button(() => (done() ? 'Replay' : playing ? 'Pause' : 'Play'), { onClick: () => { if (done()) { k = 0; playing = true } else playing = !playing update('spiro') }, }), button('Random', { onClick: () => { r = rand(6, 21) * 4; d = rand(5, 45) * 2; trace() }, }), ), )