use it elsewhere

dB ruler

Put it on your own page with two lines, or fork it and make it yours. It is CC-BY-4.0.

Embed

The script finds its own origin, so nothing else is needed; the widget runs in a sandboxed frame and cannot touch your page.

<script src="https://learn.mimmsy.com/learn-widget.js"></script> <learn-widget name="decibels-ruler"></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 decibels-ruler/ to a new name (one DNS label), set forkedFrom to { "name": "decibels-ruler", "version": 3 }, 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": "decibels-ruler",
  "title": "dB ruler",
  "version": 3,
  "claim": "A level in dB is 20·log₁₀ of an amplitude ratio, so doubling the amplitude adds 6.02 dB and multiplying it by 10 adds 20 dB, from any starting level.",
  "summary": "Slide the level from −48 to +100 dB and watch the amplitude and power ratios; the ×2 and ×10 buttons advance the marker by the same step every press.",
  "topics": [
    "audio/decibels",
    "math/logarithms"
  ],
  "aliases": [
    "dB",
    "decibel",
    "level",
    "amplitude ratio",
    "power ratio"
  ],
  "check": [
    {
      "q": "Doubling the amplitude changes the level by",
      "options": [
        "+3 dB",
        "+6 dB",
        "+10 dB",
        "+20 dB"
      ],
      "answer": 1,
      "why": "20·log₁₀ 2 ≈ 6.02 dB."
    },
    {
      "q": "+20 dB means the amplitude ratio is",
      "options": [
        "2×",
        "10×",
        "20×",
        "100×"
      ],
      "answer": 1,
      "why": "10^(20/20) = 10; the power ratio is 100."
    },
    {
      "q": "Why does the amplitude formula use 20 where the power formula uses 10?",
      "options": [
        "Power is proportional to amplitude squared, and squaring doubles the logarithm.",
        "Amplitude is measured twice per cycle.",
        "The choice is a convention, and either factor would work.",
        "Hearing is twice as sensitive to amplitude."
      ],
      "answer": 0,
      "why": "P ∝ a², so 10 log₁₀(a²) = 20 log₁₀ a."
    },
    {
      "q": "A meter reads exactly 0 dB. What does this indicate about the signal?",
      "options": [
        "The signal is equal to the reference; the ratio is 1.",
        "The signal is silent.",
        "The signal is at the threshold of clipping, regardless of the reference.",
        "The signal is too small to measure."
      ],
      "answer": 0,
      "why": "10 log₁₀ 1 = 0, so zero dB marks the reference itself. Silence would be −∞ dB."
    }
  ],
  "height": 440,
  "requires": [
    "logarithms-ruler"
  ],
  "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>dB ruler</title>
<style>
  /* Default palette so the widget looks right before the host's theme message
     arrives (and when opened standalone). The host overrides these on mount. */
  :root {
    color-scheme: light dark;
    --bg-card: #efe9da;
    --border: #d8cfba;
    --text: #211d14;
    --text-dim: #6e6553;
    --accent: #31597f;
    --accent2: #b04e1b;
    --hot: #a82433;
    --ok: #3d6b4f;
    --ink-rgb: 33, 29, 20;
    --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-card: #1e1b15;
      --border: #383225;
      --text: #e9e3d3;
      --text-dim: #9c917c;
      --accent: #8fb8e0;
      --accent2: #dd9355;
      --hot: #df7a88;
      --ok: #82bd97;
      --ink-rgb: 233, 227, 211;
    }
  }

  * { box-sizing: border-box; }
  html, body { margin: 0; }
  body {
    background: transparent;
    color: var(--text);
    font-family: var(--serif);
  }

  .widget {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 22px;
  }

  .control-row.sym.single {
    display: grid;
    grid-template-columns: 88px 1fr;
    gap: 12px 16px;
    align-items: center;
    margin-bottom: 14px;
  }
  .control-row label { font-size: 14.5px; color: var(--text-dim); white-space: nowrap; }
  .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; }
  input[type="range"] { accent-color: var(--accent); }
  @media (max-width: 600px) {
    .control-row.sym.single { grid-template-columns: 64px 1fr; }
  }

  .mono { font-family: var(--mono); }

  .chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
  .btn, .chip {
    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;
  }
  .btn:hover, .chip:hover { border-color: var(--accent); color: var(--accent); }

  .plot { display: block; width: 100%; height: 220px; margin-top: 8px; }

  .readout-row { display: flex; gap: 28px; flex-wrap: wrap; margin: 14px 0 0; }
  .readout { text-align: left; }
  .readout .big {
    display: block;
    font-size: 28px;
    font-weight: 600;
    color: var(--accent);
    line-height: 1.1;
    min-width: 5ch;
  }
  .readout.accent .big { color: var(--accent2); }
  .readout small { color: var(--text-dim); font-size: 12.5px; }
