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
@@ -19,6 +19,7 @@
|
||||
|
||||
import Potrace from './vendor/potrace/Potrace.js'
|
||||
import Posterizer from './vendor/potrace/Posterizer.js'
|
||||
import { countIslands, splitSvgIslands } from './islands.js'
|
||||
|
||||
export const MODES = ['gray', 'mono']
|
||||
export const LEVELS_MIN = 2
|
||||
@@ -44,6 +45,10 @@ export const DEFAULTS = {
|
||||
invert: false, // 反相:描摹亮部(黑底白字场景)
|
||||
color: '#1a1a1a', // mono 的填充色
|
||||
outScale: 1, // 输出尺寸倍率(只放大尺寸,不重算曲线)
|
||||
// 按孤岛拆分:potrace 出厂把整图所有轮廓塞进一个 path,粘到 Figma 只是一个整体;
|
||||
// 拆开后每个不连通的图形都是独立 path,可以单独选中/移动(用户 2026-11-05 要求)。
|
||||
// 默认开 —— 需求来源就是「现在是一个整体,我想分开」。
|
||||
splitIslands: true,
|
||||
}
|
||||
|
||||
/** 曲线精度三档(UI 用):越大越省路径数据,越小越贴合像素 */
|
||||
@@ -73,6 +78,7 @@ export function normalizeParams(p) {
|
||||
invert: Boolean(src.invert),
|
||||
color: /^#[0-9a-f]{6}$/i.test(String(src.color || '')) ? String(src.color) : DEFAULTS.color,
|
||||
outScale: Math.min(4, Math.max(1, Number(src.outScale) || 1)),
|
||||
splitIslands: src.splitIslands === undefined ? DEFAULTS.splitIslands : Boolean(src.splitIslands),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,5 +189,17 @@ export function traceImageData(imageData, params) {
|
||||
svg = posterizer.getSVG()
|
||||
}
|
||||
|
||||
return { svg, ms: Math.round(now() - t0), paths: (String(svg).match(/<path/g) || []).length, levels }
|
||||
// 拆分放在计时之内:用户看到的耗时应该包含这次操作的全部成本
|
||||
let elements = (String(svg).match(/<path/g) || []).length
|
||||
let islands = 0
|
||||
if (p.splitIslands) {
|
||||
const split = splitSvgIslands(svg)
|
||||
svg = split.svg
|
||||
elements = split.elements
|
||||
islands = split.islands
|
||||
} else {
|
||||
islands = countIslands(svg) || elements
|
||||
}
|
||||
|
||||
return { svg, ms: Math.round(now() - t0), paths: elements, elements, islands, levels }
|
||||
}
|
||||
Reference in new issue
Block a user