/* phys-fx.js — библиотека анимированных физических симуляций для Физики 11. * * Архитектура: * - Один глобальный requestAnimationFrame-цикл (Ticker). * - Каждая симуляция — класс с методами update(dt, t), render(). * - IntersectionObserver: симуляция приостанавливается, когда уходит из viewport. * - Чистый SVG (без Canvas, без WebGL, без зависимостей). * * Публичный API: window.PHYS = { util, Oscillogram, SpringMass, Pendulum, ... }. * * W0 — базовая инфраструктура + 3 компонента (Oscillogram, SpringMass, Pendulum). * Расширяется в W3 (электротехника), W5-W7 (оптика), W9-W14 (кванты, ядро). */ (function(){ 'use strict'; if (window.PHYS && window.PHYS.__installed) return; const P = window.PHYS = window.PHYS || {}; P.__installed = true; /* ============================================================ */ /* ГЛОБАЛЬНЫЙ ТАЙМЕР (один RAF на всю страницу) */ /* ============================================================ */ const Ticker = { t: 0, last: 0, subs: new Set(), running: false }; function tick(ts){ if (!Ticker.running) return; if (!Ticker.last) Ticker.last = ts; const dt = Math.min((ts - Ticker.last) / 1000, 0.1); // защита от лагов Ticker.last = ts; Ticker.t += dt; Ticker.subs.forEach(s => { if (!s.paused) { try { s.update(dt, Ticker.t); s.render && s.render(); } catch(e) {} } }); requestAnimationFrame(tick); } function startTicker(){ if (Ticker.running) return; Ticker.running = true; Ticker.last = 0; requestAnimationFrame(tick); } function stopTicker(){ Ticker.running = false; } /* ============================================================ */ /* УТИЛИТЫ */ /* ============================================================ */ const util = P.util = { subscribe(sim){ Ticker.subs.add(sim); startTicker(); }, unsubscribe(sim){ Ticker.subs.delete(sim); if (Ticker.subs.size === 0) stopTicker(); }, /* Создаёт IntersectionObserver, который ставит/снимает sim.paused */ observe(sim){ if (!sim.el || !window.IntersectionObserver) return; const io = new IntersectionObserver(entries => { entries.forEach(e => { sim.paused = !e.isIntersecting; }); }, { threshold: 0.05 }); io.observe(sim.el); sim._io = io; }, /* Безопасное удаление симуляции */ destroy(sim){ util.unsubscribe(sim); if (sim._io) { try { sim._io.disconnect(); } catch(e){} sim._io = null; } if (sim.el) sim.el.innerHTML = ''; }, /* Хелпер: создать SVG-обёртку с осями для графика */ svgFrame(w, h, opts){ opts = opts || {}; const bg = opts.bg || '#fafafa'; const border = opts.border || '1px solid #e2e8f0'; return ''; }, /* Двухмерные оси t (горизонтально) и y (вертикально). Возвращает функции toX/toY */ axes(W, H, pad, tMax, yRange){ const left = pad, right = W - pad, top = pad, bot = H - pad; const ux = (right - left) / tMax; const uy = (bot - top) / (yRange[1] - yRange[0]); function toX(t){ return left + t * ux; } function toY(y){ return bot - (y - yRange[0]) * uy; } /* SVG сетки + рамки */ let svg = ''; /* Вертикальные линии каждую секунду */ for (let s = 0; s <= tMax; s++) svg += ''; /* Горизонтальные линии */ const yStep = (yRange[1] - yRange[0]) / 4; for (let i = 0; i <= 4; i++){ const y = yRange[0] + i * yStep; svg += ''; } svg += ''; /* Ось t */ svg += ''; /* Ось y */ svg += ''; return { svg: svg, toX, toY, left, right, top, bot }; }, /* Создать ползунок-control под симуляцией. opts: { label, min, max, step, value, onChange } */ slider(opts){ const id = 'sl-' + Math.random().toString(36).slice(2,7); const html = '' + opts.label + '' + '' + '' + opts.value + (opts.unit || '') + '' + ''; return { html, id, wire(root){ const inp = root.querySelector('#' + id); const v = root.querySelector('#' + id + '-v'); if (!inp || !v) return; inp.addEventListener('input', () => { const val = parseFloat(inp.value); v.textContent = (opts.fmt ? opts.fmt(val) : val) + (opts.unit || ''); if (opts.onChange) opts.onChange(val); }); } }; } }; /* ============================================================ */ /* Oscillogram — гармонические колебания */ /* ============================================================ */ class Oscillogram { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 560; this.H = opts.height || 200; this.pad = opts.pad || 32; this.tWindow = opts.tWindow || 4; // секунд видно this.A = opts.A !== undefined ? opts.A : 1.0; this.omega = opts.omega !== undefined ? opts.omega : 2 * Math.PI; this.phi0 = opts.phi0 !== undefined ? opts.phi0 : 0; this.damping = opts.damping || 0; this.color = opts.color || '#dc2626'; this.label = opts.label || 'x(t)'; this.paused = false; this.t = 0; this.history = []; // [t, y] точки за последние tWindow секунд this._render(); util.subscribe(this); util.observe(this); } setA(v){ this.A = v; } setOmega(v){ this.omega = v; } setPhi(v){ this.phi0 = v; } setDamping(v){ this.damping = v; } reset(){ this.history = []; this.t = 0; } update(dt){ this.t += dt; const y = this.A * Math.exp(-this.damping * this.t) * Math.cos(this.omega * this.t + this.phi0); this.history.push([this.t, y]); while (this.history.length && this.history[0][0] < this.t - this.tWindow) this.history.shift(); } render(){ if (!this.el) return; const W = this.W, H = this.H, pad = this.pad; const tMin = Math.max(0, this.t - this.tWindow); const yRange = [-Math.max(1.05, this.A * 1.1), Math.max(1.05, this.A * 1.1)]; const ax = util.axes(W, H, pad, this.tWindow, yRange); let polyline = ''; if (this.history.length > 1){ const pts = this.history.map(([t, y]) => (ax.left + (t - tMin) * (ax.right - ax.left) / this.tWindow).toFixed(1) + ',' + ax.toY(y).toFixed(1)); polyline = ''; } /* Подпись y(t) */ const titleSvg = '' + this.label + ''; const svg = util.svgFrame(W, H) + ax.svg + polyline + titleSvg + ''; this.el.innerHTML = svg; } _render(){ this.render(); } } P.Oscillogram = Oscillogram; /* ============================================================ */ /* SpringMass — пружинный маятник (вертикальный) */ /* ============================================================ */ class SpringMass { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 240; this.H = opts.height || 280; this.m = opts.m !== undefined ? opts.m : 0.5; // кг this.k = opts.k !== undefined ? opts.k : 20; // Н/м this.A = opts.A !== undefined ? opts.A : 0.06; // м (амплитуда) this.color = opts.color || '#0891b2'; this.paused = false; this.t = 0; this._render(); util.subscribe(this); util.observe(this); } setMass(m){ this.m = Math.max(0.05, m); } setStiffness(k){ this.k = Math.max(1, k); } setAmplitude(A){ this.A = Math.max(0.005, A); } period(){ return 2 * Math.PI * Math.sqrt(this.m / this.k); } freq(){ return 1 / this.period(); } update(dt){ this.t += dt; } render(){ if (!this.el) return; const W = this.W, H = this.H; const T = this.period(); const omega = 2 * Math.PI / T; const A_px = 60; /* визуальная амплитуда в px */ const y0 = 90; /* y-координата равновесия груза в px */ const yCur = y0 + A_px * Math.cos(omega * this.t); /* Пружина: гармошка-зигзаг от крюка (y=20) до груза (y=yCur-18) */ const cx = W / 2, hookY = 20, massY = yCur, massR = 22; const coils = 10; const springTop = hookY; const springBot = massY - massR; const segH = (springBot - springTop) / (coils * 2); let path = 'M ' + cx + ' ' + springTop; for (let i = 0; i < coils; i++){ path += ' L ' + (cx - 14) + ' ' + (springTop + segH * (2 * i + 1)); path += ' L ' + (cx + 14) + ' ' + (springTop + segH * (2 * i + 2)); } path += ' L ' + cx + ' ' + springBot; /* Линейка справа */ const ruler = '' + '' + '' + '+A' + '' + '0' + '' + '-A' + ''; /* Период справа сверху */ const Tlabel = 'T = ' + T.toFixed(2) + ' с'; const svg = util.svgFrame(W, H, {bg:'#f8fafc'}) + '' + '' + '' + '' + '' + 'm' + ruler + Tlabel + ''; this.el.innerHTML = svg; } _render(){ this.render(); } } P.SpringMass = SpringMass; /* ============================================================ */ /* Pendulum — математический маятник */ /* ============================================================ */ class Pendulum { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 240; this.H = opts.height || 260; this.l = opts.l !== undefined ? opts.l : 1.0; // м this.g = opts.g !== undefined ? opts.g : 9.81; this.theta0 = opts.theta0 !== undefined ? opts.theta0 : Math.PI / 12; // начальный угол (рад) this.color = opts.color || '#0891b2'; this.paused = false; this.t = 0; this._render(); util.subscribe(this); util.observe(this); } setLength(l){ this.l = Math.max(0.1, l); } setG(g){ this.g = Math.max(0.5, g); } setTheta0(theta){ this.theta0 = Math.max(0.02, Math.min(Math.PI/4, theta)); } period(){ return 2 * Math.PI * Math.sqrt(this.l / this.g); } update(dt){ this.t += dt; } render(){ if (!this.el) return; const W = this.W, H = this.H; const T = this.period(); const omega = 2 * Math.PI / T; const theta = this.theta0 * Math.cos(omega * this.t); const cx = W / 2, hookY = 20; const Lpx = Math.min(160, H - 70); const bobR = 18; const bx = cx + Lpx * Math.sin(theta); const by = hookY + Lpx * Math.cos(theta); /* Дуга-траектория */ const arcR = Lpx; const arcStart = -this.theta0; const arcEnd = this.theta0; const aS = { x: cx + arcR * Math.sin(arcStart), y: hookY + arcR * Math.cos(arcStart) }; const aE = { x: cx + arcR * Math.sin(arcEnd), y: hookY + arcR * Math.cos(arcEnd) }; const largeArc = (arcEnd - arcStart) > Math.PI ? 1 : 0; const sweep = 1; const arc = ''; /* Вертикальная пунктирная ось */ const vert = ''; /* Подвес */ const string = ''; const bob = ''; /* Период */ const Tlabel = 'T = ' + T.toFixed(2) + ' с'; /* Подвес-крепление */ const hook = ''; const svg = util.svgFrame(W, H, {bg:'#f8fafc'}) + hook + vert + arc + string + bob + Tlabel + ''; this.el.innerHTML = svg; } _render(){ this.render(); } } P.Pendulum = Pendulum; /* ============================================================ */ /* EnergyView — превращения энергии при гарм. колебаниях */ /* Показывает W_к, W_п, W_мех=const на одном графике */ /* ============================================================ */ class EnergyView { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 560; this.H = opts.height || 240; this.pad = opts.pad || 36; this.A = opts.A !== undefined ? opts.A : 1.0; this.omega = opts.omega !== undefined ? opts.omega : 2 * Math.PI; this.tWindow = opts.tWindow || 4; this.paused = false; this.t = 0; this.history = []; // [t, Wk, Wp] util.subscribe(this); util.observe(this); this.render(); } setA(v){ this.A = v; this.history = []; } setOmega(v){ this.omega = v; this.history = []; } update(dt){ this.t += dt; /* Для x = A cos(ωt): v = -Aω sin(ωt) W_к = m v² / 2 = (1/2) m A² ω² sin²(ωt) W_п = k x² / 2 = (1/2) m ω² · A² cos²(ωt) (k = m ω²) В безразмерных: положим (1/2)mω²A² = 1 — тогда обе варьируются 0..1, сумма = 1 */ const c = Math.cos(this.omega * this.t); const s = Math.sin(this.omega * this.t); const Wp = c * c; const Wk = s * s; this.history.push([this.t, Wk, Wp]); while (this.history.length && this.history[0][0] < this.t - this.tWindow) this.history.shift(); } render(){ if (!this.el) return; const W = this.W, H = this.H, pad = this.pad; const tMin = Math.max(0, this.t - this.tWindow); const yRange = [0, 1.1]; const ax = util.axes(W, H, pad, this.tWindow, yRange); function path(idx, color, label){ if (this.history.length < 2) return ''; const pts = this.history.map(p => (ax.left + (p[0] - tMin) * (ax.right - ax.left) / this.tWindow).toFixed(1) + ',' + ax.toY(p[idx]).toFixed(1)); return ''; } const pK = path.call(this, 1, '#dc2626'); const pP = path.call(this, 2, '#16a34a'); /* W_мех = const = 1 (горизонтальная линия) */ const pM = ''; /* Легенда */ const legend = '' + '' + '' + 'W кинет.' + '' + 'W потенц.' + '' + 'W мех = const' + ''; const svg = util.svgFrame(W, H) + ax.svg + pM + pK + pP + legend + ''; this.el.innerHTML = svg; } } P.EnergyView = EnergyView; /* ============================================================ */ /* ResonanceCurve — резонансная кривая A(ω) при разных γ */ /* ============================================================ */ class ResonanceCurve { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 540; this.H = opts.height || 240; this.pad = opts.pad || 40; this.omega0 = opts.omega0 || 1.0; /* собственная частота (норм.) */ this.gamma = opts.gamma !== undefined ? opts.gamma : 0.15; this.omegaCur = opts.omegaCur !== undefined ? opts.omegaCur : 0.6; this.color = opts.color || '#7c3aed'; this.paused = false; this.render(); } setGamma(g){ this.gamma = Math.max(0.02, g); this.render(); } setOmegaCur(w){ this.omegaCur = Math.max(0.02, w); this.render(); } /* update не нужен — статический график, обновляется по setter */ update(){} render(){ if (!this.el) return; const W = this.W, H = this.H, pad = this.pad; const wMin = 0, wMax = 2 * this.omega0; /* Подсчитаем все амплитуды чтобы знать max */ function amp(w, g, w0){ const dw2 = (w0 * w0 - w * w); const denom = Math.sqrt(dw2 * dw2 + (2 * g * w) * (2 * g * w)); return 1 / Math.max(denom, 1e-6); } const gMin = 0.05; const ampMax = amp(this.omega0, gMin, this.omega0) * 1.1; /* Сетка */ const left = pad, right = W - pad, top = pad, bot = H - pad; const ux = (right - left) / (wMax - wMin); const uy = (bot - top) / ampMax; function toX(w){ return left + (w - wMin) * ux; } function toY(a){ return bot - a * uy; } let svg = util.svgFrame(W, H); /* Линии сетки */ svg += ''; for (let i = 0; i <= 4; i++){ const w = wMin + (wMax - wMin) * i / 4; svg += ''; } for (let i = 0; i <= 4; i++){ svg += ''; } svg += ''; /* Оси */ svg += ''; svg += ''; /* Подписи осей */ svg += 'ω'; svg += 'A'; /* Линия ω₀ — собственная частота */ svg += ''; svg += 'ω₀'; /* Кривая A(ω) */ let path = 'M '; const N = 200; for (let i = 0; i <= N; i++){ const w = wMin + (wMax - wMin) * i / N; const a = amp(w, this.gamma, this.omega0); path += toX(w).toFixed(1) + ',' + toY(Math.min(a, ampMax)).toFixed(1); if (i < N) path += ' L '; } svg += ''; /* Точка-маркер на текущей ω */ const aCur = Math.min(amp(this.omegaCur, this.gamma, this.omega0), ampMax); svg += ''; svg += ''; /* Подпись γ */ svg += 'γ = ' + this.gamma.toFixed(2) + ''; svg += 'ω = ' + this.omegaCur.toFixed(2) + ' · A = ' + aCur.toFixed(2) + ''; svg += ''; this.el.innerHTML = svg; } } P.ResonanceCurve = ResonanceCurve; /* ============================================================ */ /* TransverseWave — поперечная волна на струне */ /* y(x, t) = A sin(kx - ωt + φ) */ /* ============================================================ */ class TransverseWave { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 560; this.H = opts.height || 180; this.A = opts.A !== undefined ? opts.A : 0.4; /* отн. амплитуда (0..1) */ this.lambda = opts.lambda !== undefined ? opts.lambda : 1.0; /* отн. длина волны */ this.v = opts.v !== undefined ? opts.v : 0.8; /* скорость распространения (отн./с) */ this.color = opts.color || '#0891b2'; this.markerX = opts.markerX !== undefined ? opts.markerX : 0.4; /* пол. красной точки (0..1 от ширины) */ this.paused = false; this.t = 0; util.subscribe(this); util.observe(this); this.render(); } setA(v){ this.A = v; } setLambda(v){ this.lambda = Math.max(0.1, v); } setV(v){ this.v = v; } update(dt){ this.t += dt; } render(){ if (!this.el) return; const W = this.W, H = this.H; const yCenter = H / 2; const amp = this.A * (H / 2 - 18); const k = 2 * Math.PI / this.lambda; const omega = k * this.v; /* SVG: горизонтальная ось + волна как polyline */ let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Ось */ svg += ''; /* Кривая */ const N = 180; let path = 'M '; for (let i = 0; i <= N; i++){ const px = (W * i / N); /* Реальное x в относительных единицах (1 длина волны на ~120px) */ const x = px / 120; const y = yCenter - amp * Math.sin(k * x - omega * this.t); path += px.toFixed(1) + ',' + y.toFixed(1); if (i < N) path += ' L '; } svg += ''; /* Красный маркер — колеблющаяся точка */ const mPx = this.markerX * W; const mX = mPx / 120; const mY = yCenter - amp * Math.sin(k * mX - omega * this.t); svg += ''; svg += ''; /* Метка λ — горизонтальная скобка над волной */ const lambdaPx = 120 * this.lambda; if (lambdaPx < W - 60){ const lxStart = 20, lxEnd = lxStart + lambdaPx; svg += ''; svg += ''; svg += ''; svg += 'λ'; } svg += ''; this.el.innerHTML = svg; } } P.TransverseWave = TransverseWave; /* ============================================================ */ /* LongitudinalWave — продольная волна (сжатия/разрежения) */ /* ============================================================ */ class LongitudinalWave { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 560; this.H = opts.height || 130; this.A = opts.A !== undefined ? opts.A : 0.5; /* амплитуда (0..1) */ this.lambda = opts.lambda !== undefined ? opts.lambda : 1.0; this.v = opts.v !== undefined ? opts.v : 0.8; this.color = opts.color || '#0891b2'; this.nDots = opts.nDots || 60; this.paused = false; this.t = 0; util.subscribe(this); util.observe(this); this.render(); } setA(v){ this.A = v; } setLambda(v){ this.lambda = Math.max(0.1, v); } setV(v){ this.v = v; } update(dt){ this.t += dt; } render(){ if (!this.el) return; const W = this.W, H = this.H; const yC = H / 2; const k = 2 * Math.PI / this.lambda; const omega = k * this.v; const xScale = 120; /* px на 1 ед. */ const amp = this.A * 10; /* px смещения */ const margin = 20; let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Точки-молекулы */ let dots = ''; for (let i = 0; i < this.nDots; i++){ const x0 = margin + (W - 2 * margin) * i / (this.nDots - 1); const xRel = x0 / xScale; const disp = amp * Math.sin(k * xRel - omega * this.t); const x = x0 + disp; dots += ''; } svg += dots; /* Подписи зон сжатия / разрежения */ svg += 'сжатие ↔ разрежение'; svg += ''; this.el.innerHTML = svg; } } P.LongitudinalWave = LongitudinalWave; /* ============================================================ */ /* LCcircuit — колебательный контур */ /* q(t) = Q0 cos(ωt), i(t) = -Q0 ω sin(ωt), ω = 1/√(LC) */ /* ============================================================ */ class LCcircuit { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 480; this.H = opts.height || 280; this.L = opts.L !== undefined ? opts.L : 0.01; /* Гн */ this.C = opts.C !== undefined ? opts.C : 1e-6; /* Ф */ this.Q0 = opts.Q0 !== undefined ? opts.Q0 : 1.0; /* нормированный заряд */ this.color = opts.color || '#7c3aed'; this.paused = false; this.t = 0; util.subscribe(this); util.observe(this); this.render(); } setL(L){ this.L = Math.max(1e-4, L); } setC(C){ this.C = Math.max(1e-9, C); } period(){ return 2 * Math.PI * Math.sqrt(this.L * this.C); } freq(){ return 1 / this.period(); } update(dt){ this.t += dt; } render(){ if (!this.el) return; const W = this.W, H = this.H; const T = this.period(); const omega = 2 * Math.PI / T; const phase = omega * this.t; const q = this.Q0 * Math.cos(phase); const i = -this.Q0 * omega * Math.sin(phase); /* Геометрия: C слева вверху, L справа вверху, соединены проводами */ const cx = W / 2, cy = H / 2 - 20; const cap = {x: cx - 100, y: cy}; const ind = {x: cx + 100, y: cy}; /* Energies (для подсветки): W_C ~ q², W_L ~ i² */ const WC = q * q; const WL = (i / omega) * (i / omega); /* в норм. единицах */ const total = WC + WL; const cOpacity = 0.3 + 0.7 * WC / total; const lOpacity = 0.3 + 0.7 * WL / total; let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Провода */ const wireY1 = cy - 50, wireY2 = cy + 50; svg += ''; svg += ''; /* Конденсатор: две параллельные пластины */ const plateW = 36; svg += ''; svg += ''; svg += ''; svg += ''; /* Заряды на пластинах */ const sign = q > 0 ? 1 : -1; const topCh = sign > 0 ? '+' : '−'; const botCh = sign > 0 ? '−' : '+'; const qAbsNorm = Math.abs(q) / this.Q0; svg += '' + topCh + ''; svg += '' + botCh + ''; svg += 'C'; /* Индуктор: петли */ const coils = 4, coilR = 8, coilW = 64; let coilPath = 'M ' + (ind.x - coilW/2) + ' ' + ind.y; for (let k = 0; k < coils; k++){ const x0 = ind.x - coilW/2 + (coilW / coils) * k; coilPath += ' a ' + coilR + ' ' + coilR + ' 0 0 1 ' + (coilW / coils) + ' 0'; } svg += ''; svg += 'L'; /* Стрелка тока */ const iDir = i > 0 ? 1 : -1; const iAbs = Math.abs(i) / (this.Q0 * omega); if (iAbs > 0.05){ const aY = wireY1 - 14; const aX1 = cx - 30 * iDir; const aX2 = cx + 30 * iDir; svg += ''; svg += ''; svg += 'i'; } /* Энергетические столбцы */ const eY = H - 36, eH = 24; svg += ''; svg += ''; svg += 'W_C'; svg += ''; svg += ''; svg += 'W_L'; /* Подпись периода */ const Tdisp = T < 1e-3 ? (T * 1e6).toFixed(1) + ' мкс' : (T * 1e3).toFixed(2) + ' мс'; svg += 'T = 2π√(LC) = ' + Tdisp + ''; svg += ''; this.el.innerHTML = svg; } } P.LCcircuit = LCcircuit; /* ============================================================ */ /* ACgen — генератор переменного тока (вращающаяся рамка в B) */ /* ============================================================ */ class ACgen { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 540; this.H = opts.height || 240; this.omega = opts.omega !== undefined ? opts.omega : 2 * Math.PI; /* рад/с */ this.U0 = opts.U0 !== undefined ? opts.U0 : 1.0; this.color = opts.color || '#7c3aed'; this.tWindow = opts.tWindow || 4; this.paused = false; this.t = 0; this.history = []; util.subscribe(this); util.observe(this); this.render(); } setOmega(w){ this.omega = w; this.history = []; } update(dt){ this.t += dt; this.history.push([this.t, this.U0 * Math.sin(this.omega * this.t)]); while (this.history.length && this.history[0][0] < this.t - this.tWindow) this.history.shift(); } render(){ if (!this.el) return; const W = this.W, H = this.H; /* Левая часть: рамка в магнитном поле; правая: график U(t) */ const leftW = 200; const rightLeft = leftW + 10; let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Магнитное поле (стрелки B) */ svg += ''; for (let i = 0; i < 5; i++){ const x = 20 + i * 40; svg += ''; } svg += ''; svg += ''; svg += 'B'; /* Вращающаяся рамка: эллипс, отображающий проекцию прямоугольника */ const fx = 110, fy = H / 2; const phi = this.omega * this.t; const rx = 40 * Math.abs(Math.cos(phi)); const ry = 30; svg += ''; /* Ось вращения */ svg += ''; /* Контакт скользящих колец (схема) */ svg += ''; svg += ''; /* График U(t) — справа */ const gPad = 26; const tMin = Math.max(0, this.t - this.tWindow); const ax = util.axes(W - rightLeft, H, gPad, this.tWindow, [-this.U0 * 1.2, this.U0 * 1.2]); svg += '' + ax.svg + ''; if (this.history.length > 1){ const pts = this.history.map(([t, y]) => (rightLeft + ax.left + (t - tMin) * (ax.right - ax.left) / this.tWindow).toFixed(1) + ',' + ax.toY(y).toFixed(1)); svg += ''; } /* Подпись */ svg += 'U(t) = U₀ sin(ωt)'; svg += ''; this.el.innerHTML = svg; } } P.ACgen = ACgen; /* ============================================================ */ /* Transformer — схема трансформатора с расчётом */ /* ============================================================ */ class Transformer { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 520; this.H = opts.height || 240; this.N1 = opts.N1 || 200; this.N2 = opts.N2 || 50; this.U1 = opts.U1 || 220; this.color = opts.color || '#7c3aed'; this.render(); } setN1(n){ this.N1 = Math.max(1, n|0); this.render(); } setN2(n){ this.N2 = Math.max(1, n|0); this.render(); } setU1(u){ this.U1 = u; this.render(); } update(){ /* статика */ } render(){ if (!this.el) return; const W = this.W, H = this.H; const k = this.N1 / this.N2; const U2 = this.U1 / k; const cy = H / 2; let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Сердечник трансформатора (прямоугольник с вырезом) */ const coreL = 110, coreR = W - 110; svg += ''; svg += ''; /* Первичная обмотка слева */ const coil1X = coreL + 24; for (let i = 0; i < 6; i++){ const y = cy - 50 + i * 16; svg += ''; } /* Вторичная обмотка справа */ const coil2X = coreR - 24; /* Адаптируем число витков визуально (max 8 для удобства) */ const visTurns2 = Math.max(2, Math.min(10, Math.round(6 * this.N2 / this.N1))); for (let i = 0; i < visTurns2; i++){ const y = cy - 50 + i * (100 / visTurns2); svg += ''; } /* Провода-выходы первичной */ svg += ''; svg += ''; /* Провода вторичной */ svg += ''; svg += ''; /* Подписи N1, N2, U1, U2 */ svg += 'N₁ = ' + this.N1 + ''; svg += 'N₂ = ' + this.N2 + ''; svg += 'N₂ = ' + this.N2 + ''; svg += 'U₁ = ' + this.U1.toFixed(0) + ' В'; svg += 'U₂ = ' + U2.toFixed(1) + ' В'; /* Подпись коэф. */ svg += 'k = N₁/N₂ = ' + k.toFixed(2) + ''; svg += '' + (k > 1 ? 'понижающий' : 'повышающий') + ''; svg += ''; this.el.innerHTML = svg; } } P.Transformer = Transformer; /* ============================================================ */ /* TwoSlit — интерференция от двух щелей (опыт Юнга) */ /* ============================================================ */ class TwoSlit { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 540; this.H = opts.height || 200; this.d = opts.d !== undefined ? opts.d : 0.5; /* расстояние между щелями (отн. ед.) */ this.L = opts.L !== undefined ? opts.L : 5; /* расстояние до экрана */ this.lambda = opts.lambda !== undefined ? opts.lambda : 0.05; /* длина волны */ this.color = opts.color || '#f59e0b'; this.paused = true; /* статика */ this.render(); } setD(v){ this.d = Math.max(0.05, v); this.render(); } setLambda(v){ this.lambda = Math.max(0.005, v); this.render(); } update(){} render(){ if (!this.el) return; const W = this.W, H = this.H; /* Слева: щели (две точки), справа: экран с полосами */ let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Лазер слева */ svg += ''; svg += 'laser'; /* Щели */ const slitX = 130; const dPx = Math.min(60, this.d * 60); const s1y = H/2 - dPx/2, s2y = H/2 + dPx/2; svg += ''; svg += ''; svg += ''; /* Лучи от лазера к щелям */ svg += ''; svg += ''; /* Экран */ const screenX = W - 50; svg += ''; /* Интерференционная картина: интенсивность ~ cos²(πdy/(λL)) */ const lp = this.lambda * this.L / this.d; /* шаг полос */ const lpPx = Math.max(3, Math.min(80, lp * 200)); const halfH = (H - 40) / 2; const N = 240; for (let i = 0; i < N; i++){ const y = 20 + (H - 40) * i / N; const ry = y - H/2; const intens = Math.pow(Math.cos(Math.PI * ry / lpPx), 2); const op = intens.toFixed(3); svg += ''; } /* Подпись формулы */ svg += 'd·sin φ = k·λ (max), d=' + this.d.toFixed(2) + ', λ=' + this.lambda.toFixed(3) + ''; svg += ''; this.el.innerHTML = svg; } } P.TwoSlit = TwoSlit; /* ============================================================ */ /* DiffractionGrating — дифракционная решётка + спектр */ /* ============================================================ */ class DiffractionGrating { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 540; this.H = opts.height || 220; this.d = opts.d !== undefined ? opts.d : 2e-6; /* период решётки, м */ this.lambda = opts.lambda !== undefined ? opts.lambda : 550e-9; /* λ, м */ this.color = opts.color || '#22c55e'; this.paused = true; this.render(); } setD(v){ this.d = Math.max(0.3e-6, v); this.render(); } setLambda(v){ this.lambda = Math.max(380e-9, Math.min(760e-9, v)); this.render(); } update(){} /* Возвращает цвет HEX для данной длины волны (для видимого света) */ wavelengthToColor(lamNm){ const ranges = [ [380, 440, '#7c3aed'], [440, 490, '#3b82f6'], [490, 520, '#06b6d4'], [520, 570, '#22c55e'], [570, 590, '#facc15'], [590, 630, '#f97316'], [630, 760, '#dc2626'] ]; for (const [lo, hi, c] of ranges) if (lamNm >= lo && lamNm < hi) return c; return '#94a3b8'; } render(){ if (!this.el) return; const W = this.W, H = this.H; let svg = util.svgFrame(W, H, {bg:'#0f172a'}); /* тёмный фон для спектра */ /* Решётка слева */ const gx = 50; svg += ''; /* «штрихи» решётки */ for (let i = 0; i < 10; i++){ const y = 40 + i * (H - 80) / 10; svg += ''; } svg += 'решётка'; /* Падающий луч */ svg += ''; /* Линии порядков k = -3..3 */ const cx = gx + 6, cy = H/2; const lamNm = this.lambda * 1e9; const color = this.wavelengthToColor(lamNm); for (let k = -3; k <= 3; k++){ const sinPhi = k * this.lambda / this.d; if (Math.abs(sinPhi) > 1) continue; const phi = Math.asin(sinPhi); const dx = 400, dy = dx * Math.tan(phi); const x2 = cx + dx, y2 = cy - dy; const op = k === 0 ? 1.0 : (1 - Math.abs(k) * 0.18); svg += ''; svg += 'k=' + k + ''; } /* Подпись формулы и параметров */ svg += 'd sin φ = kλ · d=' + (this.d * 1e6).toFixed(2) + ' мкм · λ=' + (this.lambda * 1e9).toFixed(0) + ' нм'; svg += ''; this.el.innerHTML = svg; } } P.DiffractionGrating = DiffractionGrating; /* ============================================================ */ /* FlatMirror — плоское зеркало, объект и мнимое изображение */ /* ============================================================ */ class FlatMirror { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 540; this.H = opts.height || 240; this.objX = opts.objX !== undefined ? opts.objX : 100; /* px от зеркала */ this.objY = opts.objY !== undefined ? opts.objY : 50; this.objH = opts.objH !== undefined ? opts.objH : 50; this.color = opts.color || '#f59e0b'; this.paused = true; this.render(); } setObjX(v){ this.objX = Math.max(20, v); this.render(); } setObjY(v){ this.objY = v; this.render(); } update(){} render(){ if (!this.el) return; const W = this.W, H = this.H; const cy = H / 2 + 30; const mirrorX = W / 2; let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Зеркало с штриховкой */ svg += ''; for (let i = 0; i < 12; i++){ const y = 30 + i * (H - 60) / 12; svg += ''; } svg += 'зеркало'; /* Объект — стрелка */ const objX = mirrorX - this.objX; const objBaseY = cy; const objTopY = cy - this.objH; svg += ''; svg += ''; svg += 'объект'; /* Мнимое изображение справа от зеркала, симметрично */ const imgX = mirrorX + this.objX; svg += ''; svg += ''; svg += 'изображение'; /* Лучи: 1) от верха объекта горизонтально на зеркало, отражается под тем же углом 2) от верха объекта в зеркало по диагонали, отражается симметрично */ /* Луч 1 */ svg += ''; svg += ''; /* Продолжение в зазеркалье (пунктир) */ svg += ''; /* Луч 2: от верха к точке наблюдения слева внизу */ const eyeX = 40, eyeY = H - 50; const hitY = objTopY + (cy - objTopY) * (mirrorX - objX) / (eyeX - objX + 2 * (mirrorX - objX)); /* Упрощённо: луч от верха объекта к зеркалу и затем к глазу */ const hitX = mirrorX; const hitYsimple = objTopY + (cy + 30 - objTopY) * 0.3; svg += ''; svg += ''; /* Продолжение от точки отражения в зазеркалье */ svg += ''; /* Глаз */ svg += ''; svg += ''; svg += 'наблюдатель'; /* Подпись закона */ svg += '∠ пад = ∠ отр · изображение мнимое, прямое, равное'; svg += ''; this.el.innerHTML = svg; } } P.FlatMirror = FlatMirror; /* ============================================================ */ /* SphericalMirror — вогнутое / выпуклое зеркало */ /* ============================================================ */ class SphericalMirror { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 620; this.H = opts.height || 280; this.F = opts.F !== undefined ? opts.F : 80; /* фокусное расстояние в px */ this.d = opts.d !== undefined ? opts.d : 180; /* расстояние до объекта в px */ this.objH = opts.objH !== undefined ? opts.objH : 50; /* высота объекта в px */ this.mode = opts.mode || 'concave'; /* 'concave' | 'convex' */ this.color = opts.color || '#d97706'; this.paused = true; this.render(); } setF(v){ this.F = Math.max(20, v); this.render(); } setD(v){ this.d = Math.max(20, v); this.render(); } setMode(m){ this.mode = m; this.render(); } update(){} render(){ if (!this.el) return; const W = this.W, H = this.H; const cy = H / 2; const mirrorX = W - 80; /* зеркало справа */ const F = (this.mode === 'concave') ? this.F : -this.F; const focusX = mirrorX - F; const centerX = mirrorX - 2 * F; const d = this.d; const objX = mirrorX - d; /* Формула 1/d + 1/f = 1/F → f = 1/(1/F - 1/d) */ let f; if (Math.abs(1/F - 1/d) < 1e-6) f = 1e9; else f = 1 / (1/F - 1/d); const imgX = mirrorX - f; /* Линейное увеличение Γ = -f/d (мнимое: f<0 → прямое, действ.: f>0 → перевёрнутое) */ const G = -f / d; const imgH = this.objH * G; let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Главная оптическая ось */ svg += ''; /* Зеркало (дуга) */ const R = 2 * Math.abs(F); if (this.mode === 'concave'){ svg += ''; } else { svg += ''; } /* Штриховка зеркала */ for (let i = 0; i < 8; i++){ const y = cy - 90 + i * 22; const dx = this.mode === 'concave' ? 8 : -8; svg += ''; } /* Точки F и C */ svg += ''; svg += 'F'; if (this.mode === 'concave'){ svg += ''; svg += '2F'; } /* Объект — красная стрелка вверх */ const objTopY = cy - this.objH; svg += ''; svg += ''; /* Лучи (3 канонических) */ /* Луч 1: параллельный оптической оси → отражается через F */ svg += ''; if (this.mode === 'concave'){ svg += ''; /* Продолжение до изображения */ const slope1 = (cy - objTopY) / (focusX - mirrorX); const x2 = imgX, y2 = cy + slope1 * (x2 - focusX); svg += ''; } else { /* Выпуклое: отражается так, словно вышел из мнимого F справа */ const slope = (objTopY - cy) / (mirrorX - focusX); svg += ''; svg += ''; } /* Луч 2: через F → отражается параллельно оси (только вогнутое) */ if (this.mode === 'concave' && objX !== focusX){ const slope2 = (cy - objTopY) / (focusX - objX); const hitY = objTopY + slope2 * (mirrorX - objX); svg += ''; svg += ''; } /* Изображение */ if (Math.abs(imgX) < 1e8 && imgX > 20 && imgX < mirrorX){ const imgTopY = cy - imgH; const dashed = (this.mode === 'convex' || imgH > 0); /* мнимое = пунктир */ const isVirtual = (this.mode === 'convex') || (f < 0); const sd = isVirtual ? ' stroke-dasharray="4 3"' : ''; const op = isVirtual ? 0.7 : 1.0; svg += ''; svg += ''; } /* Подпись параметров */ const Glabel = isFinite(G) ? G.toFixed(2) : '—'; svg += '1/d + 1/f = 1/F · Γ = -f/d = ' + Glabel + ''; svg += '' + (this.mode==='concave'?'вогнутое':'выпуклое') + ' · F=' + Math.abs(F).toFixed(0) + 'px · d=' + d.toFixed(0) + 'px'; svg += ''; this.el.innerHTML = svg; } } P.SphericalMirror = SphericalMirror; /* ============================================================ */ /* RefractionLab — преломление на границе двух сред (Снелл) */ /* ============================================================ */ class RefractionLab { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 540; this.H = opts.height || 320; this.n1 = opts.n1 !== undefined ? opts.n1 : 1.0; /* воздух */ this.n2 = opts.n2 !== undefined ? opts.n2 : 1.5; /* стекло */ this.alpha = opts.alpha !== undefined ? opts.alpha : 35; /* градусов */ this.color = opts.color || '#d97706'; this.paused = true; this.render(); } setN1(v){ this.n1 = Math.max(1, v); this.render(); } setN2(v){ this.n2 = Math.max(1, v); this.render(); } setAlpha(v){ this.alpha = Math.max(0, Math.min(89, v)); this.render(); } update(){} render(){ if (!this.el) return; const W = this.W, H = this.H; const cx = W / 2, cy = H / 2; /* Углы (рад) */ const a = this.alpha * Math.PI / 180; const sinB = this.n1 / this.n2 * Math.sin(a); const totalInternal = Math.abs(sinB) > 1; const b = totalInternal ? null : Math.asin(sinB); /* Критический угол n1>n2 */ let critDeg = null; if (this.n1 > this.n2) critDeg = Math.asin(this.n2 / this.n1) * 180 / Math.PI; let svg = util.svgFrame(W, H, {bg:'#f8fafc'}); /* Среда 1 (верх) */ svg += ''; /* Среда 2 (низ) */ svg += ''; /* Граница */ svg += ''; /* Нормаль (пунктир) */ svg += ''; svg += 'нормаль'; /* Падающий луч (из верхней-левой области) */ const L = 130; const ix = cx - L * Math.sin(a), iy = cy - L * Math.cos(a); svg += ''; /* Стрелка падающего */ const ang_i = Math.atan2(cy - iy, cx - ix); const arrPx = cx - 30 * Math.cos(ang_i), arrPy = cy - 30 * Math.sin(ang_i); svg += ''; /* Дуга угла падения */ svg += ''; svg += 'α'; /* Отражённый луч */ const rx = cx + L * Math.sin(a), ry = cy - L * Math.cos(a); svg += ''; svg += 'отраж.'; /* Преломлённый луч или полное отражение */ if (totalInternal){ svg += 'полное внутреннее отражение'; } else { const tx = cx + L * Math.sin(b), ty = cy + L * Math.cos(b); svg += ''; const ang_t = Math.atan2(ty - cy, tx - cx); const apx = cx + 30 * Math.cos(ang_t), apy = cy + 30 * Math.sin(ang_t); svg += ''; svg += ''; svg += 'β'; } /* Подписи сред */ svg += 'n₁ = ' + this.n1.toFixed(2) + ''; svg += 'n₂ = ' + this.n2.toFixed(2) + ''; /* Формула + углы */ const bDeg = totalInternal ? '—' : (b * 180 / Math.PI).toFixed(1); svg += 'n₁ sin α = n₂ sin β'; svg += 'α=' + this.alpha.toFixed(0) + '° · β=' + bDeg + '°' + (critDeg!==null?' · αкр=' + critDeg.toFixed(1) + '°':'') + ''; svg += ''; this.el.innerHTML = svg; } } P.RefractionLab = RefractionLab; /* ============================================================ */ /* PrismSpectrum — призма, дисперсия белого света */ /* ============================================================ */ class PrismSpectrum { constructor(container, opts){ opts = opts || {}; this.el = (typeof container === 'string') ? document.querySelector(container) : container; this.W = opts.width || 580; this.H = opts.height || 260; this.alpha = opts.alpha !== undefined ? opts.alpha : 50; /* угол падения, градусы */ this.paused = true; this.render(); } setAlpha(v){ this.alpha = Math.max(20, Math.min(75, v)); this.render(); } update(){} /* Показатели преломления стекла для разных длин волн (упрощённая модель Коши) */ nForColor(lamNm){ return 1.5 + 6500 / (lamNm * lamNm); /* красный ~1.518, фиолет ~1.530 */ } render(){ if (!this.el) return; const W = this.W, H = this.H; /* Геометрия равностороннего треугольника-призмы */ const cx = W / 2, cy = H / 2 + 10; const side = 140; const h = side * Math.sqrt(3) / 2; const A = { x: cx, y: cy - h * 2/3 }; /* верхняя вершина */ const B = { x: cx - side/2, y: cy + h/3 }; /* нижняя левая */ const C = { x: cx + side/2, y: cy + h/3 }; /* нижняя правая */ let svg = util.svgFrame(W, H, {bg:'#0f172a'}); /* Призма (полупрозрачная) */ svg += ''; /* Падающий белый луч на грань AB */ const a = this.alpha * Math.PI / 180; /* Точка входа — середина грани AB */ const Pin = { x: (A.x + B.x) / 2, y: (A.y + B.y) / 2 }; /* Нормаль к AB (наружу, влево-вверх) */ const ABx = B.x - A.x, ABy = B.y - A.y; const Lab = Math.hypot(ABx, ABy); const nABx = -ABy / Lab, nABy = ABx / Lab; /* перпендикуляр */ /* Источник падающего луча */ const inLen = 160; const inx = Pin.x + inLen * (nABx * Math.cos(a) - (ABx/Lab) * Math.sin(a)); const iny = Pin.y + inLen * (nABy * Math.cos(a) - (ABy/Lab) * Math.sin(a)); svg += ''; svg += 'белый'; /* 7 цветов спектра — каждый со своим n и углом преломления */ const colors = [ { lam: 700, c: '#dc2626' }, /* красный */ { lam: 620, c: '#ea580c' }, /* оранжевый */ { lam: 580, c: '#facc15' }, /* жёлтый */ { lam: 520, c: '#16a34a' }, /* зелёный */ { lam: 480, c: '#06b6d4' }, /* голубой */ { lam: 450, c: '#1d4ed8' }, /* синий */ { lam: 420, c: '#7c3aed' } /* фиолетовый */ ]; /* Нормаль к BC (наружу, вправо-вниз) */ const BCx = C.x - B.x, BCy = C.y - B.y; const Lbc = Math.hypot(BCx, BCy); const nBCx = BCy / Lbc, nBCy = -BCx / Lbc; /* Точки выхода — равномерно по грани BC */ const outLen = 180; for (let i = 0; i < colors.length; i++){ const co = colors[i]; const n = this.nForColor(co.lam); /* Преломление при входе: sin β = sin α / n */ const sinB = Math.sin(a) / n; if (Math.abs(sinB) > 1) continue; const beta = Math.asin(sinB); /* Внутри призмы луч идёт от Pin под углом beta от нормали к AB, в сторону BC */ /* Направление внутри: поворот нормали-в-стекло (-nAB) на beta */ const dirX = -nABx * Math.cos(beta) + (ABx/Lab) * Math.sin(beta); const dirY = -nABy * Math.cos(beta) + (ABy/Lab) * Math.sin(beta); /* Найти точку выхода на BC (параметрический луч / линия BC) */ const denom = dirX * (-(C.y - B.y)) + dirY * (C.x - B.x); if (Math.abs(denom) < 1e-6) continue; const t = ((B.x - Pin.x) * (-(C.y - B.y)) + (B.y - Pin.y) * (C.x - B.x)) / denom; const Pout = { x: Pin.x + t * dirX, y: Pin.y + t * dirY }; /* Луч внутри */ svg += ''; /* Преломление при выходе: угол к нормали BC внутри */ const cosIn = -(dirX * nBCx + dirY * nBCy); /* направление к внешней нормали */ const sinIn = Math.sqrt(Math.max(0, 1 - cosIn * cosIn)); const sinOut = sinIn * n; if (sinOut > 1) continue; const cosOut = Math.sqrt(1 - sinOut * sinOut); /* Тангенциальная составляющая (вдоль BC) */ const tBCx = BCx / Lbc, tBCy = BCy / Lbc; const tanSign = (dirX * tBCx + dirY * tBCy) >= 0 ? 1 : -1; const outX = nBCx * cosOut + tBCx * sinOut * tanSign; const outY = nBCy * cosOut + tBCy * sinOut * tanSign; const Pend = { x: Pout.x + outLen * outX, y: Pout.y + outLen * outY }; svg += ''; } /* Подпись */ svg += 'дисперсия: n(λ) растёт при уменьшении λ → фиолетовый отклоняется сильнее красного'; svg += 'α = ' + this.alpha.toFixed(0) + '° · стекло (модель Коши)'; svg += ''; this.el.innerHTML = svg; } } P.PrismSpectrum = PrismSpectrum; })();