/* 力的课堂 — 交互脚本 */ // ============ 自由落体实验 ============ (function(){ const canvas = document.getElementById('gravityCanvas'); const ctx = canvas.getContext('2d'); const W = canvas.width, H = canvas.height; const balls = { big: { x: 250, y: 40, r: 22, vy: 0, color: '#ff6b6b', mass: 3 }, small: { x: 550, y: 40, r: 12, vy: 0, color: '#4f8cff', mass: 1 } }; const floorY = 350; const g = 9.8; let running = false, startTime = 0; let landedBig = false, landedSmall = false; let timeBig = 0, timeSmall = 0; let airResistance = false; function drawScene(){ ctx.clearRect(0,0,W,H); ctx.fillStyle = '#e9ecef'; ctx.fillRect(0, floorY, W, H - floorY); ctx.strokeStyle = '#adb5bd'; ctx.setLineDash([4,4]); ctx.beginPath(); ctx.moveTo(0, floorY); ctx.lineTo(W, floorY); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = '#868e96'; ctx.font = '14px sans-serif'; ctx.fillText('地面', 10, floorY - 6); ctx.strokeStyle = '#dee2e6'; ctx.lineWidth = 1; for(let y = 40; y < floorY; y += 40){ ctx.beginPath(); ctx.moveTo(40, y); ctx.lineTo(60, y); ctx.stroke(); ctx.fillStyle = '#adb5bd'; ctx.fillText((floorY - y) + ' px', 10, y + 4); } drawBall(balls.big); drawBall(balls.small); ctx.fillStyle = '#495057'; ctx.font = 'bold 13px sans-serif'; ctx.fillText('大球 (m=3kg)', balls.big.x - 30, 30); ctx.fillText('小球 (m=1kg)', balls.small.x - 30, 30); ctx.strokeStyle = 'rgba(255,107,107,.3)'; ctx.lineWidth = 2; ctx.setLineDash([6,4]); ctx.beginPath(); ctx.moveTo(balls.big.x, balls.big.y); ctx.lineTo(balls.big.x, floorY); ctx.stroke(); ctx.strokeStyle = 'rgba(79,140,255,.3)'; ctx.beginPath(); ctx.moveTo(balls.small.x, balls.small.y); ctx.lineTo(balls.small.x, floorY); ctx.stroke(); ctx.setLineDash([]); if(!running && !landedBig && !landedSmall){ ctx.fillStyle = '#868e96'; ctx.font = '16px sans-serif'; ctx.fillText('点击"开始下落"', W/2 - 60, H/2); } } function drawBall(b){ const grad = ctx.createRadialGradient(b.x - b.r*0.3, b.y - b.r*0.3, b.r*0.1, b.x, b.y, b.r); grad.addColorStop(0, '#fff'); grad.addColorStop(0.3, b.color); grad.addColorStop(1, shadeColor(b.color, -30)); ctx.fillStyle = grad; ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, Math.PI*2); ctx.fill(); ctx.strokeStyle = 'rgba(0,0,0,.2)'; ctx.lineWidth = 1; ctx.stroke(); } function shadeColor(hex, percent){ const f = parseInt(hex.slice(1),16); const t = percent < 0 ? 0 : 255; const p = Math.abs(percent)/100; const R = f >> 16, G = (f >> 8) & 0xff, B = f & 0xff; return '#' + (0x1000000 + (Math.round((t-R)*p)+R)*0x10000 + (Math.round((t-G)*p)+G)*0x100 + (Math.round((t-B)*p)+B)).toString(16).slice(1); } let lastTime = 0; function animate(t){ if(!lastTime) lastTime = t; const dt = Math.min((t - lastTime)/1000, 0.05); lastTime = t; if(running){ const elapsed = (t - startTime)/1000; if(!landedBig){ const a = airResistance ? g - 0.001 * balls.big.vy * balls.big.vy / balls.big.mass : g; balls.big.vy += a * dt * 50; if(balls.big.vy < 0) balls.big.vy = 0; balls.big.y += balls.big.vy * dt * 2; if(balls.big.y >= floorY - balls.big.r){ balls.big.y = floorY - balls.big.r; balls.big.vy = 0; landedBig = true; timeBig = elapsed; } } if(!landedSmall){ const a = airResistance ? g - 0.001 * balls.small.vy * balls.small.vy / balls.small.mass : g; balls.small.vy += a * dt * 50; if(balls.small.vy < 0) balls.small.vy = 0; balls.small.y += balls.small.vy * dt * 2; if(balls.small.y >= floorY - balls.small.r){ balls.small.y = floorY - balls.small.r; balls.small.vy = 0; landedSmall = true; timeSmall = elapsed; } } if(landedBig && landedSmall){ running = false; updateGravityUI(); } else updateGravityUI(elapsed); } drawScene(); requestAnimationFrame(animate); } function updateGravityUI(elapsed){ document.getElementById('bigTime').textContent = landedBig ? timeBig.toFixed(2) + ' s' : (elapsed ? elapsed.toFixed(2) + ' s' : '0.00 s'); document.getElementById('smallTime').textContent = landedSmall ? timeSmall.toFixed(2) + ' s' : (elapsed ? elapsed.toFixed(2) + ' s' : '0.00 s'); if(!running && !landedBig && !landedSmall) document.getElementById('gStatus').textContent = '待开始'; else if(landedBig && landedSmall){ const d = Math.abs(timeBig - timeSmall); document.getElementById('gStatus').textContent = d < 0.01 ? '同时落地!🎉' : (timeBig < timeSmall ? '大球先落地' : '小球先落地'); } else document.getElementById('gStatus').textContent = '下落中...'; } document.getElementById('dropBtn').addEventListener('click', () => { if(running) return; resetBalls(); running = true; startTime = performance.now(); updateGravityUI(0); }); document.getElementById('resetGravity').addEventListener('click', () => { resetBalls(); updateGravityUI(); }); document.getElementById('airResistance').addEventListener('change', (e) => { airResistance = e.target.checked; }); function resetBalls(){ balls.big.y = 40; balls.big.vy = 0; balls.small.y = 40; balls.small.vy = 0; landedBig = false; landedSmall = false; timeBig = 0; timeSmall = 0; running = false; updateGravityUI(); } updateGravityUI(); requestAnimationFrame(animate); })(); // ============ 拉伸弹簧实验 ============ (function(){ const canvas = document.getElementById('elasticCanvas'); const ctx = canvas.getContext('2d'); const W = canvas.width, H = canvas.height; let wallX = 80; let springY = H / 2; let l0 = 100; let k = 50; let dragging = false; let hoverHandle = false; let dragHandleX = wallX + l0; function drawScene(){ ctx.clearRect(0, 0, W, H); ctx.fillStyle = '#868e96'; ctx.fillRect(wallX - 20, springY - 80, 160, 160); ctx.strokeStyle = '#495057'; ctx.lineWidth = 2; for(let i = 0; i < 10; i++){ const y = springY - 80 + i * 18; ctx.beginPath(); ctx.moveTo(wallX - 20, y); ctx.lineTo(wallX, y + 15); ctx.stroke(); } const endX = dragHandleX; const currentLen = endX - wallX; const x = currentLen - l0; drawSpring(wallX, springY, endX, springY, 14); const isHover = hoverHandle; const glowR = isHover ? 40 : 0; if(glowR > 0){ const glow = ctx.createRadialGradient(endX, springY, 5, endX, springY, glowR); glow.addColorStop(0, 'rgba(79,140,255,.35)'); glow.addColorStop(1, 'rgba(79,140,255,0)'); ctx.fillStyle = glow; ctx.beginPath(); ctx.arc(endX, springY, glowR, 0, Math.PI*2); ctx.fill(); } ctx.fillStyle = '#2a5bd7'; ctx.fillRect(endX - 14, springY - 38, 28, 76); ctx.fillStyle = isHover ? '#4f8cff' : '#5c7cfa'; ctx.fillRect(endX - 12, springY - 36, 24, 72); ctx.fillStyle = '#fff'; ctx.font = 'bold 11px sans-serif'; ctx.textAlign = 'center'; ctx.fillText('⇔', endX, springY + 4); ctx.textAlign = 'start'; if(x > 0){ ctx.strokeStyle = '#ff6b6b'; ctx.fillStyle = '#ff6b6b'; ctx.lineWidth = 2; const forceLen = Math.min(x * 0.7, 100); ctx.beginPath(); ctx.moveTo(endX, springY + 50); ctx.lineTo(endX + forceLen, springY + 50); ctx.stroke(); ctx.beginPath(); ctx.moveTo(endX + forceLen, springY + 50); ctx.lineTo(endX + forceLen - 8, springY + 45); ctx.lineTo(endX + forceLen - 8, springY + 55); ctx.closePath(); ctx.fill(); ctx.fillStyle = '#c92a2a'; ctx.font = 'bold 14px sans-serif'; const forceN = (k * x / 50).toFixed(1); ctx.fillText('F = ' + forceN + ' N', endX + forceLen + 8, springY + 55); } ctx.strokeStyle = '#adb5bd'; ctx.setLineDash([4,4]); ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(wallX + l0, springY - 60); ctx.lineTo(wallX + l0, springY + 60); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = '#868e96'; ctx.font = '12px sans-serif'; ctx.fillText('原长 L₀=' + l0 + 'px', wallX + l0 - 30, springY - 70); ctx.fillStyle = '#495057'; ctx.font = '13px sans-serif'; ctx.fillText('当前长度 L=' + currentLen + 'px', 20, 30); ctx.fillText('形变量 x=' + x + 'px', 20, 55); drawFXChart(x); } function drawSpring(x1, y1, x2, y2, coils){ const len = x2 - x1; const amplitude = 10; ctx.strokeStyle = '#5c7cfa'; ctx.lineWidth = 3; ctx.beginPath(); ctx.moveTo(x1, y1); for(let i = 0; i <= coils * 2; i++){ const t = i / (coils * 2); const px = x1 + len * t; const py = y1 + (i % 2 === 0 ? -amplitude : amplitude); ctx.lineTo(px, py); } ctx.lineTo(x2, y2); ctx.stroke(); } function drawFXChart(currentX){ const cx = 500, cy = 100; const cw = 250, ch = 140; ctx.fillStyle = 'rgba(248,250,255,.8)'; ctx.fillRect(cx, cy, cw, ch); ctx.strokeStyle = '#dee2e6'; ctx.strokeRect(cx, cy, cw, ch); ctx.strokeStyle = '#868e96'; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(cx, cy + ch - 20); ctx.lineTo(cx + cw - 10, cy + ch - 20); ctx.lineTo(cx, cy + ch - 20); ctx.lineTo(cx, cy + 10); ctx.stroke(); ctx.fillStyle = '#868e96'; ctx.font = '10px sans-serif'; ctx.fillText('x (px)', cx + cw - 30, cy + ch - 5); ctx.save(); ctx.translate(cx + 10, cy + ch - 20); ctx.rotate(-Math.PI/2); ctx.fillText('F (N)', 0, 0); ctx.restore(); ctx.strokeStyle = '#51cf66'; ctx.lineWidth = 2; ctx.beginPath(); for(let i = 0; i <= 40; i++){ const px = i / 40; const xVal = px * 400; const fVal = k * xVal / 50; const px_ = cx + 10 + (xVal / 400) * (cw - 20); const py_ = cy + ch - 20 - (fVal / 80) * (ch - 30); if(i === 0) ctx.moveTo(px_, py_); else ctx.lineTo(px_, py_); } ctx.stroke(); const curF = k * currentX / 50; const dotPx = cx + 10 + (Math.max(0, currentX) / 400) * (cw - 20); const dotPy = cy + ch - 20 - (Math.max(0, curF) / 80) * (ch - 30); ctx.fillStyle = '#ff6b6b'; ctx.beginPath(); ctx.arc(dotPx, dotPy, 5, 0, Math.PI*2); ctx.fill(); ctx.strokeStyle = '#fff'; ctx.lineWidth = 2; ctx.stroke(); } function updateUI(){ const curLen = dragHandleX - wallX; const x = curLen - l0; const F = k * x / 50; const ep = 0.5 * k * x * x / 2500; document.getElementById('l0Val').textContent = l0; document.getElementById('kVal').textContent = k; document.getElementById('xVal').textContent = x + ' px'; document.getElementById('fVal').textContent = (x > 0 ? F.toFixed(2) : '0.00') + ' N'; document.getElementById('lenVal').textContent = curLen + ' px'; document.getElementById('epVal').textContent = (x > 0 ? ep.toFixed(2) : '0.00') + ' J'; } function toCanvasXY(clientX, clientY){ const rect = canvas.getBoundingClientRect(); return { x: (clientX - rect.left) * (W / rect.width), y: (clientY - rect.top) * (H / rect.height) }; } canvas.addEventListener('mousedown', (e) => { const {x:mx, y:my} = toCanvasXY(e.clientX, e.clientY); if(Math.abs(mx - dragHandleX) < 50 && Math.abs(my - springY) < 50){ dragging = true; canvas.style.cursor = 'grabbing'; } }); window.addEventListener('mousemove', (e) => { const {x:mx, y:my} = toCanvasXY(e.clientX, e.clientY); const nearHandle = Math.abs(mx - dragHandleX) < 50 && Math.abs(my - springY) < 50; hoverHandle = nearHandle; if(!dragging){ canvas.style.cursor = nearHandle ? 'grab' : 'default'; return; } dragHandleX = Math.max(wallX + 20, Math.min(mx, W - 40)); updateUI(); }); window.addEventListener('mouseup', () => { dragging = false; canvas.style.cursor = hoverHandle ? 'grab' : 'default'; }); canvas.addEventListener('touchstart', (e) => { const t = e.touches[0]; const {x:mx, y:my} = toCanvasXY(t.clientX, t.clientY); if(Math.abs(mx - dragHandleX) < 70 && Math.abs(my - springY) < 70){ dragging = true; } e.preventDefault(); }, {passive: false}); canvas.addEventListener('touchmove', (e) => { if(!dragging) return; const t = e.touches[0]; const {x:mx} = toCanvasXY(t.clientX, t.clientY); dragHandleX = Math.max(wallX + 20, Math.min(mx, W - 40)); updateUI(); e.preventDefault(); }, {passive: false}); canvas.addEventListener('touchend', () => dragging = false); document.getElementById('kSlider').addEventListener('input', (e) => { k = parseInt(e.target.value); updateUI(); }); document.getElementById('resetElastic').addEventListener('click', () => { dragHandleX = wallX + l0; k = 50; document.getElementById('kSlider').value = 50; updateUI(); }); function loop(){ drawScene(); requestAnimationFrame(loop); } updateUI(); loop(); })(); // ============ 推动木块实验 ============ (function(){ const canvas = document.getElementById('frictionCanvas'); const ctx = canvas.getContext('2d'); const W = canvas.width, H = canvas.height; let box = { x: 160, y: 130, w: 110, h: 75 }; let gY = 230; let nF = 100, mu = 0.3; let vel = 0, moving = false; let appF = 0, maxAppF = 300; let forceTarget = 0; let holding = false; let holdingLeft = false; let dragging = false, dragTargetX = 0, dragOffsetX = 0, hoverBox = false; let squash = 0; const muK = 0.8; function maxStaticF(){ return mu * nF; } function slidingF(){ return mu * muK * nF; } function toCanvasXY(clientX, clientY){ const rect = canvas.getBoundingClientRect(); return { x: (clientX - rect.left) * (W / rect.width), y: (clientY - rect.top) * (H / rect.height) }; } function drawScene(){ ctx.clearRect(0, 0, W, H); const groundGrad = ctx.createLinearGradient(0, gY, 0, H); groundGrad.addColorStop(0, '#f1f3f5'); groundGrad.addColorStop(1, '#e9ecef'); ctx.fillStyle = groundGrad; ctx.fillRect(0, gY, W, H - gY); ctx.strokeStyle = '#adb5bd'; ctx.lineWidth = 1; const rough = Math.floor(mu * 25); for(let x = 0; x < W; x += 6){ const off = Math.sin(x * 0.15) * rough; ctx.beginPath(); ctx.moveTo(x, gY); ctx.lineTo(x + 3, gY - 2 - off); ctx.stroke(); } ctx.fillStyle = '#868e96'; ctx.font = '11px sans-serif'; ctx.fillText('接触面 (μ=' + mu.toFixed(2) + ')', 10, gY + 18); drawForceBars(); const sq = squash; const bw = box.w * (1 - sq * 0.15); const bh = box.h * (1 + sq * 0.2); const bx = box.x + (box.w - bw) / 2; const by = box.y + (box.h - bh); if(hoverBox && !dragging){ ctx.shadowColor = '#4f8cff'; ctx.shadowBlur = 12; } else if(moving){ ctx.shadowColor = '#ff6b6b'; ctx.shadowBlur = 10; } else { ctx.shadowColor = 'rgba(0,0,0,.15)'; ctx.shadowBlur = 6; } const grad = ctx.createLinearGradient(bx, by, bx, by + bh); grad.addColorStop(0, '#ffd43b'); grad.addColorStop(1, '#fab005'); ctx.fillStyle = grad; ctx.fillRect(bx, by, bw, bh); ctx.shadowBlur = 0; ctx.strokeStyle = hoverBox ? '#4f8cff' : '#b8860b'; ctx.lineWidth = hoverBox ? 3 : 2; ctx.strokeRect(bx, by, bw, bh); ctx.fillStyle = '#868e96'; ctx.font = 'bold 14px sans-serif'; ctx.textAlign = 'center'; ctx.fillText('m', bx + bw / 2, by + bh / 2 + 5); ctx.textAlign = 'start'; const absAppF = Math.abs(appF); if(absAppF > 0.5){ const aLen = Math.min(absAppF * 0.55, 150); const label = 'F推=' + Math.round(absAppF) + 'N'; if(appF >= 0){ drawArrow(bx - aLen - 18, by + bh / 2, bx - 5, by + bh / 2, '#4f8cff', label); } else { drawArrow(bx + bw + aLen + 18, by + bh / 2, bx + bw + 5, by + bh / 2, '#4f8cff', label); } } const maxS = maxStaticF(); if(absAppF > 0.5 || moving){ if(moving){ const fricVal = slidingF(); const fLen = Math.min(fricVal * 0.55, 150); if(vel > 0){ drawArrow(bx + bw + fLen + 18, by + bh / 2 + 18, bx + bw + 5, by + bh / 2 + 18, '#ff6b6b', 'f滑=' + fricVal.toFixed(1) + 'N'); } else { drawArrow(bx - fLen - 18, by + bh / 2 + 18, bx - 5, by + bh / 2 + 18, '#ff6b6b', 'f滑=' + fricVal.toFixed(1) + 'N'); } } else { const fricVal = Math.min(absAppF, maxS); const fLen = Math.min(fricVal * 0.55, 150); if(appF >= 0){ drawArrow(bx + bw + fLen + 18, by + bh / 2 + 18, bx + bw + 5, by + bh / 2 + 18, '#f76707', 'f静=' + fricVal.toFixed(1) + 'N'); } else { drawArrow(bx - fLen - 18, by + bh / 2 + 18, bx - 5, by + bh / 2 + 18, '#f76707', 'f静=' + fricVal.toFixed(1) + 'N'); } } } drawArrow(bx + bw / 2, by - 5, bx + bw / 2, by - 50, '#5c7cfa', 'G=' + nF + 'N'); drawArrow(bx + bw / 2, by + bh + 5, bx + bw / 2, by + bh + 50, '#51cf66', 'N=' + nF + 'N'); ctx.fillStyle = moving ? '#c92a2a' : (absAppF > maxS ? '#e67700' : '#2b8a3e'); ctx.font = 'bold 15px sans-serif'; const stateTxt = moving ? ('运动中 ' + (vel > 0 ? '→' : '←') + ' (v=' + Math.abs(vel).toFixed(1) + ')') : (absAppF > maxS ? '即将滑动…' : '■ 静止'); ctx.fillText(stateTxt, 20, 28); if(hoverBox && !dragging){ ctx.fillStyle = '#4f8cff'; ctx.font = '12px sans-serif'; ctx.textAlign = 'center'; ctx.fillText('← 拖拽木块施加推力 →', bx + bw / 2, by - 60); ctx.textAlign = 'start'; } if(dragging){ const boxCenter = box.x + box.w / 2; const cy = box.y + box.h / 2; const tx = Math.max(20, Math.min(W - 20, dragTargetX)); ctx.strokeStyle = 'rgba(79,140,255,.6)'; ctx.lineWidth = 2; ctx.setLineDash([5, 4]); ctx.beginPath(); ctx.moveTo(tx, cy); ctx.lineTo(boxCenter, cy); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = 'rgba(79,140,255,.3)'; ctx.beginPath(); ctx.arc(tx, cy, 7, 0, Math.PI*2); ctx.fill(); ctx.strokeStyle = '#4f8cff'; ctx.lineWidth = 2; ctx.stroke(); } } function drawForceBars(){ const barY = gY + 35; const barH = 14; const barW = 220; const barX = W - barW - 30; const maxVal = Math.max(maxStaticF() * 1.2, maxAppF); ctx.fillStyle = '#e9ecef'; ctx.fillRect(barX, barY, barW, barH); ctx.fillRect(barX, barY + 22, barW, barH); ctx.fillRect(barX, barY + 44, barW, barH); const maxS = maxStaticF(); const sF = slidingF(); const absAppF = Math.abs(appF); const appRatio = Math.min(absAppF / maxVal, 1); const maxSRatio = Math.min(maxS / maxVal, 1); const sFRatio = Math.min(sF / maxVal, 1); const appColor = absAppF > maxS ? '#ff6b6b' : '#4f8cff'; ctx.fillStyle = appColor; ctx.fillRect(barX, barY, barW * appRatio, barH); ctx.fillStyle = '#fcc419'; ctx.fillRect(barX, barY + 22, barW * maxSRatio, barH); ctx.fillStyle = '#ff8787'; ctx.fillRect(barX, barY + 44, barW * sFRatio, barH); if(maxS > 0){ const thX = barX + barW * maxSRatio; ctx.strokeStyle = '#e67700'; ctx.lineWidth = 2; ctx.setLineDash([4, 3]); ctx.beginPath(); ctx.moveTo(thX, barY - 4); ctx.lineTo(thX, barY + barH + 70); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = '#e67700'; ctx.font = '10px sans-serif'; ctx.fillText('最大静摩擦阈值', thX + 4, barY - 6); } ctx.fillStyle = '#495057'; ctx.font = 'bold 11px sans-serif'; ctx.textAlign = 'right'; ctx.fillText('推力', barX - 5, barY + 11); ctx.fillText('最大静摩擦', barX - 5, barY + 33); ctx.fillText('滑动摩擦', barX - 5, barY + 55); ctx.textAlign = 'start'; ctx.fillStyle = appColor; ctx.font = 'bold 10px sans-serif'; ctx.fillText(Math.round(absAppF) + ' N', barX + barW + 5, barY + 11); ctx.fillStyle = '#b8860b'; ctx.fillText(maxS.toFixed(1) + ' N', barX + barW + 5, barY + 33); ctx.fillStyle = '#c92a2a'; ctx.fillText(sF.toFixed(1) + ' N', barX + barW + 5, barY + 55); } function drawArrow(x1, y1, x2, y2, color, label){ ctx.strokeStyle = color; ctx.fillStyle = color; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke(); const angle = Math.atan2(y2 - y1, x2 - x1); const headLen = 10; ctx.beginPath(); ctx.moveTo(x2, y2); ctx.lineTo(x2 - headLen * Math.cos(angle - Math.PI / 6), y2 - headLen * Math.sin(angle - Math.PI / 6)); ctx.lineTo(x2 - headLen * Math.cos(angle + Math.PI / 6), y2 - headLen * Math.sin(angle + Math.PI / 6)); ctx.closePath(); ctx.fill(); if(label){ const mx = (x1 + x2) / 2; const my = (y1 + y2) / 2; ctx.font = 'bold 11px sans-serif'; const metrics = ctx.measureText(label); ctx.fillStyle = '#fff'; ctx.fillRect(mx - metrics.width / 2 - 3, my - 8, metrics.width + 6, 16); ctx.strokeStyle = color; ctx.lineWidth = 1; ctx.strokeRect(mx - metrics.width / 2 - 3, my - 8, metrics.width + 6, 16); ctx.fillStyle = color; ctx.fillText(label, mx - metrics.width / 2, my + 4); } } function updatePhysics(){ const maxS = maxStaticF(); const sF = slidingF(); const mass = nF / 10; if(holding){ forceTarget = Math.min(forceTarget + 4, maxAppF); } else if(holdingLeft){ forceTarget = Math.max(forceTarget - 4, -maxAppF); } else if(dragging){ const boxCenter = box.x + box.w / 2; const pull = dragTargetX - boxCenter; forceTarget = Math.max(-maxAppF, Math.min(pull * 2.5, maxAppF)); } else { forceTarget *= 0.92; if(Math.abs(forceTarget) < 0.5) forceTarget = 0; } appF += (forceTarget - appF) * 0.25; const absAppF = Math.abs(appF); if(Math.abs(vel) < 0.01 && absAppF <= maxS + 0.5){ vel = 0; moving = false; } else { moving = true; vel += (appF / mass) * 0.016; if(Math.abs(vel) > 0.01){ const fricStep = (sF / mass) * 0.016; if(vel > 0) vel = Math.max(0, vel - fricStep); else vel = Math.min(0, vel + fricStep); } } // 拖拽时按距光标的剩余距离限制速度,越接近光标限速越低, // 避免速度过大冲过光标后弹簧反向拉回造成左右摆动 if(dragging && moving){ const pullNow = dragTargetX - (box.x + box.w / 2); const limitV = Math.abs(pullNow) * 0.18 + 0.5; if(vel > limitV) vel = limitV; else if(vel < -limitV) vel = -limitV; } if(moving){ box.x += vel * 0.016 * 60; if(box.x < 20){ box.x = 20; vel = 0; moving = false; } if(box.x + box.w > W - 20){ box.x = W - 20 - box.w; vel = 0; moving = false; } } squash *= 0.85; if(moving && Math.abs(vel) > 5) squash = Math.min(Math.abs(vel) / 40, 0.6); updateUI(); } function updateUI(){ document.getElementById('forceVal').textContent = Math.round(Math.abs(appF)) + ' N'; document.getElementById('normalVal').textContent = nF; document.getElementById('muVal').textContent = mu.toFixed(2); const maxS = maxStaticF(); const sF = slidingF(); document.getElementById('maxStatic').textContent = maxS.toFixed(2) + ' N'; document.getElementById('slidingF').textContent = sF.toFixed(2) + ' N'; const absAppF = Math.abs(appF); if(moving){ const dir = vel > 0 ? '→' : '←'; document.getElementById('frictionForce').textContent = '滑动摩擦 ' + sF.toFixed(2) + ' N'; document.getElementById('motionState').textContent = '运动中 ' + dir + ' (滑动摩擦)'; } else if(absAppF > 0.5){ const dir = appF > 0 ? '→' : '←'; if(absAppF <= maxS){ document.getElementById('frictionForce').textContent = '静摩擦 ' + absAppF.toFixed(2) + ' N'; document.getElementById('motionState').textContent = '静止 ' + dir + ' (静摩擦)'; } else { document.getElementById('frictionForce').textContent = '静摩擦 ' + maxS.toFixed(2) + ' N'; document.getElementById('motionState').textContent = '即将滑动 ' + dir + '…'; } } else { document.getElementById('frictionForce').textContent = '0.00 N'; document.getElementById('motionState').textContent = '静止'; } } canvas.addEventListener('mousemove', (e) => { const p = toCanvasXY(e.clientX, e.clientY); hoverBox = p.x >= box.x && p.x <= box.x + box.w && p.y >= box.y && p.y <= box.y + box.h; canvas.style.cursor = hoverBox ? 'grab' : 'default'; }); canvas.addEventListener('mousedown', (e) => { const p = toCanvasXY(e.clientX, e.clientY); if(hoverBox){ dragging = true; dragTargetX = box.x + box.w / 2; dragOffsetX = p.x - (box.x + box.w / 2); canvas.style.cursor = 'grabbing'; } }); window.addEventListener('mousemove', (e) => { if(!dragging) return; const p = toCanvasXY(e.clientX, e.clientY); dragTargetX = p.x - dragOffsetX; }); window.addEventListener('mouseup', () => { if(dragging){ dragging = false; canvas.style.cursor = hoverBox ? 'grab' : 'default'; } }); canvas.addEventListener('touchstart', (e) => { e.preventDefault(); const t = e.touches[0]; const p = toCanvasXY(t.clientX, t.clientY); if(p.x >= box.x && p.x <= box.x + box.w && p.y >= box.y && p.y <= box.y + box.h){ dragging = true; dragTargetX = box.x + box.w / 2; dragOffsetX = p.x - (box.x + box.w / 2); } }, { passive: false }); canvas.addEventListener('touchmove', (e) => { e.preventDefault(); if(!dragging) return; const t = e.touches[0]; const p = toCanvasXY(t.clientX, t.clientY); dragTargetX = p.x - dragOffsetX; }, { passive: false }); window.addEventListener('touchend', () => { dragging = false; }); const pushBtn = document.getElementById('pushBtn'); pushBtn.addEventListener('mousedown', () => { holding = true; }); pushBtn.addEventListener('mouseup', () => { holding = false; }); pushBtn.addEventListener('mouseleave', () => { holding = false; }); pushBtn.addEventListener('touchstart', (e) => { e.preventDefault(); holding = true; }); pushBtn.addEventListener('touchend', () => { holding = false; }); const pushLeftBtn = document.getElementById('pushLeftBtn'); pushLeftBtn.addEventListener('mousedown', () => { holdingLeft = true; }); pushLeftBtn.addEventListener('mouseup', () => { holdingLeft = false; }); pushLeftBtn.addEventListener('mouseleave', () => { holdingLeft = false; }); pushLeftBtn.addEventListener('touchstart', (e) => { e.preventDefault(); holdingLeft = true; }); pushLeftBtn.addEventListener('touchend', () => { holdingLeft = false; }); document.getElementById('resetBtn').addEventListener('click', () => { box.x = 160; vel = 0; moving = false; appF = 0; forceTarget = 0; squash = 0; }); document.getElementById('normalSlider').addEventListener('input', (e) => { nF = parseInt(e.target.value); }); document.getElementById('muSlider').addEventListener('input', (e) => { mu = parseFloat(e.target.value); }); function animate(){ updatePhysics(); drawScene(); requestAnimationFrame(animate); } updateUI(); animate(); })(); // ============ 滚动显示动画 ============ (function(){ const els = document.querySelectorAll('.animate-in'); const io = new IntersectionObserver((entries) => { entries.forEach(e => { if(e.isIntersecting){ e.target.classList.add('visible'); io.unobserve(e.target); } }); }, { threshold: 0.1 }); els.forEach(el => io.observe(el)); })();