feat(image-vectorize): v0.3.0 按孤岛拆分元素(Figma 里可单独选中)
用户需求:「现在元素是一个整体,我需要有个勾选按单个元素分解,孤岛是分离的, 输出时能选分类的还是整体的」。 potrace 出厂把所有轮廓塞进一个 <path>(800×800 菜单图 1 个 path / 129 个子路径, 靠 fill-rule=evenodd 互相挖洞),所以粘到 Figma 只是一个整体。 新增 plugin/islands.js: - 解析 d 为子路径(只认 potrace 输出的 M/C/L/Z,全绝对坐标) - 按几何包含建父子关系(bbox + 射线法点在多边形内),深度偶数 = 外壳、奇数 = 孔洞 - 每个外壳 + 它的孔洞合成一个 path(孔洞必须同 path,evenodd 才挖得出洞), 不同组并列输出、不包 <g>;元素按面积降序(大块在下,遮挡关系同原图) - 为什么不能按 M 直接切:切了孔洞就变实心("O" 中间被填满) UI:输出区加「按孤岛拆分」开关(默认开,ctx.settings 记忆),统计改显示元素数, 合并时标注「N 孤岛合并」;复制反馈说明粘过去是 N 个独立元素还是 1 个整体。 验证:harness 47 项(新增 8 项:多 path / 无 g / 统计一致 / 取消勾选合回 1 个 / 坐标集合完全一致 / 渲染差异 0.003%)、真站 e2e 18 项全绿。
This commit is contained in:
1 parent
d151de74a1
commit
866c420c5c
11 files changed
+1234
-39
No files matched your search
@@ -1,7 +1,10 @@
|
||||
// 单色/灰度描摹 CLI(开发用,不进 CI):把真实素材丢进去,直接看参数效果。
|
||||
//
|
||||
// node image-vectorize/tools/trace-cli.mjs <图片> [--mode mono|gray] [--levels 3]
|
||||
// [--turd 3] [--alpha 1] [--tol 0.1] [--invert] [--color #1a1a1a] [--threshold -1] [--out 目录]
|
||||
// [--turd 3] [--alpha 1] [--tol 0.1] [--invert] [--color #1a1a1a] [--threshold -1]
|
||||
// [--no-split] [--out 目录]
|
||||
//
|
||||
// --no-split:不按孤岛拆分(所有轮廓塞在一个 path 里,粘到 Figma 是整体)。默认拆分。
|
||||
//
|
||||
// 产物:<out>/<name>-<mode><levels>.svg + 一张对比图 <out>/<name>-cmp.png(原图 / 结果 / 局部放大)。
|
||||
// 为什么走浏览器:PNG/JPEG 解码和 ImageData 语义都与线上一致,Node 里没有等价的零依赖解码器。
|
||||
@@ -31,7 +34,7 @@ if (!chromium) {
|
||||
|
||||
const argv = process.argv.slice(2)
|
||||
const files = []
|
||||
const opt = { mode: 'gray', levels: 3, turd: 3, alpha: 1, tol: 0.1, invert: false, color: '#1a1a1a', threshold: -1, out: '/tmp/vz-cli' }
|
||||
const opt = { mode: 'gray', levels: 3, turd: 3, alpha: 1, tol: 0.1, invert: false, color: '#1a1a1a', threshold: -1, split: true, out: '/tmp/vz-cli' }
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i]
|
||||
if (a === '--mode') opt.mode = argv[++i]
|
||||
@@ -42,6 +45,8 @@ for (let i = 0; i < argv.length; i++) {
|
||||
else if (a === '--invert') opt.invert = true
|
||||
else if (a === '--color') opt.color = argv[++i]
|
||||
else if (a === '--threshold') opt.threshold = Number(argv[++i])
|
||||
else if (a === '--no-split') opt.split = false
|
||||
else if (a === '--split') opt.split = true
|
||||
else if (a === '--out') opt.out = argv[++i]
|
||||
else if (a.startsWith('--')) throw new Error('未知参数 ' + a)
|
||||
else files.push(a)
|
||||
@@ -69,6 +74,7 @@ const params = {
|
||||
color: opt.color,
|
||||
threshold: opt.threshold,
|
||||
outScale: 1,
|
||||
splitIslands: opt.split,
|
||||
}
|
||||
|
||||
for (const file of files) {
|
||||
@@ -86,16 +92,16 @@ for (const file of files) {
|
||||
g.drawImage(img, 0, 0)
|
||||
const imageData = g.getImageData(0, 0, c.width, c.height)
|
||||
const out = window.VZEngine.traceImageData(imageData, params)
|
||||
return { svg: out.svg, ms: out.ms, paths: out.paths, levels: out.levels, w: c.width, h: c.height }
|
||||
return { svg: out.svg, ms: out.ms, paths: out.paths, elements: out.elements, islands: out.islands, levels: out.levels, w: c.width, h: c.height }
|
||||
},
|
||||
{ dataUrl: `data:${mime};base64,${b64}`, params }
|
||||
)
|
||||
const base = path.basename(file).replace(/\.[^.]+$/, '')
|
||||
const tag = (opt.mode === 'mono' ? 'mono' : 'gray' + res.levels) + '-t' + opt.tol
|
||||
const tag = (opt.mode === 'mono' ? 'mono' : 'gray' + res.levels) + '-t' + opt.tol + (opt.split ? '-split' : '-whole')
|
||||
const svgPath = path.join(opt.out, `${base}-${tag}.svg`)
|
||||
fs.writeFileSync(svgPath, res.svg)
|
||||
console.log(
|
||||
`${base} ${tag.padEnd(6)} ${String(res.paths).padStart(3)} paths ${String(res.ms).padStart(4)}ms ` +
|
||||
`${base} ${tag.padEnd(14)} ${String(res.elements).padStart(4)} 元素 / ${String(res.islands).padStart(4)} 孤岛 ${String(res.ms).padStart(4)}ms ` +
|
||||
`${(res.svg.length / 1024).toFixed(0)}KB ${res.w}×${res.h} → ${svgPath}`
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user