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)
|
||||
Reference in new issue
Block a user