// Drawing for the optics class: refraction / total internal reflection at a flat boundary, and the converging-lens ray diagram. import * as OP from "./optics.js"; const NS = "http://www.w3.org/2000/svg"; function s(tag, attrs = {}, ...kids) { const n = document.createElementNS(NS, tag); for (const [k, v] of Object.entries(attrs || {})) { if (v == null) continue; if (k === "text") n.textContent = v; else n.setAttribute(k, v); } for (const k of kids.flat()) if (k != null) n.append(k.nodeType ? k : document.createTextNode(String(k))); return n; } function el(tag, attrs = {}, ...kids) { const n = document.createElement(tag); for (const [k, v] of Object.entries(attrs || {})) { if (v == null || v === false) continue; if (k === "text") n.textContent = v; else if (k.startsWith("on")) n.addEventListener(k.slice(2), v); else n.setAttribute(k, v === true ? "" : v); } for (const k of kids.flat()) if (k != null) n.append(k.nodeType ? k : document.createTextNode(String(k))); return n; } const D2R = Math.PI / 180; const f2 = (x) => Number(x.toPrecision(2)); // ---------- refraction / TIR ---------- // opts: hideOutcome (draw only the incident ray), wrong (a deliberate fault), showValues function rayDiagram(inst, { hideOutcome = false, wrong = null, showValues = true } = {}) { const sol = OP.solve(inst); const W = 520, H = 340, ox = 260, oy = 170, L = 150; const svg = s("svg", { viewBox: `0 0 ${W} ${H}`, class: "vz-svg vz-opt", role: "img", "aria-label": OP.describe(inst) }); const into = inst.scenario === "into"; // media: the denser block is shaded const blockTop = into ? oy : 0, blockH = into ? H - oy : oy; svg.append(s("rect", { x: 0, y: blockTop, width: W, height: blockH, class: "opt-block" })); svg.append(s("text", { x: 12, y: into ? oy + 22 : 22, class: "opt-lab", text: `${inst.medium} (n = ${inst.n})` }), s("text", { x: 12, y: into ? 22 : oy + 22, class: "opt-lab", text: "air" })); svg.append(s("line", { x1: 0, y1: oy, x2: W, y2: oy, class: "opt-surface" })); // the normal (deliberately tilted if that is the fault) const nTilt = wrong === "normal" ? 18 : 0; const nx = Math.sin(nTilt * D2R) * 140, ny = Math.cos(nTilt * D2R) * 140; svg.append(s("line", { x1: ox - nx, y1: oy - ny, x2: ox + nx, y2: oy + ny, class: "opt-normal" }), s("text", { x: ox + nx + 6, y: oy - ny + 12, class: "opt-lab small", text: "normal" })); if (inst.scenario === undefined) return svg; // incident ray: comes in from the upper left const i = inst.i; const ix = ox - Math.sin(i * D2R) * L, iy = oy - Math.cos(i * D2R) * L; const arrowId = `ar${Math.random().toString(36).slice(2, 7)}`; svg.append(s("defs", null, s("marker", { id: arrowId, viewBox: "0 0 10 10", refX: 8, refY: 5, markerWidth: 7, markerHeight: 7, orient: "auto-start-reverse" }, s("path", { d: "M0,0 L10,5 L0,10 z", class: "opt-arrow" })))); svg.append(s("line", { x1: ix, y1: iy, x2: ox, y2: oy, class: "opt-ray", "marker-end": `url(#${arrowId})` })); // angle of incidence arc and label (from the normal, or from the surface if that is the fault) const arc = (a0, a1, r, cls) => { const p = (a) => [ox + Math.sin(a * D2R) * r, oy - Math.cos(a * D2R) * r]; const [x0, y0] = p(a0), [x1, y1] = p(a1); return s("path", { d: `M${x0},${y0} A${r},${r} 0 0 ${a1 > a0 ? 1 : 0} ${x1},${y1}`, class: cls }); }; if (wrong === "surface") { svg.append(arc(-90, -i, 46, "opt-arc bad")); svg.append(s("text", { x: ox - 92, y: oy - 14, class: "opt-ang", text: `${90 - i}°` })); } else { svg.append(arc(0, -i, 40, "opt-arc")); svg.append(s("text", { x: ox - 18 - Math.sin(i / 2 * D2R) * 52, y: oy - Math.cos(i / 2 * D2R) * 52, class: "opt-ang", text: `i = ${i}°` })); } if (hideOutcome) { svg.append(s("text", { x: ox + 30, y: into ? oy + 70 : oy - 70, class: "opt-q", text: "?" })); return svg; } // outcome const drawOut = (angle, below, cls = "opt-ray") => { const ex = ox + Math.sin(angle * D2R) * L, ey = below ? oy + Math.cos(angle * D2R) * L : oy - Math.cos(angle * D2R) * L; svg.append(s("line", { x1: ox, y1: oy, x2: ex, y2: ey, class: cls, "marker-end": `url(#${arrowId})` })); const r = 40; const a = below ? 180 - angle : angle; svg.append(arc(below ? 180 : 0, a, r, "opt-arc out")); if (showValues) svg.append(s("text", { x: ox + 14 + Math.sin(angle / 2 * D2R) * 54, y: below ? oy + 12 + Math.cos(angle / 2 * D2R) * 52 : oy - Math.cos(angle / 2 * D2R) * 52, class: "opt-ang", text: `${below === into || !into ? "" : ""}${f2(angle)}°` })); }; let tirShown = sol.tir; if (wrong === "tirWrong") tirShown = true; if (wrong === "refractWrong") tirShown = false; if (tirShown) drawOut(i, false); // reflected back inside, at angle i else { let r = sol.r != null ? sol.r : 80; if (wrong === "refractWrong") r = 82; if (wrong === "away") r = Math.min(85, i + (i - (sol.r || i)) + 6); // bends the wrong way drawOut(r, true); if (!into) svg.append(s("line", { x1: ox, y1: oy, x2: ox + Math.sin(i * D2R) * L * 0.55, y2: oy - Math.cos(i * D2R) * L * 0.55, class: "opt-ray weak" })); // weak internal reflection } return svg; } // ---------- converging lens ---------- function lensDiagram(inst, { rays = 3, showImage = true, wrong = null, labels = true } = {}) { const sol = OP.solve(inst); const W = 560, H = 300, axisY = 150; const span = Math.max(inst.u, sol.real ? sol.v : 0, 2 * inst.f) * 1.15; const spanL = Math.max(inst.u, sol.real ? 0 : sol.v, 2 * inst.f) * 1.15; const lx = 30 + (spanL / (span + spanL)) * (W - 60); const k = (W - 60) / (span + spanL); const X = (x) => lx + x * k; const hMax = Math.max(inst.h, Math.abs(sol.hi || 0)) * 1.2; const ky = (H / 2 - 20) / hMax; const Y = (y) => axisY - y * ky; const svg = s("svg", { viewBox: `0 0 ${W} ${H}`, class: "vz-svg vz-opt", role: "img", "aria-label": OP.describe(inst) }); svg.append(s("line", { x1: 10, y1: axisY, x2: W - 10, y2: axisY, class: "opt-axis" })); svg.append(s("ellipse", { cx: lx, cy: axisY, rx: 7, ry: H / 2 - 16, class: "opt-lens" })); const fx = wrong === "focal" ? 2 * inst.f : inst.f; for (const [x, t] of [[-fx, "F"], [fx, "F"], [-2 * inst.f, "2F"], [2 * inst.f, "2F"]]) { svg.append(s("line", { x1: X(x), y1: axisY - 5, x2: X(x), y2: axisY + 5, class: "opt-tick" }), s("text", { x: X(x), y: axisY + 20, class: "opt-lab small", "text-anchor": "middle", text: t })); } const ar = (x1, y1, x2, y2, cls) => s("line", { x1: X(x1), y1: Y(y1), x2: X(x2), y2: Y(y2), class: cls }); // object arrow svg.append(ar(-inst.u, 0, -inst.u, inst.h, "opt-object"), s("polygon", { points: `${X(-inst.u) - 5},${Y(inst.h) + 8} ${X(-inst.u) + 5},${Y(inst.h) + 8} ${X(-inst.u)},${Y(inst.h)}`, class: "opt-object-head" })); if (labels) svg.append(s("text", { x: X(-inst.u), y: Y(inst.h) - 8, class: "opt-lab small", "text-anchor": "middle", text: "object" })); if (rays === 0 || sol.none) return svg; const farX = sol.real ? Math.max(sol.v * 1.1, 2.2 * inst.f) : 2 * inst.f; // ray 1: parallel to the axis, then through F on the far side if (rays >= 1) { svg.append(ar(-inst.u, inst.h, 0, inst.h, "opt-ray")); const fTarget = wrong === "parallel" ? 1.6 * inst.f : inst.f; const slope = -inst.h / fTarget; svg.append(ar(0, inst.h, farX, inst.h + slope * farX, "opt-ray")); if (!sol.real) svg.append(ar(0, inst.h, -sol.v, inst.h - slope * sol.v, "opt-ray virtual")); } // ray 2: straight through the optical centre if (rays >= 2) { const slope = -inst.h / inst.u; if (wrong === "centre") { svg.append(ar(-inst.u, inst.h, 0, 0, "opt-ray")); svg.append(ar(0, 0, farX, slope * farX * 0.4, "opt-ray")); } else { svg.append(ar(-inst.u, inst.h, farX, inst.h + slope * (farX + inst.u), "opt-ray")); if (!sol.real) svg.append(ar(-inst.u, inst.h, -sol.v, inst.h + slope * (inst.u - sol.v), "opt-ray virtual")); } } // ray 3: through F on the near side, then parallel (real images only) if (rays >= 3 && sol.real && inst.u > inst.f) { const slope = -inst.h / (inst.u - inst.f); const yAtLens = inst.h + slope * inst.u; svg.append(ar(-inst.u, inst.h, 0, yAtLens, "opt-ray third"), ar(0, yAtLens, farX, yAtLens, "opt-ray third")); } if (showImage) { const x = sol.real ? sol.v : -sol.v; const y = sol.hi * (sol.real ? -1 : 1); svg.append(ar(x, 0, x, y, sol.real ? "opt-image" : "opt-image virtual")); if (labels) svg.append(s("text", { x: X(x), y: Y(y) + (y < 0 ? 16 : -8), class: "opt-lab small", "text-anchor": "middle", text: sol.real ? "image (real)" : "image (virtual)" })); } return svg; } export function opticsFigure(fig, { reveal = false } = {}) { const inst = fig.inst; const show = fig.show || []; const isLens = inst.scenario === "lens" || inst.scenario === "lens2F" || inst.scenario === "magnifier"; const box = el("div", { class: "vz" }, el("div", { class: "vz-cap", text: OP.describe(inst) })); if (isLens) { if (reveal) box.append(lensDiagram(inst)); else if (show.includes("drawn")) box.append(lensDiagram(inst, { wrong: fig.wrong, rays: fig.wrong === "focal" ? 2 : 3 })); else box.append(lensDiagram(inst, { rays: 0, showImage: false })); } else { if (reveal) box.append(rayDiagram(inst)); else if (show.includes("drawn")) box.append(rayDiagram(inst, { wrong: fig.wrong, showValues: false })); else if (show.includes("boundary")) box.append(rayDiagram({ ...inst, scenario: inst.scenario }, { hideOutcome: true })); else box.append(rayDiagram(inst, { hideOutcome: true })); } if (reveal && fig.after) { box.append(el("div", { class: "vz-cap vz-after", text: `After the change: ${OP.describe(fig.after)}` })); box.append(isLens ? lensDiagram(fig.after) : rayDiagram(fig.after)); } return box; } // ---------- explorer ---------- export function opticsExplorer(inst0) { let inst = JSON.parse(JSON.stringify(inst0)); let step = 0; const isLens = () => inst.scenario === "lens" || inst.scenario === "lens2F" || inst.scenario === "magnifier"; const root = el("div", { class: "vz vz-explorer" }); const say = el("p", { class: "vz-say", "aria-live": "polite" }); const stage = el("div", { class: "vz-stage" }); const controls = el("div", { class: "vz-controls" }); const back = el("button", { class: "btn ghost", type: "button", text: "← Back", onclick: () => { if (step > 0) { step--; draw(); } } }); const next = el("button", { class: "btn", type: "button", text: "Next →", onclick: () => { if (step < steps().length - 1) { step++; draw(); } } }); const dots = el("ol", { class: "vz-dots" }); root.append(el("div", { class: "vz-steps" }, back, dots, next), say, stage, controls); const steps = () => (isLens() ? [["The lens, F and 2F", () => `A converging lens brings parallel rays together at the principal focus F, ${inst.f} cm from its centre. 2F is twice as far. The object stands ${inst.u} cm away.`], ["Ray 1: parallel, then through F", () => "A ray from the top of the object travelling parallel to the axis is refracted through F on the far side."], ["Ray 2: through the centre", () => "A ray through the centre of the lens goes straight on."], ["The image", () => { const sol = OP.solve(inst); return sol.none ? "With the object exactly at F the rays leave parallel: no image forms." : sol.real ? `The rays meet ${f2(sol.v)} cm beyond the lens: a real, inverted, ${sol.size} image. Move the object and watch it change.` : `The rays leave diverging. Traced back (dashed) they appear to come from a point ${f2(sol.v)} cm in front of the lens: a virtual, upright, enlarged image, which is how a magnifying glass works.`; }]] : [["The boundary and the normal", () => "The normal is a line at 90° to the surface. Every angle is measured from the normal, never from the surface."], ["The incident ray", () => `The ray meets the surface at an angle of incidence of ${inst.i}°.`], ["What happens at the surface", () => { const sol = OP.solve(inst); return inst.scenario === "into" ? `Entering the ${inst.medium} the light slows down and bends towards the normal: r = ${f2(sol.r)}°.` : sol.tir ? `The critical angle is ${f2(sol.c)}°. ${inst.i}° is bigger, so all the light is reflected inside: total internal reflection.` : `Leaving for air the light speeds up and bends away from the normal: r = ${f2(sol.r)}°. Push the angle past ${f2(sol.c)}° (the critical angle) and see.`; }], ["The equation", () => { const sol = OP.solve(inst); return inst.scenario === "into" ? `n = sin i / sin r: sin ${inst.i}° / sin ${f2(sol.r)}° = ${inst.n}.` : `Critical angle: sin c = 1 / n = 1 / ${inst.n}, so c = ${f2(sol.c)}°.${sol.tir ? "" : ` Refraction: sin r = n × sin i = ${inst.n} × sin ${inst.i}°.`}`; }]]); function draw() { const S = steps(); if (step >= S.length) step = S.length - 1; dots.replaceChildren(...S.map(([t], i) => el("li", { class: i === step ? "on" : i < step ? "done" : "" }, el("button", { type: "button", "aria-label": t, title: t, onclick: () => { step = i; draw(); } })))); back.disabled = step === 0; next.disabled = step === S.length - 1; say.replaceChildren(el("strong", { text: `${step + 1}. ${S[step][0]}. ` }), S[step][1]()); if (isLens()) stage.replaceChildren(lensDiagram(inst, { rays: step === 0 ? 0 : step === 1 ? 1 : step === 2 ? 2 : 3, showImage: step >= 3 })); else stage.replaceChildren(step === 0 ? rayDiagram({ ...inst, scenario: undefined }) : step === 1 ? rayDiagram(inst, { hideOutcome: true }) : rayDiagram(inst)); } function buildControls() { controls.replaceChildren(); const slider = (label, min, max, stp, get, set, fmt = (v) => v) => { const out = el("span", { class: "vz-num", text: fmt(get()) }); const inp = el("input", { type: "range", min, max, step: stp, value: get(), "aria-label": label }); inp.addEventListener("input", () => { set(Number(inp.value)); out.textContent = fmt(get()); draw(); }); return el("label", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: label }), inp, out); }; if (isLens()) { controls.append(slider("Object distance", 2, 4 * inst.f, 1, () => inst.u, (v) => { inst.u = v; inst.scenario = v < inst.f ? "magnifier" : "lens"; }, (v) => `${v} cm`)); const sel = el("select", { "aria-label": "Focal length" }, ...[5, 8, 10, 12, 15, 20].map((f) => el("option", { value: f, text: `${f} cm` }))); sel.value = inst.f; sel.addEventListener("change", () => { inst.f = Number(sel.value); inst.u = Math.min(inst.u, 4 * inst.f); inst.scenario = inst.u < inst.f ? "magnifier" : "lens"; buildControls(); draw(); }); controls.append(el("label", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: "Focal length" }), sel)); } else { controls.append(slider("Angle of incidence", 0, 85, 1, () => inst.i, (v) => { inst.i = v; if (inst.scenario !== "into") inst.scenario = v > OP.solve({ ...inst, scenario: "out" }).c ? "tir" : "out"; }, (v) => `${v}°`)); const sel = el("select", { "aria-label": "Material" }, ...OP.MEDIA.map((m) => el("option", { value: m.name, text: `${m.name} (n = ${m.n})` }))); sel.value = inst.medium; sel.addEventListener("change", () => { const m = OP.MEDIA.find((x) => x.name === sel.value); inst.medium = m.name; inst.n = m.n; if (inst.scenario !== "into") inst.scenario = inst.i > OP.solve({ ...inst, scenario: "out" }).c ? "tir" : "out"; draw(); }); const dir = el("select", { "aria-label": "Direction" }, el("option", { value: "into", text: "air → block" }), el("option", { value: "out", text: "block → air" })); dir.value = inst.scenario === "into" ? "into" : "out"; dir.addEventListener("change", () => { inst.scenario = dir.value === "into" ? "into" : inst.i > OP.solve({ ...inst, scenario: "out" }).c ? "tir" : "out"; draw(); }); controls.append(el("label", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: "Material" }), sel), el("label", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: "Direction" }), dir)); } controls.append(el("button", { type: "button", class: "react", text: "Reset", onclick: () => { inst = JSON.parse(JSON.stringify(inst0)); buildControls(); draw(); } })); } buildControls(); draw(); return root; } export const CSS = ` .vz-opt .opt-block{fill:var(--accent-wash)} .vz-opt .opt-surface{stroke:var(--ink);stroke-width:2} .vz-opt .opt-normal{stroke:var(--ink-soft);stroke-width:1.5;stroke-dasharray:6 5} .vz-opt .opt-ray{stroke:#d0892e;stroke-width:3} .vz-opt .opt-ray.weak{stroke-width:1.5;opacity:.5;stroke-dasharray:4 4} .vz-opt .opt-ray.virtual{stroke-dasharray:6 5;stroke-width:2;opacity:.8} .vz-opt .opt-ray.third{stroke:#3a9a6a} .vz-opt .opt-arrow{fill:#d0892e} .vz-opt .opt-arc{fill:none;stroke:var(--ink);stroke-width:1.5} .vz-opt .opt-arc.out{stroke:#3a9a6a} .vz-opt .opt-arc.bad{stroke:var(--bad)} .vz-opt .opt-ang{fill:var(--ink);font:600 15px var(--font-data)} .vz-opt .opt-lab{fill:var(--ink-soft);font:15px var(--font-body)} .vz-opt .opt-lab.small{font-size:13px} .vz-opt .opt-q{fill:var(--accent);font:700 34px var(--font-display)} .vz-opt .opt-axis{stroke:var(--ink-soft);stroke-width:1.2} .vz-opt .opt-lens{fill:var(--accent-wash);stroke:var(--accent);stroke-width:2} .vz-opt .opt-tick{stroke:var(--ink);stroke-width:2} .vz-opt .opt-object{stroke:var(--ink);stroke-width:3} .vz-opt .opt-object-head{fill:var(--ink)} .vz-opt .opt-image{stroke:var(--good);stroke-width:4} .vz-opt .opt-image.virtual{stroke-dasharray:6 4} .vz-ctl select{font:inherit;padding:4px 6px;border:1px solid var(--rule);border-radius:6px;background:var(--sheet);color:var(--ink)} @media (max-width:520px){.vz-opt .opt-ang{font-size:19px}.vz-opt .opt-lab{font-size:18px}.vz-opt .opt-lab.small{font-size:16px}} `;