SOURCE

console 命令行工具 X clear

                    
>
console
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>牛顿第二定律 · 固定滑块版</title>
    <style>
        /* ========== 全局重置 & 布局 ========== */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            user-select: none;
        }
        body {
            font-family: 'Segoe UI', Roboto, sans-serif;
            background: #eef2f7;
            padding-bottom: 200px; /* 为底部固定控制栏留出空间 */
            min-height: 100vh;
        }
        /* ========== 主容器 ========== */
        .container {
            max-width: 900px;
            margin: 20px auto;
            padding: 0 20px;
        }
        h1 {
            font-weight: 400;
            color: #1a2a3a;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 12px;
        }
        h1 small {
            font-size: 1rem;
            font-weight: 300;
            color: #4a6a8a;
        }

        /* ========== 画布 ========== */
        .canvas-wrap {
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
            padding: 10px;
            margin-bottom: 20px;
        }
        canvas {
            display: block;
            width: 100%;
            height: auto;
            background: #fafcff;
            border-radius: 12px;
            touch-action: none;
        }

        /* ========== 实时数据面板 ========== */
        .data-panel {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
            background: white;
            border-radius: 16px;
            padding: 16px 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.06);
            font-size: 1.1rem;
            gap: 8px 20px;
        }
        .data-item {
            display: flex;
            align-items: baseline;
            gap: 4px;
        }
        .data-item .label {
            color: #4a5a6a;
            font-weight: 400;
        }
        .data-item .value {
            font-weight: 700;
            color: #0f62fe;
            min-width: 60px;
            text-align: right;
        }

        /* ========== 固定控制栏(始终显示在底部) ========== */
        .control-bar {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(255,255,255,0.95);
            backdrop-filter: blur(8px);
            box-shadow: 0 -4px 20px rgba(0,0,0,0.1);
            padding: 16px 24px 20px;
            border-top: 1px solid #dde3ed;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: center;
            gap: 16px 30px;
            z-index: 1000;
        }
        .control-group {
            display: flex;
            align-items: center;
            gap: 12px;
            flex-wrap: wrap;
        }
        .control-item {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .control-item label {
            font-weight: 600;
            color: #2c3e50;
            font-size: 0.9rem;
            white-space: nowrap;
        }
        .control-item label span {
            color: #0f62fe;
            font-weight: 700;
            min-width: 45px;
            display: inline-block;
        }
        input[type="range"] {
            width: 130px;
            height: 4px;
            background: #d0d8e4;
            border-radius: 4px;
            -webkit-appearance: none;
            appearance: none;
        }
        input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 16px;
            height: 16px;
            background: #0f62fe;
            border-radius: 50%;
            cursor: pointer;
            border: 2px solid white;
            box-shadow: 0 2px 6px rgba(15,98,254,0.3);
        }
        input[type="range"]::-moz-range-thumb {
            width: 16px;
            height: 16px;
            background: #0f62fe;
            border-radius: 50%;
            cursor: pointer;
            border: 2px solid white;
        }

        .action-buttons {
            display: flex;
            gap: 8px;
        }
        .btn {
            padding: 6px 18px;
            border: none;
            border-radius: 30px;
            font-weight: 600;
            font-size: 0.9rem;
            background: #e6ecf5;
            color: #1a2634;
            cursor: pointer;
            transition: 0.2s;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            white-space: nowrap;
        }
        .btn-primary {
            background: #0f62fe;
            color: white;
        }
        .btn-primary:hover { background: #0050d0; }
        .btn-danger {
            background: #e74c3c;
            color: white;
        }
        .btn-danger:hover { background: #c0392b; }
        .btn-success {
            background: #2ecc71;
            color: white;
        }
        .btn-success:hover { background: #27ae60; }

        /* ========== 响应式适配 ========== */
        @media (max-width: 700px) {
            .control-bar {
                flex-direction: column;
                padding: 12px 16px;
                gap: 10px;
            }
            .control-group {
                width: 100%;
                justify-content: center;
            }
            input[type="range"] { width: 100px; }
            .data-panel { font-size: 0.95rem; }
        }
        @media (max-width: 500px) {
            .control-item label { font-size: 0.8rem; }
            input[type="range"] { width: 70px; }
            .btn { padding: 4px 12px; font-size: 0.8rem; }
        }
    </style>
</head>
<body>

<div class="container">
    <h1>
        ⚡ 牛顿第二定律 <small>F = m·a &nbsp; 滑块始终可见</small>
    </h1>

    <!-- 画布 -->
    <div class="canvas-wrap">
        <canvas id="simCanvas" width="800" height="320"></canvas>
    </div>

    <!-- 数据面板 -->
    <div class="data-panel" id="dataPanel">
        <div class="data-item"><span class="label">加速度 a =</span> <span class="value" id="accelDisplay">0.00</span> m/s²</div>
        <div class="data-item"><span class="label">速度 v =</span> <span class="value" id="velDisplay">0.00</span> m/s</div>
        <div class="data-item"><span class="label">位移 x =</span> <span class="value" id="dispDisplay">0.00</span> m</div>
        <div class="data-item"><span class="label">时间 t =</span> <span class="value" id="timeDisplay">0.00</span> s</div>
    </div>
</div>

<!-- ========== 固定控制栏(固定在底部) ========== -->
<div class="control-bar">
    <div class="control-group">
        <div class="control-item">
            <label>拉力 F <span id="forceVal">8.0</span> N</label>
            <input type="range" id="forceSlider" min="0" max="20" step="0.1" value="8.0">
        </div>
        <div class="control-item">
            <label>质量 m <span id="massVal">2.0</span> kg</label>
            <input type="range" id="massSlider" min="0.5" max="10" step="0.1" value="2.0">
        </div>
        <div class="control-item">
            <label>摩擦 μ <span id="muVal">0.10</span></label>
            <input type="range" id="muSlider" min="0" max="0.80" step="0.01" value="0.10">
        </div>
    </div>
    <div class="action-buttons">
        <button class="btn btn-primary" id="startBtn">▶ 启动</button>
        <button class="btn" id="pauseBtn">⏸ 暂停</button>
        <button class="btn btn-danger" id="resetBtn">⟲ 重置</button>
    </div>
</div>

<script>
    // ================================================================
    // 1. 获取 DOM 元素引用
    // ================================================================
    const canvas = document.getElementById('simCanvas');
    const ctx = canvas.getContext('2d');
    const W = 800, H = 320;

    const forceSlider = document.getElementById('forceSlider');
    const massSlider = document.getElementById('massSlider');
    const muSlider = document.getElementById('muSlider');
    const forceVal = document.getElementById('forceVal');
    const massVal = document.getElementById('massVal');
    const muVal = document.getElementById('muVal');
    const accelDisplay = document.getElementById('accelDisplay');
    const velDisplay = document.getElementById('velDisplay');
    const dispDisplay = document.getElementById('dispDisplay');
    const timeDisplay = document.getElementById('timeDisplay');
    const startBtn = document.getElementById('startBtn');
    const pauseBtn = document.getElementById('pauseBtn');
    const resetBtn = document.getElementById('resetBtn');

    // ================================================================
    // 2. 物理参数(可从滑块读取)
    // ================================================================
    const G = 10;                // 重力加速度 (m/s²),用于计算摩擦力
    let force = 8.0;            // 拉力 (N)
    let mass = 2.0;             // 质量 (kg)
    let mu = 0.10;              // 摩擦系数

    // 状态变量
    let position = 0;           // 位移 (m)
    let velocity = 0;           // 速度 (m/s)
    let time = 0;               // 仿真时间 (s)
    let isRunning = false;      // 是否处于运行状态
    let isPaused = false;       // 是否暂停
    let animationId = null;     // requestAnimationFrame 句柄

    // 用于绘制速度-时间曲线
    let history = [];
    const MAX_HISTORY = 120;

    // ================================================================
    // 3. 辅助函数:更新显示标签与数值
    // ================================================================
    function updateLabels() {
        forceVal.textContent = force.toFixed(1);
        massVal.textContent = mass.toFixed(1);
        muVal.textContent = mu.toFixed(2);
    }

    function updateDataDisplay() {
        // 计算当前加速度(考虑静摩擦力)
        const friction = mu * mass * G;
        let accel = 0;
        // 如果合力不足以克服摩擦且速度为零,则加速度为0
        const net = force - friction;
        if (Math.abs(velocity) < 0.001 && net <= 0) {
            accel = 0;
        } else {
            accel = net / mass;
        }
        accelDisplay.textContent = accel.toFixed(2);
        velDisplay.textContent = velocity.toFixed(2);
        dispDisplay.textContent = position.toFixed(2);
        timeDisplay.textContent = time.toFixed(2);
    }

    // ================================================================
    // 4. 物理更新(欧拉积分)
    // ================================================================
    function physicsUpdate(dt) {
        if (!isRunning || isPaused) return;

        const friction = mu * mass * G;
        const netForce = force - friction;

        let accel = 0;
        // 判断是否静止且合力不足以克服静摩擦
        if (Math.abs(velocity) < 0.001 && netForce <= 0) {
            accel = 0;
            velocity = 0;
        } else {
            accel = netForce / mass;
        }

        // 速度与位置更新(欧拉法)
        velocity += accel * dt;
        position += velocity * dt;
        time += dt;

        // 防止物体回到起点左侧(位移不小于0)
        if (position < 0) {
            position = 0;
            velocity = 0;
        }

        // 记录速度历史(用于绘制 v-t 图)
        if (isRunning && !isPaused) {
            history.push({ time: time, velocity: velocity });
            if (history.length > MAX_HISTORY) history.shift();
        }

        updateDataDisplay();
    }

    // ================================================================
    // 5. 绘图函数
    // ================================================================
    function drawScene() {
        ctx.clearRect(0, 0, W, H);

        // ---- 5.1 背景 ----
        const grad = ctx.createLinearGradient(0, 0, 0, H);
        grad.addColorStop(0, '#dce6f2');
        grad.addColorStop(1, '#f5f9ff');
        ctx.fillStyle = grad;
        ctx.fillRect(0, 0, W, H);

        // ---- 5.2 地面 ----
        const GROUND_Y = 250;
        ctx.fillStyle = '#d0dae6';
        ctx.fillRect(0, GROUND_Y, W, H - GROUND_Y);
        ctx.fillStyle = '#b0c0d0';
        ctx.fillRect(0, GROUND_Y, W, 3);

        // 刻度线(表示位移)
        ctx.strokeStyle = '#a0b0c0';
        ctx.lineWidth = 1;
        for (let x = 50; x < W; x += 50) {
            ctx.beginPath();
            ctx.moveTo(x, GROUND_Y);
            ctx.lineTo(x, GROUND_Y + 8);
            ctx.stroke();
        }

        // ---- 5.3 物体(方块) ----
        const OBJ_SIZE = 40;
        // 映射:1m = 2.5 像素,初始位置在 x=100 处
        const objX = 100 + position * 2.5;
        const objY = GROUND_Y - OBJ_SIZE;

        // 阴影
        ctx.shadowColor = 'rgba(0,0,0,0.1)';
        ctx.shadowBlur = 10;
        ctx.fillStyle = '#3a4a6a';
        ctx.beginPath();
        ctx.roundRect(objX, objY, OBJ_SIZE, OBJ_SIZE, 6);
        ctx.fill();
        ctx.shadowBlur = 0;

        // 边框
        ctx.strokeStyle = '#1a2a3a';
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.roundRect(objX, objY, OBJ_SIZE, OBJ_SIZE, 6);
        ctx.stroke();

        // ---- 5.4 力矢量箭头 ----
        // 拉力(向右)
        if (force > 0.1) {
            const arrowLen = Math.min(force * 3, 70);
            const startX = objX + OBJ_SIZE + 6;
            const startY = objY + OBJ_SIZE/2;
            ctx.fillStyle = '#e74c3c';
            ctx.strokeStyle = '#e74c3c';
            ctx.lineWidth = 3;
            ctx.beginPath();
            ctx.moveTo(startX, startY);
            ctx.lineTo(startX + arrowLen, startY);
            ctx.stroke();
            // 箭头头
            ctx.beginPath();
            ctx.moveTo(startX + arrowLen, startY);
            ctx.lineTo(startX + arrowLen - 10, startY - 6);
            ctx.lineTo(startX + arrowLen - 10, startY + 6);
            ctx.closePath();
            ctx.fill();
            ctx.font = '14px sans-serif';
            ctx.fillText('F', startX + arrowLen/2 - 10, startY - 12);
        }

        // 摩擦力(向左)
        if (mu > 0.01 && (velocity > 0.01 || force > 0.1)) {
            const fricLen = Math.min(mu * mass * G * 0.8, 50);
            const startX = objX - 6;
            const startY = objY + OBJ_SIZE/2;
            ctx.fillStyle = '#3498db';
            ctx.strokeStyle = '#3498db';
            ctx.lineWidth = 3;
            ctx.beginPath();
            ctx.moveTo(startX, startY);
            ctx.lineTo(startX - fricLen, startY);
            ctx.stroke();
            ctx.beginPath();
            ctx.moveTo(startX - fricLen, startY);
            ctx.lineTo(startX - fricLen + 10, startY - 6);
            ctx.lineTo(startX - fricLen + 10, startY + 6);
            ctx.closePath();
            ctx.fill();
            ctx.font = '14px sans-serif';
            ctx.fillStyle = '#3498db';
            ctx.fillText('f', startX - fricLen/2 - 10, startY - 12);
        }

        // ---- 5.5 速度-时间曲线(小图) ----
        if (history.length > 2) {
            const chartX = 600, chartY = 40, chartW = 170, chartH = 90;
            ctx.fillStyle = 'rgba(255,255,255,0.85)';
            ctx.strokeStyle = '#ccd8e4';
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.roundRect(chartX, chartY, chartW, chartH, 6);
            ctx.fill();
            ctx.stroke();

            // 坐标轴
            ctx.strokeStyle = '#5a6a7a';
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.moveTo(chartX + 20, chartY + chartH - 15);
            ctx.lineTo(chartX + chartW - 10, chartY + chartH - 15);
            ctx.moveTo(chartX + 20, chartY + chartH - 15);
            ctx.lineTo(chartX + 20, chartY + 8);
            ctx.stroke();

            ctx.font = '10px sans-serif';
            ctx.fillStyle = '#3a4a5a';
            ctx.fillText('v (m/s)', chartX + chartW - 40, chartY + 14);
            ctx.fillText('t', chartX + chartW - 12, chartY + chartH - 4);

            // 绘制速度曲线
            if (history.length > 1) {
                const minV = Math.min(0, ...history.map(d => d.velocity));
                const maxV = Math.max(0.5, ...history.map(d => d.velocity));
                const range = Math.max(0.5, maxV - minV);
                const xScale = (chartW - 40) / Math.max(history.length - 1, 1);
                const yScale = (chartH - 30) / range;

                ctx.strokeStyle = '#0f62fe';
                ctx.lineWidth = 2.5;
                ctx.beginPath();
                for (let i = 0; i < history.length; i++) {
                    const x = chartX + 20 + i * xScale;
                    const y = chartY + chartH - 15 - (history[i].velocity - minV) * yScale;
                    if (i === 0) ctx.moveTo(x, y);
                    else ctx.lineTo(x, y);
                }
                ctx.stroke();
            }
        }

        // ---- 5.6 文字标注 ----
        ctx.font = '16px "Segoe UI", sans-serif';
        ctx.fillStyle = '#1a2a3a';
        ctx.fillText(`质量 m = ${mass.toFixed(1)} kg`, 20, 30);
        ctx.fillText(`拉力 F = ${force.toFixed(1)} N`, 20, 58);
        ctx.fillText(`摩擦 μ = ${mu.toFixed(2)}`, 20, 86);

        // ---- 5.7 未启动时的遮罩提示 ----
        if (!isRunning && !isPaused) {
            ctx.fillStyle = 'rgba(0,0,0,0.4)';
            ctx.fillRect(0, 0, W, H);
            ctx.fillStyle = '#ffffff';
            ctx.font = 'bold 28px sans-serif';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.fillText('点击 “启动” 开始仿真', W/2, H/2 - 10);
            ctx.textAlign = 'left';
            ctx.textBaseline = 'alphabetic';
        }
    }

    // ================================================================
    // 6. 动画循环
    // ================================================================
    function loop(timestamp) {
        const dt = 0.016;          // 约 60fps 的固定步长
        physicsUpdate(dt);
        drawScene();
        animationId = requestAnimationFrame(loop);
    }

    // ================================================================
    // 7. 控制函数:启动、暂停、重置
    // ================================================================
    function startSim() {
        if (!isRunning) {
            isRunning = true;
            isPaused = false;
            startBtn.textContent = '▶ 运行中';
            startBtn.disabled = true;
            pauseBtn.textContent = '⏸ 暂停';
        } else if (isPaused) {
            isPaused = false;
            pauseBtn.textContent = '⏸ 暂停';
        }
    }

    function pauseSim() {
        if (isRunning && !isPaused) {
            isPaused = true;
            pauseBtn.textContent = '▶ 继续';
        } else if (isRunning && isPaused) {
            isPaused = false;
            pauseBtn.textContent = '⏸ 暂停';
        }
    }

    function resetSim() {
        // 停止运行
        isRunning = false;
        isPaused = false;
        if (animationId) cancelAnimationFrame(animationId);
        startBtn.textContent = '▶ 启动';
        startBtn.disabled = false;
        pauseBtn.textContent = '⏸ 暂停';
        // 重置物理量
        position = 0;
        velocity = 0;
        time = 0;
        history = [];
        updateDataDisplay();
        drawScene();
        // 重新启动循环
        animationId = requestAnimationFrame(loop);
    }

    // ================================================================
    // 8. 滑块事件监听(实时更新参数)
    // ================================================================
    function onSliderChange() {
        force = parseFloat(forceSlider.value);
        mass = parseFloat(massSlider.value);
        mu = parseFloat(muSlider.value);
        updateLabels();
        // 如果仿真未运行,立即更新数据显示和画面(以便预览)
        if (!isRunning) {
            updateDataDisplay();
            drawScene();
        }
        // 如果正在运行,数据会在下一次物理更新中刷新
    }

    forceSlider.addEventListener('input', onSliderChange);
    massSlider.addEventListener('input', onSliderChange);
    muSlider.addEventListener('input', onSliderChange);

    // ================================================================
    // 9. 按钮事件绑定
    // ================================================================
    startBtn.addEventListener('click', startSim);
    pauseBtn.addEventListener('click', pauseSim);
    resetBtn.addEventListener('click', resetSim);

    // ================================================================
    // 10. 初始化 roundRect 方法(兼容旧浏览器)
    // ================================================================
    if (!CanvasRenderingContext2D.prototype.roundRect) {
        CanvasRenderingContext2D.prototype.roundRect = function(x, y, w, h, r) {
            if (r > w/2) r = w/2;
            if (r > h/2) r = h/2;
            this.moveTo(x + r, y);
            this.arcTo(x + w, y, x + w, y + h, r);
            this.arcTo(x + w, y + h, x, y + h, r);
            this.arcTo(x, y + h, x, y, r);
            this.arcTo(x, y, x + w, y, r);
            return this;
        };
    }

    // ================================================================
    // 11. 启动程序
    // ================================================================
    function init() {
        // 从滑块读取初始值
        force = parseFloat(forceSlider.value);
        mass = parseFloat(massSlider.value);
        mu = parseFloat(muSlider.value);
        updateLabels();
        updateDataDisplay();
        // 开始动画循环(默认未运行,但需要绘制静态画面)
        animationId = requestAnimationFrame(loop);
    }

    init();
</script>
</body>
</html>