feat(canvas-v3): P2 第一批 ref/prompt/textout Canvas 化
This commit is contained in:
1 parent
84d2c1d896
commit
f9caea2cfd
11 files changed
+108
-44
No files matched your search
+48
-23
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user