// The block-on-a-spring diagram: wall, floor, spring, an optional damper // (dashpot) beside it, and the block displaced by x (a fraction of the // spring's rest length). import { arrow, color, prepare } from './canvas.js'; import { X_MAX } from './duffing.js'; // labels: which of 'x', 'm', 'k', 'beta', 'c', 'F' to draw ('beta' adds beta // next to k on the spring; 'c' labels the damper; 'F' the driving force). // damper: draw the damper. drive: the driving force now, as a fraction in // [-1, 1] of the longest arrow, or null for none. escaped: note that the // block has left the range |x| <= X_MAX. export function drawOscillator(canvas, x, { labels = [], damper = false, drive = null, escaped = false } = {}) { if (!canvas.clientWidth) return; const { ctx, w, h } = prepare(canvas); const fg = color('--fg'); const wallX = 30; const restLen = Math.min(w * 0.42, 300); const floorY = h * 0.68; const box = 64; const restX = wallX + restLen; const blockX = wallX + restLen * (1 + x); // left face of the block // with a damper, the spring sits in the upper part of the block and the // damper in the lower part const springY = damper ? floorY - box * 0.72 : floorY - box / 2; const damperY = floorY - box * 0.24; const amp = damper ? 9 : 11; // wall and floor ctx.strokeStyle = fg; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(wallX, floorY - box * 1.6); ctx.lineTo(wallX, floorY); ctx.lineTo(w - 10, floorY); ctx.stroke(); // spring (zig-zag) const coils = 14; const lead = 12; const len = blockX - wallX - 2 * lead; ctx.lineWidth = 1.5; ctx.beginPath(); ctx.moveTo(wallX, springY); ctx.lineTo(wallX + lead, springY); for (let i = 0; i < coils; i++) { ctx.lineTo(wallX + lead + ((i + 0.5) / coils) * len, springY + (i % 2 ? amp : -amp)); } ctx.lineTo(blockX - lead, springY); ctx.lineTo(blockX, springY); ctx.stroke(); // damper: a cylinder fixed to the wall, open to the right, and a piston on // a rod from the block (kept inside the cylinder for large compressions) if (damper) { const cylStart = wallX + 0.08 * restLen; const cylEnd = wallX + 0.85 * restLen; const hh = 8; const piston = Math.max(cylStart + 4, Math.min(blockX - 0.5 * restLen, cylEnd - 2)); ctx.beginPath(); ctx.moveTo(wallX, damperY); ctx.lineTo(cylStart, damperY); ctx.moveTo(cylEnd, damperY - hh); ctx.lineTo(cylStart, damperY - hh); ctx.lineTo(cylStart, damperY + hh); ctx.lineTo(cylEnd, damperY + hh); ctx.moveTo(piston, damperY - hh + 2); ctx.lineTo(piston, damperY + hh - 2); ctx.moveTo(piston, damperY); ctx.lineTo(blockX, damperY); ctx.stroke(); if (labels.includes('c')) { ctx.fillStyle = fg; ctx.font = 'italic 16px Georgia, serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('c', (cylStart + Math.min(piston, cylEnd)) / 2, damperY - 1); } } // block ctx.fillStyle = color('--accent'); ctx.fillRect(blockX, floorY - box, box, box); // equilibrium marker ctx.strokeStyle = fg; ctx.globalAlpha = 0.3; ctx.setLineDash([4, 4]); ctx.beginPath(); ctx.moveTo(restX, floorY - box * 1.4); ctx.lineTo(restX, floorY + 34); ctx.stroke(); ctx.setLineDash([]); ctx.globalAlpha = 1; ctx.font = 'italic 20px Georgia, serif'; ctx.textAlign = 'center'; if (labels.includes('m')) { ctx.fillStyle = color('--bg'); ctx.textBaseline = 'middle'; ctx.fillText('m', blockX + box / 2, floorY - box / 2); } if (labels.includes('k')) { ctx.fillStyle = fg; ctx.textBaseline = 'bottom'; ctx.fillText(labels.includes('beta') ? 'k, β' : 'k', (wallX + blockX) / 2, springY - amp - 4); } if (labels.includes('x')) arrow(ctx, restX, blockX, floorY + 22, fg, 'x', 'below'); // driving force: an arrow just right of the block (clear of the buttons in // the corner), pointing the way the force pushes, with a fixed label above if (drive !== null) { const x0 = blockX + box + 6; const len = 50 * Math.abs(drive); const y = floorY - box * 0.3; const [from, to] = drive >= 0 ? [x0, x0 + len] : [x0 + len, x0]; arrow(ctx, from, to, y, color('--force'), '', 'above'); if (labels.includes('F')) { ctx.font = 'italic 18px Georgia, serif'; ctx.textAlign = 'left'; ctx.textBaseline = 'bottom'; ctx.fillText('F cos \u03A9t', x0, floorY - box * 0.55); } } if (escaped) { ctx.fillStyle = color('--force'); ctx.font = '16px "Hanken Grotesk", system-ui, sans-serif'; ctx.textAlign = 'left'; ctx.textBaseline = 'top'; // two short lines, clear of the buttons in the top-right corner ctx.fillText(`Escaped: |x| > ${X_MAX}.`, wallX + 12, 4); ctx.fillText('Press Start or change a slider to restart.', wallX + 12, 22); } }