7212d95Hardening and softening tutorialJeremy Magland 1// The block-on-a-spring diagram: wall, floor, spring, an optional damper
2// (dashpot) beside it, and the block displaced by x (a fraction of the
3// spring's rest length).
5import { arrow, color, prepare } from './canvas.js';
6import { X_MAX } from './duffing.js';
8// labels: which of 'x', 'm', 'k', 'beta', 'c', 'F' to draw ('beta' adds beta
9// next to k on the spring; 'c' labels the damper; 'F' the driving force).
10// damper: draw the damper. drive: the driving force now, as a fraction in
11// [-1, 1] of the longest arrow, or null for none. escaped: note that the
12// block has left the range |x| <= X_MAX.
13export function drawOscillator(canvas, x, { labels = [], damper = false, drive = null, escaped = false } = {}) {
14 if (!canvas.clientWidth) return;
15 const { ctx, w, h } = prepare(canvas);
16 const fg = color('--fg');
17 const wallX = 30;
18 const restLen = Math.min(w * 0.42, 300);
19 const floorY = h * 0.68;
20 const box = 64;
21 const restX = wallX + restLen;
22 const blockX = wallX + restLen * (1 + x); // left face of the block
23 // with a damper, the spring sits in the upper part of the block and the
24 // damper in the lower part
25 const springY = damper ? floorY - box * 0.72 : floorY - box / 2;
26 const damperY = floorY - box * 0.24;
27 const amp = damper ? 9 : 11;
29 // wall and floor
30 ctx.strokeStyle = fg;
31 ctx.lineWidth = 2;
32 ctx.beginPath();
33 ctx.moveTo(wallX, floorY - box * 1.6);
34 ctx.lineTo(wallX, floorY);
35 ctx.lineTo(w - 10, floorY);
36 ctx.stroke();
38 // spring (zig-zag)
39 const coils = 14;
40 const lead = 12;
41 const len = blockX - wallX - 2 * lead;
42 ctx.lineWidth = 1.5;
43 ctx.beginPath();
44 ctx.moveTo(wallX, springY);
45 ctx.lineTo(wallX + lead, springY);
46 for (let i = 0; i < coils; i++) {
47 ctx.lineTo(wallX + lead + ((i + 0.5) / coils) * len, springY + (i % 2 ? amp : -amp));
48 }
49 ctx.lineTo(blockX - lead, springY);
50 ctx.lineTo(blockX, springY);
51 ctx.stroke();
53 // damper: a cylinder fixed to the wall, open to the right, and a piston on
54 // a rod from the block (kept inside the cylinder for large compressions)
55 if (damper) {
56 const cylStart = wallX + 0.08 * restLen;
57 const cylEnd = wallX + 0.85 * restLen;
58 const hh = 8;
59 const piston = Math.max(cylStart + 4, Math.min(blockX - 0.5 * restLen, cylEnd - 2));
60 ctx.beginPath();
61 ctx.moveTo(wallX, damperY);
62 ctx.lineTo(cylStart, damperY);
63 ctx.moveTo(cylEnd, damperY - hh);
64 ctx.lineTo(cylStart, damperY - hh);
65 ctx.lineTo(cylStart, damperY + hh);
66 ctx.lineTo(cylEnd, damperY + hh);
67 ctx.moveTo(piston, damperY - hh + 2);
68 ctx.lineTo(piston, damperY + hh - 2);
69 ctx.moveTo(piston, damperY);
70 ctx.lineTo(blockX, damperY);
71 ctx.stroke();
72 if (labels.includes('c')) {
73 ctx.fillStyle = fg;
74 ctx.font = 'italic 16px Georgia, serif';
75 ctx.textAlign = 'center';
76 ctx.textBaseline = 'middle';
77 ctx.fillText('c', (cylStart + Math.min(piston, cylEnd)) / 2, damperY - 1);
78 }
79 }
81 // block
82 ctx.fillStyle = color('--accent');
83 ctx.fillRect(blockX, floorY - box, box, box);
85 // equilibrium marker
86 ctx.strokeStyle = fg;
87 ctx.globalAlpha = 0.3;
88 ctx.setLineDash([4, 4]);
89 ctx.beginPath();
90 ctx.moveTo(restX, floorY - box * 1.4);
91 ctx.lineTo(restX, floorY + 34);
92 ctx.stroke();
93 ctx.setLineDash([]);
94 ctx.globalAlpha = 1;
96 ctx.font = 'italic 20px Georgia, serif';
97 ctx.textAlign = 'center';
98 if (labels.includes('m')) {
99 ctx.fillStyle = color('--bg');
100 ctx.textBaseline = 'middle';
101 ctx.fillText('m', blockX + box / 2, floorY - box / 2);
102 }
103 if (labels.includes('k')) {
104 ctx.fillStyle = fg;
105 ctx.textBaseline = 'bottom';
106 ctx.fillText(labels.includes('beta') ? 'k, β' : 'k', (wallX + blockX) / 2, springY - amp - 4);
107 }
108 if (labels.includes('x')) arrow(ctx, restX, blockX, floorY + 22, fg, 'x', 'below');
109 // driving force: an arrow just right of the block (clear of the buttons in
110 // the corner), pointing the way the force pushes, with a fixed label above
111 if (drive !== null) {
112 const x0 = blockX + box + 6;
113 const len = 50 * Math.abs(drive);
114 const y = floorY - box * 0.3;
115 const [from, to] = drive >= 0 ? [x0, x0 + len] : [x0 + len, x0];
116 arrow(ctx, from, to, y, color('--force'), '', 'above');
117 if (labels.includes('F')) {
118 ctx.font = 'italic 18px Georgia, serif';
119 ctx.textAlign = 'left';
120 ctx.textBaseline = 'bottom';
121 ctx.fillText('F cos \u03A9t', x0, floorY - box * 0.55);
122 }
123 }
124 if (escaped) {
125 ctx.fillStyle = color('--force');
126 ctx.font = '16px "Hanken Grotesk", system-ui, sans-serif';
127 ctx.textAlign = 'left';
128 ctx.textBaseline = 'top';
129 // two short lines, clear of the buttons in the top-right corner
130 ctx.fillText(`Escaped: |x| > ${X_MAX}.`, wallX + 12, 4);
131 ctx.fillText('Press Start or change a slider to restart.', wallX + 12, 22);
132 }
133}