import './style.css'; import { defaults, experiments, format, type Experiment, type Parameters } from './experiments'; import { createLab } from './scene/lab'; import { answerUrl, getTestLesson, testLessons } from './test4'; import { renderEquation, renderNotation, renderProse } from './math'; const app = document.querySelector('#app')!; app.innerHTML = `
E/LAB

Electrotechnique & Electrophysics

Explore the invisible. Understand the physical.

FROM EQUATIONS TO INTUITION

Electric energy, made visible.

Explore the six questions from Test 4. Change the scene, make a prediction, then follow the derivation.

4A 100p4B 100p6 questions · 3D study guide
3D VIEW
NOTICE WHAT CHANGES

THE PHYSICS

TRY THIS

Model & visual scales

BUILD YOUR ANSWER

Follow the reasoning

Original answers ↗

PAUSE & PREDICT

`; const lab = createLab(document.querySelector('#viewport')!); const allLessons: Experiment[] = [...testLessons, ...experiments]; const settings = new Map(allLessons.map(experiment => [experiment.id, defaults(experiment)])); const byId = (id: string) => document.getElementById(id)!; let active: Experiment = testLessons[0]; let collection = ''; const subjects = document.querySelector('[role="tablist"]')!; let paused = window.matchMedia('(prefers-reduced-motion: reduce)').matches; function updatePause() { byId('pause').textContent = paused ? 'Resume animation' : 'Pause animation'; byId('pause').setAttribute('aria-pressed', String(paused)); lab.setPaused(paused); } updatePause(); function parameters(): Parameters { return settings.get(active.id)!; } function update() { const p = parameters(); for (const control of active.controls) { if (control.kind !== 'range') continue; byId(`output-${control.id}`).textContent = `${format(Number(p[control.id]), 3)}${control.unit ? ` ${control.unit}` : ''}`; } if (active.id === 'semiconductor') { (byId('parameter-concentration') as HTMLInputElement).disabled = p.doping === 'intrinsic'; } if (active.id === 'test-central') (byId('parameter-rb') as HTMLInputElement).disabled = p.destination === 'infinity'; const testLesson = getTestLesson(active); if (testLesson) renderProse(byId('observation'), testLesson.observe(p)); byId('measurements').innerHTML = active.measure(p).map(m => `
${m.label}
${m.value}${m.unit}
`).join(''); byId('measurements').querySelectorAll('.measurement > span').forEach(label => renderNotation(label, label.textContent!)); lab.update(active.id, p); } function show(experiment: Experiment) { active = experiment; const testLesson = getTestLesson(experiment); const lessons: Experiment[] = testLesson ? testLessons : experiments; const index = lessons.indexOf(experiment); const nextCollection = testLesson ? 'test' : 'lab'; if (collection !== nextCollection) { collection = nextCollection; subjects.innerHTML = lessons.map((e, i) => ``).join(''); } for (const [id, selected] of [['test-collection', Boolean(testLesson)], ['lab-collection', !testLesson]] as const) { if (selected) byId(id).setAttribute('aria-current', 'page'); else byId(id).removeAttribute('aria-current'); } byId('test-overview').hidden = !testLesson; byId('test-question').hidden = !testLesson; byId('test-study').hidden = !testLesson; byId('observation-panel').hidden = !testLesson; byId('lesson-number').textContent = testLesson ? `TEST ${testLesson.section} · QUESTION ${index + 1} · ${testLesson.points} POINTS` : `EXPERIMENT ${String(index + 1).padStart(2, '0')} / 08`; if (testLesson) { byId('test-question').replaceChildren(...testLesson.questions.map((text, i) => { const paragraph = document.createElement('p'); renderProse(paragraph, text); if (i === 0) paragraph.className = 'question-lead'; return paragraph; })); byId('solution').replaceChildren(...testLesson.answers.map(part => { const details = document.createElement('details'); const summary = document.createElement('summary'); renderNotation(summary, part.title); const steps = document.createElement('ol'); steps.className = 'derivation'; for (const step of part.steps) { const item = document.createElement('li'); const why = document.createElement('p'); renderProse(why, step.why); item.append(why); if (step.math) { const math = document.createElement('p'); math.className = 'math-step'; renderEquation(math, step.math); item.append(math); } steps.append(item); } details.append(summary, steps); return details; })); renderProse(byId('prediction-question'), testLesson.prediction); renderProse(byId('prediction-answer'), testLesson.predictionAnswer); byId('prediction-answer').hidden = true; byId('reveal-prediction').textContent = 'Reveal explanation'; byId('reveal-prediction').setAttribute('aria-expanded', 'false'); const previous = byId('previous-question') as HTMLAnchorElement; const next = byId('next-question') as HTMLAnchorElement; previous.hidden = index === 0; next.hidden = index === testLessons.length - 1; previous.textContent = '← Previous question'; next.textContent = 'Next question →'; if (index > 0) previous.href = `#${testLessons[index - 1].id}`; if (index < testLessons.length - 1) next.href = `#${testLessons[index + 1].id}`; byId('question-progress').textContent = `${index + 1} / 6`; } byId('lesson-title').textContent = experiment.title; renderProse(byId('description'), experiment.description); renderProse(byId('scene-note'), experiment.sceneNote); renderProse(byId('explanation'), experiment.explanation); renderProse(byId('challenge'), experiment.challenge); renderProse(byId('limits'), experiment.limits); byId('equations').replaceChildren(...experiment.equations.map(equation => { const div = document.createElement('p'); renderEquation(div, equation); return div; })); const source = byId('source') as HTMLAnchorElement; source.textContent = `${experiment.source.title} ↗`; source.href = experiment.source.url; byId('legend').innerHTML = experiment.legend.map(([colour, text]) => `${text}`).join(''); byId('lesson').setAttribute('aria-labelledby', `tab-${experiment.id}`); document.querySelectorAll('[role="tab"]').forEach(tab => { const selected = tab.dataset.experiment === experiment.id; tab.setAttribute('aria-selected', String(selected)); tab.tabIndex = selected ? 0 : -1; }); const p = parameters(); byId('parameters').innerHTML = experiment.controls.map(control => { const id = `parameter-${control.id}`; if (control.kind === 'select') return `
`; return `
`; }).join(''); byId('parameters').querySelectorAll('label').forEach(label => { const input = document.getElementById(label.htmlFor)!; input.setAttribute('aria-label', label.textContent!); renderNotation(label, label.textContent!); }); lab.resetCamera(experiment.id); update(); } function fromHash() { const id = window.location.hash.slice(1); show(allLessons.find(experiment => experiment.id === id) ?? testLessons[0]); } let focusNextQuestion = false; document.querySelector('.lesson-pagination')!.addEventListener('click', event => { const link = (event.target as HTMLElement).closest('a'); if (link && !((event as MouseEvent).ctrlKey || (event as MouseEvent).metaKey || (event as MouseEvent).shiftKey || (event as MouseEvent).altKey)) focusNextQuestion = true; }); const onHashChange = () => { fromHash(); if (focusNextQuestion) { focusNextQuestion = false; byId('lesson-title').focus({ preventScroll: true }); byId('lesson-title').scrollIntoView({ block: 'start' }); } }; window.addEventListener('hashchange', onHashChange); fromHash(); byId('parameters').addEventListener('input', event => { const input = event.target as HTMLInputElement | HTMLSelectElement; if (!input.name) return; parameters()[input.name] = input.type === 'range' ? Number(input.value) : input.value; update(); }); byId('reset').addEventListener('click', () => { settings.set(active.id, defaults(active)); show(active); }); byId('camera-reset').addEventListener('click', () => lab.resetCamera()); byId('pause').addEventListener('click', () => { paused = !paused; updatePause(); }); byId('reveal-prediction').addEventListener('click', () => { const reveal = byId('prediction-answer').hidden; byId('prediction-answer').hidden = !reveal; byId('reveal-prediction').setAttribute('aria-expanded', String(reveal)); byId('reveal-prediction').textContent = reveal ? 'Hide explanation' : 'Reveal explanation'; }); subjects.addEventListener('click', event => { const tab = (event.target as HTMLElement).closest('[data-experiment]'); if (!tab) return; const id = tab.dataset.experiment!; if (window.location.hash === `#${id}`) show(allLessons.find(e => e.id === id)!); else window.location.hash = id; }); subjects.addEventListener('keydown', event => { const tabs = [...subjects.querySelectorAll('[role="tab"]')]; const index = tabs.indexOf(event.target as HTMLButtonElement); if (index < 0) return; let next: number; if (event.key === 'ArrowRight') next = (index + 1) % tabs.length; else if (event.key === 'ArrowLeft') next = (index - 1 + tabs.length) % tabs.length; else if (event.key === 'Home') next = 0; else if (event.key === 'End') next = tabs.length - 1; else return; event.preventDefault(); tabs[next].focus(); tabs[next].click(); }); if (import.meta.hot) import.meta.hot.dispose(() => { window.removeEventListener('hashchange', onHashChange); lab.dispose(); });