feat(canvas-v3): P2 第一批 ref/prompt/textout Canvas 化

This commit is contained in:
qiqi committed 2026-12-01 00:22:49 +08:00
1 parent 84d2c1d896
commit f9caea2cfd
11 files changed
+108 -44

No files matched your search

+14 -7
View File
@@ -1,24 +1,31 @@
/* canvas-v3-hit.js —— Canvas V3 的 ref 节点命中测试(2026-12-01,P1 下一步)
* 目的:去掉每节点 hit proxy 与 DOM 端口,用单层坐标命中;选中后才恢复 NodeCard。
/* canvas-v3-hit.js —— Canvas V3 节点命中测试(2026-12-01)
* ref: body + 端口;P2 文本节点只做 body,端口先留给选中后的 NodeCard。
*/
;(function (global) {
'use strict'
function hitRefNode (nodes, wx, wy, opts) {
function hitNode (nodes, wx, wy, opts) {
const o = opts || {}
const isType = typeof o.isType === 'function' ? o.isType : (n) => n && n.type === 'ref'
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 portR = Number(o.portRadiusWorld) > 0 ? Number(o.portRadiusWorld) : 10
const wantPorts = o.wantPorts !== false
const list = Array.isArray(nodes) ? nodes : []
for (let i = list.length - 1; i >= 0; i--) {
const n = list[i]
if (!n || n.type !== 'ref') continue
if (!n || !isType(n)) continue
const w = widthOf(n), h = heightOf(n)
const cy = n.y + h / 2
if (wx >= n.x - portR && wx <= n.x + portR && Math.abs(wy - cy) <= portR) return { node: n, port: 'in' }
if (Math.abs(wx - (n.x + w)) <= portR && Math.abs(wy - cy) <= portR) return { node: n, port: 'out' }
if (wantPorts && n.type === 'ref') {
if (wx >= n.x - portR && wx <= n.x + portR && Math.abs(wy - cy) <= portR) return { node: n, port: 'in' }
if (Math.abs(wx - (n.x + w)) <= portR && Math.abs(wy - cy) <= portR) return { node: n, port: 'out' }
}
if (wx >= n.x && wx <= n.x + w && wy >= n.y && wy <= n.y + h) return { node: n, port: '' }
}
return null
}
global.IWBCanvasV3Hit = { hitRefNode }
function hitRefNode (nodes, wx, wy, opts) {
return hitNode(nodes, wx, wy, Object.assign({}, opts || {}, { isType: (n) => n && n.type === 'ref', wantPorts: true }))
}
global.IWBCanvasV3Hit = { hitNode, hitRefNode }
})(typeof window !== 'undefined' ? window : globalThis)
+48 -23
View File
@@ -66,6 +66,7 @@
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 types = Array.isArray(o.types) && o.types.length ? o.types : ['ref']
const bg = css('--iwb-panel', '#ffffff')
const border = css('--iwb-border2', '#d7dbe2')
const text = css('--iwb-text2', '#5f6673')
@@ -75,7 +76,7 @@
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
if (!n || types.indexOf(n.type) < 0 || sel.indexOf(n.id) >= 0) continue
const x = n.x, y = n.y, nw = widthOf(n), nh = heightOf(n)
// 卡片
ctx.fillStyle = bg
@@ -87,38 +88,62 @@
// 标题
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)) || '参考图')
const title = String((n.data && (n.data.title || n.data.name)) || ({ ref: '参考图', prompt: '提示词', textout: '文本' }[n.type] || '节点'))
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 */ }
if (n.type === 'ref') {
// 图片区
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)
}
} else {
ctx.fillStyle = css('--iwb-layer2', '#f1f2f4')
roundRect(ctx, ix, iy, iw, ih, 10)
ctx.fill()
// P2 文本节点:画截断文本,选中后由 NodeCard 负责编辑。
ctx.save()
ctx.beginPath(); ctx.rect(ix, iy, iw, ih); ctx.clip()
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)
const rawText = String((n.data && (n.data.text || n.data.prompt || n.data.title)) || '')
const lineH = Math.max(12, 18 / scale)
const maxW = Math.max(20, iw - 4)
let line = ''
let ty = iy + Math.max(14, 16 / scale)
for (const ch of rawText) {
const next = line + ch
if (ctx.measureText(next).width > maxW && line) {
ctx.fillText(line, ix + 2, ty); ty += lineH; line = ch
if (ty > iy + ih) break
} else line = next
}
if (line && ty <= iy + ih) ctx.fillText(line, ix + 2, ty)
ctx.restore()
}
// 端口:canvas 命中测试用同一坐标;选中后由 NodeCard DOM 端口接管。
const portR = Math.max(2, 4 / scale)
ctx.fillStyle = bg
ctx.strokeStyle = border
ctx.lineWidth = 1 / scale
for (const [px, py] of [[x, y + nh / 2], [x + nw, y + nh / 2]]) {
ctx.beginPath(); ctx.arc(px, py, portR, 0, Math.PI * 2); ctx.fill(); ctx.stroke()
// 端口:ref 的 canvas 命中;P2 文本节点先只做 body 命中。
if (n.type === 'ref') {
const portR = Math.max(2, 4 / scale)
ctx.fillStyle = bg
ctx.strokeStyle = border
ctx.lineWidth = 1 / scale
for (const [px, py] of [[x, y + nh / 2], [x + nw, y + nh / 2]]) {
ctx.beginPath(); ctx.arc(px, py, portR, 0, Math.PI * 2); ctx.fill(); ctx.stroke()
}
}
void accent
}
+3 -1
View File
@@ -60,5 +60,7 @@
return { enabled: on, dom }
}
global.IWBCanvasV3 = { KEY, isEnabled, setEnabled, init, stats }
// P2 第一批:ref 图片节点 + prompt/textout 文本类节点。
const TYPES = ['ref', 'prompt', 'textout']
global.IWBCanvasV3 = { KEY, TYPES, isEnabled, setEnabled, init, stats }
})(typeof window !== 'undefined' ? window : globalThis)
+2 -2
View File
@@ -9,13 +9,13 @@
'use strict'
function buildNodeEls(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 { CanvasGeometry, CanvasStore, EMPTY_OBJ, NODE_W, NodeCardMemo, addRefFromUrl, batchPortTop, batchSourcesMap, canConnect, cancelRun, config, connTarget, canvasV3On, canvasV3Types, 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:未选中的 ref 节点完全由 canvas 画,DOM 不挂 NodeCard;点击走 canvas 命中测试。
if (canvasV3On && n.type === 'ref' && !sel.includes(n.id)) continue
if (canvasV3On && canvasV3Types.indexOf(n.type) >= 0 && !sel.includes(n.id)) continue
// 细粒度:只传该节点相关的 busy/progress/connTarget(其他节点引用稳定,memo 跳过)
let busySelf = nodeDerived.busyById.get(n.id) || EMPTY_OBJ
let progSelf = nodeDerived.progById.get(n.id) || EMPTY_OBJ