From fe076a74fc41399d1b67f7195631f3b8f5e81679 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sat, 1 Aug 2026 20:28:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=86=E7=A6=BBjs=20css=20=E5=92=8Ch?= =?UTF-8?q?tml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- css/styles.css | 225 ++++++++++++ forces.html | 970 +------------------------------------------------ js/forces.js | 737 +++++++++++++++++++++++++++++++++++++ 3 files changed, 966 insertions(+), 966 deletions(-) create mode 100644 css/styles.css create mode 100644 js/forces.js diff --git a/css/styles.css b/css/styles.css new file mode 100644 index 0000000..417e78f --- /dev/null +++ b/css/styles.css @@ -0,0 +1,225 @@ +/* 力的课堂 — 样式表 */ +*{margin:0;padding:0;box-sizing:border-box} +:root{ + --primary:#4f8cff; + --primary-dark:#2a5bd7; + --accent:#ff6b6b; + --gravity:#ff6b6b; + --elastic:#51cf66; + --friction:#ffd43b; + --bg:#f4f6fb; + --card:#ffffff; + --text:#1a202c; + --text-light:#5a6478; + --shadow:0 8px 32px rgba(30,50,90,.08); + --radius:16px; +} +html{scroll-behavior:smooth} +body{ + font-family:-apple-system,BlinkMacSystemFont,"PingFang SC","Microsoft YaHei","Helvetica Neue",Arial,sans-serif; + background:var(--bg); + color:var(--text); + line-height:1.7; + overflow-x:hidden; +} +nav{ + position:fixed;top:0;left:0;right:0;z-index:100; + background:rgba(255,255,255,.85); + backdrop-filter:blur(20px); + -webkit-backdrop-filter:blur(20px); + border-bottom:1px solid rgba(0,0,0,.05); + padding:14px 0; +} +.nav-inner{ + max-width:1200px;margin:0 auto;padding:0 24px; + display:flex;align-items:center;justify-content:space-between; +} +.logo{ + font-size:1.25rem;font-weight:700; + background:linear-gradient(135deg,var(--primary),var(--accent)); + -webkit-background-clip:text;-webkit-text-fill-color:transparent;background-clip:text; +} +.nav-links{display:flex;gap:28px;list-style:none;flex-wrap:wrap} +.nav-links a{ + text-decoration:none;color:var(--text-light);font-size:.92rem;font-weight:500; + transition:color .2s; +} +.nav-links a:hover{color:var(--primary)} +.hero{ + min-height:100vh; + background:linear-gradient(135deg,#667eea 0%,#764ba2 50%,#f093fb 100%); + display:flex;align-items:center;justify-content:center; + text-align:center;padding:120px 24px 80px; + position:relative;overflow:hidden; +} +.hero::before{ + content:"";position:absolute;inset:0; + background: + radial-gradient(circle at 20% 30%,rgba(255,255,255,.15),transparent 40%), + radial-gradient(circle at 80% 70%,rgba(255,255,255,.1),transparent 40%); +} +.hero-content{position:relative;z-index:1;max-width:800px;color:#fff} +.hero h1{ + font-size:clamp(2.5rem,6vw,4.5rem); + font-weight:800;margin-bottom:20px; + text-shadow:0 4px 24px rgba(0,0,0,.15); + letter-spacing:.02em; +} +.hero p{ + font-size:clamp(1.05rem,2vw,1.35rem); + opacity:.95;margin-bottom:36px;max-width:600px;margin-left:auto;margin-right:auto; +} +.hero-btn{ + display:inline-block;padding:14px 40px;border-radius:50px; + background:#fff;color:var(--primary-dark); + text-decoration:none;font-weight:700;font-size:1.05rem; + box-shadow:0 8px 24px rgba(0,0,0,.15); + transition:transform .2s,box-shadow .2s; +} +.hero-btn:hover{transform:translateY(-3px);box-shadow:0 12px 32px rgba(0,0,0,.2)} +section{padding:80px 24px;max-width:1200px;margin:0 auto} +.section-title{ + font-size:clamp(1.75rem,3vw,2.5rem); + font-weight:800;text-align:center;margin-bottom:12px; +} +.section-sub{ + text-align:center;color:var(--text-light);margin-bottom:56px; + font-size:1.05rem; +} +.card{ + background:var(--card);border-radius:var(--radius); + padding:36px;box-shadow:var(--shadow); + margin-bottom:28px; + transition:transform .25s,box-shadow .25s; +} +.card:hover{transform:translateY(-4px);box-shadow:0 12px 40px rgba(30,50,90,.12)} +.grid-2{display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:24px} +.grid-3{display:grid;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));gap:24px} +.concept-item{ + padding:28px;border-radius:14px;background:#f8faff; + border-left:4px solid var(--primary); +} +.concept-item h3{color:var(--primary-dark);margin-bottom:8px;font-size:1.2rem} +.concept-item p{color:var(--text-light)} +.vector-diagram{ + display:flex;justify-content:center;align-items:center; + margin:20px 0; +} +.vector-diagram svg{max-width:100%;height:auto} +.tag{ + display:inline-block;padding:4px 12px;border-radius:20px; + font-size:.8rem;font-weight:600;margin-right:6px; +} +.tag-gravity{background:#ffe8e8;color:#c92a2a} +.tag-elastic{background:#d3f9d8;color:#2b8a3e} +.tag-friction{background:#fff3bf;color:#b8860b} +.tag-primary{background:#d0ebff;color:#1971c2} +.formula{ + display:inline-block;padding:4px 14px;border-radius:8px; + background:linear-gradient(135deg,#e7f5ff,#f3f0ff); + font-family:"Cambria Math",Times New Roman,serif; + font-weight:700;font-size:1.15rem;color:var(--primary-dark); + margin:6px 4px; +} +.experiment-card{ + background:var(--card);border-radius:var(--radius); + padding:28px;box-shadow:var(--shadow); + border-top:4px solid var(--primary); +} +.experiment-card.gravity{border-top-color:var(--gravity)} +.experiment-card.elastic{border-top-color:var(--elastic)} +.experiment-card.friction{border-top-color:var(--friction)} +.exp-header{display:flex;align-items:center;gap:10px;margin-bottom:16px;flex-wrap:wrap} +.exp-header h3{font-size:1.25rem} +.exp-badge{ + background:linear-gradient(135deg,var(--primary),var(--accent)); + color:#fff;padding:3px 10px;border-radius:20px;font-size:.75rem;font-weight:600; +} +.exp-desc{color:var(--text-light);margin-bottom:20px;font-size:.95rem} +.canvas-wrap{ + position:relative;background:#f8faff;border-radius:12px; + padding:12px;border:1px solid #eef2f7; + margin-bottom:16px; +} +canvas{display:block;width:100%;height:auto;border-radius:8px} +.controls{ + display:flex;flex-wrap:wrap;gap:12px;align-items:center; + padding:14px;background:#f8faff;border-radius:10px;margin-bottom:14px; +} +.controls label{font-size:.88rem;color:var(--text-light);display:flex;align-items:center;gap:8px} +.controls input[type=range]{ + width:140px;accent-color:var(--primary); +} +.controls .val{ + min-width:48px;text-align:right;font-weight:700;color:var(--primary-dark); + font-variant-numeric:tabular-nums; +} +.btn{ + padding:8px 20px;border:none;border-radius:8px; + background:var(--primary);color:#fff;font-weight:600;font-size:.9rem; + cursor:pointer;transition:all .2s; +} +.btn:hover{background:var(--primary-dark)} +.btn.secondary{background:#e9ecef;color:var(--text)} +.btn.secondary:hover{background:#dee2e6} +.data-display{ + display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:10px; + padding:14px;background:linear-gradient(135deg,#f8faff,#f0f4ff); + border-radius:10px;font-size:.9rem; +} +.data-item{display:flex;justify-content:space-between;align-items:center} +.data-item span:first-child{color:var(--text-light)} +.data-item span:last-child{font-weight:700;color:var(--primary-dark);font-variant-numeric:tabular-nums} +.data-item .g{color:#c92a2a} +.data-item .e{color:#2b8a3e} +.data-item .f{color:#b8860b} +.comparison-table{ + width:100%;border-collapse:separate;border-spacing:0; + background:var(--card);border-radius:var(--radius); + overflow:hidden;box-shadow:var(--shadow); + margin-bottom:24px; +} +.comparison-table th,.comparison-table td{ + padding:16px 18px;text-align:left;font-size:.95rem; + border-bottom:1px solid #eef2f7; +} +.comparison-table th{ + background:linear-gradient(135deg,#e7f5ff,#f3f0ff); + font-weight:700;color:var(--primary-dark);font-size:1rem; +} +.comparison-table tr:last-child td{border-bottom:none} +.comparison-table td:first-child{font-weight:600;color:var(--text)} +.comparison-table .gravity-row td:first-child{color:#c92a2a} +.comparison-table .elastic-row td:first-child{color:#2b8a3e} +.comparison-table .friction-row td:first-child{color:#b8860b} +.life-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:24px} +.life-card{ + padding:28px;border-radius:14px;color:#fff;position:relative;overflow:hidden; + min-height:180px;display:flex;flex-direction:column;justify-content:flex-end; +} +.life-card.gravity{background:linear-gradient(135deg,#ff6b6b,#ee5a5a)} +.life-card.elastic{background:linear-gradient(135deg,#51cf66,#40c057)} +.life-card.friction{background:linear-gradient(135deg,#ffd43b,#fab005)} +.life-card h4{font-size:1.2rem;margin-bottom:8px} +.life-card p{font-size:.9rem;opacity:.95} +.life-card .icon{position:absolute;top:20px;right:20px;font-size:3rem;opacity:.3} +.life-examples{display:flex;flex-wrap:wrap;gap:10px;margin-top:12px} +.life-examples span{ + background:rgba(255,255,255,.2);padding:4px 12px;border-radius:20px;font-size:.82rem; +} +footer{ + text-align:center;padding:40px 24px;color:var(--text-light); + font-size:.9rem;border-top:1px solid #eef2f7;margin-top:40px; +} +@media(max-width:768px){ + .nav-links{gap:16px;font-size:.85rem} + .nav-links a{font-size:.82rem} + section{padding:56px 18px} + .card{padding:24px} + .experiment-card{padding:20px} + .controls{flex-direction:column;align-items:stretch} + .controls input[type=range]{width:100%} + .comparison-table th,.comparison-table td{padding:10px 12px;font-size:.82rem} +} +.animate-in{opacity:0;transform:translateY(30px);transition:opacity .6s,transform .6s} +.animate-in.visible{opacity:1;transform:translateY(0)} diff --git a/forces.html b/forces.html index 770e10f..60482d2 100644 --- a/forces.html +++ b/forces.html @@ -4,232 +4,7 @@ 认识力 — 经典力学互动课堂 - + @@ -258,7 +33,7 @@ footer{

📐 力的概念

-l

力是物体对物体的作用,力不能脱离物体而独立存在

+

力是物体对物体的作用,力不能脱离物体而独立存在

力的三要素

力的作用效果与力的大小、方向和作用点有关,这三者称为力的三要素。

@@ -510,743 +285,6 @@ l

力是物体对物体的作用,力不能脱离物体

纯 HTML5 Canvas 实现 · 无第三方依赖 · 响应式设计

- + - \ No newline at end of file + diff --git a/js/forces.js b/js/forces.js new file mode 100644 index 0000000..6afa184 --- /dev/null +++ b/js/forces.js @@ -0,0 +1,737 @@ +/* 力的课堂 — 交互脚本 */ +// ============ 自由落体实验 ============ +(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)); +})();