// Controls shared by the oscillator slides: the parameter sliders, a presets // dropdown, the volume control, and the mapping from slider values to the // physics of duffing.js. // // Parameters (the slider values, in params): x0, the initial displacement; // freq, the natural frequency f0 in Hz, which takes the sign of the linear // stiffness k (negative only where a page's slider allows it); and optionally // alpha, the cubic stiffness term in s^-2 (x is dimensionless), delta, the // damping coefficient in s^-1, F, the driving force per unit mass in s^-2, // and drive, the driving frequency Omega / 2 pi in Hz. The motion is // x'' + delta x' + (k/m) x + alpha x^3 = F cos(Omega t) (see duffing.js), // with k/m = sgn(f0) (2 pi f0)^2. // // alpha's lower bound moves with f0 and x0, and alpha is raised to it if // needed, so that without a driving force the block cannot escape (a // driving force can still push it over): // - k >= 0: a softening spring (alpha < 0) lets the block escape if it starts // past the barrier, that is if alpha x0^2 <= -(2 pi f0)^2. The bound is // ALPHA_MARGIN of that. The swing never grows past x0. // - k < 0 (a double well): alpha must be positive to hold the block in, and // large enough that the far turning point from rest at x0, where // x^2 = 2 |k/m| / alpha - x0^2, stays within ALPHA_MARGIN X_MAX. // Bounds are rounded inward to the slider's step. import { X_MAX } from './duffing.js'; const ALPHA_MARGIN = 0.95; // The sliders, by parameter name. A slider shows params[name] / scale. label // is text, tex an optional symbol after it (rendered by KaTeX with the rest of // the page), and unit optional HTML after that. minFor(params), if given, is // a lower bound that depends on the other parameters, in slider units. A page // can override any of these per slider. export const SLIDERS = { x0: { label: 'Initial displacement', min: 0, max: 0.5, step: 0.01, format: (v) => v.toFixed(2) }, freq: { label: 'Frequency', tex: 'f_0', min: 0, max: 2000, step: 5, format: (v) => `${v < 0 ? '−' : ''}${Math.abs(v)} Hz` }, alpha: { label: 'Cubic term', tex: '\\alpha', unit: '(107 s−2)', scale: 1e7, min: -10, max: 10, step: 0.1, format: (v) => v.toFixed(1), minFor: ({ x0, freq }) => { const w2 = (2 * Math.PI * freq) ** 2; if (freq < 0) return Math.ceil((2 * w2) / ((ALPHA_MARGIN * X_MAX) ** 2 + x0 ** 2) / 1e6) / 10; return x0 > 0 ? Math.ceil((-ALPHA_MARGIN * w2) / x0 ** 2 / 1e6) / 10 : -Infinity; }, }, delta: { label: 'Damping', tex: '\\delta', unit: '(s−1)', min: 0, max: 20, step: 0.1, format: (v) => v.toFixed(1), }, F: { label: 'Forcing', tex: 'F', unit: '(104 s−2)', scale: 1e4, min: 0, max: 30, step: 0.1, format: (v) => v.toFixed(1), }, drive: { label: 'Driving frequency', tex: '\\Omega / 2\\pi', min: 0, max: 2000, step: 5, format: (v) => `${v} Hz` }, }; // The slider specs for a page: SLIDERS with its per-slider overrides. // names: the sliders the page shows, in SLIDERS order (those in params). export function sliderSpecs(params, overrides = {}) { const names = Object.keys(SLIDERS).filter((name) => name in params); const spec = (name) => ({ ...SLIDERS[name], ...overrides[name] }); return { names, spec }; } // The physics of duffing.js for the slider values (missing ones are 0). export function physicsOf(params) { return { omega2: Math.sign(params.freq) * (2 * Math.PI * params.freq) ** 2, alpha: params.alpha ?? 0, delta: params.delta ?? 0, F: params.F ?? 0, Omega: 2 * Math.PI * (params.drive ?? 0), }; } // HTML for the sliders, one row each (label, slider, value). export function slidersHTML(names, spec) { const rows = names.map((name) => { const s = spec(name); const tex = s.tex ? ` ` : ''; const unit = s.unit ? ` ${s.unit}` : ''; return ``; }); return `
`; } // Wire up sliders built by slidersHTML inside root. onInput(name) is called // after a slider moves, with params (and any bounds) already updated. // Returns { showAll() } to show params after they change elsewhere. export function bindSliders({ root, names, spec, params, onInput }) { const inputs = {}; const outputs = {}; const show = (name) => { const { scale = 1, format } = spec(name); inputs[name].value = params[name] / scale; outputs[name].textContent = format(params[name] / scale); }; // Move the sliders' parameter-dependent lower bounds, raising any value // that falls below its bound. function updateBounds() { for (const name of names) { const { scale = 1, min, max, minFor } = spec(name); if (!minFor) continue; const lo = Math.min(max, Math.max(min, minFor(params))); inputs[name].min = lo; if (params[name] / scale < lo) params[name] = lo * scale; show(name); } } for (const name of names) { const { scale = 1, format } = spec(name); inputs[name] = root.querySelector(`input[data-param="${name}"]`); outputs[name] = root.querySelector(`output[data-param="${name}"]`); show(name); inputs[name].addEventListener('input', () => { params[name] = Number(inputs[name].value) * scale; outputs[name].textContent = format(Number(inputs[name].value)); updateBounds(); onInput(name); }); } updateBounds(); return { showAll: () => { updateBounds(); names.forEach(show); }, }; } // A presets dropdown built into container: presets is [{ name, group?, // params }], with options grouped as listed. Choosing one copies its params // into params and calls onChoose(preset). markCustom() shows "Custom" (call it // when a slider moves by hand). export function createPresetSelect({ container, presets, params, names, onChoose }) { let html = ''; let group = null; presets.forEach((pr, i) => { if ((pr.group ?? null) !== group) { if (group) html += ''; group = pr.group ?? null; if (group) html += `'; container.innerHTML = ``; const select = container.querySelector('select'); const matches = (pr) => names.every((name) => pr.params[name] === params[name]); const current = presets.findIndex(matches); select.value = current >= 0 ? String(current) : ''; select.addEventListener('change', () => { const pr = presets[Number(select.value)]; Object.assign(params, pr.params); onChoose(pr); }); return { markCustom: () => { select.value = ''; } }; } export const VOLUME_ICON = ''; // HTML for the subtle volume control (see sound.js). export const volumeHTML = () => ``;