perf(canvas): 平移/缩放改走 DOM + 远视距 LOD(v3.25.68,p95 211ms→52ms)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m3s

This commit is contained in:
qiqi committed 2026-11-20 22:53:21 +08:00
1 parent 0baeda2abd
commit e79095e848
13 files changed
+104 -33

No files matched your search

+20 -6
View File
@@ -1187,7 +1187,7 @@
// wheel 缩放 + 画布尺寸跟随;返回 cleanup
function startCanvasZoomAndSize(ctx) {
const { canvasEl, refs, setViewport, setCanvasSize } = ctx
const { canvasEl, refs, setViewport, setCanvasSize, applyViewportDom } = ctx
const el0 = canvasEl && canvasEl.el
if (!el0) return undefined
const onWheel = (e) => {
@@ -1204,11 +1204,25 @@
refs.zoomTimer = setTimeout(() => { refs.zooming = false; try { setViewport((v) => ({ ...v })) } catch (e2) { /* ignore */ } }, 160)
} catch (e2) { /* ignore */ }
const factor = Math.pow(1.0015, -e.deltaY)
setViewport((v) => {
const ns = Math.min(3, Math.max(0.01, v.scale * factor))
const k = ns / v.scale
return { scale: ns, x: e.clientX - (e.clientX - v.x) * k, y: e.clientY - (e.clientY - v.y) * k }
})
// v3.25.68(借鉴 tldraw 的 debounced zoom):滚轮先只改 DOM transform,
// 停止滚动 110ms 后才提交一次 state。原来每个 wheel 事件都 setViewport,
// 一个滚轮手势里可能连着重渲染十几次整个世界层 —— 这是缩放时最明显的卡顿来源。
const base = refs.viewportLive || refs.viewport
const ns = Math.min(3, Math.max(0.01, base.scale * factor))
const k = ns / base.scale
const nx = e.clientX - (e.clientX - base.x) * k
const ny = e.clientY - (e.clientY - base.y) * k
refs.viewportLive = { x: nx, y: ny, scale: ns }
try { if (typeof applyViewportDom === 'function') applyViewportDom(nx, ny, ns) } catch (e3) { /* ignore */ }
try {
clearTimeout(refs.zoomCommit)
refs.zoomCommit = setTimeout(function () {
const live = refs.viewportLive
refs.viewportLive = null
if (!live) return
try { setViewport({ x: live.x, y: live.y, scale: live.scale }) } catch (e4) { /* ignore */ }
}, 110)
} catch (e5) { /* ignore */ }
}
el0.addEventListener('wheel', onWheel, { passive: false })
// 画布尺寸跟随容器真实尺寸。