在线工具:元器件:阻容感:阻抗串并联计算器
差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 后一修订版 | 前一修订版 | ||
| 在线工具:元器件:阻容感:阻抗串并联计算器 [2026/11/21 08:14] – 创建 hwwiki | 在线工具:元器件:阻容感:阻抗串并联计算器 [2026/11/24 07:17] (当前版本) – hwwiki | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | // DokuWiki 内联版 —— 需在「管理 → 配置设置 → htmlok」中允许 HTML | ||
| - | // 整段粘贴到页面;样式限定在 #zc-tool 内,不影响 wiki 主题 | ||
| < | < | ||
| < | < | ||
| - | /* ---------- DokuWiki 内联容器 ---------- */ | + | /* 先兜底:挡住 wiki 主题对裸标签的颜色/ |
| + | | ||
| + | #zc-tool, #zc-tool *{color: | ||
| #zc-tool{ | #zc-tool{ | ||
| - | --bg:# | + | |
| - | --text:# | + | |
| - | --r:# | + | |
| - | --mono:' | + | |
| - | background:# | + | |
| - | font-family: | + | |
| - | font-size: | + | |
| container-type: | container-type: | ||
| } | } | ||
| #zc-tool *{box-sizing: | #zc-tool *{box-sizing: | ||
| - | + | #zc-tool{ | |
| - | + | | |
| + | | ||
| + | --border:# | ||
| + | --border-strong:# | ||
| + | --text:# | ||
| + | --muted:# | ||
| + | --accent:# | ||
| + | --accent-bg:# | ||
| + | --r:# | ||
| + | --l:# | ||
| + | --c:# | ||
| + | --z:# | ||
| + | --y:# | ||
| + | --danger:# | ||
| + | --mono:' | ||
| + | } | ||
| #zc-tool *{box-sizing: | #zc-tool *{box-sizing: | ||
| - | + | #zc-tool, # | |
| + | # | ||
| #zc-tool h1, #zc-tool h2, #zc-tool h3, #zc-tool p{margin:0} | #zc-tool h1, #zc-tool h2, #zc-tool h3, #zc-tool p{margin:0} | ||
| - | |||
| - | /* ---------- 顶栏 ---------- */ | ||
| #zc-tool .topbar{ | #zc-tool .topbar{ | ||
| - | position: | + | position: |
| background:# | background:# | ||
| padding: | padding: | ||
| 行 31: | 行 37: | ||
| } | } | ||
| #zc-tool .brand{display: | #zc-tool .brand{display: | ||
| + | #zc-tool .site-link{ | ||
| + | display: | ||
| + | font-size: | ||
| + | padding:5px 10px; | ||
| + | transition: | ||
| + | } | ||
| + | #zc-tool .site-link: | ||
| + | #zc-tool .site-link svg{opacity: | ||
| + | #zc-tool .site-link: | ||
| #zc-tool .mark{ | #zc-tool .mark{ | ||
| width: | width: | ||
| 行 38: | 行 53: | ||
| box-shadow: | box-shadow: | ||
| } | } | ||
| - | #zc-tool .brand h1{font-size: | + | #zc-tool .brand h1{font-size: |
| #zc-tool .brand p{font-size: | #zc-tool .brand p{font-size: | ||
| - | |||
| - | /* ---------- 工作频率条 ---------- */ | ||
| #zc-tool .freq-bar{ | #zc-tool .freq-bar{ | ||
| display: | display: | ||
| 行 59: | 行 72: | ||
| #zc-tool .fq: | #zc-tool .fq: | ||
| #zc-tool .fq.on{background: | #zc-tool .fq.on{background: | ||
| - | |||
| - | /* ---------- 输入控件 ---------- */ | ||
| #zc-tool input.num, #zc-tool select{ | #zc-tool input.num, #zc-tool select{ | ||
| font-family: | font-family: | ||
| 行 71: | 行 82: | ||
| box-shadow: | box-shadow: | ||
| } | } | ||
| - | input[type=number]:: | + | # |
| - | #zc-tool input[type=number]:: | + | |
| #zc-tool input[type=number]{-moz-appearance: | #zc-tool input[type=number]{-moz-appearance: | ||
| #zc-tool select{cursor: | #zc-tool select{cursor: | ||
| - | |||
| - | /* ---------- 布局 ---------- */ | ||
| #zc-tool main{ | #zc-tool main{ | ||
| max-width: | max-width: | ||
| display: | display: | ||
| } | } | ||
| - | @container (max-width: 1000px){ | + | @container (max-width: |
| - | | + | #zc-tool .panel.results{position: |
| - | #zc-tool .panel.results{position: | + | |
| - | } | + | |
| #zc-tool .panel{ | #zc-tool .panel{ | ||
| background: | background: | ||
| 行 95: | 行 101: | ||
| #zc-tool .panel-head h2{font-size: | #zc-tool .panel-head h2{font-size: | ||
| #zc-tool .head-hint{font-size: | #zc-tool .head-hint{font-size: | ||
| + | #zc-tool .head-actions{display: | ||
| + | #zc-tool .head-actions .icon-btn{width: | ||
| + | #zc-tool .head-actions .icon-btn: | ||
| + | #zc-tool .mini-btn{ | ||
| + | border:1px solid var(--border-strong); | ||
| + | padding:4px 11px; | ||
| + | font-family: | ||
| + | } | ||
| + | #zc-tool .mini-btn: | ||
| #zc-tool .presets{display: | #zc-tool .presets{display: | ||
| #zc-tool .chip{ | #zc-tool .chip{ | ||
| 行 102: | 行 117: | ||
| } | } | ||
| #zc-tool .chip: | #zc-tool .chip: | ||
| - | |||
| - | /* ---------- 树 ---------- */ | ||
| #zc-tool .schematic-label{ | #zc-tool .schematic-label{ | ||
| font-size: | font-size: | ||
| 行 115: | 行 128: | ||
| } | } | ||
| #zc-tool .schematic svg{display: | #zc-tool .schematic svg{display: | ||
| + | #zc-tool .schematic svg g[data-el]{cursor: | ||
| + | #zc-tool .schematic.dim svg g[data-el]: | ||
| + | #zc-tool .schematic svg g.hl line, #zc-tool .schematic svg g.hl path, #zc-tool .schematic svg g.hl rect{stroke-width: | ||
| + | #zc-tool .el-row.hl{background:# | ||
| + | #zc-tool .schematic-label .sub{font-weight: | ||
| #zc-tool .tree{display: | #zc-tool .tree{display: | ||
| #zc-tool .node{ | #zc-tool .node{ | ||
| 行 138: | 行 156: | ||
| #zc-tool .seg button.on{background: | #zc-tool .seg button.on{background: | ||
| #zc-tool .seg button: | #zc-tool .seg button: | ||
| + | #zc-tool .seg.mini button{padding: | ||
| + | #zc-tool .ro-mode{margin-left: | ||
| #zc-tool .btn{ | #zc-tool .btn{ | ||
| border:1px solid var(--border-strong); | border:1px solid var(--border-strong); | ||
| 行 160: | 行 180: | ||
| border:1px dashed # | border:1px dashed # | ||
| } | } | ||
| - | |||
| - | /* ---------- 元件行 ---------- */ | ||
| #zc-tool .el-row{ | #zc-tool .el-row{ | ||
| position: | position: | ||
| 行 175: | 行 193: | ||
| #zc-tool .el-row[data-tone=" | #zc-tool .el-row[data-tone=" | ||
| #zc-tool .el-row[data-tone=" | #zc-tool .el-row[data-tone=" | ||
| + | #zc-tool .el-row[data-tone=" | ||
| #zc-tool .el-row .type{width: | #zc-tool .el-row .type{width: | ||
| - | #zc-tool .el-row .num{flex:1 1 120px;min-width:92px;max-width:240px} | + | #zc-tool .el-row .num{flex:1 1 96px;min-width:72px;max-width:214px} |
| - | #zc-tool .el-row[data-tone=" | + | #zc-tool .el-row[data-tone=" |
| + | #zc-tool .el-row[data-tone=" | ||
| #zc-tool .el-row .unit{width: | #zc-tool .el-row .unit{width: | ||
| #zc-tool .unit-fixed{font-family: | #zc-tool .unit-fixed{font-family: | ||
| 行 185: | 行 205: | ||
| white-space: | white-space: | ||
| } | } | ||
| + | #zc-tool .row-acts{display: | ||
| + | #zc-tool .el-row: | ||
| + | #zc-tool .row-acts .icon-btn{ | ||
| + | width: | ||
| + | display: | ||
| + | } | ||
| + | #zc-tool .row-acts .icon-btn svg{display: | ||
| + | #zc-tool .row-acts .icon-btn: | ||
| + | #zc-tool .row-acts .icon-btn[data-act=" | ||
| #zc-tool .hint{font-size: | #zc-tool .hint{font-size: | ||
| #zc-tool .hint > div + div{margin-top: | #zc-tool .hint > div + div{margin-top: | ||
| 行 196: | 行 225: | ||
| } | } | ||
| #zc-tool .formula b{color:# | #zc-tool .formula b{color:# | ||
| - | + | #zc-tool .results{position: | |
| - | /* ---------- 结果面板 ---------- */ | + | |
| - | #zc-tool .results{position: | + | |
| #zc-tool .results:: | #zc-tool .results:: | ||
| #zc-tool .results:: | #zc-tool .results:: | ||
| 行 223: | 行 250: | ||
| #zc-tool .eq-sec.p{border-left-color:# | #zc-tool .eq-sec.p{border-left-color:# | ||
| #zc-tool .eq-sec-tag{font-size: | #zc-tool .eq-sec-tag{font-size: | ||
| - | # | + | # |
| - | #sweep .lg{opacity: | + | # |
| - | #sweep .lg.off{opacity: | + | # |
| - | #sweep .lg: | + | # |
| - | #sweep .lg[data-cv=" | + | # |
| - | #sweep .lg.na, #sweep .lg.na: | + | # |
| + | #zc-tool .range-edit{display: | ||
| + | #zc-tool .range-edit input, #zc-tool .range-edit select{ | ||
| + | font-family: | ||
| + | border:1px solid var(--border-strong); | ||
| + | } | ||
| + | #zc-tool .range-edit input{width: | ||
| + | #zc-tool .range-edit select{width: | ||
| + | #zc-tool .range-edit input: | ||
| + | #zc-tool .range-edit .sep{color:# | ||
| + | #zc-tool .chart-wrap{position: | ||
| + | #zc-tool .chart-tip{ | ||
| + | position: | ||
| + | background: | ||
| + | padding:5px 8px; | ||
| + | white-space: | ||
| + | box-shadow: | ||
| + | } | ||
| + | #zc-tool .chart-tip b{color:# | ||
| + | #zc-tool .chart-tip[hidden]{display: | ||
| #zc-tool .chart-hint{font-size: | #zc-tool .chart-hint{font-size: | ||
| - | |||
| - | /* ---------- 右键菜单 ---------- */ | ||
| #zc-tool .ctxmenu{ | #zc-tool .ctxmenu{ | ||
| position: | position: | ||
| 行 251: | 行 295: | ||
| } | } | ||
| #zc-tool .toast.show{opacity: | #zc-tool .toast.show{opacity: | ||
| + | #zc-tool .lg{opacity: | ||
| + | #zc-tool .lg.off{opacity: | ||
| + | # | ||
| </ | </ | ||
| <div id=" | <div id=" | ||
| - | |||
| - | |||
| <header class=" | <header class=" | ||
| <div class=" | <div class=" | ||
| 行 260: | 行 305: | ||
| <div> | <div> | ||
| < | < | ||
| - | < | + | < |
| </ | </ | ||
| </ | </ | ||
| + | <a class=" | ||
| + | | ||
| + | | ||
| + | | ||
| </ | </ | ||
| 行 269: | 行 318: | ||
| <div class=" | <div class=" | ||
| < | < | ||
| - | <span class=" | + | <div class=" |
| + | <button class=" | ||
| + | <button class=" | ||
| + | <button class=" | ||
| + | | ||
| </ | </ | ||
| 行 304: | 行 357: | ||
| </ | </ | ||
| - | <div class=" | + | <div class=" |
| <div class=" | <div class=" | ||
| 行 344: | 行 397: | ||
| <div class=" | <div class=" | ||
| - | <div class=" | + | <div class=" |
| - | <svg id=" | + | <span class=" |
| - | <div class=" | + | <input id=" |
| + | <select id=" | ||
| + | <option value=" | ||
| + | <option value=" | ||
| + | </ | ||
| + | <span class=" | ||
| + | <input id=" | ||
| + | <select id=" | ||
| + | <option value=" | ||
| + | <option value=" | ||
| + | </ | ||
| + | </ | ||
| + | | ||
| + | | ||
| + | | ||
| + | <div class=" | ||
| + | </div> | ||
| + | <div class=" | ||
| </ | </ | ||
| </ | </ | ||
| 行 356: | 行 426: | ||
| </ | </ | ||
| - | <div class=" | + | <div class=" |
| + | </ | ||
| < | < | ||
| + | (function(){ | ||
| + | /* 脚本作用域:document.querySelector(All) 只在容器内查找,避免误选 wiki 页面元素 */ | ||
| + | var __root = window.document.getElementById(' | ||
| + | if(!__root) return; | ||
| + | var __doc = new Proxy(window.document, | ||
| + | get: function(t, k){ | ||
| + | if(k === ' | ||
| + | if(k === ' | ||
| + | if(k === ' | ||
| + | return function(s){ | ||
| + | if(s === ' | ||
| + | var e = null; | ||
| + | try{ | ||
| + | e = __root.querySelector('#' | ||
| + | }catch(err){} | ||
| + | return e || t.getElementById(s); | ||
| + | }; | ||
| + | if(k === ' | ||
| + | if(k === ' | ||
| + | var v = t[k]; | ||
| + | return (typeof v === ' | ||
| + | }, | ||
| + | set: function(t, k, v){ t[k] = v; return true; }, | ||
| + | has: function(t, k){ return k in t; } | ||
| + | }); | ||
| + | var document = __doc; | ||
| + | |||
| (function(){ | (function(){ | ||
| 'use strict'; | 'use strict'; | ||
| 行 367: | 行 465: | ||
| ' | ' | ||
| ' | ' | ||
| - | ' | + | ' |
| + | ' | ||
| }; | }; | ||
| 行 414: | 行 513: | ||
| const a = Math.abs(v); | const a = Math.abs(v); | ||
| if(a === 0) return '0 S'; | if(a === 0) return '0 S'; | ||
| - | | + | |
| - | if(a >= 1e-3) return trim(v*1e3) + ' mS'; | + | const p = Math.pow(10, |
| - | if(a >= 1e-6) return trim(v*1e6) + ' µS'; | + | const snap = (Math.abs(a / p - 1) < 1e-4) ? (v < 0 ? -p : p) : v; |
| - | if(a >= 1e-9) return trim(v*1e9) + ' nS'; | + | const sa = Math.abs(snap); |
| - | return | + | if(sa >= 1) return trim(snap) + ' S'; |
| + | if(sa >= 1e-3) return trim(snap*1e3) + ' mS'; | ||
| + | if(sa >= 1e-6) return trim(snap*1e6) + ' µS'; | ||
| + | if(sa >= 1e-9) return trim(snap*1e9) + ' nS'; | ||
| + | return | ||
| + | } | ||
| + | |||
| + | /* 导纳读数:与 fmtRect 同构,单位换成 S */ | ||
| + | function fmtRectY(y){ | ||
| + | if(isInf(y)) return '∞ S(短路)'; | ||
| + | const re = y.re, im = y.im; | ||
| + | const scale = Math.max(Math.abs(re), | ||
| + | const eps = 1e-12 * (scale || 1); | ||
| + | if(Math.abs(im) < eps) return fmtSiemens(re); | ||
| + | if(Math.abs(re) < eps) return (im < 0 ? ' | ||
| + | return fmtSiemens(re) + (im < 0 ? ' − j' : ' + j') + fmtSiemens(Math.abs(im)); | ||
| + | } | ||
| + | |||
| + | /* 阻抗取倒数。开路 → 0(无电流),短路 → ∞ */ | ||
| + | function recipZ(z){ | ||
| + | if(isInf(z)) return {re: 0, im: 0}; | ||
| + | const d = z.re * z.re + z.im * z.im; | ||
| + | if(d === 0) return {re: Infinity, im: 0}; | ||
| + | return {re: z.re / d, im: -z.im / d}; | ||
| + | } | ||
| + | |||
| + | function elY(e, f){ | ||
| + | return recipZ(elZ(e, | ||
| + | } | ||
| + | |||
| + | function fmtQ(v){ | ||
| + | if(!isFinite(v)) return ' | ||
| + | if(v >= 1e4) return '> 10⁴'; | ||
| + | return trim(v, 3); | ||
| } | } | ||
| 行 463: | 行 595: | ||
| case ' | case ' | ||
| return {re: n0(e.re), im: n0(e.im)}; | return {re: n0(e.re), im: n0(e.im)}; | ||
| + | } | ||
| + | case ' | ||
| + | const g = n0(e.re) * (U[e.unit] || 1); // 电导 G | ||
| + | const b = n0(e.im) * (U[e.unit] || 1); // 电纳 B | ||
| + | const d = g * g + b * b; | ||
| + | if(d === 0) return {re: Infinity, im: 0}; // Y = 0 ⇒ 开路 | ||
| + | return {re: g / d, im: -b / d}; // Z = 1/Y | ||
| } | } | ||
| } | } | ||
| 行 511: | 行 650: | ||
| L: | L: | ||
| C: | C: | ||
| - | Z: | + | Z: |
| + | Y: | ||
| }; | }; | ||
| 行 518: | 行 658: | ||
| {v:' | {v:' | ||
| {v:' | {v:' | ||
| - | {v:' | + | {v:' |
| + | {v:' | ||
| ]; | ]; | ||
| 行 601: | 行 742: | ||
| let controls = ''; | let controls = ''; | ||
| - | if(e.type === ' | + | |
| + | controls = | ||
| + | '< | ||
| + | '< | ||
| + | '< | ||
| + | '< | ||
| + | TYPES.filter(function(x){ return x.v === ' | ||
| + | .map(function(u){ return '< | ||
| + | '</ | ||
| + | } else if(e.type === ' | ||
| controls = | controls = | ||
| '< | '< | ||
| 行 619: | 行 769: | ||
| row.innerHTML = typeSel + controls + | row.innerHTML = typeSel + controls + | ||
| '< | '< | ||
| - | '< | + | |
| + | '< | ||
| + | '< | ||
| + | ' stroke-width=" | ||
| + | '< | ||
| + | '< | ||
| + | '</ | ||
| + | '</ | ||
| + | '< | ||
| + | '< | ||
| + | | ||
| + | '</ | ||
| return row; | return row; | ||
| } | } | ||
| 行 638: | 行 799: | ||
| (depth < 6 ? '< | (depth < 6 ? '< | ||
| (depth > 0 ? '< | (depth > 0 ? '< | ||
| - | (depth > 0 ? '< | + | (depth > 0 ? '< |
| + | (depth === 0 | ||
| + | ? '< | ||
| + | ' title=" | ||
| + | '< | ||
| + | '< | ||
| + | '</ | ||
| + | | ||
| box.appendChild(head); | box.appendChild(head); | ||
| 行 664: | 行 832: | ||
| treeEl.appendChild(renderNode(state.root, | treeEl.appendChild(renderNode(state.root, | ||
| recalc(); | recalc(); | ||
| + | if(skipHist) skipHist = false; | ||
| + | else pushHistory(); | ||
| } | } | ||
| 行 692: | 行 862: | ||
| /* 快捷频率按钮激活态 */ | /* 快捷频率按钮激活态 */ | ||
| - | document.getElementById(' | + | document.querySelectorAll(' |
| const hz = parseFloat(b.dataset.f) * parseFloat(b.dataset.u); | const hz = parseFloat(b.dataset.f) * parseFloat(b.dataset.u); | ||
| b.classList.toggle(' | b.classList.toggle(' | ||
| }); | }); | ||
| + | const isY = (roMode === ' | ||
| treeEl.querySelectorAll(' | treeEl.querySelectorAll(' | ||
| const e = findEl(+span.dataset.elz); | const e = findEl(+span.dataset.elz); | ||
| - | span.textContent = e ? 'Z = ' + fmtRect(elZ(e, f)) : '—'; | + | |
| + | span.textContent = isY | ||
| + | | ||
| + | | ||
| }); | }); | ||
| treeEl.querySelectorAll(' | treeEl.querySelectorAll(' | ||
| const n = findNode(+span.dataset.eq); | const n = findNode(+span.dataset.eq); | ||
| - | span.textContent = n ? 'Z = ' + fmtRect(calcNode(n, f)) : '—'; | + | |
| + | const z = calcNode(n, f); | ||
| + | span.textContent = isY | ||
| + | | ||
| + | | ||
| }); | }); | ||
| 行 710: | 行 888: | ||
| drawCircuit(); | drawCircuit(); | ||
| lastResult = {z: z, f: f}; | lastResult = {z: z, f: f}; | ||
| + | scheduleSave(); | ||
| } | } | ||
| 行 797: | 行 976: | ||
| s += ' | s += ' | ||
| '< | '< | ||
| + | } | ||
| + | if(Rs > 0 && Math.abs(Xs) >= tol){ | ||
| + | s += '< | ||
| } | } | ||
| s += '</ | s += '</ | ||
| 行 826: | 行 1008: | ||
| s += ' | s += ' | ||
| '< | '< | ||
| + | } | ||
| + | if(G > 0 && !noB){ | ||
| + | s += '< | ||
| } | } | ||
| s += '</ | s += '</ | ||
| 行 839: | 行 1024: | ||
| } | } | ||
| function shortHz(f){ | function shortHz(f){ | ||
| - | if(f >= 1e9) return '1G'; | + | if(f >= 1e9) return |
| - | if(f >= 1e6) return (f/1e6) + ' | + | if(f >= 1e6) return |
| - | if(f >= 1e3) return (f/1e3) + ' | + | if(f >= 1e3) return |
| - | return | + | return |
| } | } | ||
| /* 曲线显隐状态:|Z| 恒显示,Re / Im 默认关闭 */ | /* 曲线显隐状态:|Z| 恒显示,Re / Im 默认关闭 */ | ||
| const cvOn = {z: true, re: false, im: false}; | const cvOn = {z: true, re: false, im: false}; | ||
| + | |||
| + | /* 元件行读数模式:' | ||
| + | let roMode = ' | ||
| + | let sweepGeom = null; | ||
| function drawSweep(fNow){ | function drawSweep(fNow){ | ||
| const svg = $(' | const svg = $(' | ||
| + | const tipEl = $(' | ||
| + | if(tipEl) tipEl.hidden = true; | ||
| const W = 340, H = 170; | const W = 340, H = 170; | ||
| - | const fmin = 1, fmax = 1e9, N = 200; | + | const N = 200; |
| - | const lf0 = 0, lf1 = 9; | + | const rLo = Math.max(1e-3, |
| + | const rHi = Math.max(rLo * 10, (parseFloat($(' | ||
| + | const fmin = rLo, fmax = rHi; | ||
| + | const lf0 = Math.log10(fmin), lf1 = Math.log10(fmax); | ||
| const pts = []; | const pts = []; | ||
| 行 861: | 行 1055: | ||
| pts.push({f: | pts.push({f: | ||
| } else { | } else { | ||
| - | pts.push({f: | + | pts.push({f: |
| } | } | ||
| } | } | ||
| 行 907: | 行 1101: | ||
| s += '< | s += '< | ||
| ' | ' | ||
| - | for(let d=0;d<=9;d++){ | + | |
| + | const span = lf1 - lf0; | ||
| + | const step = span > 6 ? 3 : (span > 3 ? 2 : 1); | ||
| + | | ||
| + | if((d - dStart) % step !== 0) continue; | ||
| const x = X(Math.pow(10, | const x = X(Math.pow(10, | ||
| - | | + | s += '< |
| - | | + | s += '< |
| - | s += '< | + | |
| - | } | + | |
| } | } | ||
| 行 934: | 行 1130: | ||
| const pathRe = curve(function(p){ return p.re; }, C_RE, 1.5, '2 4'); | const pathRe = curve(function(p){ return p.re; }, C_RE, 1.5, '2 4'); | ||
| const mainPath = curve(function(p){ return p.m; }, C_AMP, 2); | const mainPath = curve(function(p){ return p.m; }, C_AMP, 2); | ||
| - | const pathIm = curve(function(p){ return p.im; }, C_IM, 1.7, '5 3'); | + | const pathIm = curve(function(p){ return |
| const hasRe = !!pathRe, hasIm = !!pathIm; | const hasRe = !!pathRe, hasIm = !!pathIm; | ||
| 行 965: | 行 1161: | ||
| }); | }); | ||
| - | if(fNow >= fmin && fNow <= 1e9){ | + | if(fNow >= fmin && fNow <= fmax){ |
| const x = X(fNow); | const x = X(fNow); | ||
| s += '< | s += '< | ||
| 行 978: | 行 1174: | ||
| } | } | ||
| + | sweepGeom = {pad: pad, iw: iw, ih: ih, lf0: lf0, lf1: lf1, lo: lo, hi: hi}; | ||
| svg.innerHTML = s; | svg.innerHTML = s; | ||
| } | } | ||
| 行 987: | 行 1184: | ||
| | | ||
| | | ||
| - | const SYMC = {R:'# | + | const SYMC = {R:'# |
| const WIRE = '# | const WIRE = '# | ||
| 行 995: | 行 1192: | ||
| function elLabel(e){ | function elLabel(e){ | ||
| const f = function(v){ return fmtOhm(v).replace(/ | const f = function(v){ return fmtOhm(v).replace(/ | ||
| - | if(e.type === ' | + | if(e.type === 'Z' || e.type === 'Y'){ |
| - | const re = n0(e.re), im = n0(e.im); | + | const k = e.type === ' |
| - | if(im === 0) return | + | const re = n0(e.re) |
| - | if(re === 0) return (im < 0 ? ' | + | const g = e.type === ' |
| - | return | + | ? function(v){ return fmtSiemens(v).replace(/ |
| + | : f; | ||
| + | if(im === 0) return | ||
| + | if(re === 0) return (im < 0 ? ' | ||
| + | return | ||
| } | } | ||
| const val = n0(e.value) * (U[e.unit] || 1); | const val = n0(e.value) * (U[e.unit] || 1); | ||
| 行 1025: | 行 1226: | ||
| } else { | } else { | ||
| g.rect(x+8, cy-8, 20, 16, '# | g.rect(x+8, cy-8, 20, 16, '# | ||
| - | g.text(x+18, | + | g.text(x+18, |
| } | } | ||
| } | } | ||
| 行 1035: | 行 1236: | ||
| w: W, h: H, | w: W, h: H, | ||
| draw: function(g, x, y){ | draw: function(g, x, y){ | ||
| - | const cy = y + H / 2; | + | |
| - | const col = SYMC[e.type] || '# | + | |
| - | const sx = x + 15; | + | const col = SYMC[e.type] || '# |
| - | g.line(x, cy, sx, cy); | + | const sx = x + 15; |
| - | g.line(sx + 36, cy, x + W, cy); | + | /* 透明热区:点在符号附近的空白处也能命中,触屏尤其需要 */ |
| - | drawSym(g, e.type, sx, cy, 36, col); | + | g.rect(x + 2, y + 2, W - 4, H - 4, ' |
| - | g.text(x + W/2, y + H - 5, elLabel(e), 8, col); | + | |
| + | g.line(sx + 36, cy, x + W, cy); | ||
| + | drawSym(g, e.type, sx, cy, 36, col); | ||
| + | g.text(x + W/2, y + H - 5, elLabel(e), 8, col); | ||
| + | }); | ||
| } | } | ||
| }; | }; | ||
| 行 1166: | 行 1371: | ||
| parts.push('< | parts.push('< | ||
| '" | '" | ||
| + | }, | ||
| + | /* 把一段绘制包进 <g data-el> | ||
| + | group: function(id, | ||
| + | const start = parts.length; | ||
| + | fn(); | ||
| + | const inner = parts.splice(start, | ||
| + | parts.push('< | ||
| } | } | ||
| }; | }; | ||
| 行 1182: | 行 1394: | ||
| | | ||
| treeEl.addEventListener(' | treeEl.addEventListener(' | ||
| + | const roBtn = ev.target.closest(' | ||
| + | if(roBtn){ | ||
| + | if(roBtn.dataset.ro !== roMode){ | ||
| + | setRoMode(roBtn.dataset.ro); | ||
| + | scheduleSave(); | ||
| + | } | ||
| + | return; | ||
| + | } | ||
| const btn = ev.target.closest(' | const btn = ev.target.closest(' | ||
| if(!btn) return; | if(!btn) return; | ||
| 行 1195: | 行 1415: | ||
| renderTree(); | renderTree(); | ||
| } | } | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | if(act === ' | ||
| + | const row = btn.closest(' | ||
| + | if(!row) return; | ||
| + | const id = +row.dataset.id; | ||
| + | const p = findParent(id); | ||
| + | const e = findEl(id); | ||
| + | if(!p || !e) return; | ||
| + | if(act === ' | ||
| + | const copy = mkEl(e.type); | ||
| + | copy.value = e.value; copy.unit = e.unit; copy.re = e.re; copy.im = e.im; | ||
| + | p.children.splice(p.children.indexOf(e) + 1, 0, copy); | ||
| + | } else { | ||
| + | const i = p.children.indexOf(e); | ||
| + | const j = act === ' | ||
| + | if(j < 0 || j >= p.children.length) return; | ||
| + | p.children[i] = p.children[j]; | ||
| + | p.children[j] = e; | ||
| + | } | ||
| + | renderTree(); | ||
| return; | return; | ||
| } | } | ||
| 行 1231: | 行 1473: | ||
| e[inp.dataset.field] = isFinite(v) ? v : 0; | e[inp.dataset.field] = isFinite(v) ? v : 0; | ||
| recalc(); | recalc(); | ||
| + | scheduleHist(); | ||
| }); | }); | ||
| 行 1253: | 行 1496: | ||
| e.re = inherited !== null ? inherited : 50; | e.re = inherited !== null ? inherited : 50; | ||
| e.im = inherited !== null ? 0 : 50; | e.im = inherited !== null ? 0 : 50; | ||
| + | } else if(t === ' | ||
| + | e.re = d.re; | ||
| + | e.im = d.im; | ||
| } | } | ||
| renderTree(); | renderTree(); | ||
| 行 1259: | 行 1505: | ||
| recalc(); | recalc(); | ||
| } | } | ||
| + | }); | ||
| + | |||
| + | /* ---------- 示意图与元件行联动 ---------- */ | ||
| + | function setHl(id){ | ||
| + | const host = $(' | ||
| + | const svg = host ? host.querySelector(' | ||
| + | if(svg){ | ||
| + | svg.querySelectorAll(' | ||
| + | g.classList.toggle(' | ||
| + | }); | ||
| + | host.classList.toggle(' | ||
| + | } | ||
| + | treeEl.querySelectorAll(' | ||
| + | r.classList.toggle(' | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | treeEl.addEventListener(' | ||
| + | const row = ev.target.closest ? ev.target.closest(' | ||
| + | if(row) setHl(+row.dataset.id); | ||
| + | }); | ||
| + | treeEl.addEventListener(' | ||
| + | const row = ev.target.closest ? ev.target.closest(' | ||
| + | if(!row) return; | ||
| + | if(ev.relatedTarget && row.contains(ev.relatedTarget)) return; | ||
| + | setHl(null); | ||
| + | }); | ||
| + | |||
| + | /* 点示意图上的符号 → 定位到对应元件行(触屏也可用) */ | ||
| + | $(' | ||
| + | const g = ev.target.closest ? ev.target.closest(' | ||
| + | if(!g) return; | ||
| + | const row = treeEl.querySelector(' | ||
| + | if(!row) return; | ||
| + | if(row.scrollIntoView) row.scrollIntoView({block:' | ||
| + | row.classList.add(' | ||
| + | setTimeout(function(){ row.classList.remove(' | ||
| }); | }); | ||
| freqVal.addEventListener(' | freqVal.addEventListener(' | ||
| freqUnit.addEventListener(' | freqUnit.addEventListener(' | ||
| + | |||
| + | [' | ||
| + | $(id).addEventListener(' | ||
| + | }); | ||
| + | [' | ||
| + | $(id).addEventListener(' | ||
| + | }); | ||
| /* 快捷频率 */ | /* 快捷频率 */ | ||
| - | document.getElementById(' | + | document.querySelectorAll(' |
| b.addEventListener(' | b.addEventListener(' | ||
| freqVal.value = b.dataset.f; | freqVal.value = b.dataset.f; | ||
| 行 1283: | 行 1573: | ||
| drawSweep(readFreq()); | drawSweep(readFreq()); | ||
| }); | }); | ||
| + | |||
| + | /* ============================================================ | ||
| + | 8.6 状态持久化:URL 分享 + 本地自动保存 | ||
| + | | ||
| + | const LS_KEY = ' | ||
| + | let saveTimer = null; | ||
| + | let persistOn = false; | ||
| + | |||
| + | function packTree(n){ | ||
| + | if(n.kind === ' | ||
| + | return [n.mode, n.children.map(packTree)]; | ||
| + | } | ||
| + | function unpackTree(a){ | ||
| + | if(a[0] === ' | ||
| + | const e = mkEl(a[0]); | ||
| + | e.value = a[1]; e.unit = a[2]; e.re = a[3]; e.im = a[4]; | ||
| + | return e; | ||
| + | } | ||
| + | return mkNode(a[0], | ||
| + | } | ||
| + | function snapshot(){ | ||
| + | return {r: packTree(state.root), | ||
| + | lo: [$(' | ||
| + | } | ||
| + | function encodeState(){ | ||
| + | const json = JSON.stringify(snapshot()); | ||
| + | return btoa(unescape(encodeURIComponent(json))) | ||
| + | .replace(/ | ||
| + | } | ||
| + | function decodeState(str){ | ||
| + | const b = str.replace(/ | ||
| + | return JSON.parse(decodeURIComponent(escape(atob(b)))); | ||
| + | } | ||
| + | function loadHash(){ | ||
| + | const m = location.hash.match(/ | ||
| + | if(!m) return null; | ||
| + | try{ return decodeState(m[1]); | ||
| + | } | ||
| + | function updateHash(){ | ||
| + | try{ | ||
| + | const code = encodeState(); | ||
| + | if(location.hash !== '# | ||
| + | history.replaceState(null, | ||
| + | } | ||
| + | }catch(e){} | ||
| + | } | ||
| + | function saveLocal(){ | ||
| + | try{ localStorage.setItem(LS_KEY, | ||
| + | } | ||
| + | function loadLocal(){ | ||
| + | try{ | ||
| + | const v = localStorage.getItem(LS_KEY); | ||
| + | return v ? JSON.parse(v) : null; | ||
| + | }catch(e){ return null; } | ||
| + | } | ||
| + | function scheduleSave(){ | ||
| + | if(!persistOn) return; | ||
| + | clearTimeout(saveTimer); | ||
| + | saveTimer = setTimeout(function(){ saveLocal(); | ||
| + | } | ||
| + | |||
| + | [' | ||
| + | document.addEventListener(ev, | ||
| + | }); | ||
| + | |||
| + | $(' | ||
| + | persistOn = true; | ||
| + | updateHash(); | ||
| + | const url = location.href.split('#' | ||
| + | if(navigator.clipboard && navigator.clipboard.writeText && window.isSecureContext){ | ||
| + | navigator.clipboard.writeText(url).then(function(){ | ||
| + | showToast(' | ||
| + | }, function(){ | ||
| + | showToast(' | ||
| + | }); | ||
| + | } else { | ||
| + | showToast(' | ||
| + | } | ||
| + | }); | ||
| + | |||
| + | |||
| + | /* ---------- 撤销 / 重做 ---------- */ | ||
| + | let histStack = []; | ||
| + | let histPos = -1; | ||
| + | let skipHist = false; | ||
| + | let valHistTimer = null; | ||
| + | |||
| + | function pushHistory(){ | ||
| + | const snap = JSON.stringify(packTree(state.root)); | ||
| + | if(histPos >= 0 && histStack[histPos] === snap) return; | ||
| + | histStack = histStack.slice(0, | ||
| + | histStack.push(snap); | ||
| + | if(histStack.length > 80) histStack.shift(); | ||
| + | histPos = histStack.length - 1; | ||
| + | syncHistBtns(); | ||
| + | } | ||
| + | function applyHistory(i){ | ||
| + | if(i < 0 || i >= histStack.length) return; | ||
| + | histPos = i; | ||
| + | try{ state.root = unpackTree(JSON.parse(histStack[i])); | ||
| + | skipHist = true; | ||
| + | renderTree(); | ||
| + | syncHistBtns(); | ||
| + | } | ||
| + | function undo(){ if(histPos > 0) applyHistory(histPos - 1); } | ||
| + | function redo(){ if(histPos < histStack.length - 1) applyHistory(histPos + 1); } | ||
| + | function syncHistBtns(){ | ||
| + | const u = $(' | ||
| + | if(u) u.disabled = histPos <= 0; | ||
| + | if(r) r.disabled = histPos >= histStack.length - 1; | ||
| + | } | ||
| + | function scheduleHist(){ | ||
| + | clearTimeout(valHistTimer); | ||
| + | valHistTimer = setTimeout(pushHistory, | ||
| + | } | ||
| /* ---------- 预设 ---------- */ | /* ---------- 预设 ---------- */ | ||
| 行 1303: | 行 1708: | ||
| }; | }; | ||
| - | document.getElementById(' | + | document.querySelectorAll(' |
| btn.addEventListener(' | btn.addEventListener(' | ||
| const p = PRESETS[btn.dataset.preset]; | const p = PRESETS[btn.dataset.preset]; | ||
| 行 1312: | 行 1717: | ||
| renderTree(); | renderTree(); | ||
| }); | }); | ||
| + | }); | ||
| + | |||
| + | /* ---------- 撤销按钮与快捷键 ---------- */ | ||
| + | /* ---------- 元件读数:Z / Y ---------- */ | ||
| + | function setRoMode(m, | ||
| + | roMode = (m === ' | ||
| + | treeEl.querySelectorAll(' | ||
| + | b.classList.toggle(' | ||
| + | }); | ||
| + | if(!silent) recalc(); | ||
| + | } | ||
| + | |||
| + | $(' | ||
| + | $(' | ||
| + | |||
| + | document.addEventListener(' | ||
| + | if(!(ev.ctrlKey || ev.metaKey)) return; | ||
| + | const t = ev.target; | ||
| + | if(t && (t.tagName === ' | ||
| + | const k = (ev.key || '' | ||
| + | if(k === ' | ||
| + | else if((k === ' | ||
| }); | }); | ||
| 行 1362: | 行 1789: | ||
| return new Blob([arr], {type: mime}); | return new Blob([arr], {type: mime}); | ||
| } | } | ||
| + | |||
| + | /* ============================================================ | ||
| + | 8.4 曲线游标读数 | ||
| + | | ||
| + | (function(){ | ||
| + | const svg = $(' | ||
| + | const tip = $(' | ||
| + | if(!svg || !tip) return; | ||
| + | |||
| + | /* 把某个屏幕坐标换算到图内,命中曲线才显示读数 */ | ||
| + | function showTip(clientX, | ||
| + | tip.hidden = true; | ||
| + | if(!sweepGeom) return; | ||
| + | const g = sweepGeom; | ||
| + | const pt = svg.createSVGPoint(); | ||
| + | pt.x = clientX; pt.y = clientY; | ||
| + | const loc = pt.matrixTransform(svg.getScreenCTM().inverse()); | ||
| + | const t = (loc.x - g.pad.l) / g.iw; | ||
| + | if(t < 0 || t > 1 || loc.y < g.pad.t - 3 || loc.y > g.pad.t + g.ih + 3) return; | ||
| + | |||
| + | const f = Math.pow(10, | ||
| + | const z = calcNode(state.root, | ||
| + | |||
| + | /* 只在贴近某条可见曲线时显示:把各曲线的值映射回像素,取最小垂直距离。 | ||
| + | | ||
| + | const yOf = function(v){ | ||
| + | if(!isFinite(v) || v <= 0) return null; | ||
| + | const vv = Math.min(Math.max(v, | ||
| + | return g.pad.t + g.ih * (1 - (Math.log10(vv) - g.lo) / (g.hi - g.lo)); | ||
| + | }; | ||
| + | let near = Infinity; | ||
| + | if(!isInf(z)){ | ||
| + | const cands = [yOf(Math.hypot(z.re, | ||
| + | if(cvOn.re) cands.push(yOf(Math.abs(z.re))); | ||
| + | if(cvOn.im) cands.push(yOf(Math.abs(z.im))); | ||
| + | for(let i=0; | ||
| + | if(cands[i] == null) continue; | ||
| + | const d = Math.abs(cands[i] - loc.y); | ||
| + | if(d < near) near = d; | ||
| + | } | ||
| + | } | ||
| + | if(near > 15) return; | ||
| + | |||
| + | let h = '< | ||
| + | if(isInf(z)){ | ||
| + | h += '< | ||
| + | } else { | ||
| + | const mag = Math.hypot(z.re, | ||
| + | const ang = mag === 0 ? 0 : Math.atan2(z.im, | ||
| + | h += '< | ||
| + | h += '< | ||
| + | (z.im >= 0 ? ' | ||
| + | } | ||
| + | tip.innerHTML = h; | ||
| + | tip.hidden = false; | ||
| + | |||
| + | const r = svg.getBoundingClientRect(); | ||
| + | const sc = r.width / 340; | ||
| + | const tw = tip.offsetWidth, | ||
| + | let lx = loc.x * sc; | ||
| + | lx = Math.min(Math.max(lx, | ||
| + | let ty = loc.y * sc - 9; | ||
| + | if(ty - th < 0){ tip.style.transform = ' | ||
| + | else { tip.style.transform = ' | ||
| + | tip.style.left = lx + ' | ||
| + | tip.style.top = ty + ' | ||
| + | } | ||
| + | |||
| + | svg.addEventListener(' | ||
| + | svg.addEventListener(' | ||
| + | |||
| + | /* 触屏:按住或滑动看读数,抬手后停 1.6s 再消失,免得手指一离开就没了 */ | ||
| + | let hideTimer = null; | ||
| + | const at = function(ev){ | ||
| + | return ev.touches && ev.touches.length ? ev.touches[0] : (ev.changedTouches[0] || null); | ||
| + | }; | ||
| + | |||
| + | svg.addEventListener(' | ||
| + | const p = at(ev); | ||
| + | if(!p) return; | ||
| + | clearTimeout(hideTimer); | ||
| + | showTip(p.clientX, | ||
| + | }, {passive: true}); | ||
| + | |||
| + | svg.addEventListener(' | ||
| + | const p = at(ev); | ||
| + | if(!p) return; | ||
| + | clearTimeout(hideTimer); | ||
| + | showTip(p.clientX, | ||
| + | }, {passive: true}); | ||
| + | |||
| + | svg.addEventListener(' | ||
| + | clearTimeout(hideTimer); | ||
| + | hideTimer = setTimeout(function(){ tip.hidden = true; }, 1600); | ||
| + | }); | ||
| + | })(); | ||
| /* ============================================================ | /* ============================================================ | ||
| 行 1368: | 行 1891: | ||
| | | ||
| const ctxmenu = $(' | const ctxmenu = $(' | ||
| + | let ctxTarget = null; | ||
| + | let ctxFromTouch = false; | ||
| - | $(' | + | function |
| - | | + | |
| + | ctxTarget = {el: svg, name: name}; | ||
| + | ctxFromTouch = !!fromTouch; | ||
| ctxmenu.hidden = false; | ctxmenu.hidden = false; | ||
| const mw = ctxmenu.offsetWidth, | const mw = ctxmenu.offsetWidth, | ||
| - | let x = ev.clientX, y = ev.clientY; | + | let x = clientX, y = clientY; |
| if(x + mw > window.innerWidth - 8) x = window.innerWidth - mw - 8; | if(x + mw > window.innerWidth - 8) x = window.innerWidth - mw - 8; | ||
| if(y + mh > window.innerHeight - 8) y = window.innerHeight - mh - 8; | if(y + mh > window.innerHeight - 8) y = window.innerHeight - mh - 8; | ||
| ctxmenu.style.left = Math.max(8, x) + ' | ctxmenu.style.left = Math.max(8, x) + ' | ||
| ctxmenu.style.top | ctxmenu.style.top | ||
| + | const tip = $(' | ||
| + | if(tip) tip.hidden = true; | ||
| + | } | ||
| + | |||
| + | /* 曲线图与拓扑示意图共用同一个菜单 */ | ||
| + | const CTX_TARGETS = [ | ||
| + | {host: ' | ||
| + | {host: ' | ||
| + | ]; | ||
| + | |||
| + | function svgOf(host){ | ||
| + | return host.tagName.toLowerCase() === ' | ||
| + | } | ||
| + | |||
| + | CTX_TARGETS.forEach(function(t){ | ||
| + | const host = $(t.host); | ||
| + | if(!host) return; | ||
| + | |||
| + | host.addEventListener(' | ||
| + | ev.preventDefault(); | ||
| + | openCtxMenu(ev.clientX, | ||
| + | }); | ||
| + | |||
| + | /* 触屏:长按 0.5s 代替右键,移动超过 10px 视为滚动 */ | ||
| + | let timer = null, sx = 0, sy = 0; | ||
| + | const cancel = function(){ clearTimeout(timer); | ||
| + | |||
| + | host.addEventListener(' | ||
| + | if(ev.touches.length !== 1) return; | ||
| + | const p = ev.touches[0]; | ||
| + | sx = p.clientX; sy = p.clientY; | ||
| + | cancel(); | ||
| + | timer = setTimeout(function(){ | ||
| + | timer = null; | ||
| + | openCtxMenu(sx, | ||
| + | if(navigator.vibrate) navigator.vibrate(12); | ||
| + | }, 520); | ||
| + | }, {passive: true}); | ||
| + | |||
| + | host.addEventListener(' | ||
| + | if(!timer) return; | ||
| + | const p = ev.touches[0]; | ||
| + | if(Math.abs(p.clientX - sx) > 10 || Math.abs(p.clientY - sy) > 10) cancel(); | ||
| + | }, {passive: true}); | ||
| + | |||
| + | host.addEventListener(' | ||
| + | host.addEventListener(' | ||
| }); | }); | ||
| document.addEventListener(' | document.addEventListener(' | ||
| + | /* 长按后浏览器会补一个 click,只吞掉这一下,之后照常响应 */ | ||
| + | if(ctxFromTouch){ ctxFromTouch = false; return; } | ||
| if(!ctxmenu.contains(ev.target)) ctxmenu.hidden = true; | if(!ctxmenu.contains(ev.target)) ctxmenu.hidden = true; | ||
| }); | }); | ||
| 行 1390: | 行 1966: | ||
| ctxmenu.addEventListener(' | ctxmenu.addEventListener(' | ||
| const btn = ev.target.closest(' | const btn = ev.target.closest(' | ||
| - | if(!btn) return; | + | if(!btn |
| const act = btn.dataset.act; | const act = btn.dataset.act; | ||
| + | const target = ctxTarget; | ||
| ctxmenu.hidden = true; | ctxmenu.hidden = true; | ||
| - | svgToPng($(' | + | svgToPng(target.el, 2).then(function(dataUrl){ |
| if(act === ' | if(act === ' | ||
| const a = document.createElement(' | const a = document.createElement(' | ||
| a.href = dataUrl; | a.href = dataUrl; | ||
| - | a.download = 'impedance-chart.png'; | + | a.download = target.name + ' |
| document.body.appendChild(a); | document.body.appendChild(a); | ||
| a.click(); | a.click(); | ||
| 行 1408: | 行 1985: | ||
| if(navigator.clipboard && window.ClipboardItem && window.isSecureContext){ | if(navigator.clipboard && window.ClipboardItem && window.isSecureContext){ | ||
| navigator.clipboard.write([new ClipboardItem({' | navigator.clipboard.write([new ClipboardItem({' | ||
| - | showToast(' | + | showToast(' |
| }, function(){ | }, function(){ | ||
| showToast(' | showToast(' | ||
| 行 1421: | 行 1998: | ||
| 9. 启动 | 9. 启动 | ||
| | | ||
| - | state.root | + | (function boot(){ |
| - | renderTree(); | + | const DEF = function(){ return |
| + | const src = loadHash() || loadLocal(); | ||
| + | if(src && src.r){ | ||
| + | try{ | ||
| + | state.root = unpackTree(src.r); | ||
| + | if(src.f != null) freqVal.value = src.f; | ||
| + | if(src.u != null) freqUnit.value = src.u; | ||
| + | if(src.m === ' | ||
| + | if(src.lo && src.lo.length === 2){ $(' | ||
| + | if(src.hi && src.hi.length === 2){ $(' | ||
| + | }catch(e){ state.root = DEF(); } | ||
| + | } else { | ||
| + | state.root = DEF(); | ||
| + | } | ||
| + | | ||
| + | })(); | ||
| })(); | })(); | ||
| + | })(); | ||
| + | |||
| </ | </ | ||
| </ | </ | ||
在线工具/元器件/阻容感/阻抗串并联计算器.1795220061.txt.gz · 最后更改: 2026/11/21 08:14 由 hwwiki