@xenra/dsh-draworld 0.1.38 → 0.1.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -7
- package/index.js +98 -11
- package/package.json +1 -1
- package/pixelops.js +16 -1
package/README.md
CHANGED
|
@@ -16,9 +16,17 @@
|
|
|
16
16
|
|
|
17
17
|
## 安装
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
插件没有第三方依赖(纯 ESM,渲染在浏览器里做)。三种装法:
|
|
20
20
|
|
|
21
|
-
**A.
|
|
21
|
+
**A. 从 npm 装(推荐:管理页会显示「版本 / 可更新到 x」,能一键更新)**
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
@xenra/dsh-draworld
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
装完在插件详情里就有「版本」区块(`可更新到 x.y.z` / `已是最新` / `上次检查于`)和原生「更新」按钮——这块只有注册表安装才渲染,git 安装没有。
|
|
28
|
+
|
|
29
|
+
**B. 从仓库地址装(便于改源码,但没有管理页的版本区块)**
|
|
22
30
|
|
|
23
31
|
在 DeepSeek Harness 的插件管理里填安装地址,任选其一:
|
|
24
32
|
|
|
@@ -28,17 +36,18 @@ https://github.com/RUO-MO/dsh-draworld
|
|
|
28
36
|
```
|
|
29
37
|
|
|
30
38
|
插件管理器会先用 `git ls-remote` 检查仓库连通性,再用 pnpm 装源码(仓库无构建脚本,`--ignore-scripts` 即可)。
|
|
31
|
-
需要本机有 `git`、能访问 `github.com`;**用 https,不要用 ssh**——很多网络下 22 端口被拒。
|
|
39
|
+
需要本机有 `git`、能访问 `github.com`;**用 https,不要用 ssh**——很多网络下 22 端口被拒。git 装的情况下更新走画界自己的一键更新(见下)。
|
|
32
40
|
|
|
33
|
-
**
|
|
41
|
+
**C. 从本地目录装**
|
|
34
42
|
|
|
35
43
|
1. 下载 [最新 Release](https://github.com/RUO-MO/dsh-draworld/releases) 的 zip 解压,或 `git clone https://github.com/RUO-MO/dsh-draworld.git`。
|
|
36
44
|
2. 插件管理里添加这个目录。
|
|
37
45
|
|
|
38
46
|
装完都要**停用再启用**(宿主代码生效);只改界面代码时 `Ctrl+R` 刷新页面即可。
|
|
39
|
-
想确认装的是新版本:打开画界后访问 `http://127.0.0.1:<DSH端口>/canvas-api/diagnostics`,`api=3`、`scope.mode=session
|
|
47
|
+
想确认装的是新版本:打开画界后访问 `http://127.0.0.1:<DSH端口>/canvas-api/diagnostics`,`api=3`、`scope.mode=session`;
|
|
48
|
+
不确定端口就打开画界右列的「版本」卡,或直接读 `<数据目录>/endpoint.json`(里面有 `origin`/`port`/`pid`/`version`)。
|
|
40
49
|
|
|
41
|
-
> **更新**:
|
|
50
|
+
> **更新**:npm 装的,管理页自己会查、点「更新」即可;git 装的,画界右列「版本」卡里有「检查更新 / 一键更新」(走宿主 `pluginManager.updateBundle`,按 tag 重装)。两条路都不用重新找安装地址。
|
|
42
51
|
|
|
43
52
|
> 插件管理按 `package.json` 的 `version` 判断版本。本地目录装了插件、文件改了但版本号没动,它不认——升级请先改版本号。
|
|
44
53
|
|
|
@@ -184,13 +193,16 @@ https://github.com/RUO-MO/dsh-draworld
|
|
|
184
193
|
|
|
185
194
|
- **前端**:浏览器早就把参考图解码进 `<canvas>` 了,最快。宿主把任务塞队列,前端 700ms 轮询 `/canvas-api/jobs` 取走,算完 POST `/canvas-api/job`,宿主落盘成 PNG 或转成折线/数值交给模型。
|
|
186
195
|
- **宿主侧(0.1.35 起)**:面板没开(`/jobs` 2.5 秒没人来取)时,宿主自己算——`pixels.js` 是纯 JS 的 PNG 解码/编码(8bit,colorType 0/2/4/6,不支持调色板/隔行/16bit)+ 光栅化 + marching squares / Zhang-Suen 骨架 / RDP / Chaikin / 三次贝塞尔,`pixelops.js` 跑 crop / ref / pick / palette / trace / copy / fill / compare。所以「不开界面也能 `copy` → `fix`」这条已经通了,不再 5 秒超时。非 PNG 参考图(JPEG 等)会用 PowerShell 的 `System.Drawing` 转一次临时 PNG 再读。
|
|
187
|
-
-
|
|
196
|
+
- 宿主算不出来才回落到队列,超时默认 5 秒(`trace`/`copy`/`fill`/`compare`/`palette` 这类整幅重活 30 秒,`heavyTimeout` 可调)、话术直说「宿主侧没算成、面板也没接任务」。不想让宿主插手、只走浏览器:插件配置写 `headless: false`(也可 `POST /canvas-api/config {"headless": false}` 临时切)。
|
|
197
|
+
- **`trace` 带调参反馈(0.1.39 起)**:返回体里多了 `stats` —— `inkRatio`(墨迹占比,判断阈值是不是把阴影也吃进来了)、`widthAvg/Min/Max`(量出来的线宽,拿来设笔刷 size)、`pointsPerLine`(重采样疏密,对应 `fitStep`/`step`)、`droppedPct`(被 `minLength` 丢掉的短段比例)。调 `threshold`/`tolerance`/`fitStep` 时看这几个数,不用只靠肉眼比成品。
|
|
188
198
|
- **图片类型按内容判**:`import` 不看扩展名,读魔数认 PNG / JPEG / WebP / GIF,缓存名统一是 `img-<stamp>-<rand>.<真扩展名>`;`ref` 遇到非 PNG 会先转一份 `views/ref-…-<mtime>.png` 再交给 `read_image`(同一文件按 mtime 复用,原文件保留不动)。
|
|
189
199
|
|
|
190
200
|
**填色边界**:`copy` 走中心线时,色块另跑一遍边界追踪取闭合轮廓(骨架不闭合),洞按嵌套层数 even-odd 挖掉;`fill` 的泛洪边界只认笔迹(不含已填色块),区域撞到「超过画布 62%」会拒填并提示先有线稿或用 `rect` 收窄。
|
|
191
201
|
|
|
192
202
|
**会话绑定**:工具和画布协议挂在 `agent.ctx` 上,只有打开过画界的会话才有。打开画界时前端 POST `/canvas-api/attach`(带 `sessionId`),关掉 15 秒后 `/detach`。attach 会先按 id 找 agent(`get` 找不到就从列表里翻),找不到就每秒重试、最多 20 秒;20 秒还拿不到就**退回全局注册**(`diagnostics.scope.fallback = "attach-pending"`),保证工具至少能用。想直接退回「所有会话都有」:插件配置写 `toolScope: "global"`(不推荐,会把正在写代码的会话带偏)。
|
|
193
203
|
|
|
204
|
+
**服务是懒注入的(0.1.39 修)**:`tools` / `systemPrompt` / `agents` 不能在 `apply()` 里 `ctx.get` 一次就算——DSH 的 cordis 容器按需注入,插件加载那一刻这几个常常还是 `undefined`,于是 `paint_canvas` 永远挂不上(`diagnostics.tools.service:false`)。现在改成 `ctx.inject(["tools"|"systemPrompt"|"agents"], cb)`,服务一到位就把 `wanted` 里的会话重绑一遍,所以「先开会话、后开面板」也不会再丢工具。诊断里新增 `services`(四个服务到没到位)、`tools.bindError`(绑不上的具体原因)、`endpoint`、`timeouts`,出问题不用再猜。
|
|
205
|
+
|
|
194
206
|
**单一文档**:同一缓存目录只保留一个内存文档(`Document.forDir`,按目录缓存到 `globalThis`),插件被重复加载也不会出现两份各画各的;每次请求与每次工具执行前都核对 `doc.json` mtime,别的窗口刚画过就整份换成更新的那版,避免「模型画了但界面不更新」。
|
|
195
207
|
|
|
196
208
|
## HTTP 接口
|
|
@@ -211,6 +223,7 @@ POST /canvas-api/detach 解绑(15 秒宽限)
|
|
|
211
223
|
GET /canvas-api/jobs 前端取像素任务(crop / ref / pick / compare / palette)
|
|
212
224
|
POST /canvas-api/job 前端回传任务结果(body: id, result)
|
|
213
225
|
GET /canvas-api/update 版本自检(`?force=1` 立刻查一次)
|
|
226
|
+
GET /canvas-api/endpoint 当前端点(origin/port/pid/version/dataDir),也落盘成 `<数据目录>/endpoint.json`
|
|
214
227
|
GET /canvas-api/image/<file> 取素材原字节
|
|
215
228
|
GET /canvas-api/diagnostics 工具/路由/文档/绑定/任务队列/版本状态
|
|
216
229
|
```
|
package/index.js
CHANGED
|
@@ -1185,9 +1185,12 @@ function shapeToPoints(shape, points) {
|
|
|
1185
1185
|
}
|
|
1186
1186
|
|
|
1187
1187
|
export function apply(ctx, config = {}) {
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1188
|
+
// 服务不能在 apply 时直接 get:DSH 的 cordis 容器是按需注入的,
|
|
1189
|
+
// ctx.get("tools") 在插件加载那一刻经常还是 undefined(0.1.39 前一直因此挂不上工具)。
|
|
1190
|
+
// 先在 get 里碰碰运气,真正的来源走下面的 ctx.inject([...])。
|
|
1191
|
+
let tool = ctx.get ? ctx.get("tools") : ctx.tools;
|
|
1192
|
+
let prompt = ctx.get ? ctx.get("systemPrompt") : ctx.systemPrompt;
|
|
1193
|
+
let agents = ctx.get ? ctx.get("agents") : ctx.agents;
|
|
1191
1194
|
let webServer = ctx.get ? ctx.get("webServer") : ctx.webServer;
|
|
1192
1195
|
const rootDir = path.resolve(config.dir || path.join(process.cwd(), ".dsh-draworld"));
|
|
1193
1196
|
const paths = new PathSettings(rootDir);
|
|
@@ -1204,6 +1207,12 @@ export function apply(ctx, config = {}) {
|
|
|
1204
1207
|
scope: config.toolScope === "global" ? "global" : "session",
|
|
1205
1208
|
// 会话补绑 20 秒还没成功,会退回全局注册并把原因记在这(诊断里可见)
|
|
1206
1209
|
scopeFallback: null,
|
|
1210
|
+
// 各服务到没到位:诊断里直接列出来,省得靠 bound:false 猜
|
|
1211
|
+
services: { tools: false, systemPrompt: false, agents: false, webServer: false },
|
|
1212
|
+
// 最近一次绑定失败的原因(resolveAgent 找不到 / register 抛错)
|
|
1213
|
+
bindError: null,
|
|
1214
|
+
// 端点信息:GUI 端口每次重启都可能变,写进 endpoint.json 给脚本用
|
|
1215
|
+
endpoint: null,
|
|
1207
1216
|
// 版本自检:插件自己去 GitHub 看有没有新 Release,界面上给个提示
|
|
1208
1217
|
update: { checkedAt: null, latest: null, hasUpdate: false, url: null, error: null, enabled: config.updateCheck !== false },
|
|
1209
1218
|
};
|
|
@@ -1213,6 +1222,11 @@ export function apply(ctx, config = {}) {
|
|
|
1213
1222
|
// 宿主这边没有图像解码依赖,硬塞 @napi-rs/canvas 会让安装变重。
|
|
1214
1223
|
// 流程:工具 execute 把任务塞进队列 → 前端 700ms 轮询取走 → 算完 POST 回来 → 这里转成工具输出。
|
|
1215
1224
|
const PIXEL_TIMEOUT_MS = Math.max(1500, Number(config.pixelTimeout || 5000));
|
|
1225
|
+
// 整幅(1080×1440 这种)做骨架细化+折线拼接是重活,5 秒根本不够:
|
|
1226
|
+
// 给这几类单独一档超时,默认 30 秒,可用 heavyTimeout 调。
|
|
1227
|
+
const HEAVY_KINDS = new Set(["trace", "copy", "fill", "compare", "palette"]);
|
|
1228
|
+
const HEAVY_TIMEOUT_MS = Math.max(PIXEL_TIMEOUT_MS, Number(config.heavyTimeout || 30000));
|
|
1229
|
+
const timeoutFor = (kind) => (HEAVY_KINDS.has(kind) ? HEAVY_TIMEOUT_MS : PIXEL_TIMEOUT_MS);
|
|
1216
1230
|
const jobs = new Map();
|
|
1217
1231
|
let jobSeq = 0;
|
|
1218
1232
|
const jobStats = { done: 0, timeout: 0, failed: 0, queued: 0, headless: 0, last: null };
|
|
@@ -1249,7 +1263,7 @@ export function apply(ctx, config = {}) {
|
|
|
1249
1263
|
function enqueue() {
|
|
1250
1264
|
jobSeq += 1;
|
|
1251
1265
|
const id = `j${jobSeq}`;
|
|
1252
|
-
const limit = Math.max(1500, Number(timeoutMs) ||
|
|
1266
|
+
const limit = Math.max(1500, Number(timeoutMs) || timeoutFor(kind));
|
|
1253
1267
|
const timer = setTimeout(() => {
|
|
1254
1268
|
if (!jobs.has(id)) return;
|
|
1255
1269
|
jobs.delete(id);
|
|
@@ -1404,7 +1418,7 @@ export function apply(ctx, config = {}) {
|
|
|
1404
1418
|
|
|
1405
1419
|
/** 兜底把工具挂到插件全局:会话补绑失败时至少还能用(诊断里会标 scopeFallback)。 */
|
|
1406
1420
|
const enableGlobalFallback = (reason) => {
|
|
1407
|
-
if (state.scope === "global") return false;
|
|
1421
|
+
if (state.scope === "global" && state.toolOk) return false;
|
|
1408
1422
|
state.scope = "global";
|
|
1409
1423
|
state.scopeFallback = String(reason || "attach-pending");
|
|
1410
1424
|
if (prompt) {
|
|
@@ -1472,6 +1486,11 @@ export function apply(ctx, config = {}) {
|
|
|
1472
1486
|
}
|
|
1473
1487
|
bound.set(agent.id, { agent, disposers: list });
|
|
1474
1488
|
state.toolOk = list.length > 0;
|
|
1489
|
+
if (list.length === 0) {
|
|
1490
|
+
state.bindError = "agent.ctx 上没有 tools/systemPrompt 的注册口(宿主版本不同?)";
|
|
1491
|
+
} else {
|
|
1492
|
+
state.bindError = null;
|
|
1493
|
+
}
|
|
1475
1494
|
return true;
|
|
1476
1495
|
};
|
|
1477
1496
|
|
|
@@ -1502,6 +1521,7 @@ export function apply(ctx, config = {}) {
|
|
|
1502
1521
|
wanted.add(id);
|
|
1503
1522
|
const agent = resolveAgent(id);
|
|
1504
1523
|
const didBind = agent ? bindAgent(agent) : false;
|
|
1524
|
+
if (!didBind) state.bindError = `找不到会话 ${id} 的 agent(agents 服务未就绪或 id 不是 agent id)`;
|
|
1505
1525
|
if (!didBind) {
|
|
1506
1526
|
// 会话在 attach 之前就建好了、又拿不到 agent:定时重试,20 秒还没成再退到全局
|
|
1507
1527
|
if (!bindRetries.has(id)) {
|
|
@@ -1581,6 +1601,26 @@ export function apply(ctx, config = {}) {
|
|
|
1581
1601
|
state.routeError = `getOrigin 失败: ${(err && err.message) || err}`;
|
|
1582
1602
|
return;
|
|
1583
1603
|
}
|
|
1604
|
+
// 端口每个进程都可能变(59311 → 54578 这种):把端点写进数据目录,
|
|
1605
|
+
// 外部脚本读 endpoint.json 就行,不用再去猜端口。
|
|
1606
|
+
try {
|
|
1607
|
+
const info = {
|
|
1608
|
+
ok: true,
|
|
1609
|
+
api: 3,
|
|
1610
|
+
plugin: name,
|
|
1611
|
+
version: readLocalVersion(),
|
|
1612
|
+
origin: state.origin,
|
|
1613
|
+
port: Number((String(state.origin).match(/:(\d+)/) || [])[1] || 0),
|
|
1614
|
+
pid: process.pid,
|
|
1615
|
+
dataDir: dir,
|
|
1616
|
+
updatedAt: Date.now(),
|
|
1617
|
+
};
|
|
1618
|
+
state.endpoint = info;
|
|
1619
|
+
mkdirSync(dir, { recursive: true });
|
|
1620
|
+
writeFileSync(path.join(dir, "endpoint.json"), JSON.stringify(info, null, 2), "utf8");
|
|
1621
|
+
} catch {
|
|
1622
|
+
/* 写不了就只留在内存里,诊断还能看到 */
|
|
1623
|
+
}
|
|
1584
1624
|
try {
|
|
1585
1625
|
const dispose = webServer.register({
|
|
1586
1626
|
kind: "prefix",
|
|
@@ -1610,15 +1650,23 @@ export function apply(ctx, config = {}) {
|
|
|
1610
1650
|
return;
|
|
1611
1651
|
}
|
|
1612
1652
|
|
|
1653
|
+
// 端点:脚本先问这里(或者直接读 <数据目录>/endpoint.json),别去猜端口
|
|
1654
|
+
if (route === "/endpoint" && req.method === "GET") {
|
|
1655
|
+
json(res, 200, { ok: true, ...(state.endpoint || { origin: state.origin, dataDir: dir }) });
|
|
1656
|
+
return;
|
|
1657
|
+
}
|
|
1613
1658
|
if (route === "/diagnostics" && req.method === "GET") {
|
|
1614
1659
|
json(res, 200, {
|
|
1615
1660
|
ok: true,
|
|
1616
1661
|
api: 3,
|
|
1617
1662
|
plugin: "@xenra/dsh-draworld",
|
|
1618
1663
|
brushes: BRUSH_IDS.length,
|
|
1619
|
-
tools: { service: Boolean(tool), ok: state.toolOk, error: state.toolError },
|
|
1664
|
+
tools: { service: Boolean(tool), ok: state.toolOk, error: state.toolError, bindError: state.bindError },
|
|
1665
|
+
services: state.services,
|
|
1620
1666
|
scope: { mode: state.scope, fallback: state.scopeFallback, attached: [...wanted], bound: [...bound.keys()], retries: [...bindRetries.entries()] },
|
|
1621
1667
|
routes: { error: state.routeError, origin: state.origin },
|
|
1668
|
+
endpoint: state.endpoint,
|
|
1669
|
+
timeouts: { light: PIXEL_TIMEOUT_MS, heavy: HEAVY_TIMEOUT_MS },
|
|
1622
1670
|
doc: {
|
|
1623
1671
|
dir,
|
|
1624
1672
|
revision: docs.revision,
|
|
@@ -2713,7 +2761,9 @@ export function apply(ctx, config = {}) {
|
|
|
2713
2761
|
smooth: job.smooth === undefined ? null : job.smooth,
|
|
2714
2762
|
fit: job.fit === undefined ? null : job.fit,
|
|
2715
2763
|
paths: job.paths,
|
|
2764
|
+
stats: job.stats,
|
|
2716
2765
|
hint: "paths 是从参考图抽出的轮廓折线(画布坐标),每段直接当 draw 的 points 用;长图可分批 batches=[…]。"
|
|
2766
|
+
+ "stats 给的是 inkRatio/widthAvg/widthMin/widthMax/droppedPct,调 threshold/tolerance/fitStep 时看这几个数,不用只靠肉眼。"
|
|
2717
2767
|
+ "描完用 view rect+zoom 对照,再用 compare 看 coveragePct。",
|
|
2718
2768
|
};
|
|
2719
2769
|
}
|
|
@@ -2779,18 +2829,55 @@ export function apply(ctx, config = {}) {
|
|
|
2779
2829
|
isConcurrencySafe: () => false,
|
|
2780
2830
|
};
|
|
2781
2831
|
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2832
|
+
let globalRegistered = false;
|
|
2833
|
+
|
|
2834
|
+
/** 服务到位后统一收口:刷新诊断字段、把等着绑的会话重绑一遍、必要时补全局注册。 */
|
|
2835
|
+
const onServicesReady = () => {
|
|
2836
|
+
state.services.tools = Boolean(tool);
|
|
2837
|
+
state.services.systemPrompt = Boolean(prompt);
|
|
2838
|
+
state.services.agents = Boolean(agents);
|
|
2839
|
+
state.services.webServer = Boolean(webServer);
|
|
2840
|
+
for (const id of wanted) {
|
|
2841
|
+
if (bound.has(id)) continue;
|
|
2842
|
+
const agent = resolveAgent(id);
|
|
2843
|
+
if (agent) bindAgent(agent);
|
|
2844
|
+
}
|
|
2845
|
+
if (state.scope === "global" && tool && !globalRegistered) {
|
|
2846
|
+
if (prompt) {
|
|
2847
|
+
try {
|
|
2848
|
+
disposers.push(prompt.section({ name: SECTION_NAME, order: SECTION_ORDER, text: PROTOCOL }));
|
|
2849
|
+
} catch (err) {
|
|
2850
|
+
state.toolError = `section: ${(err && err.message) || err}`;
|
|
2851
|
+
}
|
|
2852
|
+
}
|
|
2786
2853
|
try {
|
|
2787
2854
|
disposers.push(tool.register(definition));
|
|
2788
2855
|
state.toolOk = true;
|
|
2856
|
+
globalRegistered = true;
|
|
2857
|
+
state.bindError = null;
|
|
2789
2858
|
} catch (err) {
|
|
2790
|
-
state.toolError =
|
|
2859
|
+
state.toolError = `register: ${(err && err.message) || err}`;
|
|
2791
2860
|
}
|
|
2792
2861
|
}
|
|
2862
|
+
};
|
|
2863
|
+
|
|
2864
|
+
// cordis 的服务是懒注入的:ctx.get 在 apply 那一刻常常拿不到,
|
|
2865
|
+
// 必须用 ctx.inject 等回调(回调就是「该服务可用」的时刻)。
|
|
2866
|
+
if (typeof ctx.inject === "function") {
|
|
2867
|
+
ctx.inject(["tools"], (scope) => {
|
|
2868
|
+
tool = (scope && scope.tools) || (scope && scope.get ? scope.get("tools") : null);
|
|
2869
|
+
onServicesReady();
|
|
2870
|
+
});
|
|
2871
|
+
ctx.inject(["systemPrompt"], (scope) => {
|
|
2872
|
+
prompt = (scope && scope.systemPrompt) || (scope && scope.get ? scope.get("systemPrompt") : null);
|
|
2873
|
+
onServicesReady();
|
|
2874
|
+
});
|
|
2875
|
+
ctx.inject(["agents"], (scope) => {
|
|
2876
|
+
agents = (scope && scope.agents) || (scope && scope.get ? scope.get("agents") : null);
|
|
2877
|
+
onServicesReady();
|
|
2878
|
+
});
|
|
2793
2879
|
}
|
|
2880
|
+
onServicesReady();
|
|
2794
2881
|
|
|
2795
2882
|
if (webServer) mount();
|
|
2796
2883
|
else if (typeof ctx.inject === "function") {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xenra/dsh-draworld",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.39",
|
|
4
4
|
"description": "星染世界:DeepSeek Harness 里的一块真画布: a right-sidebar canvas tab plus a paint_canvas tool, so the model and the human draw on the same document.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/pixelops.js
CHANGED
|
@@ -106,9 +106,24 @@ export async function runHeadless(kind, params, doc, resolve) {
|
|
|
106
106
|
maxLines: params.maxLines,
|
|
107
107
|
});
|
|
108
108
|
const paths = t.paths.map((line) => line.map((p) => [Math.round(rect.x + p[0]), Math.round(rect.y + p[1])]));
|
|
109
|
+
// 调 THRESHOLD/TOLERANCE/STEP 时得有反馈:给线宽分布和墨迹占比,
|
|
110
|
+
// 不然只能肉眼反复试(0.1.38 之前 trace 只回 lines/points/dropped)。
|
|
111
|
+
const mask = inkMask(cut.data, cut.w, cut.h, params.threshold);
|
|
112
|
+
let inkPx = 0;
|
|
113
|
+
for (let i = 0; i < mask.length; i += 1) inkPx += mask[i];
|
|
114
|
+
const widths = pathWidths(mask, cut.w, cut.h, t.paths);
|
|
115
|
+
const widthAvg = widths.length ? Number((widths.reduce((a, b) => a + b, 0) / widths.length).toFixed(2)) : null;
|
|
116
|
+
const stats = {
|
|
117
|
+
inkRatio: Number((inkPx / (cut.w * cut.h)).toFixed(4)),
|
|
118
|
+
widthAvg,
|
|
119
|
+
widthMin: widths.length ? Number(Math.min(...widths).toFixed(2)) : null,
|
|
120
|
+
widthMax: widths.length ? Number(Math.max(...widths).toFixed(2)) : null,
|
|
121
|
+
pointsPerLine: t.lines ? Number((t.points / t.lines).toFixed(1)) : null,
|
|
122
|
+
droppedPct: t.lines + t.dropped > 0 ? Number((t.dropped / (t.lines + t.dropped) * 100).toFixed(1)) : null,
|
|
123
|
+
};
|
|
109
124
|
return {
|
|
110
125
|
ok: true, rect, paths, lines: t.lines, points: t.points, dropped: t.dropped,
|
|
111
|
-
tolerance: t.tolerance, smooth: t.smooth, fit: t.fit, mode: t.mode, headless: true,
|
|
126
|
+
tolerance: t.tolerance, smooth: t.smooth, fit: t.fit, mode: t.mode, headless: true, stats,
|
|
112
127
|
};
|
|
113
128
|
}
|
|
114
129
|
if (kind === "copy") {
|