feat(image-vectorize): v0.2.1 精度可调 + 复制到剪贴板 + 高级默认展开
用户反馈三项: 1. 高级参数默认展开(不再藏起来),折叠按钮保留、展开状态记忆在 ctx.settings 2. 新增「复制 SVG」:一次写入 image/svg+xml + text/html + text/plain 三种口味, 粘到 Figma / Illustrator 直接是真矢量。Chrome 对可写 MIME 有白名单, 塞了不支持的类型会整次 write 失败,所以先 ClipboardItem.supports() 探测再逐级降级 3. 边缘精度可调:optTolerance 默认 0.2 → 0.05(三档 精细/标准/轻量), 大图降采样上限 2000 → 3000px(实测单色 3200px 96ms、灰度 4 层 3000px 179ms) 预览底部的说明改为胶囊提示(原 #75758a 裸文字在浅灰棋盘上几乎看不见), 面板间距压紧,高级区展开后整列参数在 980px 高下不用滚。 验证:本地 harness 38 项、真站 e2e 17 项(含真实剪贴板读回校验)全绿。
This commit is contained in:
1 parent
c4e4b15375
commit
d151de74a1
10 files changed
+367
-49
No files matched your search
@@ -24,20 +24,31 @@ export const MODES = ['gray', 'mono']
|
||||
export const LEVELS_MIN = 2
|
||||
export const LEVELS_MAX = 5
|
||||
|
||||
/** 描摹上限边(超过就降采样):potrace 是纯 JS 单线程,2000px 以上会明显卡主线程 */
|
||||
export const MAX_TRACE_SIDE = 2000
|
||||
/**
|
||||
* 描摹上限边(超过就降采样)。potrace 是纯 JS 单线程,实测:
|
||||
* 单色 3200px 96ms、灰度 4 层 3000px 179ms(3600px 已跳到 389ms,内存压力明显)。
|
||||
* 所以取 3000 —— 菜单/详情页这类大图的细节不再被降采样吃掉,同时仍在该帧内完成。
|
||||
*/
|
||||
export const MAX_TRACE_SIDE = 3000
|
||||
|
||||
export const DEFAULTS = {
|
||||
mode: 'gray', // 'gray' 灰度分层 / 'mono' 纯单色
|
||||
levels: 3, // 灰度层数(2–5,仅 gray)
|
||||
threshold: -1, // -1 = 自动(两种模式都支持)
|
||||
turdSize: 3, // 去小点:面积小于它的碎片直接丢(毛刺/噪点克星)
|
||||
alphaMax: 1, // 拐角平滑:0 全折线、1 默认、1.334 最圆
|
||||
alphaMax: 1, // 拐角阈值:0 全折线、1 默认、1.334 最圆
|
||||
// 曲线拟合容差(越小越贴合原图像素边缘,路径数据也越大)。
|
||||
// 上游默认 0.2 是「视觉上够用」的省数据值;实测 0.2 在文字笔画与圆角处会
|
||||
// 吃掉约半个像素的弯折,所以这里默认降到 0.05(用户 2026-11-05 要求「边缘精度再高一点」)。
|
||||
optTolerance: 0.05,
|
||||
invert: false, // 反相:描摹亮部(黑底白字场景)
|
||||
color: '#1a1a1a', // mono 的填充色
|
||||
outScale: 1, // 输出尺寸倍率(只放大尺寸,不重算曲线)
|
||||
}
|
||||
|
||||
/** 曲线精度三档(UI 用):越大越省路径数据,越小越贴合像素 */
|
||||
export const TOLERANCE_TONES = { fine: 0.05, normal: 0.15, light: 0.4 }
|
||||
|
||||
const clampInt = (v, min, max, dflt) => {
|
||||
const n = Math.round(Number(v))
|
||||
return Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : dflt
|
||||
@@ -55,6 +66,10 @@ export function normalizeParams(p) {
|
||||
const n = Number(src.alphaMax)
|
||||
return Number.isFinite(n) ? Math.min(1.334, Math.max(0, n)) : DEFAULTS.alphaMax
|
||||
})(),
|
||||
optTolerance: (() => {
|
||||
const n = Number(src.optTolerance)
|
||||
return Number.isFinite(n) ? Math.min(2, Math.max(0.01, n)) : DEFAULTS.optTolerance
|
||||
})(),
|
||||
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)),
|
||||
@@ -143,7 +158,7 @@ export function traceImageData(imageData, params) {
|
||||
turdSize: p.turdSize,
|
||||
alphaMax: p.alphaMax,
|
||||
optCurve: true,
|
||||
optTolerance: 0.2,
|
||||
optTolerance: p.optTolerance,
|
||||
blackOnWhite: !p.invert,
|
||||
color: p.color,
|
||||
})
|
||||
@@ -159,7 +174,7 @@ export function traceImageData(imageData, params) {
|
||||
turdSize: p.turdSize,
|
||||
alphaMax: p.alphaMax,
|
||||
optCurve: true,
|
||||
optTolerance: 0.2,
|
||||
optTolerance: p.optTolerance,
|
||||
blackOnWhite: !p.invert,
|
||||
fillStrategy: 'median', // 灰度:每层取该层亮度中位数
|
||||
color: '#000000', // 灰度靠 fill-opacity 叠出来,底色固定黑
|
||||
|
||||
@@ -19,8 +19,10 @@ var __IWB_VECTORIZE_MANIFEST__ = {
|
||||
threshold: { type: 'number', default: -1 },
|
||||
turdSize: { type: 'number', default: 3 },
|
||||
alphaMax: { type: 'number', default: 1 },
|
||||
optTolerance: { type: 'number', default: 0.05 },
|
||||
invert: { type: 'boolean', default: false },
|
||||
color: { type: 'string', default: '#1a1a1a' },
|
||||
outScale: { type: 'number', default: 2 }
|
||||
outScale: { type: 'number', default: 2 },
|
||||
advOpen: { type: 'boolean', default: true }
|
||||
},
|
||||
}
|
||||
@@ -132,12 +132,16 @@
|
||||
letter-spacing: 0.2px;
|
||||
}
|
||||
|
||||
/* 底部提示:浅灰棋盘上 #75758a 的裸文字几乎看不见(实测),改成深色胶囊 */
|
||||
.vz-hint {
|
||||
position: absolute;
|
||||
bottom: 14px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
color: #75758a;
|
||||
padding: 5px 11px;
|
||||
border-radius: 999px;
|
||||
background: rgba(22, 22, 30, 0.8);
|
||||
color: #dcdce8;
|
||||
font-size: 11px;
|
||||
pointer-events: none;
|
||||
white-space: nowrap;
|
||||
@@ -176,7 +180,7 @@
|
||||
.vz-panel {
|
||||
flex: 0 0 auto;
|
||||
width: 312px;
|
||||
padding: 14px 16px 18px;
|
||||
padding: 12px 15px 14px;
|
||||
background: #1e1e26;
|
||||
border-left: 1px solid #2e2e3a;
|
||||
overflow-y: auto;
|
||||
@@ -185,7 +189,7 @@
|
||||
.vz-panel::-webkit-scrollbar { width: 8px; }
|
||||
.vz-panel::-webkit-scrollbar-thumb { background: #3a3a48; border-radius: 4px; }
|
||||
|
||||
.vz-group + .vz-group { margin-top: 18px; padding-top: 16px; border-top: 1px solid #2b2b36; }
|
||||
.vz-group + .vz-group { margin-top: 14px; padding-top: 12px; border-top: 1px solid #2b2b36; }
|
||||
|
||||
.vz-group-title {
|
||||
font-size: 11px;
|
||||
@@ -206,7 +210,7 @@
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
padding: 8px 9px;
|
||||
padding: 7px 9px;
|
||||
border: 1px solid #37374a;
|
||||
border-radius: 8px;
|
||||
background: #262631;
|
||||
@@ -229,7 +233,7 @@
|
||||
.vz-preset.active small { color: #79b79b; }
|
||||
|
||||
/* 分段控件 */
|
||||
.vz-row { margin-bottom: 11px; }
|
||||
.vz-row { margin-bottom: 9px; }
|
||||
/* 按模式隐藏的行(层数只在灰度、阈值只在手动)必须真的消失。
|
||||
踩过的坑:flex/块级元素上加 `hidden` 属性,如果 CSS 里另有 display 声明就会失效,
|
||||
于是「隐藏」只变成视觉上还在 —— 这里直接 !important 兜住所有形态。 */
|
||||
|
||||
@@ -12,11 +12,11 @@
|
||||
*/
|
||||
'use strict'
|
||||
|
||||
import { DEFAULTS, LEVELS_MIN, LEVELS_MAX, normalizeParams } from './engine.js'
|
||||
import { DEFAULTS, LEVELS_MIN, LEVELS_MAX, TOLERANCE_TONES, normalizeParams } from './engine.js'
|
||||
import { loadImage, prepareSource, svgToPngBlob, svgStats } from './trace.js'
|
||||
import { traceImageData } from './engine.js'
|
||||
|
||||
const SETTING_KEYS = ['preset', 'mode', 'levels', 'threshold', 'turdSize', 'alphaMax', 'invert', 'color', 'outScale']
|
||||
const SETTING_KEYS = ['preset', 'mode', 'levels', 'threshold', 'turdSize', 'alphaMax', 'optTolerance', 'invert', 'color', 'outScale', 'advOpen']
|
||||
|
||||
/** 三个预设:都围绕「自己出图」的实际场景,不放没用的组合 */
|
||||
const PRESETS = {
|
||||
@@ -52,7 +52,7 @@ function rangeHTML(name, label, min, max, step, value, hint) {
|
||||
'</div></div>'
|
||||
}
|
||||
|
||||
function panelHTML(params, preset, outScale) {
|
||||
function panelHTML(params, preset, outScale, advOpen) {
|
||||
const presets = Object.keys(PRESETS).map(function (key) {
|
||||
const p = PRESETS[key]
|
||||
return '<button type="button" class="vz-preset' + (key === preset ? ' active' : '') + '" data-preset="' + key + '">' +
|
||||
@@ -72,10 +72,14 @@ function panelHTML(params, preset, outScale) {
|
||||
|
||||
'<div class="vz-group">' +
|
||||
rangeHTML('turdSize', '去小点', 0, 40, 1, params.turdSize, HINTS.turdSize) +
|
||||
'<div class="vz-row"><div class="vz-row-label">边缘</div><div class="vz-seg" data-seg-group="alphaTone">' +
|
||||
'<div class="vz-row"><div class="vz-row-label">边缘<span>拐角处理</span></div><div class="vz-seg" data-seg-group="alphaTone">' +
|
||||
'<button type="button" data-seg="alphaTone" data-value="hard">硬朗</button>' +
|
||||
'<button type="button" data-seg="alphaTone" data-value="normal">标准</button>' +
|
||||
'<button type="button" data-seg="alphaTone" data-value="round">圆润</button></div></div>' +
|
||||
'<div class="vz-row"><div class="vz-row-label">精度<span>越高越贴合原图,曲线数据略多</span></div><div class="vz-seg" data-seg-group="precision">' +
|
||||
'<button type="button" data-seg="precision" data-value="fine">精细</button>' +
|
||||
'<button type="button" data-seg="precision" data-value="normal">标准</button>' +
|
||||
'<button type="button" data-seg="precision" data-value="light">轻量</button></div></div>' +
|
||||
'</div>' +
|
||||
|
||||
'<div class="vz-group">' +
|
||||
@@ -94,8 +98,8 @@ function panelHTML(params, preset, outScale) {
|
||||
'</div>' +
|
||||
|
||||
'<div class="vz-group">' +
|
||||
'<button type="button" class="vz-adv-toggle" data-act="adv" aria-expanded="false">高级参数<span data-adv-caret>▾</span></button>' +
|
||||
'<div class="vz-adv" data-adv hidden>' +
|
||||
'<button type="button" class="vz-adv-toggle" data-act="adv" aria-expanded="' + (advOpen ? 'true' : 'false') + '">高级参数<span data-adv-caret>' + (advOpen ? '▴' : '▾') + '</span></button>' +
|
||||
'<div class="vz-adv" data-adv' + (advOpen ? '' : ' hidden') + '>' +
|
||||
'<div class="vz-row"><div class="vz-row-label">阈值<span>自动按直方图取,手动可救极端图</span></div><div class="vz-seg" data-seg-group="thrMode">' +
|
||||
'<button type="button" data-seg="thrMode" data-value="auto">自动</button>' +
|
||||
'<button type="button" data-seg="thrMode" data-value="manual">手动</button></div></div>' +
|
||||
@@ -104,7 +108,6 @@ function panelHTML(params, preset, outScale) {
|
||||
segHTML('invert', [['0', '正片'], ['1', '负片']], params.invert ? 1 : 0) + '</div></div>' +
|
||||
'<div class="vz-row"><div class="vz-row-label">单色填充<span>仅「纯单色」生效</span></div>' +
|
||||
'<input type="color" class="vz-color" data-color="color" value="' + esc(params.color) + '"></div>' +
|
||||
'<div class="vz-note">矢量结果可无限放大不失真。「保存为节点」输出 PNG(按输出倍率重绘),要真矢量文件用「下载 SVG」。</div>' +
|
||||
'</div>' +
|
||||
'</div>'
|
||||
}
|
||||
@@ -117,6 +120,7 @@ function viewHTML(name) {
|
||||
'<span class="vz-title">位图转矢量<em data-meta>' + esc(name) + '</em></span>' +
|
||||
'<div class="vz-spacer"></div>' +
|
||||
'<button type="button" class="vz-btn" data-act="cancel">取消</button>' +
|
||||
'<button type="button" class="vz-btn" data-act="copy">复制 SVG</button>' +
|
||||
'<button type="button" class="vz-btn" data-act="svg">下载 SVG</button>' +
|
||||
'<button type="button" class="vz-btn vz-btn-primary" data-act="save">保存为节点</button>' +
|
||||
'</header>' +
|
||||
@@ -126,7 +130,7 @@ function viewHTML(name) {
|
||||
'<img class="vz-stage" data-stage="original" alt="原图" hidden>' +
|
||||
'<div class="vz-badge" data-badge>矢量预览</div>' +
|
||||
'<div class="vz-status" data-status hidden><i class="vz-dot"></i><span data-status-text></span></div>' +
|
||||
'<div class="vz-hint">按住空格键对比原图</div>' +
|
||||
'<div class="vz-hint">空格 对比原图 · 「复制 SVG」可直接粘到 Figma</div>' +
|
||||
'</div>' +
|
||||
'<aside class="vz-panel" data-panel></aside>' +
|
||||
'</div></div></div>'
|
||||
@@ -135,6 +139,8 @@ function viewHTML(name) {
|
||||
/** 边缘预设 → alphaMax(越圆 alphaMax 越大) */
|
||||
const ALPHA_TONE = { hard: 0.4, normal: 1, round: 1.2 }
|
||||
const toneOf = (alphaMax) => (alphaMax <= 0.6 ? 'hard' : alphaMax >= 1.1 ? 'round' : 'normal')
|
||||
/** 容差 → 精度档(0.05 精细 / 0.15 标准 / 0.4 轻量) */
|
||||
const tolToneOf = (tol) => (tol <= 0.08 ? 'fine' : tol >= 0.3 ? 'light' : 'normal')
|
||||
|
||||
export function createVectorizer(options) {
|
||||
const settings = options.settings
|
||||
@@ -148,6 +154,7 @@ export function createVectorizer(options) {
|
||||
let params = normalizeParams(DEFAULTS)
|
||||
let preset = 'text'
|
||||
let outScale = 2
|
||||
let advOpen = true
|
||||
let currentSvg = ''
|
||||
let currentBlobUrl = ''
|
||||
let seq = 0
|
||||
@@ -164,6 +171,7 @@ export function createVectorizer(options) {
|
||||
try { value = settings.get(key) } catch (e) { continue }
|
||||
if (value === undefined || value === null || value === '') continue
|
||||
if (key === 'invert') value = value === true || value === 'true' || value === 1 || value === '1'
|
||||
if (key === 'advOpen') value = !(value === false || value === 'false' || value === 0 || value === '0')
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
@@ -178,9 +186,11 @@ export function createVectorizer(options) {
|
||||
settings.set('threshold', params.threshold)
|
||||
settings.set('turdSize', params.turdSize)
|
||||
settings.set('alphaMax', params.alphaMax)
|
||||
settings.set('optTolerance', params.optTolerance)
|
||||
settings.set('invert', params.invert)
|
||||
settings.set('color', params.color)
|
||||
settings.set('outScale', outScale)
|
||||
settings.set('advOpen', advOpen)
|
||||
} catch (e) { /* 记忆失败不影响主流程 */ }
|
||||
}
|
||||
|
||||
@@ -236,6 +246,9 @@ export function createVectorizer(options) {
|
||||
panel.querySelectorAll('[data-seg-group="alphaTone"] button').forEach(function (btn) {
|
||||
btn.classList.toggle('active', btn.dataset.value === toneOf(params.alphaMax))
|
||||
})
|
||||
panel.querySelectorAll('[data-seg-group="precision"] button').forEach(function (btn) {
|
||||
btn.classList.toggle('active', btn.dataset.value === tolToneOf(params.optTolerance))
|
||||
})
|
||||
panel.querySelectorAll('[data-seg-group="invert"] button').forEach(function (btn) {
|
||||
btn.classList.toggle('active', (btn.dataset.value === '1') === params.invert)
|
||||
})
|
||||
@@ -314,6 +327,81 @@ export function createVectorizer(options) {
|
||||
debounceTimer = window.setTimeout(function () { writeSettings(); retrace() }, 120)
|
||||
}
|
||||
|
||||
// ---------- 复制到剪贴板 ----------
|
||||
|
||||
/**
|
||||
* 把 SVG 放进剪贴板,尽量让设计软件(Figma / Illustrator / Sketch)认成**矢量**而不是文本。
|
||||
*
|
||||
* 三种「口味」各有用途,缺一不可:
|
||||
* - `image/svg+xml`:真正的矢量 MIME,Illustrator / Inkscape 直接吃;
|
||||
* - `text/html` 里带 `<svg>`:Figma 走 HTML 解析分支时会认成矢量;
|
||||
* - `text/plain`:兜底(也有软件只读纯文本里的 SVG 源码)。
|
||||
* Chrome 对可写 MIME 有白名单,塞了不支持的类型会**整次 write 失败**,
|
||||
* 所以先用 `ClipboardItem.supports()` 探测,失败再逐级降级。
|
||||
*/
|
||||
async function writeSvgToClipboard(svgText) {
|
||||
const nav = typeof navigator === 'undefined' ? null : navigator
|
||||
const Item = typeof window === 'undefined' ? null : window.ClipboardItem
|
||||
const plain = new Blob([svgText], { type: 'text/plain' })
|
||||
const html = new Blob([svgText], { type: 'text/html' })
|
||||
let svgFlavor = false
|
||||
|
||||
if (nav && nav.clipboard && typeof nav.clipboard.write === 'function' && typeof Item === 'function') {
|
||||
try {
|
||||
svgFlavor = typeof Item.supports !== 'function' || Item.supports('image/svg+xml')
|
||||
} catch (e) {
|
||||
svgFlavor = false
|
||||
}
|
||||
const tryWrite = async function (withSvg) {
|
||||
const flavors = { 'text/plain': plain, 'text/html': html }
|
||||
if (withSvg) flavors['image/svg+xml'] = new Blob([svgText], { type: 'image/svg+xml' })
|
||||
await nav.clipboard.write([new Item(flavors)])
|
||||
}
|
||||
try {
|
||||
await tryWrite(svgFlavor)
|
||||
return svgFlavor ? 'svg' : 'text'
|
||||
} catch (error) {
|
||||
// 某个口味被拒会让整次写入失败:去掉矢量口味再试一次
|
||||
if (svgFlavor) {
|
||||
try {
|
||||
await tryWrite(false)
|
||||
return 'text'
|
||||
} catch (error2) { /* 落到 writeText */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nav && nav.clipboard && typeof nav.clipboard.writeText === 'function') {
|
||||
await nav.clipboard.writeText(svgText)
|
||||
return 'text'
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
async function copySvg(button) {
|
||||
if (!currentSvg) { setStatus('error', '还没有可复制的矢量结果'); return }
|
||||
const label = button.textContent
|
||||
button.disabled = true
|
||||
try {
|
||||
const how = await writeSvgToClipboard(currentSvg)
|
||||
if (!how) throw new Error('当前环境不允许写剪贴板')
|
||||
button.textContent = '已复制'
|
||||
setStatus('busy', how === 'svg'
|
||||
? '已复制矢量,直接粘到 Figma / Illustrator 即可'
|
||||
: '已复制 SVG 源码(粘不进去的话改用「下载 SVG」再拖入)')
|
||||
const text = els ? els.statusText.textContent : ''
|
||||
window.setTimeout(function () {
|
||||
if (els && els.statusText.textContent === text) setStatus(null)
|
||||
}, 2600)
|
||||
} catch (error) {
|
||||
setStatus('error', '复制失败:' + (error && error.message ? error.message : String(error)))
|
||||
} finally {
|
||||
window.setTimeout(function () {
|
||||
if (els) { button.disabled = false; button.textContent = label }
|
||||
}, 1200)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 输出 ----------
|
||||
|
||||
function downloadSvg() {
|
||||
@@ -379,9 +467,11 @@ export function createVectorizer(options) {
|
||||
if (box) {
|
||||
const open = box.hidden
|
||||
box.hidden = !open
|
||||
advOpen = open
|
||||
adv.setAttribute('aria-expanded', String(open))
|
||||
const caret = adv.querySelector('[data-adv-caret]')
|
||||
if (caret) caret.textContent = open ? '▴' : '▾'
|
||||
writeSettings()
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -411,6 +501,9 @@ export function createVectorizer(options) {
|
||||
} else if (name === 'alphaTone') {
|
||||
params.alphaMax = ALPHA_TONE[value] === undefined ? 1 : ALPHA_TONE[value]
|
||||
preset = ''
|
||||
} else if (name === 'precision') {
|
||||
params.optTolerance = TOLERANCE_TONES[value] === undefined ? DEFAULTS.optTolerance : TOLERANCE_TONES[value]
|
||||
preset = ''
|
||||
} else if (name === 'invert') {
|
||||
params.invert = value === '1'
|
||||
preset = ''
|
||||
@@ -434,6 +527,7 @@ export function createVectorizer(options) {
|
||||
if (!act) return
|
||||
const name = act.dataset.act
|
||||
if (name === 'cancel') { close(); return }
|
||||
if (name === 'copy') { copySvg(act); return }
|
||||
if (name === 'svg') { downloadSvg(); return }
|
||||
if (name === 'save') { saveNode(act); return }
|
||||
}
|
||||
@@ -489,6 +583,7 @@ export function createVectorizer(options) {
|
||||
preset = PRESETS[stored.preset] ? stored.preset : 'text'
|
||||
params = normalizeParams(Object.assign({}, DEFAULTS, PRESETS[preset], stored))
|
||||
outScale = Math.max(1, Math.min(4, Math.round(Number(stored.outScale) || 2)))
|
||||
advOpen = stored.advOpen === undefined ? true : Boolean(stored.advOpen)
|
||||
|
||||
const img = await loadImage(request.imageDataUrl)
|
||||
const name = String(request.name || 'image.png')
|
||||
@@ -511,7 +606,7 @@ export function createVectorizer(options) {
|
||||
}
|
||||
els.original.src = request.imageDataUrl
|
||||
els.meta.textContent = name + ' · ' + source.srcW + '×' + source.srcH
|
||||
els.panel.innerHTML = panelHTML(params, preset, outScale)
|
||||
els.panel.innerHTML = panelHTML(params, preset, outScale, advOpen)
|
||||
|
||||
root.addEventListener('click', onRootClick)
|
||||
els.panel.addEventListener('input', onPanelInput)
|
||||
|
||||
Reference in new issue
Block a user