以下は、指定された要件を満たす自由落下シミュレーションの単一HTMLページです。バニラJavaScriptを使用しており、UIは日本語で提供されています。アクセシビリティにも配慮しています。
html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>自由落下シミュレーション</title>
<style>
body {
font-family: Arial, sans-serif;
}
canvas {
border: 1px solid black;
}
.controls {
margin: 10px 0;
}
.explanation {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>自由落下シミュレーション</h1>
<div class="controls">
<label for="mode">モード:</label>
<select id="mode" aria-label="モード選択">
<option value="ideal">理想(抵抗なし)</option>
<option value="resistance">抗力あり(空気抵抗)</option>
</select>
<div id="resistanceOptions" style="display: none;">
<label for="resistanceModel">抗力モデル:</label>
<select id="resistanceModel" aria-label="抗力モデル選択">
<option value="linear">一次抵抗</option>
<option value="quadratic">二次抵抗</option>
</select>
<div id="linearParams" style="display: none;">
<label for="c">c (kg/s):</label>
<input type="number" id="c" value="0.1" step="0.01">
</div>
<div id="quadraticParams" style="display: none;">
<label for="rho">ρ (kg/m³):</label>
<input type="number" id="rho" value="1.225" step="0.01">
<label for="Cd">Cd:</label>
<input type="number" id="Cd" value="0.5" step="0.01">
<label for="A">A (m²):</label>
<input type="number" id="A" value="0.1" step="0.01">
</div>
</div>
<label for="h">初期高度 h (m):</label>
<input type="number" id="h" value="100" step="1">
<label for="v0">初期速度 v0 (m/s):</label>
<input type="number" id="v0" value="0" step="0.1">
<label for="g">重力加速度 g (m/s²):</label>
<input type="number" id="g" value="9.81" step="0.01">
<button id="startPause" aria-label="開始/一時停止">開始</button>
<button id="reset" aria-label="リセット">リセット</button>
</div>
<canvas id="graph" width="800" height="400"></canvas>
<div class="explanation">
<h2>説明</h2>
<p>このシミュレーションは、自由落下する物体の運動をモデル化します。理想モードでは空気抵抗を無視し、抗力モードでは一次または二次抵抗を考慮します。</p>
<p>一次抵抗モデル: Fd = c v</p>
<p>二次抵抗モデル: Fd = (1/2) ρ C_d A v|v|</p>
<p>地面に衝突するとシミュレーションは停止し、到達時刻と終端速度が表示されます。</p>
</div>
<script>
const canvas = document.getElementById('graph');
const ctx = canvas.getContext('2d');
let running = false;
let startTime = 0;
let h = 100;
let v = 0;
let t = 0;
const dt = 0.01;
document.getElementById('mode').addEventListener('change', updateResistanceOptions);
document.getElementById('resistanceModel').addEventListener('change', updateResistanceParams);
document.getElementById('startPause').addEventListener('click', toggleSimulation);
document.getElementById('reset').addEventListener('click', resetSimulation);
function updateResistanceOptions() {
const mode = document.getElementById('mode').value;
const resistanceOptions = document.getElementById('resistanceOptions');
if (mode === 'resistance') {
resistanceOptions.style.display = 'block';
} else {
resistanceOptions.style.display = 'none';
}
}
function updateResistanceParams() {
const model = document.getElementById('resistanceModel').value;
const linearParams = document.getElementById('linearParams');
const quadraticParams = document.getElementById('quadraticParams');
if (model === 'linear') {
linearParams.style.display = 'block';
quadraticParams.style.display = 'none';
} else {
linearParams.style.display = 'none';
quadraticParams.style.display = 'block';
}
}
function toggleSimulation() {
running = !running;
if (running) {
startTime = performance.now();
simulate();
document.getElementById('startPause').textContent = '一時停止';
} else {
document.getElementById('startPause').textContent = '開始';
}
}
function resetSimulation() {
running = false;
t = 0;
h = parseFloat(document.getElementById('h').value);
v = parseFloat(document.getElementById('v0').value);
document.getElementById('startPause').textContent = '開始';
drawGraph();
}
function simulate() {
if (!running || h <= 0) return;
const g = parseFloat(document.getElementById('g').value);
const mode = document.getElementById('mode').value;
const model = document.getElementById('resistanceModel').value;
let Fd = 0;
if (mode === 'resistance') {
if (model === 'linear') {
const c = parseFloat(document.getElementById('c').value);
Fd = c * v;
} else {
const rho = parseFloat(document.getElementById('rho').value);
const Cd = parseFloat(document.getElementById('Cd').value);
const A = parseFloat(document.getElementById('A').value);
Fd = 0.5 * rho * Cd * A * v * Math.abs(v);
}
}
const a = g - Fd / (h > 0 ? 1 : 0.001); // 分母が0にならないよう微小値を加える
const v_new = v + a * dt;
const h_new = h + v * dt + 0.5 * a * dt * dt;
v = v_new;
h = h_new;
t += dt;
drawGraph();
requestAnimationFrame(simulate);
}
function drawGraph() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = 'black';
ctx.beginPath();
ctx.moveTo(0, canvas.height);
ctx.lineTo(canvas.width, canvas.height);
ctx.stroke();
ctx.strokeStyle = 'blue';
ctx.beginPath();
ctx.moveTo(0, canvas.height - h);
for (let i = 0; i < canvas.width; i++) {
const time = (i / canvas.width) * t;
const height = calculateHeight(time);
ctx.lineTo(i, canvas.height - height);
}
ctx.stroke();
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.moveTo(0, canvas.height / 2);
for (let i = 0; i < canvas.width; i++) {
const time = (i / canvas.width) * t;
const velocity = calculateVelocity(time);
ctx.lineTo(i, canvas.height / 2 - velocity * 50); // スケール調整
}
ctx.stroke();
}
function calculateHeight(time) {
const g = parseFloat(document.getElementById('g').value);
const mode = document.getElementById('mode').value;
const model = document.getElementById('resistanceModel').value;
let h_calc = parseFloat(document.getElementById('h').value);
let v_calc = parseFloat(document.getElementById('v0').value);
let t_calc = 0;
while (t_calc < time && h_calc > 0) {
let Fd = 0