</style>
</head>
<body>
  <div class="widget">
    <div class="control-row sym single">
      <label for="db-a">amplitude ratio</label>
      <span class="ctl wide"><input type="range" id="db-a" min="0" max="1000" value="500"><span class="mono val" id="db-a-label">1.00×</span></span>
    </div>
    <div class="chips">
      <button class="btn" id="db-x2">×2 amplitude</button>
      <button class="btn" id="db-h2">÷2 amplitude</button>
      <button class="btn" id="db-x10">×10 amplitude</button>
      <button class="chip" data-db="0">0 dB</button>
      <button class="chip" data-db="-6.0206">−6 dB</button>
      <button class="chip" data-db="96.33">16-bit span</button>
    </div>
    <canvas id="db-canvas" class="plot"></canvas>
    <div class="readout-row">
      <div class="readout">
        <span class="big mono" id="db-amp">1.00×</span>
        <small>amplitude ratio · a/a&#8202;ref</small>
      </div>
      <div class="readout">
        <span class="big mono" id="db-pow">1.00×</span>
        <small>power ratio · (a/a&#8202;ref)²</small>
      </div>
      <div class="readout accent">
        <span class="big mono" id="db-db">+0.0</span>
        <small>level · dB</small>
      </div>
    </div>
  </div>
  <script src="/w/_sdk/host.js?v=1"></script>
  <script src="widget.js?v=1"></script>
</body>
</html>

widget.js

/* dB ruler: a level in dB with its amplitude and power ratios; ×2 amplitude is always +6.02 dB. */
(() => {
  'use strict';

  const $ = (id) => document.getElementById(id);

  const rootStyle = getComputedStyle(document.documentElement);
  const tv = (n) => rootStyle.getPropertyValue(n).trim();
  const wash = (a) => 'rgba(' + tv('--ink-rgb') + ',' + a + ')';
  let ACCENT, ACCENT2, DIM, INK;
  function readTheme() {
    ACCENT = tv('--accent'); ACCENT2 = tv('--accent2');
    DIM = tv('--text-dim'); INK = tv('--text');
  }

  const DB_MIN = -48;
  const DB_MAX = 100;
  let db = 0;

  const sliderToDb = (v) => DB_MIN + (v / 1000) * (DB_MAX - DB_MIN);
  const dbToSlider = (d) => Math.round(((d - DB_MIN) / (DB_MAX - DB_MIN)) * 1000);

  function fmtRatio(r) {
    if (r >= 10000) return r.toExponential(1) + '×';
    if (r >= 100) return r.toFixed(0) + '×';
    if (r >= 1) return r.toFixed(2) + '×';
    return r.toFixed(3) + '×';
  }

  function setupCanvas(canvas) {
    const dpr = Math.min(window.devicePixelRatio || 1, 2);
    const w = canvas.clientWidth;
    const h = canvas.clientHeight;
    canvas.width = Math.max(1, w * dpr);
    canvas.height = Math.max(1, h * dpr);
    const ctx = canvas.getContext('2d');
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
    return { ctx, w, h };
  }

  function renderDb() {
    const amp = Math.pow(10, db / 20);
    const pow = amp * amp;
    $('db-a-label').textContent = fmtRatio(amp);
    $('db-amp').textContent = fmtRatio(amp);
    $('db-pow').textContent = fmtRatio(pow);
    $('db-db').textContent = (db >= 0 ? '+' : '') + db.toFixed(1);

    const { ctx, w, h } = setupCanvas($('db-canvas'));
    ctx.clearRect(0, 0, w, h);
    ctx.font = '11px ui-monospace, Menlo, monospace';

    const m = { l: 28, r: 28 };
    const rw = w - m.l - m.r;
    const X = (d) => m.l + ((d - DB_MIN) / (DB_MAX - DB_MIN)) * rw;
    const ry = h * 0.54;

    ctx.strokeStyle = wash(0.35);
    ctx.lineWidth = 1.4;
    ctx.beginPath();
    ctx.moveTo(m.l, ry);
    ctx.lineTo(w - m.r, ry);
    ctx.stroke();

    for (let d = DB_MIN; d <= DB_MAX; d += 6) {
      const major = d % 24 === 0;
      ctx.strokeStyle = wash(major ? 0.4 : 0.18);
      ctx.beginPath();
      ctx.moveTo(X(d), ry - (major ? 9 : 5));
      ctx.lineTo(X(d), ry + (major ? 9 : 5));
      ctx.stroke();
      if (major) {
        ctx.fillStyle = DIM;
        ctx.textAlign = 'center';
        ctx.fillText((d > 0 ? '+' : '') + d, X(d), ry + 24);
      }
    }
    ctx.fillStyle = DIM;
    ctx.textAlign = 'left';
    ctx.fillText('each small tick = one amplitude doubling (+6.02 dB)', m.l, 16);

    ctx.fillStyle = ACCENT2;
    ctx.beginPath();
    ctx.arc(X(0), ry, 4, 0, Math.PI * 2);
    ctx.fill();
    ctx.textAlign = 'center';
    ctx.fillText('0 dB = reference', X(0), ry - 16);

    ctx.fillStyle = INK;
    ctx.beginPath();
    ctx.arc(X(db), ry, 5.5, 0, Math.PI * 2);
    ctx.fill();
    ctx.fillStyle = ACCENT;
    ctx.textAlign = 'center';
    const lx = Math.min(Math.max(X(db), m.l + 40), w - m.r - 40);
    ctx.fillText((db >= 0 ? '+' : '') + db.toFixed(1) + ' dB', lx, ry + 40);
  }

  function setDb(value) {
    db = Math.min(DB_MAX, Math.max(DB_MIN, value));
    $('db-a').value = dbToSlider(db);
    renderDb();
  }

  function wire() {
    $('db-a').addEventListener('input', () => { db = sliderToDb(+$('db-a').value); renderDb(); });
    $('db-x2').addEventListener('click', () => setDb(db + 20 * Math.log10(2)));
    $('db-h2').addEventListener('click', () => setDb(db + 20 * Math.log10(0.5)));
    $('db-x10').addEventListener('click', () => setDb(db + 20 * Math.log10(10)));
    document.querySelectorAll('.chip[data-db]').forEach((chip) => {
      chip.addEventListener('click', () => setDb(+chip.dataset.db));
    });
    let t;
    window.addEventListener('resize', () => { clearTimeout(t); t = setTimeout(renderDb, 120); });
  }

  readTheme();
  wire();
  $('db-a').value = dbToSlider(db);
  renderDb();

  LearnWidget.connect().then((host) => {
    host.on('theme.changed', () => { readTheme(); renderDb(); });
    readTheme();
    renderDb();
  });
})();