// rack‑pinion‑anim.js.txt FIX‑1.2 深度校验|资源释放修复 (function () { let animActType = "DA"; let animValveType = "butterfly"; let currentAngle = 0; let canvasDom = null; let animRequestId = null; const COLOR_BODY = "#000000"; const COLOR_AUX = "#666666"; const BG_CANVAS = "#f8f8f8"; const LINE_BOLD = 2.5; const LINE_THIN = 1.2; // =========对外统一AnimApi,对齐main‑sizing.js调用接口========= window.AnimApi = { setCanvasDom(dom) { canvasDom = dom; renderAnimCanvas(); }, setActType(actType) { animActType = actType; currentAngle = 0; renderAnimCanvas(); }, setValveType(vType) { animValveType = vType; renderAnimCanvas(); }, setAngle(deg) { const safeDeg = Math.max(0, Math.min(90, Number(deg)||0)); currentAngle = safeDeg; renderAnimCanvas(); }, getAngle() { return currentAngle; }, destroy() { if (animRequestId) { cancelAnimationFrame(animRequestId); animRequestId = null; } canvasDom = null; animActType = "DA"; animValveType = "butterfly"; currentAngle = 0; } }; function drawValveGraphic(ctx, cx, cy, deg, vType) { const rad = deg * Math.PI / 180; ctx.save(); ctx.translate(cx, cy); ctx.rotate(rad); if (vType === "butterfly") { ctx.beginPath(); ctx.ellipse(0, 0, 38, 7, 0, 0, Math.PI * 2); ctx.strokeStyle = COLOR_BODY; ctx.lineWidth = LINE_BOLD; ctx.stroke(); ctx.beginPath(); ctx.moveTo(0, -38); ctx.lineTo(0, 38); ctx.strokeStyle = COLOR_AUX; ctx.lineWidth = LINE_THIN; ctx.stroke(); } else if (vType === "ball") { ctx.beginPath(); ctx.arc(0, 0, 34, 0, Math.PI * 2); ctx.strokeStyle = COLOR_BODY; ctx.lineWidth = LINE_BOLD; ctx.stroke(); ctx.beginPath(); ctx.rect(-9, -34, 18, 68); ctx.strokeStyle = COLOR_AUX; ctx.lineWidth = LINE_THIN; ctx.stroke(); } else if (vType === "plug") { ctx.beginPath(); ctx.rect(-30, -30, 60, 60); ctx.strokeStyle = COLOR_BODY; ctx.lineWidth = LINE_BOLD; ctx.stroke(); ctx.beginPath(); ctx.rect(-7, -30, 14, 60); ctx.strokeStyle = COLOR_AUX; ctx.lineWidth = LINE_THIN; ctx.stroke(); } ctx.restore(); } function drawRackPinion(ctx, degAngle, w, h) { const rad = degAngle * Math.PI / 180; ctx.save(); const gearCx = w * 0.55; const gearCy = h * 0.5; ctx.translate(gearCx, gearCy); ctx.rotate(rad); ctx.beginPath(); ctx.arc(0, 0, 42, 0, Math.PI * 2); ctx.strokeStyle = COLOR_BODY; ctx.lineWidth = LINE_BOLD; ctx.stroke(); for (let i = 0; i < 12; i++) { const tr = i * (Math.PI / 6); ctx.beginPath(); ctx.moveTo(Math.cos(tr) * 42, Math.sin(tr) * 42); ctx.lineTo(Math.cos(tr) * 48, Math.sin(tr) * 48); ctx.strokeStyle = COLOR_AUX; ctx.lineWidth = LINE_THIN; ctx.stroke(); } ctx.restore(); const rackTravelRatio = degAngle / 90; const rackOffsetX = -48 + rackTravelRatio * 96; ctx.save(); ctx.translate(gearCx + rackOffsetX, gearCy); ctx.beginPath(); ctx.rect(-70, -22, 70, 44); ctx.strokeStyle = COLOR_BODY; ctx.lineWidth = LINE_BOLD; ctx.stroke(); ctx.restore(); ctx.save(); ctx.beginPath(); ctx.rect(w * 0.12, h * 0.22, w * 0.34, h * 0.56); ctx.strokeStyle = COLOR_BODY; ctx.lineWidth = LINE_BOLD; ctx.stroke(); ctx.restore(); // SR弹簧跟随转角动态伸缩 if (animActType === "SR") { ctx.save(); let sx1 = w * 0.14; let sy1 = h * 0.26; let sy2Base = h * 0.74; const springCompressRatio = degAngle / 90; const sy2 = sy1 + (sy2Base - sy1) * (1 - springCompressRatio); const springCount = 10; ctx.beginPath(); for (let i = 0; i <= springCount; i++) { const t = i / springCount; const y = sy1 + t * (sy2 - sy1); const x = (i % 2 === 0) ? sx1 : sx1 + 22; if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); } ctx.strokeStyle = COLOR_AUX; ctx.lineWidth = LINE_THIN; ctx.stroke(); ctx.restore(); } } function renderAnimCanvas() { if (!canvasDom) return; const ctx = canvasDom.getContext('2d'); if(!ctx) return; const w = canvasDom.width; const h = canvasDom.height; ctx.fillStyle = BG_CANVAS; ctx.fillRect(0, 0, w, h); drawRackPinion(ctx, currentAngle, w, h); drawValveGraphic(ctx, w * 0.74, h * 0.5, currentAngle, animValveType); ctx.font = "13px Consolas, monospace"; ctx.fillStyle = "#222222"; ctx.fillText(`Angle: ${currentAngle}°`, 12, h - 8); } // 删除旧版不规范暴露的全局函数,避免冲突 delete window.setAnimCanvasDom; delete window.setActAnimConfig; delete window.setAnimValveType; delete window.setAnimAngle; })();