perf: v3.26.144 Canvas 边线 + 平移全量渲染
This commit is contained in:
1 parent
acae6e789d
commit
c9e01db607
18 files changed
+173
-18
No files matched your search
@@ -384,6 +384,9 @@
|
||||
if (o.drag && o.drag.start && o.drag.start[n.id] != null) return true
|
||||
if (o.resize && o.resize.nodeId === n.id) return true
|
||||
if (o.sel && o.sel.indexOf(n.id) >= 0) return true
|
||||
// v3.26.144:平移只改 DOM transform,viewport state 不更新 ⇒ 裁剪窗口会停在起点;
|
||||
// 平移期间全量渲染(103 节点规模可接受),避免超过 280px 后新区域节点迟到/空白。
|
||||
if (o.pan) return true
|
||||
const r = heavyNodeOf(n.type) ? rects.tight : rects.wide
|
||||
const nw = o.widthOf ? o.widthOf(n) : 0
|
||||
const nh = o.heightOf ? o.heightOf(n) : 0
|
||||
@@ -406,7 +409,7 @@
|
||||
}
|
||||
function edgeLayerProps (canvasSize) {
|
||||
const r = edgeLayerSize(canvasSize)
|
||||
return { width: r.w, height: r.h, viewBox: '0 0 ' + r.w + ' ' + r.h, style: { left: 0, top: 0, width: r.w + 'px', height: r.h + 'px', overflow: 'hidden', transform: 'translateZ(0)' } }
|
||||
return { width: r.w, height: r.h, viewBox: '0 0 ' + r.w + ' ' + r.h, style: { left: 0, top: 0, width: r.w + 'px', height: r.h + 'px', overflow: 'hidden' } }
|
||||
}
|
||||
function edgeGroupTransform (viewport) {
|
||||
const x = Number(viewport && viewport.x) || 0
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
/* ui-canvas-edges.js —— 边线 canvas 视觉层(2026-11-30)
|
||||
*
|
||||
* 为什么不再用 SVG 画线:
|
||||
* - SVG 每次平移/缩放都要重栅格化一个屏幕大小的矢量层;WKWebView 上这是明确的热点。
|
||||
* - canvas 只做一次 2D stroke,100 条贝塞尔很便宜,且不产生 SVG 的 DOM/合成层。
|
||||
*
|
||||
* 交互仍由 .iwb-edges(透明 hit path)负责:canvas 只画,不接收指针事件。
|
||||
* 平移/拖节点走命令式 draw(applyViewportDom / dragApplyVisual),不触发 React。
|
||||
*/
|
||||
;(function (global) {
|
||||
'use strict'
|
||||
const React = global.React || {}
|
||||
const el = (type, prop, ...children) => React.createElement(type, prop, ...children)
|
||||
const useRef = React.useRef
|
||||
const useEffect = React.useEffect
|
||||
|
||||
function cssVar(name, fallback) {
|
||||
try {
|
||||
const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim()
|
||||
return v || fallback
|
||||
} catch (e) { return fallback }
|
||||
}
|
||||
|
||||
function draw(canvas, o) {
|
||||
if (!canvas || !o || typeof canvas.getContext !== 'function') return
|
||||
const rect = canvas.getBoundingClientRect ? canvas.getBoundingClientRect() : null
|
||||
const w = Math.max(1, Math.round((rect && rect.width) || canvas.clientWidth || 1))
|
||||
const h = Math.max(1, Math.round((rect && rect.height) || canvas.clientHeight || 1))
|
||||
const dpr = Math.min(2, (typeof devicePixelRatio === 'number' && devicePixelRatio > 0) ? devicePixelRatio : 1)
|
||||
const bw = Math.round(w * dpr), bh = Math.round(h * dpr)
|
||||
if (canvas.width !== bw || canvas.height !== bh) { canvas.width = bw; canvas.height = bh }
|
||||
const ctx = canvas.getContext('2d')
|
||||
if (!ctx) return
|
||||
ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
|
||||
ctx.clearRect(0, 0, w, h)
|
||||
const vp = o.viewport || { x: 0, y: 0, scale: 1 }
|
||||
const scale = Number(vp.scale) > 0 ? Number(vp.scale) : 1
|
||||
const baseW = parseFloat(cssVar('--iwb-edge-w', '1.4')) || 1.4
|
||||
const edgeColor = cssVar('--iwb-edge', '#8b95a5')
|
||||
const hotColor = cssVar('--iwb-edge-hot', '#6b7280')
|
||||
const warnColor = cssVar('--iwb-warn', '#e0a030')
|
||||
const errColor = cssVar('--iwb-err', '#e05252')
|
||||
const hoverId = o.hoverEdgeId || ''
|
||||
const selected = o.selEdge || ''
|
||||
const focus = o.focusNodeId || ''
|
||||
const edgeHover = o.edgeHover || null
|
||||
const edges = Array.isArray(o.edges) ? o.edges : []
|
||||
const pathOf = typeof o.computeEdgePath === 'function' ? o.computeEdgePath : null
|
||||
if (!pathOf) return
|
||||
|
||||
ctx.setTransform(dpr * scale, 0, 0, dpr * scale, dpr * (Number(vp.x) || 0), dpr * (Number(vp.y) || 0))
|
||||
ctx.lineCap = 'round'
|
||||
ctx.lineJoin = 'round'
|
||||
|
||||
for (let i = 0; i < edges.length; i++) {
|
||||
const ed = edges[i]
|
||||
if (!ed) continue
|
||||
let d = ''
|
||||
try { d = pathOf(ed, o.graph, o.nodeMap, o.measuredH, o.pathCache) || '' } catch (e) { d = '' }
|
||||
if (!d || typeof Path2D === 'undefined') continue
|
||||
const isSel = selected === ed.id
|
||||
const isHot = focus && (ed.to === focus || ed.from === focus)
|
||||
const isHover = hoverId === ed.id
|
||||
const isDetach = !!(edgeHover && edgeHover.mode === 'detach' && Array.isArray(edgeHover.edgeIds) && edgeHover.edgeIds.indexOf(ed.id) >= 0)
|
||||
const isInsert = !!(edgeHover && edgeHover.mode === 'insert' && edgeHover.edgeId === ed.id)
|
||||
ctx.strokeStyle = isSel ? warnColor : (isDetach ? errColor : (isHot || isHover || isInsert ? hotColor : edgeColor))
|
||||
ctx.lineWidth = (isSel || isHot || isHover || isInsert || isDetach) ? Math.max(2.2, baseW * 1.6) : baseW
|
||||
ctx.setLineDash(isDetach ? [6 / scale, 4 / scale] : (isInsert ? [6 / scale, 4 / scale] : []))
|
||||
ctx.stroke(new Path2D(d))
|
||||
}
|
||||
|
||||
if (o.tempD && typeof Path2D !== 'undefined') {
|
||||
ctx.strokeStyle = o.connTarget && o.connTarget.valid === false ? errColor : hotColor
|
||||
ctx.lineWidth = Math.max(2, baseW)
|
||||
ctx.setLineDash([6 / scale, 4 / scale])
|
||||
try { ctx.stroke(new Path2D(o.tempD)) } catch (e) { /* ignore */ }
|
||||
}
|
||||
ctx.setLineDash([])
|
||||
}
|
||||
|
||||
function EdgeCanvas(props) {
|
||||
const ref = useRef(null)
|
||||
const latest = useRef(props)
|
||||
latest.current = props
|
||||
useEffect(() => {
|
||||
const canvas = ref.current
|
||||
if (!canvas) return
|
||||
draw(canvas, latest.current)
|
||||
if (typeof props.onReady === 'function') {
|
||||
props.onReady(function (x, y, scale) {
|
||||
const p = latest.current || {}
|
||||
draw(canvas, Object.assign({}, p, { viewport: { x: x, y: y, scale: scale } }))
|
||||
})
|
||||
}
|
||||
})
|
||||
useEffect(() => () => { if (typeof props.onReady === 'function') props.onReady(null) }, [])
|
||||
return el('canvas', { className: 'iwb-edge-canvas', 'aria-hidden': 'true', ref: ref })
|
||||
}
|
||||
|
||||
global.IWBCanvasEdges = { EdgeCanvas, draw }
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
@@ -61,6 +61,8 @@
|
||||
const gEl = el0.querySelector('[data-edge-id="' + ed.id + '"]')
|
||||
if (gEl && d) { const ps = gEl.querySelectorAll('path'); for (let i = 0; i < ps.length; i++) ps[i].setAttribute('d', d) }
|
||||
}
|
||||
// 边线视觉层是 canvas:拖动中坐标已就地更新,命令式重画一次(不触发 React)。
|
||||
try { if (typeof refs.edgeCanvasDraw === 'function') refs.edgeCanvasDraw(refs.viewport.x, refs.viewport.y, refs.viewport.scale) } catch (e) { /* ignore */ }
|
||||
// 节点组外框实时跟随:外框由成员包围盒派生,拖动中不重渲染 → 必须就地改 DOM(否则松手才跳)。
|
||||
// 用 refs.nodeGroupMap 反查受影响的组,避免每帧 O(groups×members) 扫描。
|
||||
if (refs.nodeGroupMap && refs.nodeGroupMap.size) {
|
||||
|
||||
@@ -195,10 +195,16 @@ function EdgePath(props) {
|
||||
const relation = focusNodeId ? (ed.to === focusNodeId ? ' iwb-edge-upstream' : (ed.from === focusNodeId ? ' iwb-edge-downstream' : '')) : ''
|
||||
const cls = 'iwb-edge' + (selEdge === ed.id ? ' iwb-edge-sel' : '') + relation + ((edgeHover && edgeHover.mode === 'insert' && edgeHover.edgeId === ed.id) ? ' iwb-edge-hot' : '') + ((edgeHover && edgeHover.mode === 'detach' && edgeHover.edgeIds.indexOf(ed.id) >= 0) ? ' iwb-edge-detach' : '')
|
||||
// 双击连线 = 断开连线:这是用户明确要保留的操作习惯(2026-11-28 确认),不要删。
|
||||
const hnd = { onPointerDown: (e) => { e.stopPropagation(); onSelect(ed.id) }, onDoubleClick: (e) => { e.stopPropagation(); onDelete(ed.id) } }
|
||||
const hnd = {
|
||||
onPointerDown: (e) => { e.stopPropagation(); onSelect(ed.id) },
|
||||
onDoubleClick: (e) => { e.stopPropagation(); onDelete(ed.id) },
|
||||
onPointerEnter: () => { if (typeof props.onHover === 'function') props.onHover(ed.id) },
|
||||
onPointerLeave: () => { if (typeof props.onHover === 'function') props.onHover('') },
|
||||
}
|
||||
return el('g', { 'data-edge-id': ed.id },
|
||||
el('path', { key: ed.id + '_hit', className: 'iwb-edge-hit', d, fill: 'none', ...hnd }),
|
||||
el('path', { key: ed.id, className: cls, d, fill: 'none', title: '点击选中(Delete 断开)· 双击直接断开', ...hnd }),
|
||||
// 视觉由 canvas 层画;保留一个 display:none 的旧路径节点给 e2e/选择器兼容(不参与绘制)。
|
||||
el('path', { key: ed.id, className: cls, d, fill: 'none', style: props.visual === false ? { display: 'none' } : undefined, title: '点击选中(Delete 断开)· 双击直接断开', ...hnd }),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user