はじめに
高校物理の波動分野でつまずきやすいテーマ、「光の屈折(スネルの法則)」。
教科書の静止画では、
- 「素元波の包絡線が新しい波面になる」
- 「境界面に先に到達した端が減速するため、波面が傾いて進行方向が変わる」
というダイナミックな時間変化がどうしてもイメージしにくいという課題があります。
そこで、HTML5 Canvas + Vanilla JS での、ホイヘンスの原理シミュレータを作成しました。
【関連動画】
【埋め込み済みサイト】
ソースコード
以下のコードを .html として保存するだけで、ブラウザで動作します。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ホイヘンスの原理シミュレータ</title>
<style>
:root {
--bg-dark: #0a0d14;
--panel-bg: #121722;
--panel-border: #232c40;
--accent-blue: #0a84ff;
--accent-cyan: #4cd6fb;
--accent-amber: #ffcc00;
--accent-green: #30d158;
--text-main: #f5f5f7;
--text-muted: #8b93a7;
--font-stack: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans JP", sans-serif;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: var(--font-stack);
background-color: var(--bg-dark);
color: var(--text-main);
margin: 0;
padding: 2px;
overflow: hidden;
display: flex;
justify-content: center;
}
.app-container {
display: flex;
flex-direction: column;
width: 100%;
max-width: 860px;
}
.player-wrapper {
background: var(--panel-bg);
border: 1px solid var(--panel-border);
border-radius: 6px;
overflow: hidden;
width: 100%;
}
.viewport-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 4px 8px;
background: rgba(255, 255, 255, 0.02);
border-bottom: 1px solid var(--panel-border);
gap: 6px;
}
.header-title {
font-size: 11px;
font-weight: 700;
display: flex;
align-items: center;
gap: 5px;
white-space: nowrap;
}
.badge {
font-size: 9px;
font-weight: 700;
padding: 1px 5px;
border-radius: 3px;
background: rgba(10, 132, 255, 0.15);
color: var(--accent-cyan);
border: 1px solid rgba(10, 132, 255, 0.3);
white-space: nowrap;
}
.toggle-group {
display: flex;
gap: 8px;
white-space: nowrap;
}
.toggle-label {
font-size: 10px;
color: var(--text-muted);
display: flex;
align-items: center;
gap: 3px;
cursor: pointer;
user-select: none;
}
.toggle-label input {
width: 12px;
height: 12px;
cursor: pointer;
}
.controls-toolbar {
display: flex;
align-items: center;
padding: 4px 8px;
background: #0e121c;
border-bottom: 1px solid var(--panel-border);
gap: 6px;
}
.preset-group {
display: flex;
align-items: center;
gap: 4px;
width: 100%;
}
.control-label {
font-size: 10px;
font-weight: 700;
color: var(--text-muted);
white-space: nowrap;
}
.preset-buttons {
display: flex;
gap: 4px;
flex: 1;
}
.preset-btn {
flex: 1;
font-size: 10px;
font-weight: 600;
padding: 3px 0;
text-align: center;
background: rgba(255, 255, 255, 0.05);
border: 1px solid var(--panel-border);
border-radius: 4px;
color: var(--text-muted);
cursor: pointer;
white-space: nowrap;
}
.preset-btn.active {
background: rgba(76, 214, 251, 0.15);
border-color: var(--accent-cyan);
color: var(--accent-cyan);
font-weight: 700;
}
/* キャンバス表示 */
.canvas-container {
display: flex;
justify-content: center;
background: #000;
}
canvas {
display: block;
width: 100%;
height: auto;
max-width: 860px;
}
/* プレイヤー下部操作バー */
.player-controls {
display: flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
background: #0e111a;
border-top: 1px solid var(--panel-border);
}
.playback-buttons {
display: flex;
align-items: center;
gap: 3px;
}
.cue-btn {
font-size: 10px;
font-weight: 700;
font-family: "Consolas", monospace;
padding: 4px 6px;
border-radius: 3px;
border: 1px solid var(--panel-border);
background: rgba(255, 255, 255, 0.08);
color: var(--text-main);
cursor: pointer;
}
.cue-btn.highlight-btn {
border-color: var(--accent-green);
color: var(--accent-green);
background: rgba(48, 209, 88, 0.12);
}
.playback-buttons button.play-btn {
display: flex;
align-items: center;
justify-content: center;
width: 26px;
height: 26px;
border-radius: 4px;
background: var(--accent-blue);
border: none;
color: #fff;
cursor: pointer;
}
.timeline-container {
flex: 1;
display: flex;
align-items: center;
}
input[type="range"] {
-webkit-appearance: none;
appearance: none;
width: 100%;
height: 5px;
border-radius: 2px;
background: #252b3d;
outline: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 15px;
height: 15px;
border-radius: 50%;
background: var(--accent-cyan);
cursor: pointer;
}
.timecode-display {
font-family: "Consolas", monospace;
font-size: 11px;
display: flex;
align-items: center;
gap: 2px;
background: rgba(0, 0, 0, 0.4);
padding: 3px 5px;
border-radius: 3px;
border: 1px solid var(--panel-border);
}
.timecode { color: var(--accent-cyan); font-weight: 700; min-width: 28px; text-align: right; }
.time-unit { color: var(--text-muted); font-size: 9px; }
</style>
</head>
<body>
<div class="app-container">
<div class="player-wrapper">
<!-- 1行ですっきり収まるヘッダー -->
<div class="viewport-header">
<div class="header-title">
<span class="badge">物理</span>
<span>ホイヘンスの原理</span>
</div>
<div class="toggle-group">
<label class="toggle-label">
<input type="checkbox" id="showAngleProofToggle" checked> 角度・三角比
</label>
<label class="toggle-label">
<input type="checkbox" id="showReflectionToggle"> 反射波
</label>
</div>
</div>
<!-- キャンバスの文字サイズに合わせたスリムな媒質選択バー -->
<div class="controls-toolbar">
<div class="preset-group">
<span class="control-label">媒質2:</span>
<div class="preset-buttons">
<button class="preset-btn active" data-n2="1.50">ガラス (n=1.50)</button>
<button class="preset-btn" data-n2="1.33">水 (n=1.33)</button>
<button class="preset-btn" data-n2="1.00">同一 (n=1.00)</button>
</div>
</div>
</div>
<!-- メインキャンバス -->
<div class="canvas-container">
<canvas id="refractionCanvas" width="860" height="520"></canvas>
</div>
<!-- プレイヤー操作部(t=0〜5.0) -->
<div class="player-controls">
<div class="playback-buttons">
<button id="playBtn" class="play-btn" title="再生 / 停止">
<svg id="playIcon" width="12" height="12" viewBox="0 0 24 24" fill="currentColor">
<path d="M8 5v14l11-7z"/>
</svg>
</button>
<button id="jumpA" class="cue-btn" title="点A到達 (t=0)">t=0</button>
<button id="jumpB" class="cue-btn highlight-btn" title="点B到達 (t=T)">t=T</button>
<button id="stepBtn" class="cue-btn" title="コマ送り">+</button>
</div>
<div class="timeline-container">
<input type="range" id="timeSlider" min="0.0" max="5.0" step="0.01" value="0.0">
</div>
<div class="timecode-display">
<span id="timeLabel" class="timecode">0.00</span>
<span class="time-unit">/T</span>
</div>
</div>
</div>
</div>
<script>
const canvas = document.getElementById('refractionCanvas');
const ctx = canvas.getContext('2d');
const timeSlider = document.getElementById('timeSlider');
const timeLabel = document.getElementById('timeLabel');
const playBtn = document.getElementById('playBtn');
const playIcon = document.getElementById('playIcon');
const jumpABtn = document.getElementById('jumpA');
const jumpBBtn = document.getElementById('jumpB');
const stepBtn = document.getElementById('stepBtn');
const presetButtons = document.querySelectorAll('.preset-btn');
const showReflectionToggle = document.getElementById('showReflectionToggle');
const showAngleProofToggle = document.getElementById('showAngleProofToggle');
const yInterface = 250;
const v1 = 70.0;
const T = 3.0;
const lambda1 = v1 * T;
const theta1Deg = 45.0;
const theta1 = theta1Deg * Math.PI / 180;
let nRel = 1.50;
let v2 = v1 / nRel;
let distAB = 0;
let xA = 0, yA = yInterface;
let xB = 0, yB = yInterface;
let xC = 0, yC = 0;
let xD = 0, yD = 0;
let theta2 = 0;
const RAY_COUNT = 9;
function getRayRatios() {
const arr = [];
for (let i = 0; i < RAY_COUNT; i++) arr.push(i / (RAY_COUNT - 1));
return arr;
}
function updatePhysicsParameters() {
v2 = v1 / nRel;
distAB = lambda1 / Math.sin(theta1);
xA = (canvas.width - distAB) / 2;
xB = xA + distAB;
xC = xB - lambda1 * Math.sin(theta1);
yC = yB - lambda1 * Math.cos(theta1);
const sinTheta2 = (v2 * T) / distAB;
theta2 = Math.asin(sinTheta2);
const distAD = v2 * T;
xD = xA + distAD * Math.sin(theta2);
yD = yA + distAD * Math.cos(theta2);
}
function getWavefrontPoint(u, t) {
const t_enter = u * T;
const intX = xA + u * distAB;
const intY = yInterface;
if (t <= t_enter) {
const d = (t - t_enter) * v1;
return {
x: intX + d * Math.sin(theta1),
y: intY + d * Math.cos(theta1)
};
} else {
const d = (t - t_enter) * v2;
return {
x: intX + d * Math.sin(theta2),
y: intY + d * Math.cos(theta2)
};
}
}
function drawBackground() {
ctx.fillStyle = '#0a0e17';
ctx.fillRect(0, 0, canvas.width, yInterface);
const factor = Math.min(1.0, Math.max(0.15, (nRel - 0.5) * 0.5));
ctx.fillStyle = `rgba(18, 45, 82, ${factor})`;
ctx.fillRect(0, yInterface, canvas.width, canvas.height - yInterface);
ctx.strokeStyle = '#384766';
ctx.lineWidth = 2.5;
ctx.beginPath();
ctx.moveTo(0, yInterface);
ctx.lineTo(canvas.width, yInterface);
ctx.stroke();
ctx.font = '800 16px "Noto Sans JP", sans-serif';
ctx.fillStyle = '#ffcc00';
ctx.fillText(`媒質 1 (空気): 速い [速度 v₁]`, 20, 32);
ctx.fillStyle = '#4cd6fb';
ctx.fillText(`媒質 2: 遅い [速度 v₂ = v₁ / ${nRel.toFixed(2)}]`, 20, yInterface + 34);
}
function drawNormals() {
[xA, xB].forEach((x, idx) => {
ctx.save();
ctx.strokeStyle = 'rgba(255, 255, 255, 0.4)';
ctx.lineWidth = 2.0;
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(x, yInterface - 140);
ctx.lineTo(x, yInterface + 150);
ctx.stroke();
ctx.fillStyle = 'rgba(255, 255, 255, 0.7)';
ctx.font = '700 14px "Noto Sans JP", sans-serif';
ctx.fillText(`法線 (${idx === 0 ? 'A' : 'B'})`, x + 8, yInterface - 120);
ctx.restore();
});
}
function drawRayTracks() {
ctx.save();
[0, 1].forEach(u => {
const intX = xA + u * distAB;
ctx.strokeStyle = 'rgba(255, 204, 0, 0.4)';
ctx.lineWidth = 1.8;
ctx.beginPath();
ctx.moveTo(intX - 300 * Math.sin(theta1), yInterface - 300 * Math.cos(theta1));
ctx.lineTo(intX, yInterface);
ctx.stroke();
ctx.strokeStyle = 'rgba(76, 214, 251, 0.4)';
ctx.beginPath();
ctx.moveTo(intX, yInterface);
ctx.lineTo(intX + 350 * Math.sin(theta2), yInterface + 350 * Math.cos(theta2));
ctx.stroke();
if (showReflectionToggle.checked) {
ctx.strokeStyle = 'rgba(255, 55, 95, 0.35)';
ctx.beginPath();
ctx.moveTo(intX, yInterface);
ctx.lineTo(intX + 300 * Math.sin(theta1), yInterface - 300 * Math.cos(theta1));
ctx.stroke();
}
});
ctx.restore();
}
function drawPeriodicWavefronts(t) {
const period = T;
for (let k = -4; k <= 5; k++) {
const waveT = t + k * period;
if (waveT < -2.0 || waveT > 9.0) continue;
const isMainWave = (k === 0);
ctx.save();
const pts = [];
const steps = 30;
for (let i = 0; i <= steps; i++) {
pts.push(getWavefrontPoint(i / steps, waveT));
}
ctx.strokeStyle = isMainWave ? '#ff3b30' : 'rgba(255, 255, 255, 0.22)';
ctx.lineWidth = isMainWave ? 4.0 : 1.8;
ctx.beginPath();
ctx.moveTo(pts[0].x, pts[0].y);
for (let i = 1; i < pts.length; i++) {
ctx.lineTo(pts[i].x, pts[i].y);
}
ctx.stroke();
if (isMainWave && pts.length > 0) {
ctx.fillStyle = '#ff3b30';
ctx.beginPath();
ctx.arc(pts[0].x, pts[0].y, 5, 0, 2 * Math.PI);
ctx.fill();
}
ctx.restore();
}
}
function drawHuygensWavelets(t) {
if (t <= 0) return;
const ratios = getRayRatios();
ratios.forEach(u => {
const t_enter = u * T;
const intX = xA + u * distAB;
if (t >= t_enter) {
const dt = Math.min(t, T) - t_enter;
const r2 = v2 * dt;
ctx.save();
ctx.strokeStyle = (t >= T) ? 'rgba(76, 214, 251, 0.3)' : 'rgba(76, 214, 251, 0.85)';
ctx.lineWidth = 1.8;
if (t >= T) ctx.setLineDash([4, 4]);
ctx.beginPath();
ctx.arc(intX, yInterface, r2, 0, Math.PI);
ctx.stroke();
ctx.restore();
if (showReflectionToggle.checked) {
const r1 = v1 * dt;
ctx.save();
ctx.strokeStyle = (t >= T) ? 'rgba(255, 55, 95, 0.3)' : 'rgba(255, 55, 95, 0.8)';
ctx.lineWidth = 1.6;
if (t >= T) ctx.setLineDash([4, 4]);
ctx.beginPath();
ctx.arc(intX, yInterface, r1, Math.PI, 2 * Math.PI);
ctx.stroke();
ctx.restore();
}
ctx.fillStyle = '#4cd6fb';
ctx.beginPath();
ctx.arc(intX, yInterface, 3.5, 0, 2 * Math.PI);
ctx.fill();
}
});
}
function drawRightAngleSymbol(ox, oy, a1, a2, size, col) {
const x1 = ox + size * Math.cos(a1);
const y1 = oy + size * Math.sin(a1);
const x2 = x1 + size * Math.cos(a2);
const y2 = y1 + size * Math.sin(a2);
const x3 = ox + size * Math.cos(a2);
const y3 = oy + size * Math.sin(a2);
ctx.save();
ctx.strokeStyle = col;
ctx.lineWidth = 2.0;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.stroke();
ctx.restore();
}
function drawGeometricProof(t) {
if (t < T) return;
ctx.save();
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 3.5;
ctx.beginPath();
ctx.moveTo(xA, yA);
ctx.lineTo(xB, yB);
ctx.stroke();
ctx.strokeStyle = '#ffcc00';
ctx.lineWidth = 2.2;
ctx.fillStyle = 'rgba(255, 204, 0, 0.1)';
ctx.beginPath();
ctx.moveTo(xA, yA);
ctx.lineTo(xC, yC);
ctx.lineTo(xB, yB);
ctx.closePath();
ctx.fill();
ctx.stroke();
const aCB = Math.atan2(yB - yC, xB - xC);
const aCA = Math.atan2(yA - yC, xA - xC);
drawRightAngleSymbol(xC, yC, aCB, aCA, 14, '#ffcc00');
ctx.strokeStyle = '#4cd6fb';
ctx.lineWidth = 2.2;
ctx.fillStyle = 'rgba(76, 214, 251, 0.1)';
ctx.beginPath();
ctx.moveTo(xA, yA);
ctx.lineTo(xD, yD);
ctx.lineTo(xB, yB);
ctx.closePath();
ctx.fill();
ctx.stroke();
const aDA = Math.atan2(yA - yD, xA - xD);
const aDB = Math.atan2(yB - yD, xB - xD);
drawRightAngleSymbol(xD, yD, aDA, aDB, 14, '#4cd6fb');
drawPointLabel(xD, yD, "D", '#4cd6fb', -10, 26);
ctx.font = '800 16px "Consolas", monospace';
ctx.fillStyle = '#4cd6fb';
ctx.fillText("AD = v₂T", (xA + xD) / 2 - 80, (yA + yD) / 2 + 24);
ctx.strokeStyle = '#4cd6fb';
ctx.lineWidth = 2.0;
ctx.beginPath();
const aBD = Math.atan2(yD - yB, xD - xB);
ctx.arc(xB, yB, 40, aBD, Math.PI, false);
ctx.stroke();
ctx.fillText("θ₂", xB - 60, yB + 18);
ctx.font = '800 16px "Consolas", monospace';
ctx.fillStyle = '#ffcc00';
ctx.fillText("BC = v₁T", (xB + xC) / 2 + 14, (yB + yC) / 2 - 10);
ctx.strokeStyle = '#ffcc00';
ctx.lineWidth = 2.0;
ctx.beginPath();
ctx.arc(xA, yA, 40, -theta1, 0);
ctx.stroke();
ctx.fillText("θ₁", xA + 46, yA - 10);
drawPointLabel(xA, yA, "A", '#ffffff', -24, -10);
drawPointLabel(xB, yB, "B", '#ffffff', 14, -10);
drawPointLabel(xC, yC, "C", '#ffcc00', -16, -16);
ctx.restore();
}
function drawPointLabel(x, y, text, color, ox, oy) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x, y, 5.5, 0, 2 * Math.PI);
ctx.fill();
ctx.font = '900 18px "Noto Sans JP", sans-serif';
ctx.fillText(text, x + ox, y + oy);
}
function drawHUD() {
ctx.save();
const hudW = 240;
const hudH = 110;
const hudX = canvas.width - hudW - 14;
const hudY = 14;
ctx.fillStyle = 'rgba(12, 16, 25, 0.94)';
ctx.strokeStyle = '#2d374d';
ctx.lineWidth = 1.5;
ctx.roundRect(hudX, hudY, hudW, hudH, 8);
ctx.fill();
ctx.stroke();
ctx.font = '800 14px "Consolas", monospace';
ctx.fillStyle = '#ffcc00';
ctx.fillText(`入射角 θ₁ = ${theta1Deg.toFixed(1)}°`, hudX + 14, hudY + 26);
ctx.fillStyle = '#4cd6fb';
ctx.fillText(`屈折角 θ₂ = ${(theta2 * 180 / Math.PI).toFixed(1)}°`, hudX + 14, hudY + 52);
ctx.strokeStyle = '#2d374d';
ctx.beginPath();
ctx.moveTo(hudX + 14, hudY + 66);
ctx.lineTo(hudX + hudW - 14, hudY + 66);
ctx.stroke();
ctx.fillStyle = '#30d158';
const ratioSin = (Math.sin(theta1) / Math.sin(theta2)).toFixed(3);
ctx.fillText(`sin θ₁ / sin θ₂ = ${ratioSin}`, hudX + 14, hudY + 86);
ctx.fillText(`v₁ / v₂ = ${nRel.toFixed(3)}`, hudX + 14, hudY + 104);
ctx.restore();
}
function render() {
const t = parseFloat(timeSlider.value);
timeLabel.innerText = t.toFixed(2);
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBackground();
drawNormals();
drawRayTracks();
drawPeriodicWavefronts(t);
drawHuygensWavelets(t);
if (showAngleProofToggle.checked) {
drawGeometricProof(t);
}
drawHUD();
}
let isPlaying = false;
let animationId = null;
function setPlayState(playing) {
isPlaying = playing;
if (isPlaying) {
playIcon.innerHTML = '<path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/>';
animate();
} else {
playIcon.innerHTML = '<path d="M8 5v14l11-7z"/>';
cancelAnimationFrame(animationId);
}
}
function animate() {
if (!isPlaying) return;
let currentT = parseFloat(timeSlider.value);
currentT += 0.015;
if (currentT > parseFloat(timeSlider.max)) currentT = 0.0;
timeSlider.value = currentT;
render();
animationId = requestAnimationFrame(animate);
}
presetButtons.forEach(btn => {
btn.addEventListener('click', () => {
presetButtons.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
nRel = parseFloat(btn.dataset.n2);
updatePhysicsParameters();
render();
});
});
[showReflectionToggle, showAngleProofToggle].forEach(toggle => {
toggle.addEventListener('change', render);
});
timeSlider.addEventListener('input', () => {
setPlayState(false);
render();
});
playBtn.addEventListener('click', () => setPlayState(!isPlaying));
jumpABtn.addEventListener('click', () => {
setPlayState(false);
timeSlider.value = 0.00;
render();
});
jumpBBtn.addEventListener('click', () => {
setPlayState(false);
timeSlider.value = T.toFixed(2);
render();
});
stepBtn.addEventListener('click', () => {
setPlayState(false);
let currentT = parseFloat(timeSlider.value) + 0.1;
if (currentT > parseFloat(timeSlider.max)) currentT = 0.0;
timeSlider.value = currentT.toFixed(2);
render();
});
updatePhysicsParameters();
render();
</script>
</body>
</html>