Embed
The script finds its own origin, so nothing else is needed; the widget runs in a sandboxed frame and cannot touch your page. Set the controls and the two lines update; the preview beneath shows what your readers get.
<script src="https://learn.mimmsy.com/learn-widget.js"></script>
<learn-widget name="sinusoid-phase-lab"></learn-widget> It is CC-BY-4.0: keep the credit line the frame shows.
Preview
Fork
A fork is a copy whose manifest names its parent and the parent's version; lineage is kept forever, and the copy is yours to change. The library is its own repository: clone it, copy sinusoid-phase-lab/ to a new name (one DNS label), set forkedFrom to { "name": "sinusoid-phase-lab", "version": 1 }, change what you want, run npm run check, and open a pull request. Passing the check is the whole gate. Without a checkout, submit the same files to POST /api/widgets and it is served instantly as an unreviewed draft.
Source
The whole widget is these files; the repository has their history.
widget.json
{
"name": "sinusoid-phase-lab",
"title": "Amplitude, frequency, phase",
"version": 1,
"claim": "Adding a phase φ to A·sin(2πft + φ) slides the waveform earlier by φ/(2πf) seconds without changing its shape, amplitude or frequency.",
"summary": "Adjust amplitude, frequency and phase against a fixed reference sinusoid (A = 0.70, f = 3 Hz, φ = 0°). With A and f matched, the phase slider slides the trace along the time axis, and at 360° the traces coincide again.",
"topics": [
"signals/sinusoids",
"math/parameterization"
],
"aliases": [
"phase",
"phase shift",
"sinusoid",
"time shift"
],
"params": {
"amp": {
"type": "number",
"default": 0.7,
"min": 0.05,
"max": 1,
"label": "amplitude"
},
"freq": {
"type": "integer",
"default": 3,
"min": 1,
"max": 8,
"label": "frequency (Hz)"
},
"phase": {
"type": "integer",
"default": 0,
"min": 0,
"max": 360,
"label": "phase (degrees)"
}
},
"check": [
{
"q": "Two sinusoids share amplitude and frequency but have phases 0° and 90°. They differ in…",
"options": [
"pitch",
"timing only; one is shifted by a quarter period",
"waveform shape",
"loudness"
],
"answer": 1,
"why": "Phase shifts the waveform in time by φ/(2πf) without changing shape, amplitude or frequency; 90° is one quarter of a full cycle."
}
],
"capabilities": [],
"height": 505,
"requires": [],
"forkedFrom": null,
"authors": []
}
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Amplitude, frequency, phase</title>
<style>
:root {
color-scheme: light dark;
--bg: #f7f3ea;
--bg-card: #efe9da;
--border: #d8cfba;
--text: #211d14;
--text-dim: #6e6553;
--accent: #31597f;
--accent2: #b04e1b;
--hot: #a82433;
--ok: #3d6b4f;
--ink-rgb: 33, 29, 20;
--paper-rgb: 247, 243, 234;
--accent-rgb: 49, 89, 127;
--accent2-rgb: 176, 78, 27;
--hot-rgb: 168, 36, 51;
--ok-rgb: 61, 107, 79;
--serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
--mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #161410;
--bg-card: #1e1b15;
--border: #383225;
--text: #e9e3d3;
--text-dim: #9c917c;
--accent: #8fb8e0;
--accent2: #dd9355;
--hot: #df7a88;
--ok: #82bd97;
--ink-rgb: 233, 227, 211;
--paper-rgb: 22, 20, 16;
--accent-rgb: 143, 184, 224;
--accent2-rgb: 221, 147, 85;
--hot-rgb: 223, 122, 136;
--ok-rgb: 130, 189, 151;
}
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: transparent; color: var(--text); font-family: var(--serif); font-size: 15px; line-height: 1.5; }
.widget { background: var(--bg-card); border: 1px solid var(--border); border-radius: 4px; padding: 22px; }
.control-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
.control-row label { font-size: 14px; color: var(--text-dim); white-space: nowrap; }
.control-row.sym { display: grid; grid-template-columns: 88px 1fr 1fr; gap: 12px 16px; align-items: center; }
.control-row.sym.single { grid-template-columns: 88px 1fr; }
.ctl { display: flex; align-items: center; gap: 10px; min-width: 0; }
.ctl input[type="range"] { flex: 1; min-width: 0; }
.ctl .val { flex: none; width: 64px; text-align: right; font-size: 13px; }
@media (max-width: 600px) {
.control-row.sym { grid-template-columns: 64px 1fr; }
.control-row.sym .ctl:nth-of-type(2) { grid-column: 2; }
}
input[type="range"] { flex: 1; min-width: 110px; accent-color: var(--accent); }
.toggle { display: inline-flex; align-items: baseline; gap: 7px; font-size: 13.5px; color: var(--text-dim); cursor: pointer; }
.toggle input { accent-color: var(--accent2); }
.control-row .toggle { white-space: normal; }
.mono { font-family: var(--mono); }
.dim { color: var(--text-dim); font-size: 12.5px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.chip, .btn {
font-family: var(--mono); font-size: 12.5px; color: var(--text); background: transparent;
border: 1px solid var(--border); border-radius: 3px; padding: 6px 13px; cursor: pointer;
transition: border-color 0.15s, color 0.15s;
}
.chip:hover, .btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { border-color: var(--accent); color: var(--accent); }
.btn.playing, .btn.playing:hover { border-color: var(--hot); color: var(--hot); }
.btn.off { border-color: var(--hot); color: var(--hot); opacity: 0.85; }
.btn:disabled { opacity: 0.35; cursor: default; }
.btn:disabled:hover { border-color: var(--border); color: var(--text); }
.chip.sel-chip { border-color: var(--accent); color: var(--accent); }
.readout-row { display: flex; gap: 28px; flex-wrap: wrap; margin: 14px 0 0; }
.readout { text-align: left; }
.readout.inline { margin-left: auto; text-align: right; }
.readout .big { display: block; font-size: 30px; font-weight: 600; color: var(--accent); line-height: 1.1; min-width: 5ch; }
.readout.accent .big { color: var(--accent2); }
.readout .big.hot { color: var(--hot); }
.readout small { color: var(--text-dim); font-size: 12.5px; }
@media (max-width: 560px) { .readout .big { font-size: 25px; } }
.plot { display: block; width: 100%; height: 220px; margin-top: 8px; }
.plot.tall { height: 280px; }
.plot.duo { height: 320px; }
.plot.phasor { height: 400px; }
.plot.spec { height: 230px; }
.mono, .ctl .val, .readout .big, .readout small { font-variant-numeric: tabular-nums; }
</style>
</head>
<body>
<div class="widget">
<div class="control-row sym single">
<label for="sl-a">amplitude</label>
<span class="ctl wide"><input type="range" id="sl-a" min="5" max="100" value="70"><span class="mono val" id="sl-a-label">0.70</span></span>
</div>
<div class="control-row sym single">
<label for="sl-f">frequency</label>
<span class="ctl wide"><input type="range" id="sl-f" min="1" max="8" value="3"><span class="mono val" id="sl-f-label">3 Hz</span></span>
</div>
<div class="control-row sym single">
<label for="sl-ph">phase</label>
<span class="ctl wide"><input type="range" id="sl-ph" min="0" max="360" value="0"><span class="mono val" id="sl-ph-label">0°</span></span>
</div>
<canvas id="sl-canvas" class="plot tall"></canvas>
<div class="readout-row">
<div class="readout">
<span class="big mono" id="sl-shift">0.0</span>
<small>time shift from phase · φ/(2πf) · ms</small>
</div>
<div class="readout">
<span class="big mono" id="sl-match">aligned</span>
<small>vs. reference at A = 0.70, f = 3 Hz, φ = 0°</small>
</div>
</div>
</div>
<script src="/w/_sdk/host.js?v=1"></script>
<script src="/w/_lib/dsp.js?v=1"></script>
<script src="widget.js?v=1"></script>
</body>
</html>
widget.js
/* Sinusoid lab: A, f and φ against a fixed reference; phase slides the waveform without deforming it. */
(() => {
'use strict';
const $ = (id) => document.getElementById(id);
const { T, setupCanvas, hline, trace, signal } = DSP;
const REF = { a: 0.7, f: 3, ph: 0 };
function render() {
const a = $('sl-a').value / 100;
const f = +$('sl-f').value;
const deg = +$('sl-ph').value;
const ph = (deg * Math.PI) / 180;
$('sl-a-label').textContent = a.toFixed(2);
$('sl-f-label').textContent = f + ' Hz';
$('sl-ph-label').textContent = deg + '°';
$('sl-shift').textContent = ((deg / 360 / f) * 1000).toFixed(1);
const aligned = Math.abs(a - REF.a) < 0.005 && f === REF.f && deg % 360 === 0;
const m = $('sl-match');
m.textContent = aligned ? 'aligned' : 'offset';
m.classList.toggle('hot', !aligned);
const { ctx, w, h } = setupCanvas($('sl-canvas'));
const x0 = 14;
const pw = w - 28;
const mid = h / 2;
const amp = h * 0.4;
ctx.clearRect(0, 0, w, h);
hline(ctx, x0, w - 14, mid, T.GRID);
trace(ctx, (t) => REF.a * Math.sin(2 * Math.PI * REF.f * t + REF.ph), { x0, pw, mid, amp, color: T.ACCENT, alpha: 0.35, lw: 1.6 });
trace(ctx, (t) => a * Math.sin(2 * Math.PI * f * t + ph), { x0, pw, mid, amp, color: T.ACCENT2, lw: 2.2 });
ctx.font = DSP.font(10);
DSP.label(ctx, '1-second window · dim = reference, bright = yours', x0 + 2, 13);
}
['sl-a', 'sl-f', 'sl-ph'].forEach((id) => $(id).addEventListener('input', () => {
render();
signal('interaction', { control: id });
}));
function applyParams(p) {
if (typeof p.amp === 'number') $('sl-a').value = Math.round(p.amp * 100);
if (typeof p.freq === 'number') $('sl-f').value = p.freq;
if (typeof p.phase === 'number') $('sl-ph').value = p.phase;
render();
}
render();
DSP.onResize(render);
DSP.connect({ render, params: applyParams });
})();