feat(canvas-v3): Phase 0 flag + ref node canvas 垂直切片(默认关闭)
This commit is contained in:
1 parent
d04dac64bc
commit
2ff86d0840
17 files changed
+490
-13
No files matched your search
@@ -0,0 +1,143 @@
|
||||
/* canvas-v3-node-layer.js —— Canvas V3 图片节点静态层(2026-12-01,P1 垂直切片)
|
||||
*
|
||||
* 仅当 data-canvas-v3="on" 时由 app 挂载;未选中的 ref 节点由 canvas 画卡片/标题/图片,
|
||||
* DOM 只保留一个透明 hit proxy。选中后恢复完整 NodeCard。
|
||||
* 目标与限制见 docs/plans/canvas-v3-long-term.md。
|
||||
*/
|
||||
;(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
|
||||
const useState = React.useState
|
||||
const imgCache = new Map()
|
||||
|
||||
function css (name, fallback) {
|
||||
try {
|
||||
const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim()
|
||||
return v || fallback
|
||||
} catch (e) { return fallback }
|
||||
}
|
||||
|
||||
function roundRect (ctx, x, y, w, h, r) {
|
||||
const rr = Math.max(0, Math.min(r, w / 2, h / 2))
|
||||
ctx.beginPath()
|
||||
ctx.moveTo(x + rr, y)
|
||||
ctx.arcTo(x + w, y, x + w, y + h, rr)
|
||||
ctx.arcTo(x + w, y + h, x, y + h, rr)
|
||||
ctx.arcTo(x, y + h, x, y, rr)
|
||||
ctx.arcTo(x, y, x + w, y, rr)
|
||||
ctx.closePath()
|
||||
}
|
||||
|
||||
function imageOf (url, onLoad) {
|
||||
if (!url) return null
|
||||
let entry = imgCache.get(url)
|
||||
if (!entry) {
|
||||
entry = { img: new Image(), loaded: false }
|
||||
entry.img.decoding = 'async'
|
||||
entry.img.onload = () => { entry.loaded = true; if (typeof onLoad === 'function') onLoad() }
|
||||
entry.img.onerror = () => { entry.failed = true; if (typeof onLoad === 'function') onLoad() }
|
||||
entry.img.src = url
|
||||
imgCache.set(url, entry)
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
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)
|
||||
if (canvas.width !== Math.round(w * dpr) || canvas.height !== Math.round(h * dpr)) {
|
||||
canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr)
|
||||
}
|
||||
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 nodes = Array.isArray(o.nodes) ? o.nodes : []
|
||||
const sel = Array.isArray(o.sel) ? o.sel : []
|
||||
const widthOf = typeof o.widthOf === 'function' ? o.widthOf : (n) => n.w || 240
|
||||
const heightOf = typeof o.heightOf === 'function' ? o.heightOf : (n) => n.h || 220
|
||||
const imageFor = typeof o.imageFor === 'function' ? o.imageFor : () => ''
|
||||
const onImageLoad = o.onImageLoad
|
||||
const bg = css('--iwb-panel', '#ffffff')
|
||||
const border = css('--iwb-border2', '#d7dbe2')
|
||||
const text = css('--iwb-text2', '#5f6673')
|
||||
const accent = css('--iwb-brand', '#6b7280')
|
||||
const radius = 14
|
||||
const headH = 34
|
||||
ctx.setTransform(dpr * scale, 0, 0, dpr * scale, dpr * (Number(vp.x) || 0), dpr * (Number(vp.y) || 0))
|
||||
ctx.lineJoin = 'round'
|
||||
for (const n of nodes) {
|
||||
if (!n || n.type !== 'ref' || sel.indexOf(n.id) >= 0) continue
|
||||
const x = n.x, y = n.y, nw = widthOf(n), nh = heightOf(n)
|
||||
// 卡片
|
||||
ctx.fillStyle = bg
|
||||
roundRect(ctx, x, y, nw, nh, radius)
|
||||
ctx.fill()
|
||||
ctx.strokeStyle = border
|
||||
ctx.lineWidth = 1 / scale
|
||||
ctx.stroke()
|
||||
// 标题
|
||||
ctx.fillStyle = text
|
||||
ctx.font = '600 ' + Math.max(10, 12 / scale * 0.9) + 'px -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif'
|
||||
const title = String((n.data && (n.data.title || n.data.name)) || '参考图')
|
||||
ctx.save()
|
||||
ctx.beginPath(); ctx.rect(x + 10, y, Math.max(0, nw - 20), headH); ctx.clip()
|
||||
ctx.fillText(title, x + 12, y + 21)
|
||||
ctx.restore()
|
||||
// 图片区
|
||||
const pad = 10
|
||||
const ix = x + pad, iy = y + headH, iw = Math.max(1, nw - pad * 2), ih = Math.max(1, nh - headH - pad)
|
||||
const raw = n.data && (n.data.data || n.data.url)
|
||||
const url = raw ? imageFor(n, raw) : ''
|
||||
const entry = url ? imageOf(url, onImageLoad) : null
|
||||
if (entry && entry.loaded && !entry.failed) {
|
||||
const iw0 = entry.img.naturalWidth || iw
|
||||
const ih0 = entry.img.naturalHeight || ih
|
||||
const k = Math.min(iw / iw0, ih / ih0)
|
||||
const dw = iw0 * k, dh = ih0 * k
|
||||
try { ctx.drawImage(entry.img, ix + (iw - dw) / 2, iy + (ih - dh) / 2, dw, dh) } catch (e) { /* cross-origin */ }
|
||||
} else {
|
||||
ctx.fillStyle = css('--iwb-layer2', '#f1f2f4')
|
||||
roundRect(ctx, ix, iy, iw, ih, 10)
|
||||
ctx.fill()
|
||||
ctx.fillStyle = text
|
||||
ctx.font = Math.max(10, 12 / scale * 0.9) + 'px -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif'
|
||||
ctx.fillText(entry && entry.failed ? '图片源失效' : '图片加载中…', ix + 12, iy + 24)
|
||||
}
|
||||
// 选中/连线提示用边框色(当前未选中,保留扩展点)
|
||||
void accent
|
||||
}
|
||||
}
|
||||
|
||||
function CanvasNodeLayer (props) {
|
||||
const ref = useRef(null)
|
||||
const latest = useRef(props)
|
||||
latest.current = props
|
||||
const [rev, setRev] = useState(0)
|
||||
void rev
|
||||
useEffect(() => {
|
||||
const canvas = ref.current
|
||||
if (!canvas) return
|
||||
const p = latest.current || {}
|
||||
draw(canvas, Object.assign({}, p, { onImageLoad: () => setRev((v) => v + 1) }))
|
||||
if (typeof p.onReady === 'function') {
|
||||
p.onReady(function (x, y, scale) {
|
||||
const q = latest.current || {}
|
||||
draw(canvas, Object.assign({}, q, { viewport: { x: x, y: y, scale: scale }, onImageLoad: () => setRev((v) => v + 1) }))
|
||||
})
|
||||
}
|
||||
})
|
||||
useEffect(() => () => { if (typeof props.onReady === 'function') props.onReady(null) }, [])
|
||||
return el('canvas', { className: 'iwb-node-canvas', 'aria-hidden': 'true', ref: ref })
|
||||
}
|
||||
|
||||
global.IWBCanvasV3NodeLayer = { CanvasNodeLayer, draw }
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
@@ -0,0 +1,64 @@
|
||||
/* canvas-v3.js —— Canvas 静态渲染迁移的特性开关(2026-12-01,Phase 0)
|
||||
*
|
||||
* 默认关闭;开启后只改变 data-canvas-v3 与后续渲染器选择,不影响数据格式。
|
||||
* 目标架构见 docs/plans/canvas-v3-long-term.md。
|
||||
*/
|
||||
;(function (global) {
|
||||
'use strict'
|
||||
const KEY = 'iwb_canvas_v3'
|
||||
const ATTR = 'canvasV3'
|
||||
|
||||
// src 内不直调 localStorage:统一走 IWBStorageLocal 门面(桌面端会落到资料目录底座)。
|
||||
function store () {
|
||||
try {
|
||||
const S = global.IWBStorageLocal
|
||||
return (S && typeof S.getStore === 'function') ? S.getStore() : null
|
||||
} catch (e) { return null }
|
||||
}
|
||||
|
||||
function isEnabled () {
|
||||
try {
|
||||
const s = store()
|
||||
if (s && typeof s.rawGet === 'function') return s.rawGet(KEY, '0') === '1'
|
||||
} catch (e) { /* ignore */ }
|
||||
return false
|
||||
}
|
||||
|
||||
function syncRoot (on) {
|
||||
try {
|
||||
if (typeof document !== 'undefined' && document.documentElement) {
|
||||
document.documentElement.dataset[ATTR] = on ? 'on' : 'off'
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
function setEnabled (on) {
|
||||
const next = !!on
|
||||
try {
|
||||
const s = store()
|
||||
if (s && typeof s.rawSet === 'function') s.rawSet(KEY, next ? '1' : '0')
|
||||
} catch (e) { /* ignore */ }
|
||||
syncRoot(next)
|
||||
try {
|
||||
if (typeof window !== 'undefined' && typeof window.dispatchEvent === 'function' && typeof CustomEvent === 'function') {
|
||||
window.dispatchEvent(new CustomEvent('iwb:canvas-v3', { detail: { enabled: next } }))
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
return next
|
||||
}
|
||||
|
||||
function init () {
|
||||
const on = isEnabled()
|
||||
syncRoot(on)
|
||||
return on
|
||||
}
|
||||
|
||||
function stats () {
|
||||
const on = isEnabled()
|
||||
let dom = -1
|
||||
try { dom = document.getElementsByTagName('*').length } catch (e) { /* ignore */ }
|
||||
return { enabled: on, dom }
|
||||
}
|
||||
|
||||
global.IWBCanvasV3 = { KEY, isEnabled, setEnabled, init, stats }
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
+24
-1
@@ -9,11 +9,34 @@
|
||||
'use strict'
|
||||
|
||||
function buildNodeEls(ctx) {
|
||||
const { CanvasGeometry, CanvasStore, EMPTY_OBJ, NODE_W, NodeCardMemo, addRefFromUrl, batchPortTop, batchSourcesMap, canConnect, cancelRun, config, connTarget, defaultNodeBoxWidth, deleteNodes, duplicateNodes, edges, el, generationConcurrency, hOf, inPortWorld, isBatchSrc, lodViewport, nodeDerived, nodeOnMeasure, nodeOnPreview, nodeOnSelect, nodeTypeH, nodeWidth, nodes, pushHistory, refs, runLLM, runLayerNode, runNode, runOutSources, saveNow, sel, selRef, setConn, setConnTarget, setNotice, setSel, taskCountOf, thumbRev, typesRev, uiZoom, uid, updateNode, visibleIds, wLodEnabled, zoomToNode } = ctx
|
||||
const { CanvasGeometry, CanvasStore, EMPTY_OBJ, NODE_W, NodeCardMemo, addRefFromUrl, batchPortTop, batchSourcesMap, canConnect, cancelRun, config, connTarget, canvasV3On, defaultNodeBoxWidth, deleteNodes, duplicateNodes, edges, el, generationConcurrency, hOf, inPortWorld, isBatchSrc, lodViewport, nodeDerived, nodeOnMeasure, nodeOnPreview, nodeOnSelect, nodeTypeH, nodeWidth, nodes, pushHistory, refs, runLLM, runLayerNode, runNode, runOutSources, saveNow, sel, selRef, setConn, setConnTarget, setNotice, setSel, taskCountOf, thumbRev, typesRev, uiZoom, uid, updateNode, visibleIds, wLodEnabled, zoomToNode } = ctx
|
||||
const nodeEls = []
|
||||
const batchSourcesById = batchSourcesMap
|
||||
for (const n of nodes) {
|
||||
if (!visibleIds.has(n.id)) continue
|
||||
// Canvas V3 P1:未选中的 ref 节点只保留透明 hit proxy,视觉由 canvas 画;选中后恢复完整 NodeCard。
|
||||
if (canvasV3On && n.type === 'ref' && !sel.includes(n.id)) {
|
||||
nodeEls.push(el('div', {
|
||||
key: n.id,
|
||||
className: 'iwb-node-hit-proxy',
|
||||
'data-node-id': n.id,
|
||||
style: { transform: 'translate(' + n.x + 'px, ' + n.y + 'px)', width: nodeWidth(n) + 'px', height: hOf(n) + 'px' },
|
||||
onPointerDown: (e) => {
|
||||
if (e.button !== 0) return
|
||||
e.stopPropagation()
|
||||
const curSel = selRef.current
|
||||
const ids = (e.shiftKey || e.metaKey || e.ctrlKey)
|
||||
? (curSel.includes(n.id) ? curSel.filter((x) => x !== n.id) : curSel.concat([n.id]))
|
||||
: (curSel.includes(n.id) ? curSel.slice() : [n.id])
|
||||
const start = {}
|
||||
for (const x of refs.nodes) if (ids.includes(x.id)) start[x.id] = { x: x.x, y: x.y }
|
||||
refs.bodyDown = { id: n.id, ids, start, sx: e.clientX / uiZoom(), sy: e.clientY / uiZoom(), active: false }
|
||||
setSel(ids)
|
||||
},
|
||||
onDoubleClick: (e) => { e.stopPropagation() },
|
||||
}, null))
|
||||
continue
|
||||
}
|
||||
// 细粒度:只传该节点相关的 busy/progress/connTarget(其他节点引用稳定,memo 跳过)
|
||||
let busySelf = nodeDerived.busyById.get(n.id) || EMPTY_OBJ
|
||||
let progSelf = nodeDerived.progById.get(n.id) || EMPTY_OBJ
|
||||
|
||||
@@ -61,8 +61,9 @@
|
||||
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)。
|
||||
// 边线/节点 canvas:拖动中坐标已就地更新,命令式重画一次(不触发 React)。
|
||||
try { if (typeof refs.edgeCanvasDraw === 'function') refs.edgeCanvasDraw(refs.viewport.x, refs.viewport.y, refs.viewport.scale) } catch (e) { /* ignore */ }
|
||||
try { if (typeof refs.nodeCanvasDraw === 'function') refs.nodeCanvasDraw(refs.viewport.x, refs.viewport.y, refs.viewport.scale) } catch (e) { /* ignore */ }
|
||||
// 节点组外框实时跟随:外框由成员包围盒派生,拖动中不重渲染 → 必须就地改 DOM(否则松手才跳)。
|
||||
// 用 refs.nodeGroupMap 反查受影响的组,避免每帧 O(groups×members) 扫描。
|
||||
if (refs.nodeGroupMap && refs.nodeGroupMap.size) {
|
||||
|
||||
@@ -110,6 +110,9 @@
|
||||
el('label', { className: 'iwb-diag-kv', title: '打开后整个画布世界会被提升成一个常驻合成层:拖拽/缩放更顺,但内存会明显上涨(实测 +700MB 级)。改完等 10 秒看「进程实测」对比。' },
|
||||
el('input', { type: 'checkbox', checked: !!(extras && extras.layers), onChange: (e) => { if (typeof props.onToggleLayers === 'function') props.onToggleLayers(e.target.checked) } }),
|
||||
' 合成层常驻提升'),
|
||||
el('label', { className: 'iwb-diag-kv', title: 'Canvas V3 实验:静态节点内容改 Canvas 绘制,只给选中/编辑节点挂 DOM。默认关闭,开启后需刷新;见 docs/plans/canvas-v3-long-term.md' },
|
||||
el('input', { type: 'checkbox', checked: !!(extras && extras.canvasV3), onChange: (e) => { if (typeof props.onToggleCanvasV3 === 'function') props.onToggleCanvasV3(e.target.checked) } }),
|
||||
' Canvas V3(实验,需刷新)'),
|
||||
el('label', { className: 'iwb-diag-kv', title: '关掉所有 backdrop-filter(顶栏/停靠栏的毛玻璃),用于二分 graphics 占用' },
|
||||
el('input', { type: 'checkbox', checked: !!(extras && extras.noblur), onChange: (e) => { if (typeof props.onToggleNoBlur === 'function') props.onToggleNoBlur(e.target.checked) } }),
|
||||
' 禁用毛玻璃'),
|
||||
|
||||
Reference in new issue
Block a user