@xenra/dsh-draworld 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +271 -2
- package/client.js +4697 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +7 -0
- package/index.js +2907 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +63 -4
- package/pixelops.js +420 -0
- package/pixels.js +926 -0
package/client.js
ADDED
|
@@ -0,0 +1,4697 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @xenra/dsh-draworld 前端:右侧栏里的真画布。
|
|
3
|
+
*
|
|
4
|
+
* 文档(笔迹 JSON)由宿主持有,这里只负责三件事:
|
|
5
|
+
* 1. 把笔迹画到 <canvas>(按压感变粗细,所以有起收笔);
|
|
6
|
+
* 2. 接收鼠标/触控绘制,落笔后提交给宿主;
|
|
7
|
+
* 3. 渲染结果回传 PNG,供模型用 read_image 验证。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
window.__ModuleLoader__.load({
|
|
11
|
+
id: '@xenra/dsh-draworld',
|
|
12
|
+
factory(require) {
|
|
13
|
+
const React = require('react');
|
|
14
|
+
const h = React.createElement;
|
|
15
|
+
|
|
16
|
+
const TAB_ID = '@xenra/dsh-draworld/panel';
|
|
17
|
+
const TAB_KIND = 'paintCanvas';
|
|
18
|
+
/** 左栏全局面板的 id;sidebar.panellist 与 main 两个槽位用同一个 id 对上。 */
|
|
19
|
+
const PANEL_ID = 'xenra-draworld';
|
|
20
|
+
const API = '/canvas-api';
|
|
21
|
+
/** 本插件的词条命名空间。 */
|
|
22
|
+
const NS = 'xenraDraworld';
|
|
23
|
+
|
|
24
|
+
/** 画布预设:比例、纸张(300dpi)、像素画布。尺寸都落在宿主 16~4096 的钳位里。 */
|
|
25
|
+
const CANVAS_PRESETS = [
|
|
26
|
+
{ id: '1-1', label: '1:1', w: 1024, h: 1024, group: 'ratio' },
|
|
27
|
+
{ id: '4-3', label: '4:3', w: 1024, h: 768, group: 'ratio' },
|
|
28
|
+
{ id: '3-4', label: '3:4', w: 768, h: 1024, group: 'ratio' },
|
|
29
|
+
{ id: '16-9', label: '16:9', w: 1024, h: 576, group: 'ratio' },
|
|
30
|
+
{ id: '9-16', label: '9:16', w: 576, h: 1024, group: 'ratio' },
|
|
31
|
+
{ id: 'screen', label: '屏幕', w: 1920, h: 1080, group: 'paper' },
|
|
32
|
+
{ id: 'a4', label: 'A4', w: 2480, h: 3508, group: 'paper' },
|
|
33
|
+
{ id: 'a5', label: 'A5', w: 1748, h: 2480, group: 'paper' },
|
|
34
|
+
{ id: 'a6', label: 'A6', w: 1240, h: 1748, group: 'paper' },
|
|
35
|
+
{ id: 'b5', label: 'B5', w: 2079, h: 2953, group: 'paper' },
|
|
36
|
+
{ id: 'p16', label: '16', w: 16, h: 16, group: 'pixel' },
|
|
37
|
+
{ id: 'p32', label: '32', w: 32, h: 32, group: 'pixel' },
|
|
38
|
+
{ id: 'p64', label: '64', w: 64, h: 64, group: 'pixel' },
|
|
39
|
+
{ id: 'p128', label: '128', w: 128, h: 128, group: 'pixel' },
|
|
40
|
+
{ id: 'p256', label: '256', w: 256, h: 256, group: 'pixel' },
|
|
41
|
+
{ id: 'p512', label: '512', w: 512, h: 512, group: 'pixel' },
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const DICT = {
|
|
45
|
+
zh: {
|
|
46
|
+
title: '画界',
|
|
47
|
+
open: '打开画界',
|
|
48
|
+
'page.back': '回到对话',
|
|
49
|
+
'page.tools': '画笔',
|
|
50
|
+
'page.edit': '编辑',
|
|
51
|
+
'page.canvas': '画布',
|
|
52
|
+
'page.tab': '右栏画布',
|
|
53
|
+
'page.hint': '左边画板,右上画笔、右下编辑与画布设置',
|
|
54
|
+
'page.layerHint': '点一行选中它;◉ 控制显隐,不透明度和混合模式只作用于当前层',
|
|
55
|
+
'page.importLayer': '导入图片',
|
|
56
|
+
'page.importing': '导入中…',
|
|
57
|
+
'page.imported': '已导入',
|
|
58
|
+
'page.new': '新建',
|
|
59
|
+
'page.new.title': '新建绘画',
|
|
60
|
+
'page.new.saveQ': '当前这幅画要先留一份吗?',
|
|
61
|
+
'page.new.saveHint': '「保存并新建」把当前渲染存成 PNG 落到保存路径(和「另存」同一处);选「不保存」就直接换新画布,旧内容不再保留。',
|
|
62
|
+
'page.new.saveAnd': '保存并新建',
|
|
63
|
+
'page.new.skipSave': '不保存,直接新建',
|
|
64
|
+
'page.new.cancel': '取消',
|
|
65
|
+
'page.new.pick': '新画布怎么来?',
|
|
66
|
+
'page.new.willSave': '会先把当前这幅存成 PNG',
|
|
67
|
+
'page.new.blank': '空白画布',
|
|
68
|
+
'page.new.blankHint': '挑一个尺寸,开一张干净的画布',
|
|
69
|
+
'page.new.currentSize': '当前尺寸',
|
|
70
|
+
'page.new.moreSizes': '更多尺寸 / 背景',
|
|
71
|
+
'page.new.fromImage': '导入图片',
|
|
72
|
+
'page.new.fromImageHint': '按图片尺寸建画布,图片放最底层(图层 1),上面自动留一层「绘画」',
|
|
73
|
+
'page.new.chooseImage': '选择图片',
|
|
74
|
+
'page.new.creating': '创建中…',
|
|
75
|
+
'page.new.failed': '新建失败',
|
|
76
|
+
'guide.title': '画界',
|
|
77
|
+
'guide.description': '在右栏和模型一起在这块画布上画',
|
|
78
|
+
'settings.subtitle': '画布参数由所有会话共用,保存后立刻生效',
|
|
79
|
+
'settings.presets': '尺寸预设',
|
|
80
|
+
'settings.presetRatio': '比例',
|
|
81
|
+
'settings.presetPaper': '纸张(300dpi)',
|
|
82
|
+
'settings.presetPixel': '像素画布',
|
|
83
|
+
'settings.presetHint': '点一下立刻改尺寸,笔迹不移动;大画布更吃内存',
|
|
84
|
+
'settings.size': '自定义',
|
|
85
|
+
'settings.width': '宽',
|
|
86
|
+
'settings.height': '高',
|
|
87
|
+
'settings.background': '背景',
|
|
88
|
+
'settings.dir': '缓存目录',
|
|
89
|
+
'settings.saveDir': '保存路径',
|
|
90
|
+
'settings.pathHint': '绝对路径;留空表示跟随缓存目录',
|
|
91
|
+
'settings.dirHint': '画布文档 doc.json 和最近一次渲染 preview.png 放这里',
|
|
92
|
+
'settings.saveHint': '另存的成品 PNG 放这里;清空则跟随缓存目录下的 exports',
|
|
93
|
+
'settings.export': '另存当前画布',
|
|
94
|
+
'settings.exported': '已保存到',
|
|
95
|
+
'settings.copy': '复制路径',
|
|
96
|
+
'settings.copied': '已复制',
|
|
97
|
+
'settings.save': '保存',
|
|
98
|
+
'settings.saved': '已保存',
|
|
99
|
+
'settings.reset': '恢复默认',
|
|
100
|
+
'settings.hint': '改尺寸不会挪动已有笔迹,只改画布边界',
|
|
101
|
+
'settings.unreachable': '读不到画布参数,宿主可能没起来',
|
|
102
|
+
clear: '清空',
|
|
103
|
+
undo: '撤销',
|
|
104
|
+
redo: '重做',
|
|
105
|
+
replay: '回放',
|
|
106
|
+
layers: '图层',
|
|
107
|
+
layerAdd: '+层',
|
|
108
|
+
layerRemove: '删层',
|
|
109
|
+
layerVisibility: '显隐',
|
|
110
|
+
opacity: '不透明度',
|
|
111
|
+
blendNormal: '正常',
|
|
112
|
+
blendMultiply: '正片叠底',
|
|
113
|
+
toolBrush: '笔',
|
|
114
|
+
toolEraser: '擦',
|
|
115
|
+
toolLine: '线',
|
|
116
|
+
toolRect: '矩形',
|
|
117
|
+
toolEllipse: '圆',
|
|
118
|
+
brush: '笔刷',
|
|
119
|
+
brushHint: '照画世界Pro 的画笔库分八类,32 支的写法各不相同(点开有样笔预览)',
|
|
120
|
+
'brushGroup.basic': '常规画笔',
|
|
121
|
+
'brushGroup.line': '勾线画笔',
|
|
122
|
+
'brushGroup.sketch': '铅笔 / 素描',
|
|
123
|
+
'brushGroup.water': '水彩',
|
|
124
|
+
'brushGroup.inkwash': '水墨',
|
|
125
|
+
'brushGroup.oil': '油画 / 厚涂',
|
|
126
|
+
'brushGroup.air': '喷枪',
|
|
127
|
+
'brushGroup.fx': '特效 / 网点',
|
|
128
|
+
'brush.round': '圆头',
|
|
129
|
+
'brush.hard': '硬边圆',
|
|
130
|
+
'brush.softround': '软边圆',
|
|
131
|
+
'brush.ballpoint': '圆珠笔',
|
|
132
|
+
'brush.marker': '马克笔',
|
|
133
|
+
'brush.flat': '平头笔',
|
|
134
|
+
'brush.liner': '勾线笔',
|
|
135
|
+
'brush.gpen': 'G 笔',
|
|
136
|
+
'brush.ink': '墨水笔',
|
|
137
|
+
'brush.outline': '描边笔',
|
|
138
|
+
'brush.willow': '柳叶笔',
|
|
139
|
+
'brush.pencil': '铅笔',
|
|
140
|
+
'brush.sketch': '素描铅笔',
|
|
141
|
+
'brush.charcoal': '炭笔',
|
|
142
|
+
'brush.charcoalStick': '炭条',
|
|
143
|
+
'brush.chalk': '粉笔',
|
|
144
|
+
'brush.watercolor': '水彩笔',
|
|
145
|
+
'brush.wash': '水彩铺色',
|
|
146
|
+
'brush.wetedge': '湿边水彩',
|
|
147
|
+
'brush.blender': '晕染笔',
|
|
148
|
+
'brush.inkbrush': '水墨毛笔',
|
|
149
|
+
'brush.hairbrush': '尖锋毛笔',
|
|
150
|
+
'brush.drybrush': '干笔',
|
|
151
|
+
'brush.oil': '油画笔',
|
|
152
|
+
'brush.impasto': '厚涂笔',
|
|
153
|
+
'brush.palette': '刮刀',
|
|
154
|
+
'brush.airbrush': '软喷枪',
|
|
155
|
+
'brush.airhard': '硬喷枪',
|
|
156
|
+
'brush.airnoise': '噪点喷枪',
|
|
157
|
+
'brush.halftone': '漫画网点',
|
|
158
|
+
'brush.glow': '光效笔',
|
|
159
|
+
'brush.sparkle': '粒子笔',
|
|
160
|
+
layerCollapse: '收起',
|
|
161
|
+
layerExpand: '展开',
|
|
162
|
+
taper: '起收笔渐细',
|
|
163
|
+
taperHint: '手绘时两头自动变细',
|
|
164
|
+
staleHost: '画界宿主还是旧进程:笔刷目前只在前端生效,落笔后会按圆头重画。到「插件管理」把画界停用再启用(或重启 DSH)后重开这一页。',
|
|
165
|
+
updateTip: '有新版本',
|
|
166
|
+
updateNow: '当前',
|
|
167
|
+
updateGo: '去 Release 下载',
|
|
168
|
+
updateApply: '一键更新',
|
|
169
|
+
updateApplying: '更新中…',
|
|
170
|
+
updateDone: '已触发更新,重启 DSH 后生效',
|
|
171
|
+
updateFail: '更新没成',
|
|
172
|
+
versionCard: '版本',
|
|
173
|
+
versionCurrent: '当前',
|
|
174
|
+
versionLatest: '最新',
|
|
175
|
+
versionNever: '还没查过',
|
|
176
|
+
versionUpToDate: '已是最新',
|
|
177
|
+
versionCheckNow: '检查更新',
|
|
178
|
+
versionChecking: '检查中…',
|
|
179
|
+
versionOff: '自动检查已关闭',
|
|
180
|
+
versionCheckFail: '检查失败',
|
|
181
|
+
versionCheckedAt: '检查于',
|
|
182
|
+
zoomFit: '适应',
|
|
183
|
+
zoomActual: '1:1',
|
|
184
|
+
panHint: '空格拖动/中键拖动平移,滚轮缩放',
|
|
185
|
+
'settings.alpha': '透明底',
|
|
186
|
+
'settings.alphaHint': '勾上就导出不含背景的 PNG,方便贴到别的画面上',
|
|
187
|
+
color: '颜色',
|
|
188
|
+
size: '笔宽',
|
|
189
|
+
strokes: '笔迹',
|
|
190
|
+
loading: '加载中…',
|
|
191
|
+
empty: '在这块画布上画点什么',
|
|
192
|
+
syncFailed: '与宿主失联',
|
|
193
|
+
},
|
|
194
|
+
en: {
|
|
195
|
+
title: 'Canvas',
|
|
196
|
+
open: 'Open canvas',
|
|
197
|
+
'page.back': 'Back to chat',
|
|
198
|
+
'page.tools': 'Brush',
|
|
199
|
+
'page.edit': 'Edit',
|
|
200
|
+
'page.canvas': 'Canvas',
|
|
201
|
+
'page.tab': 'Side canvas',
|
|
202
|
+
'page.hint': 'Board left, brushes top right, edit and canvas settings bottom right',
|
|
203
|
+
'page.layerHint': 'Click a row to select it; ◉ toggles visibility. Opacity and blend apply to the active layer only',
|
|
204
|
+
'page.importLayer': 'Import image',
|
|
205
|
+
'page.importing': 'Importing…',
|
|
206
|
+
'page.imported': 'Imported',
|
|
207
|
+
'page.new': 'New',
|
|
208
|
+
'page.new.title': 'New painting',
|
|
209
|
+
'page.new.saveQ': 'Keep a copy of this painting first?',
|
|
210
|
+
'page.new.saveHint': 'Save & new writes the current render to a PNG in the save path (same place as Export). Skip discards the old canvas.',
|
|
211
|
+
'page.new.saveAnd': 'Save & new',
|
|
212
|
+
'page.new.skipSave': 'Skip, just new',
|
|
213
|
+
'page.new.cancel': 'Cancel',
|
|
214
|
+
'page.new.pick': 'How should the new canvas start?',
|
|
215
|
+
'page.new.willSave': 'The current painting will be saved as PNG',
|
|
216
|
+
'page.new.blank': 'Blank canvas',
|
|
217
|
+
'page.new.blankHint': 'Pick a size and start clean',
|
|
218
|
+
'page.new.currentSize': 'Current size',
|
|
219
|
+
'page.new.moreSizes': 'More sizes / background',
|
|
220
|
+
'page.new.fromImage': 'Import image',
|
|
221
|
+
'page.new.fromImageHint': 'Canvas takes the image size, image becomes layer 1, and a "Drawing" layer is added on top',
|
|
222
|
+
'page.new.chooseImage': 'Choose image',
|
|
223
|
+
'page.new.creating': 'Creating…',
|
|
224
|
+
'page.new.failed': 'Could not create the canvas',
|
|
225
|
+
'guide.title': 'Canvas',
|
|
226
|
+
'guide.description': 'Draw on one shared canvas with the model in the right sidebar',
|
|
227
|
+
'settings.subtitle': 'Canvas settings are shared by every session and apply on save',
|
|
228
|
+
'settings.presets': 'Size presets',
|
|
229
|
+
'settings.presetRatio': 'Ratio',
|
|
230
|
+
'settings.presetPaper': 'Paper (300 dpi)',
|
|
231
|
+
'settings.presetPixel': 'Pixel canvas',
|
|
232
|
+
'settings.presetHint': 'One click applies the size; existing strokes stay put. Bigger canvases use more memory',
|
|
233
|
+
'settings.size': 'Custom',
|
|
234
|
+
'settings.width': 'Width',
|
|
235
|
+
'settings.height': 'Height',
|
|
236
|
+
'settings.background': 'Background',
|
|
237
|
+
'settings.dir': 'Cache directory',
|
|
238
|
+
'settings.saveDir': 'Save path',
|
|
239
|
+
'settings.pathHint': 'Absolute path; leave empty to follow the cache directory',
|
|
240
|
+
'settings.dirHint': 'Holds doc.json and the latest preview.png',
|
|
241
|
+
'settings.saveHint': 'Exports land here; empty follows exports/ inside the cache directory',
|
|
242
|
+
'settings.export': 'Save canvas as PNG',
|
|
243
|
+
'settings.exported': 'Saved to',
|
|
244
|
+
'settings.copy': 'Copy path',
|
|
245
|
+
'settings.copied': 'Copied',
|
|
246
|
+
'settings.save': 'Save',
|
|
247
|
+
'settings.saved': 'Saved',
|
|
248
|
+
'settings.reset': 'Reset defaults',
|
|
249
|
+
'settings.hint': 'Changing the size moves no existing stroke; only the canvas bounds change',
|
|
250
|
+
'settings.unreachable': 'Cannot read canvas settings; the host may not be running',
|
|
251
|
+
clear: 'Clear',
|
|
252
|
+
undo: 'Undo',
|
|
253
|
+
redo: 'Redo',
|
|
254
|
+
replay: 'Replay',
|
|
255
|
+
layers: 'Layers',
|
|
256
|
+
layerAdd: '+ layer',
|
|
257
|
+
layerRemove: 'Remove',
|
|
258
|
+
layerVisibility: 'Visibility',
|
|
259
|
+
opacity: 'Opacity',
|
|
260
|
+
blendNormal: 'Normal',
|
|
261
|
+
blendMultiply: 'Multiply',
|
|
262
|
+
toolBrush: 'Brush',
|
|
263
|
+
toolEraser: 'Erase',
|
|
264
|
+
toolLine: 'Line',
|
|
265
|
+
toolRect: 'Rect',
|
|
266
|
+
toolEllipse: 'Ellipse',
|
|
267
|
+
brush: 'Brush type',
|
|
268
|
+
brushHint: 'Mirrors Huashijie Pro’s brush library in eight groups; each of the 32 brushes renders differently (preview inside)',
|
|
269
|
+
'brushGroup.basic': 'Basic',
|
|
270
|
+
'brushGroup.line': 'Inking',
|
|
271
|
+
'brushGroup.sketch': 'Pencil / sketch',
|
|
272
|
+
'brushGroup.water': 'Watercolor',
|
|
273
|
+
'brushGroup.inkwash': 'Ink wash',
|
|
274
|
+
'brushGroup.oil': 'Oil / impasto',
|
|
275
|
+
'brushGroup.air': 'Airbrush',
|
|
276
|
+
'brushGroup.fx': 'Effects / halftone',
|
|
277
|
+
'brush.round': 'Round',
|
|
278
|
+
'brush.hard': 'Hard round',
|
|
279
|
+
'brush.softround': 'Soft round',
|
|
280
|
+
'brush.ballpoint': 'Ballpoint',
|
|
281
|
+
'brush.marker': 'Marker',
|
|
282
|
+
'brush.flat': 'Flat',
|
|
283
|
+
'brush.liner': 'Liner',
|
|
284
|
+
'brush.gpen': 'G-pen',
|
|
285
|
+
'brush.ink': 'Ink pen',
|
|
286
|
+
'brush.outline': 'Outline',
|
|
287
|
+
'brush.willow': 'Willow',
|
|
288
|
+
'brush.pencil': 'Pencil',
|
|
289
|
+
'brush.sketch': 'Sketch pencil',
|
|
290
|
+
'brush.charcoal': 'Charcoal',
|
|
291
|
+
'brush.charcoalStick': 'Charcoal stick',
|
|
292
|
+
'brush.chalk': 'Chalk',
|
|
293
|
+
'brush.watercolor': 'Watercolor',
|
|
294
|
+
'brush.wash': 'Wash',
|
|
295
|
+
'brush.wetedge': 'Wet edge',
|
|
296
|
+
'brush.blender': 'Blender',
|
|
297
|
+
'brush.inkbrush': 'Ink brush',
|
|
298
|
+
'brush.hairbrush': 'Fine brush',
|
|
299
|
+
'brush.drybrush': 'Dry brush',
|
|
300
|
+
'brush.oil': 'Oil brush',
|
|
301
|
+
'brush.impasto': 'Impasto',
|
|
302
|
+
'brush.palette': 'Palette knife',
|
|
303
|
+
'brush.airbrush': 'Soft airbrush',
|
|
304
|
+
'brush.airhard': 'Hard airbrush',
|
|
305
|
+
'brush.airnoise': 'Noise airbrush',
|
|
306
|
+
'brush.halftone': 'Halftone',
|
|
307
|
+
'brush.glow': 'Glow',
|
|
308
|
+
'brush.sparkle': 'Sparkle',
|
|
309
|
+
layerCollapse: 'Hide',
|
|
310
|
+
layerExpand: 'Layers',
|
|
311
|
+
taper: 'Taper ends',
|
|
312
|
+
taperHint: 'Hand strokes thin out at both ends',
|
|
313
|
+
staleHost: 'The host is still an old process: brushes only work in the preview and strokes get redrawn as round. Disable and re-enable 画界 in Plugins (or restart DSH), then reopen this page.',
|
|
314
|
+
updateTip: 'Update available',
|
|
315
|
+
updateNow: 'now',
|
|
316
|
+
updateGo: 'Get it on Releases',
|
|
317
|
+
updateApply: 'Update now',
|
|
318
|
+
updateApplying: 'Updating…',
|
|
319
|
+
updateDone: 'Update triggered: restart DSH to apply',
|
|
320
|
+
updateFail: 'Update failed',
|
|
321
|
+
versionCard: 'Version',
|
|
322
|
+
versionCurrent: 'current',
|
|
323
|
+
versionLatest: 'latest',
|
|
324
|
+
versionNever: 'not checked yet',
|
|
325
|
+
versionUpToDate: 'Up to date',
|
|
326
|
+
versionCheckNow: 'Check now',
|
|
327
|
+
versionChecking: 'Checking…',
|
|
328
|
+
versionOff: 'Auto check off',
|
|
329
|
+
versionCheckFail: 'Check failed',
|
|
330
|
+
versionCheckedAt: 'checked',
|
|
331
|
+
zoomFit: 'Fit',
|
|
332
|
+
zoomActual: '1:1',
|
|
333
|
+
panHint: 'Space or middle-drag to pan, wheel to zoom',
|
|
334
|
+
'settings.alpha': 'Transparent',
|
|
335
|
+
'settings.alphaHint': 'Export PNG without the background, ready to paste elsewhere',
|
|
336
|
+
color: 'Color',
|
|
337
|
+
size: 'Size',
|
|
338
|
+
strokes: 'strokes',
|
|
339
|
+
loading: 'Loading…',
|
|
340
|
+
empty: 'Draw something',
|
|
341
|
+
syncFailed: 'Host unreachable',
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* 笔刷表:照画世界Pro 画笔库的分类(常规/勾线/铅笔/水彩/水墨/油画/喷枪/特效)把每一支做成一行参数。
|
|
347
|
+
* engine 决定怎么落笔,w/minRatio/gamma 决定粗细随压感怎么变;加一支只要加一行,界面和模型接口都从这张表长出来。
|
|
348
|
+
*/
|
|
349
|
+
const BRUSHES = [
|
|
350
|
+
// 常规画笔
|
|
351
|
+
{ id: 'round', group: 'basic', engine: 'solid', w: 1, minRatio: 0.28 },
|
|
352
|
+
{ id: 'hard', group: 'basic', engine: 'solid', w: 0.95, minRatio: 0.6, alpha: 0.95, cap: 'round' },
|
|
353
|
+
{ id: 'softround', group: 'basic', engine: 'soft', w: 1.1, minRatio: 0.45 },
|
|
354
|
+
{ id: 'ballpoint', group: 'basic', engine: 'solid', w: 0.55, minRatio: 0.85, alpha: 0.95 },
|
|
355
|
+
{ id: 'marker', group: 'basic', engine: 'flat', w: 0.95, minRatio: 1, cap: 'square' },
|
|
356
|
+
{ id: 'flat', group: 'basic', engine: 'flat', w: 1, minRatio: 0.8, cap: 'butt' },
|
|
357
|
+
|
|
358
|
+
// 勾线画笔
|
|
359
|
+
{ id: 'liner', group: 'line', engine: 'solid', w: 0.5, minRatio: 0.2, gamma: 1.6 },
|
|
360
|
+
{ id: 'gpen', group: 'line', engine: 'solid', w: 0.85, minRatio: 0.25 },
|
|
361
|
+
{ id: 'ink', group: 'line', engine: 'solid', w: 0.9, minRatio: 0.7 },
|
|
362
|
+
{ id: 'outline', group: 'line', engine: 'solid', w: 0.7, minRatio: 0.55 },
|
|
363
|
+
{ id: 'willow', group: 'line', engine: 'solid', w: 1, minRatio: 0.08, gamma: 2 },
|
|
364
|
+
|
|
365
|
+
// 铅笔 / 素描
|
|
366
|
+
{ id: 'pencil', group: 'sketch', engine: 'grain', w: 0.95, minRatio: 0.5, density: 0.85 },
|
|
367
|
+
{ id: 'sketch', group: 'sketch', engine: 'grain', w: 0.8, minRatio: 0.6, density: 0.5, fine: 1 },
|
|
368
|
+
{ id: 'charcoal', group: 'sketch', engine: 'grain', w: 1.15, minRatio: 0.45, density: 1, coarse: 1 },
|
|
369
|
+
{ id: 'charcoalStick', group: 'sketch', engine: 'grain', w: 1.35, minRatio: 0.35, density: 1, coarse: 1, spread: 0.5 },
|
|
370
|
+
{ id: 'chalk', group: 'sketch', engine: 'chalk', w: 1.1, minRatio: 0.4, density: 1 },
|
|
371
|
+
|
|
372
|
+
// 水彩
|
|
373
|
+
{ id: 'watercolor', group: 'water', engine: 'wet', w: 1.15, minRatio: 0.5, rim: 1.15 },
|
|
374
|
+
{ id: 'wash', group: 'water', engine: 'wet', w: 1.5, minRatio: 0.7, rim: 0.9 },
|
|
375
|
+
{ id: 'wetedge', group: 'water', engine: 'wet', w: 1.05, minRatio: 0.45, rim: 1.5 },
|
|
376
|
+
{ id: 'blender', group: 'water', engine: 'soft', w: 1.35, minRatio: 0.6 },
|
|
377
|
+
|
|
378
|
+
// 水墨
|
|
379
|
+
{ id: 'inkbrush', group: 'inkwash', engine: 'inkflow', w: 1, minRatio: 0.15, gamma: 1.9 },
|
|
380
|
+
{ id: 'hairbrush', group: 'inkwash', engine: 'inkflow', w: 1.05, minRatio: 0.1, gamma: 2.2, halo: 0.7 },
|
|
381
|
+
{ id: 'drybrush', group: 'inkwash', engine: 'dry', w: 1.2, minRatio: 0.35 },
|
|
382
|
+
|
|
383
|
+
// 油画 / 厚涂
|
|
384
|
+
{ id: 'oil', group: 'oil', engine: 'oil', w: 1.1, minRatio: 0.55 },
|
|
385
|
+
{ id: 'impasto', group: 'oil', engine: 'oil', w: 1, minRatio: 0.5, thick: 1 },
|
|
386
|
+
{ id: 'palette', group: 'oil', engine: 'dry', w: 1.3, minRatio: 0.75, cap: 'butt', lanes: 3 },
|
|
387
|
+
|
|
388
|
+
// 喷枪
|
|
389
|
+
{ id: 'airbrush', group: 'air', engine: 'spray', soft: 1, w: 1.25, minRatio: 0.4 },
|
|
390
|
+
{ id: 'airhard', group: 'air', engine: 'spray', w: 0.95, minRatio: 0.6, tight: 1 },
|
|
391
|
+
{ id: 'airnoise', group: 'air', engine: 'spray', w: 1.6, minRatio: 0.5, spread: 1.6 },
|
|
392
|
+
|
|
393
|
+
// 特效 / 网点
|
|
394
|
+
{ id: 'halftone', group: 'fx', engine: 'halftone', w: 1, minRatio: 0.6 },
|
|
395
|
+
{ id: 'glow', group: 'fx', engine: 'glow', w: 1.2, minRatio: 0.5 },
|
|
396
|
+
{ id: 'sparkle', group: 'fx', engine: 'scatter', w: 1.3, minRatio: 0.4 },
|
|
397
|
+
];
|
|
398
|
+
const BRUSH_IDS = BRUSHES.map((b) => b.id);
|
|
399
|
+
// 笔刷的中文名:界面上「笔 · 圆头」这类显示要用,之前缺这张表所以显示成 undefined
|
|
400
|
+
const BRUSH_LABELS = {
|
|
401
|
+
round: '圆头', hard: '硬边圆', softround: '软边圆', ballpoint: '圆珠笔', marker: '马克笔', flat: '平头',
|
|
402
|
+
liner: '勾线笔', gpen: 'G 笔', ink: '墨水笔', outline: '描边笔', willow: '柳叶笔',
|
|
403
|
+
pencil: '铅笔', sketch: '素描铅笔', charcoal: '炭笔', charcoalStick: '炭条', chalk: '粉笔',
|
|
404
|
+
watercolor: '水彩笔', wash: '铺色', wetedge: '湿边', blender: '晕染',
|
|
405
|
+
inkbrush: '水墨毛笔', hairbrush: '尖锋毛笔', drybrush: '干笔',
|
|
406
|
+
oil: '油画笔', impasto: '厚涂', palette: '刮刀',
|
|
407
|
+
airbrush: '软喷枪', airhard: '硬喷枪', airnoise: '噪点喷枪',
|
|
408
|
+
halftone: '漫画网点', glow: '光效', sparkle: '粒子',
|
|
409
|
+
};
|
|
410
|
+
const BRUSH_BY_ID = new Map(BRUSHES.map((b) => [b.id, { ...b, label: b.label || BRUSH_LABELS[b.id] || b.id }]));
|
|
411
|
+
const BRUSH_GROUPS = ['basic', 'line', 'sketch', 'water', 'inkwash', 'oil', 'air', 'fx'];
|
|
412
|
+
const ERASER_SPEC = { id: 'round', engine: 'solid', w: 1, minRatio: 0.3 };
|
|
413
|
+
|
|
414
|
+
/** #rrggbb → rgba(),软笔和湿笔的径向渐变要用。 */
|
|
415
|
+
function rgbaOf(hex, alpha) {
|
|
416
|
+
const h = String(hex || '#111111').replace('#', '');
|
|
417
|
+
const full = h.length === 3 ? h.split('').map((c) => c + c).join('') : h.padEnd(6, '0').slice(0, 6);
|
|
418
|
+
const r = parseInt(full.slice(0, 2), 16);
|
|
419
|
+
const g = parseInt(full.slice(2, 4), 16);
|
|
420
|
+
const b = parseInt(full.slice(4, 6), 16);
|
|
421
|
+
return `rgba(${Number.isFinite(r) ? r : 17},${Number.isFinite(g) ? g : 17},${Number.isFinite(b) ? b : 17},${alpha})`;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** 稳定伪随机:同一笔每次重绘抖动一致,回放和导出不会跳来跳去。 */
|
|
425
|
+
function jitterOf(seed, i) {
|
|
426
|
+
const x = Math.sin(seed * 12.9898 + i * 78.233) * 43758.5453;
|
|
427
|
+
return (x - Math.floor(x)) * 2 - 1;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const clamp01 = (n) => Math.max(0, Math.min(1, Number(n) || 0));
|
|
431
|
+
|
|
432
|
+
/** 沿折线等距取点,widthAt 给每个点配一个笔宽;喷枪/网点/光效这类「按点撒」的引擎靠它。 */
|
|
433
|
+
function resamplePath(pts, widthAt, gap, maxCount) {
|
|
434
|
+
const segs = [];
|
|
435
|
+
let total = 0;
|
|
436
|
+
for (let i = 1; i < pts.length; i += 1) {
|
|
437
|
+
const ax = Number(pts[i - 1][0]);
|
|
438
|
+
const ay = Number(pts[i - 1][1]);
|
|
439
|
+
const bx = Number(pts[i][0]);
|
|
440
|
+
const by = Number(pts[i][1]);
|
|
441
|
+
if (![ax, ay, bx, by].every(Number.isFinite)) continue;
|
|
442
|
+
const len = Math.hypot(bx - ax, by - ay);
|
|
443
|
+
if (len < 0.05) continue;
|
|
444
|
+
segs.push({ ax, ay, bx, by, len, start: total, i });
|
|
445
|
+
total += len;
|
|
446
|
+
}
|
|
447
|
+
const out = [];
|
|
448
|
+
if (!segs.length) return out;
|
|
449
|
+
const step = Math.max(gap, total / Math.max(1, maxCount || 2400));
|
|
450
|
+
let si = 0;
|
|
451
|
+
for (let d = 0; d <= total; d += step) {
|
|
452
|
+
while (si < segs.length - 1 && d > segs[si].start + segs[si].len) si += 1;
|
|
453
|
+
const s = segs[si];
|
|
454
|
+
const f = s.len > 0 ? clamp01((d - s.start) / s.len) : 0;
|
|
455
|
+
out.push({
|
|
456
|
+
x: s.ax + (s.bx - s.ax) * f,
|
|
457
|
+
y: s.ay + (s.by - s.ay) * f,
|
|
458
|
+
w: widthAt(pts[s.i], s.i),
|
|
459
|
+
d,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return out;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** 逐段描一笔 ribbon:多数引擎的底子。 */
|
|
466
|
+
function ribbon(g, pts, widthOf, mul, cap) {
|
|
467
|
+
g.lineCap = cap || 'round';
|
|
468
|
+
g.lineJoin = 'round';
|
|
469
|
+
for (let i = 1; i < pts.length; i += 1) {
|
|
470
|
+
const a = pts[i - 1];
|
|
471
|
+
const b = pts[i];
|
|
472
|
+
const ax = Number(a[0]);
|
|
473
|
+
const ay = Number(a[1]);
|
|
474
|
+
const bx = Number(b[0]);
|
|
475
|
+
const by = Number(b[1]);
|
|
476
|
+
if (![ax, ay, bx, by].every(Number.isFinite)) continue;
|
|
477
|
+
g.lineWidth = Math.max(0.4, ((widthOf(a, i - 1) + widthOf(b, i)) / 2) * mul);
|
|
478
|
+
g.beginPath();
|
|
479
|
+
g.moveTo(ax, ay);
|
|
480
|
+
g.lineTo(bx, by);
|
|
481
|
+
g.stroke();
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* 各笔刷的落笔方式。每支笔的差别是真的在这层分出来的:
|
|
487
|
+
* solid 实边、flat 平头等宽、soft 软边、grain 颗粒、chalk 粉笔、dry 飞白、wet 水彩、
|
|
488
|
+
* inkflow 水墨、oil 厚涂、spray 喷枪、halftone 网点、glow 光效、scatter 粒子。
|
|
489
|
+
*/
|
|
490
|
+
const BRUSH_ENGINES = {
|
|
491
|
+
solid(g, c) {
|
|
492
|
+
g.globalAlpha = c.alpha;
|
|
493
|
+
ribbon(g, c.pts, c.widthAt, 1, c.spec.cap || 'round');
|
|
494
|
+
},
|
|
495
|
+
|
|
496
|
+
flat(g, c) {
|
|
497
|
+
// 平头笔:整条笔迹合成一个多边形一次填掉,接口处不会有台阶或发丝白缝
|
|
498
|
+
const pts = c.pts;
|
|
499
|
+
const left = [];
|
|
500
|
+
const right = [];
|
|
501
|
+
for (let i = 0; i < pts.length; i += 1) {
|
|
502
|
+
const p = pts[i];
|
|
503
|
+
const prev = pts[Math.max(0, i - 1)];
|
|
504
|
+
const next = pts[Math.min(pts.length - 1, i + 1)];
|
|
505
|
+
const dx = Number(next[0]) - Number(prev[0]);
|
|
506
|
+
const dy = Number(next[1]) - Number(prev[1]);
|
|
507
|
+
const len = Math.hypot(dx, dy) || 1;
|
|
508
|
+
const nx = -dy / len;
|
|
509
|
+
const ny = dx / len;
|
|
510
|
+
const w = c.widthAt(p, i) / 2;
|
|
511
|
+
left.push([Number(p[0]) + nx * w, Number(p[1]) + ny * w]);
|
|
512
|
+
right.push([Number(p[0]) - nx * w, Number(p[1]) - ny * w]);
|
|
513
|
+
}
|
|
514
|
+
g.globalAlpha = c.alpha;
|
|
515
|
+
g.beginPath();
|
|
516
|
+
g.moveTo(left[0][0], left[0][1]);
|
|
517
|
+
for (let i = 1; i < left.length; i += 1) g.lineTo(left[i][0], left[i][1]);
|
|
518
|
+
for (let i = right.length - 1; i >= 0; i -= 1) g.lineTo(right[i][0], right[i][1]);
|
|
519
|
+
g.closePath();
|
|
520
|
+
g.fill();
|
|
521
|
+
},
|
|
522
|
+
|
|
523
|
+
soft(g, c) {
|
|
524
|
+
const passes = [[1.2, 0.12], [0.98, 0.45]];
|
|
525
|
+
for (const [k, a] of passes) {
|
|
526
|
+
g.globalAlpha = c.alpha * a;
|
|
527
|
+
ribbon(g, c.pts, c.widthAt, k, 'round');
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
|
|
531
|
+
grain(g, c) {
|
|
532
|
+
// 一条淡底保证线是连续的,上面撒细颗粒;颗粒按笔迹 id 做种子,重绘/回放/导出完全一致
|
|
533
|
+
g.globalAlpha = c.alpha * 0.32;
|
|
534
|
+
ribbon(g, c.pts, c.widthAt, 0.86, 'round');
|
|
535
|
+
const gap = Math.max(0.55, c.size * 0.012);
|
|
536
|
+
const dots = resamplePath(c.pts, c.widthAt, gap, 6000);
|
|
537
|
+
const spread = c.spec.spread || 0.2;
|
|
538
|
+
for (let k = 0; k < dots.length; k += 1) {
|
|
539
|
+
const p = dots[k];
|
|
540
|
+
const n1 = jitterOf(c.seed, k * 3 + 1);
|
|
541
|
+
const n2 = jitterOf(c.seed, k * 3 + 2);
|
|
542
|
+
if (Math.abs(n2) > (c.spec.density || 1)) continue;
|
|
543
|
+
const ox = jitterOf(c.seed, k * 3 + 5) * p.w * spread;
|
|
544
|
+
const oy = jitterOf(c.seed, k * 3 + 6) * p.w * spread;
|
|
545
|
+
const r = Math.max(0.25, p.w * (c.spec.coarse
|
|
546
|
+
? 0.03 + 0.05 * Math.abs(n1)
|
|
547
|
+
: 0.025 + 0.04 * Math.abs(n1)));
|
|
548
|
+
g.globalAlpha = c.alpha * (0.14 + 0.22 * Math.abs(jitterOf(c.seed, k * 3 + 7)));
|
|
549
|
+
g.beginPath();
|
|
550
|
+
g.arc(p.x + ox, p.y + oy, r, 0, Math.PI * 2);
|
|
551
|
+
g.fill();
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
|
|
555
|
+
chalk(g, c) {
|
|
556
|
+
BRUSH_ENGINES.grain(g, { ...c, spec: { ...c.spec, density: 1, coarse: 1 } });
|
|
557
|
+
const dots = resamplePath(c.pts, c.widthAt, Math.max(1.6, c.size * 0.06), 1600);
|
|
558
|
+
for (let k = 0; k < dots.length; k += 1) {
|
|
559
|
+
const p = dots[k];
|
|
560
|
+
const n = Math.abs(jitterOf(c.seed, k * 5 + 4));
|
|
561
|
+
g.globalAlpha = c.alpha * 0.12;
|
|
562
|
+
g.beginPath();
|
|
563
|
+
g.arc(
|
|
564
|
+
p.x + jitterOf(c.seed, k * 5 + 2) * p.w * 0.42,
|
|
565
|
+
p.y + jitterOf(c.seed, k * 5 + 3) * p.w * 0.42,
|
|
566
|
+
Math.max(0.4, p.w * (0.05 + 0.06 * n)),
|
|
567
|
+
0,
|
|
568
|
+
Math.PI * 2,
|
|
569
|
+
);
|
|
570
|
+
g.fill();
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
|
|
574
|
+
dry(g, c) {
|
|
575
|
+
// 飞白:先铺一层薄的实条,再沿法线劈成几股细毛,每股各自按 ~10px 的段落随机断开
|
|
576
|
+
const lanes = c.spec.lanes || 6;
|
|
577
|
+
g.globalAlpha = c.alpha * 0.36;
|
|
578
|
+
ribbon(g, c.pts, c.widthAt, 0.8, 'round');
|
|
579
|
+
g.lineCap = 'round';
|
|
580
|
+
for (let lane = 0; lane < lanes; lane += 1) {
|
|
581
|
+
const f = (lane / Math.max(1, lanes - 1)) - 0.5;
|
|
582
|
+
for (let i = 1; i < c.pts.length; i += 1) {
|
|
583
|
+
const a = c.pts[i - 1];
|
|
584
|
+
const b = c.pts[i];
|
|
585
|
+
const ax = Number(a[0]);
|
|
586
|
+
const ay = Number(a[1]);
|
|
587
|
+
const bx = Number(b[0]);
|
|
588
|
+
const by = Number(b[1]);
|
|
589
|
+
if (![ax, ay, bx, by].every(Number.isFinite)) continue;
|
|
590
|
+
const len = Math.hypot(bx - ax, by - ay);
|
|
591
|
+
if (len < 0.05) continue;
|
|
592
|
+
const nx = -(by - ay) / len;
|
|
593
|
+
const ny = (bx - ax) / len;
|
|
594
|
+
const w = (c.widthAt(a, i - 1) + c.widthAt(b, i)) / 2;
|
|
595
|
+
// 每 8 段(约 10px)换一次噪声,断口才有「干笔刷过纸面」的长度
|
|
596
|
+
const n = jitterOf(c.seed, lane * 811 + Math.floor(i / 8) * 7);
|
|
597
|
+
if (n < -0.6) continue;
|
|
598
|
+
const off = f * w * 0.8 + n * w * 0.06;
|
|
599
|
+
g.globalAlpha = c.alpha * (0.26 + 0.24 * Math.abs(n));
|
|
600
|
+
g.lineWidth = Math.max(0.3, w * (0.1 + 0.05 * Math.abs(n)));
|
|
601
|
+
g.beginPath();
|
|
602
|
+
g.moveTo(ax + nx * off, ay + ny * off);
|
|
603
|
+
g.lineTo(bx + nx * off, by + ny * off);
|
|
604
|
+
g.stroke();
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
},
|
|
608
|
+
|
|
609
|
+
wet(g, c) {
|
|
610
|
+
// 水彩:先铺一条很淡的水体,再撒边缘略深的湿边圆;叠几笔才出浓度
|
|
611
|
+
g.globalAlpha = c.alpha * 0.16;
|
|
612
|
+
ribbon(g, c.pts, c.widthAt, 0.94, 'round');
|
|
613
|
+
const dots = resamplePath(c.pts, c.widthAt, Math.max(1.4, c.size * 0.08), 2200);
|
|
614
|
+
const rim = c.spec.rim || 1.1;
|
|
615
|
+
g.globalAlpha = 1;
|
|
616
|
+
for (const p of dots) {
|
|
617
|
+
const r = Math.max(1.6, p.w * 0.85);
|
|
618
|
+
const grad = g.createRadialGradient(p.x, p.y, r * 0.1, p.x, p.y, r);
|
|
619
|
+
grad.addColorStop(0, rgbaOf(c.color, c.alpha * 0.07));
|
|
620
|
+
grad.addColorStop(0.66, rgbaOf(c.color, c.alpha * 0.07 * rim));
|
|
621
|
+
grad.addColorStop(1, rgbaOf(c.color, 0));
|
|
622
|
+
g.fillStyle = grad;
|
|
623
|
+
g.beginPath();
|
|
624
|
+
g.arc(p.x, p.y, r, 0, Math.PI * 2);
|
|
625
|
+
g.fill();
|
|
626
|
+
}
|
|
627
|
+
},
|
|
628
|
+
|
|
629
|
+
inkflow(g, c) {
|
|
630
|
+
// 水墨:外面一层淡墨渗开,里面一条实心芯,两头靠 taper 收成尖
|
|
631
|
+
const halo = c.spec.halo === undefined ? 1 : c.spec.halo;
|
|
632
|
+
g.globalAlpha = c.alpha * 0.12;
|
|
633
|
+
ribbon(g, c.pts, c.widthAt, 1 + 0.8 * halo, 'round');
|
|
634
|
+
g.globalAlpha = c.alpha * 0.95;
|
|
635
|
+
ribbon(g, c.pts, c.widthAt, 1, 'round');
|
|
636
|
+
},
|
|
637
|
+
|
|
638
|
+
oil(g, c) {
|
|
639
|
+
const thick = c.spec.thick === 1;
|
|
640
|
+
g.globalAlpha = c.alpha * (thick ? 1 : 0.92);
|
|
641
|
+
ribbon(g, c.pts, c.widthAt, 1, 'round');
|
|
642
|
+
// 毛刷拖痕:沿法线错开的细条,厚涂多压一遍
|
|
643
|
+
const lanes = thick ? 6 : 4;
|
|
644
|
+
for (let lane = 0; lane < lanes; lane += 1) {
|
|
645
|
+
const f = (lane / Math.max(1, lanes - 1)) - 0.5;
|
|
646
|
+
for (let i = 1; i < c.pts.length; i += 1) {
|
|
647
|
+
const a = c.pts[i - 1];
|
|
648
|
+
const b = c.pts[i];
|
|
649
|
+
const ax = Number(a[0]);
|
|
650
|
+
const ay = Number(a[1]);
|
|
651
|
+
const bx = Number(b[0]);
|
|
652
|
+
const by = Number(b[1]);
|
|
653
|
+
const len = Math.hypot(bx - ax, by - ay);
|
|
654
|
+
if (len < 0.05) continue;
|
|
655
|
+
const nx = -(by - ay) / len;
|
|
656
|
+
const ny = (bx - ax) / len;
|
|
657
|
+
const w = (c.widthAt(a, i - 1) + c.widthAt(b, i)) / 2;
|
|
658
|
+
const n = jitterOf(c.seed, i * 13 + lane);
|
|
659
|
+
if (Math.abs(n) > 0.85) continue;
|
|
660
|
+
const off = f * w * 0.66 + n * w * 0.1;
|
|
661
|
+
g.globalAlpha = c.alpha * 0.12;
|
|
662
|
+
g.lineWidth = Math.max(0.3, w * 0.14);
|
|
663
|
+
g.beginPath();
|
|
664
|
+
g.moveTo(ax + nx * off, ay + ny * off);
|
|
665
|
+
g.lineTo(bx + nx * off, by + ny * off);
|
|
666
|
+
g.stroke();
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
},
|
|
670
|
+
|
|
671
|
+
spray(g, c) {
|
|
672
|
+
const soft = c.spec.soft === 1;
|
|
673
|
+
const tight = c.spec.tight === 1;
|
|
674
|
+
const spread = c.spec.spread || 1;
|
|
675
|
+
if (soft) {
|
|
676
|
+
// 软喷枪:一串相互融掉的渐变圆,边界没有颗粒感
|
|
677
|
+
const dots = resamplePath(c.pts, c.widthAt, Math.max(1.2, c.size * 0.08), 1800);
|
|
678
|
+
g.globalAlpha = 1;
|
|
679
|
+
for (const p of dots) {
|
|
680
|
+
const r = Math.max(1.8, p.w * 1.1);
|
|
681
|
+
const grad = g.createRadialGradient(p.x, p.y, r * 0.2, p.x, p.y, r);
|
|
682
|
+
grad.addColorStop(0, rgbaOf(c.color, c.alpha * 0.07));
|
|
683
|
+
grad.addColorStop(0.6, rgbaOf(c.color, c.alpha * 0.04));
|
|
684
|
+
grad.addColorStop(1, rgbaOf(c.color, 0));
|
|
685
|
+
g.fillStyle = grad;
|
|
686
|
+
g.beginPath();
|
|
687
|
+
g.arc(p.x, p.y, r, 0, Math.PI * 2);
|
|
688
|
+
g.fill();
|
|
689
|
+
}
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
const dots = resamplePath(c.pts, c.widthAt, Math.max(0.9, c.size * 0.05), 3000);
|
|
693
|
+
const per = tight ? 3 : 4;
|
|
694
|
+
for (let k = 0; k < dots.length; k += 1) {
|
|
695
|
+
const p = dots[k];
|
|
696
|
+
const base = p.w * (tight ? 0.09 : 0.11);
|
|
697
|
+
for (let j = 0; j < per; j += 1) {
|
|
698
|
+
const n1 = jitterOf(c.seed, k * 7 + j * 3 + 1);
|
|
699
|
+
const n2 = jitterOf(c.seed, k * 7 + j * 3 + 2);
|
|
700
|
+
const n3 = Math.abs(jitterOf(c.seed, k * 7 + j * 3 + 3));
|
|
701
|
+
g.globalAlpha = c.alpha * (tight ? 0.2 : 0.11) * (0.5 + 0.5 * Math.abs(n1));
|
|
702
|
+
g.beginPath();
|
|
703
|
+
g.arc(p.x + n1 * p.w * spread, p.y + n2 * p.w * spread, Math.max(0.3, base * (0.4 + 0.6 * n3)), 0, Math.PI * 2);
|
|
704
|
+
g.fill();
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
},
|
|
708
|
+
|
|
709
|
+
halftone(g, c) {
|
|
710
|
+
// 网点:把笔迹盖住的格子挑出来,每格一个圆点,密的地方自然连成一片
|
|
711
|
+
const grid = Math.max(3, c.size * 0.2);
|
|
712
|
+
const dots = resamplePath(c.pts, c.widthAt, Math.max(1.4, grid * 0.55), 1600);
|
|
713
|
+
const cells = new Set();
|
|
714
|
+
for (const p of dots) {
|
|
715
|
+
const reach = Math.ceil((p.w * 0.86) / grid);
|
|
716
|
+
const gx0 = Math.round(p.x / grid);
|
|
717
|
+
const gy0 = Math.round(p.y / grid);
|
|
718
|
+
for (let dx = -reach; dx <= reach; dx += 1) {
|
|
719
|
+
for (let dy = -reach; dy <= reach; dy += 1) {
|
|
720
|
+
const cx = (gx0 + dx) * grid;
|
|
721
|
+
const cy = (gy0 + dy) * grid;
|
|
722
|
+
if (Math.hypot(cx - p.x, cy - p.y) > p.w * 0.86) continue;
|
|
723
|
+
cells.add(`${gx0 + dx},${gy0 + dy}`);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
g.globalAlpha = c.alpha * 0.9;
|
|
728
|
+
for (const key of cells) {
|
|
729
|
+
const comma = key.indexOf(',');
|
|
730
|
+
const cx = Number(key.slice(0, comma)) * grid;
|
|
731
|
+
const cy = Number(key.slice(comma + 1)) * grid;
|
|
732
|
+
g.beginPath();
|
|
733
|
+
g.arc(cx, cy, grid * 0.3, 0, Math.PI * 2);
|
|
734
|
+
g.fill();
|
|
735
|
+
}
|
|
736
|
+
},
|
|
737
|
+
|
|
738
|
+
glow(g, c) {
|
|
739
|
+
const dots = resamplePath(c.pts, c.widthAt, Math.max(1.6, c.size * 0.12), 1600);
|
|
740
|
+
for (const p of dots) {
|
|
741
|
+
const r = Math.max(1.8, p.w * 1.3);
|
|
742
|
+
const grad = g.createRadialGradient(p.x, p.y, r * 0.1, p.x, p.y, r);
|
|
743
|
+
grad.addColorStop(0, rgbaOf(c.color, c.alpha * 0.16));
|
|
744
|
+
grad.addColorStop(0.4, rgbaOf(c.color, c.alpha * 0.07));
|
|
745
|
+
grad.addColorStop(1, rgbaOf(c.color, 0));
|
|
746
|
+
g.fillStyle = grad;
|
|
747
|
+
g.globalAlpha = 1;
|
|
748
|
+
g.beginPath();
|
|
749
|
+
g.arc(p.x, p.y, r, 0, Math.PI * 2);
|
|
750
|
+
g.fill();
|
|
751
|
+
}
|
|
752
|
+
},
|
|
753
|
+
|
|
754
|
+
scatter(g, c) {
|
|
755
|
+
const dots = resamplePath(c.pts, c.widthAt, Math.max(1.6, c.size * 0.14), 1800);
|
|
756
|
+
g.globalAlpha = 1;
|
|
757
|
+
for (let k = 0; k < dots.length; k += 1) {
|
|
758
|
+
const p = dots[k];
|
|
759
|
+
const n1 = jitterOf(c.seed, k * 5 + 1);
|
|
760
|
+
const n2 = jitterOf(c.seed, k * 5 + 2);
|
|
761
|
+
const n3 = Math.abs(jitterOf(c.seed, k * 5 + 3));
|
|
762
|
+
const x = p.x + n1 * p.w * 1.0;
|
|
763
|
+
const y = p.y + n2 * p.w * 1.0;
|
|
764
|
+
if (n3 > 0.85) {
|
|
765
|
+
g.globalAlpha = c.alpha * 0.05;
|
|
766
|
+
g.beginPath();
|
|
767
|
+
g.arc(x, y, Math.max(1.2, p.w * 0.14), 0, Math.PI * 2);
|
|
768
|
+
g.fill();
|
|
769
|
+
} else {
|
|
770
|
+
g.globalAlpha = c.alpha * (0.25 + 0.35 * n3);
|
|
771
|
+
g.beginPath();
|
|
772
|
+
g.arc(x, y, Math.max(0.35, p.w * (0.03 + 0.09 * n3)), 0, Math.PI * 2);
|
|
773
|
+
g.fill();
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
},
|
|
777
|
+
};
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* 按笔刷把一笔画出来:宽度随压感变,taper=true 时起收笔渐细,具体落笔交给 engine。
|
|
781
|
+
* 橡皮固定走圆头实心,在图层上擦成透明(destination-out)。
|
|
782
|
+
*/
|
|
783
|
+
/** Chaikin 角切:把折线磨圆一遍,点会变多,形状更顺。 */
|
|
784
|
+
function chaikin(pts, closed) {
|
|
785
|
+
const n = pts.length;
|
|
786
|
+
if (n < 3) return pts;
|
|
787
|
+
const out = [];
|
|
788
|
+
if (!closed) out.push(pts[0]);
|
|
789
|
+
const limit = closed ? n : n - 1;
|
|
790
|
+
for (let i = 0; i < limit; i += 1) {
|
|
791
|
+
const p = pts[i];
|
|
792
|
+
const q = pts[(i + 1) % n];
|
|
793
|
+
out.push([p[0] * 0.75 + q[0] * 0.25, p[1] * 0.75 + q[1] * 0.25]);
|
|
794
|
+
out.push([p[0] * 0.25 + q[0] * 0.75, p[1] * 0.25 + q[1] * 0.75]);
|
|
795
|
+
}
|
|
796
|
+
if (!closed) out.push(pts[n - 1]);
|
|
797
|
+
return out;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/** 描线的后处理:Chaikin 磨圆 + 轻微 RDP 收紧,出来就是一条顺的线而不是锯齿折线。 */
|
|
801
|
+
function smoothPath(pts, closed, iterations) {
|
|
802
|
+
const iters = Math.max(0, Math.min(3, Number(iterations) || 0));
|
|
803
|
+
if (iters === 0 || pts.length < 3) return pts;
|
|
804
|
+
let s = pts;
|
|
805
|
+
for (let k = 0; k < iters; k += 1) s = chaikin(s, closed);
|
|
806
|
+
// 磨圆之后点会翻倍,用很小容差压一下,保留曲率但别把点堆成山
|
|
807
|
+
return rdp(s, 0.6);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* 渲染前的曲线补点:稀疏折线用 Catmull-Rom 补成曲线采样。
|
|
812
|
+
* 这是「线条不流畅」的另一半原因——数据再顺,直接 lineTo 连接也会一段一段带折角。
|
|
813
|
+
*/
|
|
814
|
+
function curvePts(pts) {
|
|
815
|
+
const n = pts.length;
|
|
816
|
+
if (n < 3) return pts;
|
|
817
|
+
let total = 0;
|
|
818
|
+
for (let i = 1; i < n; i += 1) total += Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]);
|
|
819
|
+
const avg = total / (n - 1);
|
|
820
|
+
if (avg <= 2.5 || n > 4000) return pts;
|
|
821
|
+
const samples = Math.max(2, Math.min(8, Math.round(avg / 2)));
|
|
822
|
+
const pAt = (i) => pts[Math.max(0, Math.min(n - 1, i))];
|
|
823
|
+
const out = [pts[0]];
|
|
824
|
+
const hasPressure = pts[0].length > 2;
|
|
825
|
+
for (let i = 0; i < n - 1; i += 1) {
|
|
826
|
+
const p0 = pAt(i - 1);
|
|
827
|
+
const p1 = pAt(i);
|
|
828
|
+
const p2 = pAt(i + 1);
|
|
829
|
+
const p3 = pAt(i + 2);
|
|
830
|
+
for (let s = 1; s <= samples; s += 1) {
|
|
831
|
+
const t = s / samples;
|
|
832
|
+
const t2 = t * t;
|
|
833
|
+
const t3 = t2 * t;
|
|
834
|
+
const x = 0.5 * (2 * p1[0] + (-p0[0] + p2[0]) * t + (2 * p0[0] - 5 * p1[0] + 4 * p2[0] - p3[0]) * t2 + (-p0[0] + 3 * p1[0] - 3 * p2[0] + p3[0]) * t3);
|
|
835
|
+
const y = 0.5 * (2 * p1[1] + (-p0[1] + p2[1]) * t + (2 * p0[1] - 5 * p1[1] + 4 * p2[1] - p3[1]) * t2 + (-p0[1] + 3 * p1[1] - 3 * p2[1] + p3[1]) * t3);
|
|
836
|
+
if (hasPressure && p1.length > 2 && p2.length > 2) {
|
|
837
|
+
const pr = Number(p1[2]) + (Number(p2[2]) - Number(p1[2])) * t;
|
|
838
|
+
out.push([x, y, pr]);
|
|
839
|
+
} else {
|
|
840
|
+
out.push([x, y]);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
return out;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
function paintStroke(g, stroke) {
|
|
848
|
+
const rawPts = stroke.points || [];
|
|
849
|
+
if (rawPts.length < 2) return;
|
|
850
|
+
// 填充笔迹:points 是一个多边形,直接填色(copy 出来的色块用它,比刷子糊一万笔准)
|
|
851
|
+
if (stroke.mode === 'fill') {
|
|
852
|
+
const pts = rawPts;
|
|
853
|
+
g.save();
|
|
854
|
+
g.globalCompositeOperation = 'source-over';
|
|
855
|
+
g.globalAlpha = stroke.opacity === undefined ? 1 : Math.max(0, Math.min(1, Number(stroke.opacity)));
|
|
856
|
+
g.fillStyle = stroke.color || '#ffffff';
|
|
857
|
+
g.beginPath();
|
|
858
|
+
g.moveTo(Number(pts[0][0]), Number(pts[0][1]));
|
|
859
|
+
for (let i = 1; i < pts.length; i += 1) g.lineTo(Number(pts[i][0]), Number(pts[i][1]));
|
|
860
|
+
g.closePath();
|
|
861
|
+
// holes:区域里的洞(眼睛高光之类)用 evenodd 挖掉
|
|
862
|
+
if (Array.isArray(stroke.holes)) {
|
|
863
|
+
for (const hole of stroke.holes) {
|
|
864
|
+
if (!hole || hole.length < 3) continue;
|
|
865
|
+
g.moveTo(Number(hole[0][0]), Number(hole[0][1]));
|
|
866
|
+
for (let i = 1; i < hole.length; i += 1) g.lineTo(Number(hole[i][0]), Number(hole[i][1]));
|
|
867
|
+
g.closePath();
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
g.fill('evenodd');
|
|
871
|
+
g.restore();
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
const eraser = stroke.mode === 'eraser';
|
|
875
|
+
// 笔迹补成曲线再画:稀疏控制点直连会出折角,这是「线条不流畅」的主因之一
|
|
876
|
+
const pts = curvePts(rawPts);
|
|
877
|
+
const spec = eraser ? ERASER_SPEC : (BRUSH_BY_ID.get(stroke.brush) || BRUSH_BY_ID.get('round'));
|
|
878
|
+
const size = Number(stroke.size) || 8;
|
|
879
|
+
const defaultMin = spec.minRatio === undefined ? 0.3 : spec.minRatio;
|
|
880
|
+
const minRatio = stroke.minSize === undefined
|
|
881
|
+
? defaultMin
|
|
882
|
+
: Math.max(0, Math.min(1, Number(stroke.minSize) || 0));
|
|
883
|
+
const taperOn = stroke.taper === true;
|
|
884
|
+
const segs = pts.length - 1;
|
|
885
|
+
const alpha = (stroke.opacity === undefined ? 1 : Number(stroke.opacity)) * (spec.alpha === undefined ? 1 : spec.alpha);
|
|
886
|
+
const color = stroke.color || '#111';
|
|
887
|
+
const pressureAt = (p) => (p.length > 2 ? Math.max(0.02, Math.min(1, Number(p[2]))) : 0.75);
|
|
888
|
+
const taperAt = (i) => {
|
|
889
|
+
if (!taperOn || segs < 4) return 1;
|
|
890
|
+
const edge = Math.max(1, Math.floor(segs * 0.15));
|
|
891
|
+
if (i <= edge) return Math.max(0.18, i / edge);
|
|
892
|
+
if (i >= segs - edge) return Math.max(0.18, (segs - i) / edge);
|
|
893
|
+
return 1;
|
|
894
|
+
};
|
|
895
|
+
const widthAt = (p, i) => {
|
|
896
|
+
const shaped = Math.pow(pressureAt(p), spec.gamma || 1);
|
|
897
|
+
return Math.max(0.6, size * (spec.w || 1) * (minRatio + (1 - minRatio) * shaped) * taperAt(i));
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
g.save();
|
|
901
|
+
if (eraser) {
|
|
902
|
+
g.globalCompositeOperation = 'destination-out';
|
|
903
|
+
g.strokeStyle = '#000';
|
|
904
|
+
g.fillStyle = '#000';
|
|
905
|
+
} else {
|
|
906
|
+
g.globalCompositeOperation = 'source-over';
|
|
907
|
+
g.strokeStyle = color;
|
|
908
|
+
g.fillStyle = color;
|
|
909
|
+
}
|
|
910
|
+
const engine = BRUSH_ENGINES[spec.engine] || BRUSH_ENGINES.solid;
|
|
911
|
+
engine(g, {
|
|
912
|
+
pts,
|
|
913
|
+
spec,
|
|
914
|
+
size,
|
|
915
|
+
alpha,
|
|
916
|
+
color,
|
|
917
|
+
widthAt,
|
|
918
|
+
seed: String(stroke.id || 'x').split('').reduce((n, ch) => n + ch.charCodeAt(0), 0) || 1,
|
|
919
|
+
});
|
|
920
|
+
g.restore();
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/** 图层合成用的离屏画布,尺寸变了再重建,避免每帧分配。 */
|
|
924
|
+
let scratch = null;
|
|
925
|
+
let scratchW = 0;
|
|
926
|
+
let scratchH = 0;
|
|
927
|
+
const scratchOf = (w, hgt) => {
|
|
928
|
+
if (!scratch) scratch = document.createElement('canvas');
|
|
929
|
+
if (scratchW !== w || scratchH !== hgt) {
|
|
930
|
+
scratch.width = w;
|
|
931
|
+
scratch.height = hgt;
|
|
932
|
+
scratchW = w;
|
|
933
|
+
scratchH = hgt;
|
|
934
|
+
}
|
|
935
|
+
return scratch;
|
|
936
|
+
};
|
|
937
|
+
|
|
938
|
+
const layersOf = (doc) => {
|
|
939
|
+
if (Array.isArray(doc.layers) && doc.layers.length > 0) return doc.layers;
|
|
940
|
+
return [{ id: 'l1', visible: true, opacity: 1, blend: 'normal', strokes: doc.strokes || [] }];
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
/** 导入图片的缓存:file -> { el, ok, promise }。渲染同步进行,所以素材要提前载入。 */
|
|
944
|
+
const IMAGES = new Map();
|
|
945
|
+
const imageUrl = (file) => `${API}/image/${String(file).split('/').map(encodeURIComponent).join('/')}`;
|
|
946
|
+
|
|
947
|
+
/** 取一张图的缓存项;第一次用就开载,载入结果记在 ok 上。 */
|
|
948
|
+
function imageEntry(file) {
|
|
949
|
+
const hit = IMAGES.get(file);
|
|
950
|
+
if (hit) return hit;
|
|
951
|
+
const el = new Image();
|
|
952
|
+
const entry = { el, ok: false, promise: null };
|
|
953
|
+
entry.promise = new Promise((resolve) => {
|
|
954
|
+
el.addEventListener('load', () => {
|
|
955
|
+
entry.ok = true;
|
|
956
|
+
resolve();
|
|
957
|
+
}, { once: true });
|
|
958
|
+
el.addEventListener('error', () => {
|
|
959
|
+
entry.ok = false;
|
|
960
|
+
resolve();
|
|
961
|
+
}, { once: true });
|
|
962
|
+
});
|
|
963
|
+
el.src = imageUrl(file);
|
|
964
|
+
IMAGES.set(file, entry);
|
|
965
|
+
return entry;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
/** 把文档里引用到的图片都备好;已经载入的跳过,载入失败的也算「有结果」,不会卡住轮询。 */
|
|
969
|
+
function ensureImages(doc) {
|
|
970
|
+
const pending = [];
|
|
971
|
+
for (const layer of layersOf(doc)) {
|
|
972
|
+
const meta = layer.image;
|
|
973
|
+
if (!meta || !meta.file) continue;
|
|
974
|
+
const entry = imageEntry(meta.file);
|
|
975
|
+
if (!entry.ok) {
|
|
976
|
+
// 最多等 2 秒:图片一直不回来也不能把轮询卡死
|
|
977
|
+
pending.push(Promise.race([
|
|
978
|
+
entry.promise,
|
|
979
|
+
new Promise((resolve) => setTimeout(resolve, 2000)),
|
|
980
|
+
]));
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
return Promise.all(pending);
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/** 图片层:按文档里记的落位画上去;还没载入就先不画(下一帧会补)。 */
|
|
987
|
+
function drawImageLayer(ctx, meta) {
|
|
988
|
+
const entry = IMAGES.get(meta.file);
|
|
989
|
+
const img = entry && entry.el;
|
|
990
|
+
if (!img || !img.naturalWidth) return;
|
|
991
|
+
const w = Number(meta.w) || img.naturalWidth;
|
|
992
|
+
const h = Number(meta.h) || img.naturalHeight;
|
|
993
|
+
ctx.drawImage(img, Number(meta.x) || 0, Number(meta.y) || 0, w, h);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/** 把文档画到画布:逐层合成,支持显隐、不透明度、正片叠底;withBackground=false 得到透明底。 */
|
|
997
|
+
function renderDoc(canvas, doc, opts = {}) {
|
|
998
|
+
if (!canvas || !doc) return;
|
|
999
|
+
const w = doc.width || 1024;
|
|
1000
|
+
const hgt = doc.height || 1024;
|
|
1001
|
+
if (canvas.width !== w) canvas.width = w;
|
|
1002
|
+
if (canvas.height !== hgt) canvas.height = hgt; const g = canvas.getContext('2d');
|
|
1003
|
+
g.save();
|
|
1004
|
+
g.globalCompositeOperation = 'source-over';
|
|
1005
|
+
g.globalAlpha = 1;
|
|
1006
|
+
g.clearRect(0, 0, w, hgt);
|
|
1007
|
+
if (opts.withBackground !== false) {
|
|
1008
|
+
g.fillStyle = doc.background || '#ffffff';
|
|
1009
|
+
g.fillRect(0, 0, w, hgt);
|
|
1010
|
+
}
|
|
1011
|
+
g.restore();
|
|
1012
|
+
|
|
1013
|
+
for (const layer of layersOf(doc)) {
|
|
1014
|
+
if (layer.visible === false) continue;
|
|
1015
|
+
const opacity = layer.opacity === undefined ? 1 : Number(layer.opacity);
|
|
1016
|
+
if (!opacity) continue;
|
|
1017
|
+
const blend = layer.blend === 'multiply' ? 'multiply' : 'source-over';
|
|
1018
|
+
const strokes = layer.strokes || [];
|
|
1019
|
+
const hasImage = Boolean(layer.image && layer.image.file);
|
|
1020
|
+
// 有橡皮、带透明度或混合模式的层必须走离屏,否则橡皮会把背景也擦穿
|
|
1021
|
+
const hasEraser = strokes.some((s) => s.mode === 'eraser');
|
|
1022
|
+
if (opacity === 1 && blend === 'source-over' && !hasEraser) {
|
|
1023
|
+
if (hasImage) drawImageLayer(g, layer.image);
|
|
1024
|
+
for (const s of strokes) paintStroke(g, s);
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
// 需要整层合成时先画到离屏,再按不透明度/混合模式贴回来
|
|
1028
|
+
const sctx = scratchOf(w, hgt).getContext('2d');
|
|
1029
|
+
sctx.save();
|
|
1030
|
+
sctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
1031
|
+
sctx.globalCompositeOperation = 'source-over';
|
|
1032
|
+
sctx.globalAlpha = 1;
|
|
1033
|
+
sctx.clearRect(0, 0, w, hgt);
|
|
1034
|
+
sctx.restore();
|
|
1035
|
+
if (hasImage) drawImageLayer(sctx, layer.image);
|
|
1036
|
+
for (const s of strokes) paintStroke(sctx, s);
|
|
1037
|
+
g.save();
|
|
1038
|
+
g.globalAlpha = opacity;
|
|
1039
|
+
g.globalCompositeOperation = blend;
|
|
1040
|
+
g.drawImage(scratchOf(w, hgt), 0, 0);
|
|
1041
|
+
g.restore();
|
|
1042
|
+
}
|
|
1043
|
+
g.save();
|
|
1044
|
+
g.globalCompositeOperation = 'source-over';
|
|
1045
|
+
g.globalAlpha = 1;
|
|
1046
|
+
g.restore();
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/** 造一块离屏画布。 */
|
|
1050
|
+
function surface(w, h) {
|
|
1051
|
+
const c = document.createElement('canvas');
|
|
1052
|
+
c.width = Math.max(1, Math.min(4096, Math.round(w)));
|
|
1053
|
+
c.height = Math.max(1, Math.min(4096, Math.round(h)));
|
|
1054
|
+
return c;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/** 只留一层(模型要"只看线稿"时用):强制可见、不透明、正常混合。 */
|
|
1058
|
+
function docOnlyLayer(doc, ref) {
|
|
1059
|
+
const list = layersOf(doc);
|
|
1060
|
+
if (!ref) return doc;
|
|
1061
|
+
const layer = list.find((l) => l.id === ref || l.name === ref);
|
|
1062
|
+
if (!layer) throw new Error(`没有这一层:${ref}`);
|
|
1063
|
+
return { ...doc, layers: [{ ...layer, visible: true, opacity: 1, blend: 'normal' }] };
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
/** 去掉所有图片层,只留笔迹(compare 时"你画的"一侧)。 */
|
|
1067
|
+
function docWithoutImages(doc) {
|
|
1068
|
+
return {
|
|
1069
|
+
...doc,
|
|
1070
|
+
layers: layersOf(doc).map((l) => (l.image ? { ...l, image: null } : l)),
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/** 文档里第一张可见的图片层(参考图)。 */
|
|
1075
|
+
function refLayerOf(doc) {
|
|
1076
|
+
const list = layersOf(doc);
|
|
1077
|
+
return list.find((l) => l.image && l.image.file && l.visible !== false) || list.find((l) => l.image && l.image.file) || null;
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
/** 把某块区域裁出来并按 zoom 放大,返回 { dataUrl, width, height }。 */
|
|
1081
|
+
function cropCanvas(src, rect, zoom, background) {
|
|
1082
|
+
const sx = Math.max(0, Math.round(rect.x));
|
|
1083
|
+
const sy = Math.max(0, Math.round(rect.y));
|
|
1084
|
+
const sw = Math.max(1, Math.round(rect.w));
|
|
1085
|
+
const sh = Math.max(1, Math.round(rect.h));
|
|
1086
|
+
const z = Math.max(0.2, Math.min(8, Number(zoom) || 1));
|
|
1087
|
+
const out = surface(sw * z, sh * z);
|
|
1088
|
+
const g = out.getContext('2d');
|
|
1089
|
+
if (background) {
|
|
1090
|
+
g.fillStyle = background;
|
|
1091
|
+
g.fillRect(0, 0, out.width, out.height);
|
|
1092
|
+
}
|
|
1093
|
+
// 放大到 2 倍以上时关掉插值,让人看清像素块;缩小则保留平滑
|
|
1094
|
+
g.imageSmoothingEnabled = z < 2;
|
|
1095
|
+
g.drawImage(src, sx, sy, sw, sh, 0, 0, out.width, out.height);
|
|
1096
|
+
return { dataUrl: out.toDataURL('image/png'), width: out.width, height: out.height, zoom: z };
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
function parseRectIn(rect, doc) {
|
|
1100
|
+
const src = Array.isArray(rect) ? { x: rect[0], y: rect[1], w: rect[2], h: rect[3] } : (rect || {});
|
|
1101
|
+
const n = (v, d) => (v === null || v === undefined || v === '' || !Number.isFinite(Number(v)) ? d : Number(v));
|
|
1102
|
+
const x = Math.max(0, Math.round(n(src.x, 0)));
|
|
1103
|
+
const y = Math.max(0, Math.round(n(src.y, 0)));
|
|
1104
|
+
const w = Math.max(1, Math.round(n(src.w, (doc.width || 1024) - x)));
|
|
1105
|
+
const h = Math.max(1, Math.round(n(src.h, (doc.height || 1024) - y)));
|
|
1106
|
+
return { x, y, w: Math.min(w, (doc.width || 1024) - x), h: Math.min(h, (doc.height || 1024) - y) };
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
function toHex(r, g, b) {
|
|
1110
|
+
return `#${[r, g, b].map((v) => v.toString(16).padStart(2, '0')).join('')}`;
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
// ---- 纯算法:轮廓追踪 / 折线简化 / 主色聚类 ---------------------------------
|
|
1114
|
+
// 写成不碰 DOM 的纯函数,方便自测;跑起来仍然只在浏览器里。
|
|
1115
|
+
|
|
1116
|
+
/** 把像素数据化成「墨迹掩码」:暗=墨,透明当空白。 */
|
|
1117
|
+
function inkMask(data, w, h, threshold) {
|
|
1118
|
+
const t = Number.isFinite(Number(threshold)) ? Number(threshold) : 0.45;
|
|
1119
|
+
const mask = new Uint8Array(w * h);
|
|
1120
|
+
for (let y = 0; y < h; y += 1) {
|
|
1121
|
+
for (let x = 0; x < w; x += 1) {
|
|
1122
|
+
const i = (y * w + x) * 4;
|
|
1123
|
+
const a = data[i + 3] / 255;
|
|
1124
|
+
if (a < 0.12) continue;
|
|
1125
|
+
const lum = (0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2]) / 255;
|
|
1126
|
+
// 深色是墨;透明底上任何不透明像素都算墨
|
|
1127
|
+
const dark = 1 - lum;
|
|
1128
|
+
mask[y * w + x] = dark >= t || (a < 0.98 && data[i + 3] > 40) ? 1 : 0;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
return mask;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/** 3×3 盒子模糊两遍:让细线的两条边并成一条,追踪结果不会碎成双线。 */
|
|
1135
|
+
function blurMask(mask, w, h) {
|
|
1136
|
+
let src = mask;
|
|
1137
|
+
for (let pass = 0; pass < 2; pass += 1) {
|
|
1138
|
+
const out = new Uint8Array(w * h);
|
|
1139
|
+
for (let y = 0; y < h; y += 1) {
|
|
1140
|
+
for (let x = 0; x < w; x += 1) {
|
|
1141
|
+
let sum = 0;
|
|
1142
|
+
let n = 0;
|
|
1143
|
+
for (let dy = -1; dy <= 1; dy += 1) {
|
|
1144
|
+
const yy = y + dy;
|
|
1145
|
+
if (yy < 0 || yy >= h) continue;
|
|
1146
|
+
for (let dx = -1; dx <= 1; dx += 1) {
|
|
1147
|
+
const xx = x + dx;
|
|
1148
|
+
if (xx < 0 || xx >= w) continue;
|
|
1149
|
+
sum += src[yy * w + xx];
|
|
1150
|
+
n += 1;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
out[y * w + x] = sum / n >= 0.5 ? 1 : 0;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
src = out;
|
|
1157
|
+
}
|
|
1158
|
+
return src;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
/** Ramer–Douglas–Peucker:把像素锯齿折线压成少量控制点。 */
|
|
1162
|
+
function rdp(points, tolerance) {
|
|
1163
|
+
if (points.length <= 2 || !(tolerance > 0)) return points;
|
|
1164
|
+
const keep = new Uint8Array(points.length);
|
|
1165
|
+
keep[0] = 1;
|
|
1166
|
+
keep[points.length - 1] = 1;
|
|
1167
|
+
const stack = [[0, points.length - 1]];
|
|
1168
|
+
while (stack.length) {
|
|
1169
|
+
const [first, last] = stack.pop();
|
|
1170
|
+
const ax = points[first][0];
|
|
1171
|
+
const ay = points[first][1];
|
|
1172
|
+
const bx = points[last][0];
|
|
1173
|
+
const by = points[last][1];
|
|
1174
|
+
const dx = bx - ax;
|
|
1175
|
+
const dy = by - ay;
|
|
1176
|
+
const len = Math.hypot(dx, dy) || 1e-6;
|
|
1177
|
+
let index = -1;
|
|
1178
|
+
let best = tolerance;
|
|
1179
|
+
for (let i = first + 1; i < last; i += 1) {
|
|
1180
|
+
const px = points[i][0] - ax;
|
|
1181
|
+
const py = points[i][1] - ay;
|
|
1182
|
+
const dist = Math.abs(px * dy - py * dx) / len;
|
|
1183
|
+
if (dist > best) {
|
|
1184
|
+
best = dist;
|
|
1185
|
+
index = i;
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
if (index >= 0) {
|
|
1189
|
+
keep[index] = 1;
|
|
1190
|
+
stack.push([first, index], [index, last]);
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
return points.filter((_, i) => keep[i]);
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
/**
|
|
1197
|
+
* Marching squares 抽等值轮廓:返回一串折线(局部像素坐标)。
|
|
1198
|
+
* 输入是墨迹掩码,输出是「参考图的边界」,不是中心线——描出来就是原图的形。
|
|
1199
|
+
*/
|
|
1200
|
+
function marchingSquares(mask, w, h) {
|
|
1201
|
+
const segs = [];
|
|
1202
|
+
const edgePoint = (key, x, y) => {
|
|
1203
|
+
// 每条边的中点用整数 key 记,拼接时不会因浮点误差错位
|
|
1204
|
+
if (key === 't') return [x * 2 + 1, y * 2];
|
|
1205
|
+
if (key === 'r') return [x * 2 + 2, y * 2 + 1];
|
|
1206
|
+
if (key === 'b') return [x * 2 + 1, y * 2 + 2];
|
|
1207
|
+
return [x * 2, y * 2 + 1];
|
|
1208
|
+
};
|
|
1209
|
+
const push = (a, b) => {
|
|
1210
|
+
segs.push([a, b]);
|
|
1211
|
+
};
|
|
1212
|
+
for (let y = 0; y < h - 1; y += 1) {
|
|
1213
|
+
for (let x = 0; x < w - 1; x += 1) {
|
|
1214
|
+
const v0 = mask[y * w + x];
|
|
1215
|
+
const v1 = mask[y * w + x + 1];
|
|
1216
|
+
const v2 = mask[(y + 1) * w + x + 1];
|
|
1217
|
+
const v3 = mask[(y + 1) * w + x];
|
|
1218
|
+
const code = (v0 << 3) | (v1 << 2) | (v2 << 1) | v3;
|
|
1219
|
+
if (code === 0 || code === 15) continue;
|
|
1220
|
+
const T = edgePoint('t', x, y);
|
|
1221
|
+
const R = edgePoint('r', x, y);
|
|
1222
|
+
const B = edgePoint('b', x, y);
|
|
1223
|
+
const L = edgePoint('l', x, y);
|
|
1224
|
+
switch (code) {
|
|
1225
|
+
case 1: case 14: push(L, B); break;
|
|
1226
|
+
case 2: case 13: push(B, R); break;
|
|
1227
|
+
case 3: case 12: push(L, R); break;
|
|
1228
|
+
case 4: case 11: push(T, R); break;
|
|
1229
|
+
case 6: case 9: push(T, B); break;
|
|
1230
|
+
case 7: case 8: push(L, T); break;
|
|
1231
|
+
// 5 / 10 是鞍点,两条都要出
|
|
1232
|
+
case 5: push(L, T); push(B, R); break;
|
|
1233
|
+
case 10: push(T, R); push(L, B); break;
|
|
1234
|
+
default: break;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
return segs;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
/** 把线段拼成折线:端点 key 相同的接起来,闭合的就首尾相连。 */
|
|
1242
|
+
function stitchSegments(segs) {
|
|
1243
|
+
const keyOf = (p) => `${p[0]},${p[1]}`;
|
|
1244
|
+
const byKey = new Map();
|
|
1245
|
+
segs.forEach((seg, i) => {
|
|
1246
|
+
for (const p of seg) {
|
|
1247
|
+
const k = keyOf(p);
|
|
1248
|
+
const list = byKey.get(k);
|
|
1249
|
+
if (list) list.push(i);
|
|
1250
|
+
else byKey.set(k, [i]);
|
|
1251
|
+
}
|
|
1252
|
+
});
|
|
1253
|
+
const used = new Set();
|
|
1254
|
+
const lines = [];
|
|
1255
|
+
for (let i = 0; i < segs.length; i += 1) {
|
|
1256
|
+
if (used.has(i)) continue;
|
|
1257
|
+
used.add(i);
|
|
1258
|
+
const line = [segs[i][0], segs[i][1]];
|
|
1259
|
+
// 往后接
|
|
1260
|
+
for (;;) {
|
|
1261
|
+
const tailKey = keyOf(line[line.length - 1]);
|
|
1262
|
+
const next = (byKey.get(tailKey) || []).find((j) => !used.has(j));
|
|
1263
|
+
if (next === undefined) break;
|
|
1264
|
+
used.add(next);
|
|
1265
|
+
const seg = segs[next];
|
|
1266
|
+
const p = keyOf(seg[0]) === tailKey ? seg[1] : seg[0];
|
|
1267
|
+
line.push(p);
|
|
1268
|
+
if (keyOf(p) === keyOf(line[0])) break; // 闭环
|
|
1269
|
+
}
|
|
1270
|
+
// 往前接(开路径的情况)
|
|
1271
|
+
for (;;) {
|
|
1272
|
+
const headKey = keyOf(line[0]);
|
|
1273
|
+
const prev = (byKey.get(headKey) || []).find((j) => !used.has(j));
|
|
1274
|
+
if (prev === undefined) break;
|
|
1275
|
+
used.add(prev);
|
|
1276
|
+
const seg = segs[prev];
|
|
1277
|
+
const p = keyOf(seg[0]) === headKey ? seg[1] : seg[0];
|
|
1278
|
+
line.unshift(p);
|
|
1279
|
+
}
|
|
1280
|
+
lines.push(line);
|
|
1281
|
+
}
|
|
1282
|
+
return lines;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
/** 点在多边形内(射线法):判断轮廓嵌套用。 */
|
|
1286
|
+
function pointInPolygon(x, y, pts) {
|
|
1287
|
+
let inside = false;
|
|
1288
|
+
for (let i = 0, j = pts.length - 1; i < pts.length; j = i, i += 1) {
|
|
1289
|
+
const xi = pts[i][0];
|
|
1290
|
+
const yi = pts[i][1];
|
|
1291
|
+
const xj = pts[j][0];
|
|
1292
|
+
const yj = pts[j][1];
|
|
1293
|
+
if ((yi > y) !== (yj > y) && x < ((xj - xi) * (y - yi)) / (yj - yi || 1e-9) + xi) inside = !inside;
|
|
1294
|
+
}
|
|
1295
|
+
return inside;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
/** 骨架线在每点处的墨宽:沿法线往两边数连续墨点,取中位数当作这根线的笔宽。 */
|
|
1299
|
+
function pathWidth(mask, w, h, path) {
|
|
1300
|
+
if (!path || path.length < 2) return 0;
|
|
1301
|
+
const samples = Math.min(12, path.length);
|
|
1302
|
+
const values = [];
|
|
1303
|
+
for (let k = 0; k < samples; k += 1) {
|
|
1304
|
+
const idx = Math.floor((k * (path.length - 1)) / Math.max(1, samples - 1));
|
|
1305
|
+
const p = path[idx];
|
|
1306
|
+
const q = path[Math.min(path.length - 1, idx + 1)];
|
|
1307
|
+
const r = path[Math.max(0, idx - 1)];
|
|
1308
|
+
const dx = q[0] - r[0];
|
|
1309
|
+
const dy = q[1] - r[1];
|
|
1310
|
+
const len = Math.hypot(dx, dy) || 1;
|
|
1311
|
+
// 法线方向
|
|
1312
|
+
const nx = -dy / len;
|
|
1313
|
+
const ny = dx / len;
|
|
1314
|
+
let n = 1;
|
|
1315
|
+
for (const sign of [1, -1]) {
|
|
1316
|
+
for (let step = 1; step <= 8; step += 1) {
|
|
1317
|
+
const px = Math.round(p[0] + nx * step * sign);
|
|
1318
|
+
const py = Math.round(p[1] + ny * step * sign);
|
|
1319
|
+
if (px < 0 || py < 0 || px >= w || py >= h) break;
|
|
1320
|
+
if (!mask[py * w + px]) break;
|
|
1321
|
+
n += 1;
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
values.push(n);
|
|
1325
|
+
}
|
|
1326
|
+
if (values.length === 0) return 0;
|
|
1327
|
+
values.sort((a, b) => a - b);
|
|
1328
|
+
return values[Math.floor(values.length / 2)];
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
/** 一批线的墨宽(和 paths 一一对应),用来按原线宽落笔。 */
|
|
1332
|
+
function pathWidths(mask, w, h, paths) {
|
|
1333
|
+
return paths.map((p) => pathWidth(mask, w, h, p));
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
/** 多边形面积(鞋带公式),用来判断这个闭合轮廓够不够大、值不值得填。 */
|
|
1337
|
+
function polygonArea(pts) {
|
|
1338
|
+
let sum = 0;
|
|
1339
|
+
for (let i = 0; i < pts.length; i += 1) {
|
|
1340
|
+
const a = pts[i];
|
|
1341
|
+
const b = pts[(i + 1) % pts.length];
|
|
1342
|
+
sum += a[0] * b[1] - b[0] * a[1];
|
|
1343
|
+
}
|
|
1344
|
+
return Math.abs(sum) / 2;
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
/**
|
|
1348
|
+
* 按闭合轮廓取内部主色,映射到 palette 里最接近的一档 —— 就是「按区域填色」。
|
|
1349
|
+
* 返回 [{ points, color, area }],points 已换算到画布坐标。
|
|
1350
|
+
*/
|
|
1351
|
+
function regionFills(refCanvas, rect, traced, palette, opts = {}) {
|
|
1352
|
+
const minFill = Math.max(20, Number(opts.minFill) || 320);
|
|
1353
|
+
const maxFills = Math.max(1, Number(opts.maxFills) || 60);
|
|
1354
|
+
const cands = traced.paths
|
|
1355
|
+
.map((p, i) => ({ pts: p, closed: Boolean(traced.closed && traced.closed[i]), area: polygonArea(p) }))
|
|
1356
|
+
.filter((c) => c.closed && c.area >= minFill)
|
|
1357
|
+
.sort((a, b) => b.area - a.area);
|
|
1358
|
+
// 轮廓嵌套:大的在外,小的在内;偶数层要填,奇数层是洞(眼睛高光之类)
|
|
1359
|
+
const parents = cands.map(() => -1);
|
|
1360
|
+
for (let i = 0; i < cands.length; i += 1) {
|
|
1361
|
+
const p0 = cands[i].pts[0];
|
|
1362
|
+
let best = -1;
|
|
1363
|
+
for (let j = 0; j < i; j += 1) {
|
|
1364
|
+
if (cands[j].area <= cands[i].area) continue;
|
|
1365
|
+
if (!pointInPolygon(p0[0], p0[1], cands[j].pts)) continue;
|
|
1366
|
+
if (best === -1 || cands[j].area < cands[best].area) best = j;
|
|
1367
|
+
}
|
|
1368
|
+
parents[i] = best;
|
|
1369
|
+
}
|
|
1370
|
+
const depths = cands.map((_, i) => {
|
|
1371
|
+
let d = 0;
|
|
1372
|
+
let p = parents[i];
|
|
1373
|
+
while (p !== -1 && d < 8) {
|
|
1374
|
+
d += 1;
|
|
1375
|
+
p = parents[p];
|
|
1376
|
+
}
|
|
1377
|
+
return d;
|
|
1378
|
+
});
|
|
1379
|
+
const fills = [];
|
|
1380
|
+
for (const [ci, cand] of cands.entries()) {
|
|
1381
|
+
if (fills.length >= maxFills) break;
|
|
1382
|
+
if (depths[ci] % 2 !== 0) continue;
|
|
1383
|
+
const holes = cands.filter((_, j) => parents[j] === ci).slice(0, 10);
|
|
1384
|
+
let x0 = Infinity;
|
|
1385
|
+
let y0 = Infinity;
|
|
1386
|
+
let x1 = -Infinity;
|
|
1387
|
+
let y1 = -Infinity;
|
|
1388
|
+
for (const p of cand.pts) {
|
|
1389
|
+
if (p[0] < x0) x0 = p[0];
|
|
1390
|
+
if (p[1] < y0) y0 = p[1];
|
|
1391
|
+
if (p[0] > x1) x1 = p[0];
|
|
1392
|
+
if (p[1] > y1) y1 = p[1];
|
|
1393
|
+
}
|
|
1394
|
+
const w = Math.max(1, Math.ceil(x1 - x0));
|
|
1395
|
+
const h = Math.max(1, Math.ceil(y1 - y0));
|
|
1396
|
+
// 铺满整块的那条轮廓多半是背景,跳过
|
|
1397
|
+
if (w * h > rect.w * rect.h * 0.82) continue;
|
|
1398
|
+
// 遮罩:把多边形填实
|
|
1399
|
+
const maskCan = surface(w, h);
|
|
1400
|
+
const mg = maskCan.getContext('2d');
|
|
1401
|
+
mg.fillStyle = '#000';
|
|
1402
|
+
mg.beginPath();
|
|
1403
|
+
mg.moveTo(cand.pts[0][0] - x0, cand.pts[0][1] - y0);
|
|
1404
|
+
for (let i = 1; i < cand.pts.length; i += 1) mg.lineTo(cand.pts[i][0] - x0, cand.pts[i][1] - y0);
|
|
1405
|
+
mg.closePath();
|
|
1406
|
+
for (const hole of holes) {
|
|
1407
|
+
mg.moveTo(hole.pts[0][0] - x0, hole.pts[0][1] - y0);
|
|
1408
|
+
for (let i = 1; i < hole.pts.length; i += 1) mg.lineTo(hole.pts[i][0] - x0, hole.pts[i][1] - y0);
|
|
1409
|
+
mg.closePath();
|
|
1410
|
+
}
|
|
1411
|
+
mg.fill('evenodd');
|
|
1412
|
+
// 参考图这块 + 用遮罩抠出来
|
|
1413
|
+
const colCan = surface(w, h);
|
|
1414
|
+
const cg = colCan.getContext('2d');
|
|
1415
|
+
cg.drawImage(refCanvas, rect.x + x0, rect.y + y0, w, h, 0, 0, w, h);
|
|
1416
|
+
cg.globalCompositeOperation = 'destination-in';
|
|
1417
|
+
cg.drawImage(maskCan, 0, 0);
|
|
1418
|
+
const data = cg.getImageData(0, 0, w, h).data;
|
|
1419
|
+
// 内部主色(5 位量化取最高档的均值)
|
|
1420
|
+
const buckets = new Map();
|
|
1421
|
+
for (let i = 0; i < data.length; i += 4 * 3) {
|
|
1422
|
+
if (data[i + 3] < 32) continue;
|
|
1423
|
+
const key = ((data[i] >> 3) << 10) | ((data[i + 1] >> 3) << 5) | (data[i + 2] >> 3);
|
|
1424
|
+
const cur = buckets.get(key) || { r: 0, g: 0, b: 0, n: 0 };
|
|
1425
|
+
cur.r += data[i];
|
|
1426
|
+
cur.g += data[i + 1];
|
|
1427
|
+
cur.b += data[i + 2];
|
|
1428
|
+
cur.n += 1;
|
|
1429
|
+
buckets.set(key, cur);
|
|
1430
|
+
}
|
|
1431
|
+
if (buckets.size === 0) continue;
|
|
1432
|
+
const top = [...buckets.values()].sort((a, b) => b.n - a.n)[0];
|
|
1433
|
+
const rgb = [top.r / top.n, top.g / top.n, top.b / top.n];
|
|
1434
|
+
let best = palette[0];
|
|
1435
|
+
let bestD = Infinity;
|
|
1436
|
+
for (const c of palette) {
|
|
1437
|
+
const d = (c.rgb[0] - rgb[0]) ** 2 + (c.rgb[1] - rgb[1]) ** 2 + (c.rgb[2] - rgb[2]) ** 2;
|
|
1438
|
+
if (d < bestD) {
|
|
1439
|
+
bestD = d;
|
|
1440
|
+
best = c;
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
fills.push({
|
|
1444
|
+
points: cand.pts.map((p) => [Math.round(rect.x + p[0]), Math.round(rect.y + p[1])]),
|
|
1445
|
+
holes: holes.map((h) => h.pts.map((p) => [Math.round(rect.x + p[0]), Math.round(rect.y + p[1])])),
|
|
1446
|
+
color: best ? best.hex : toHex(Math.round(rgb[0]), Math.round(rgb[1]), Math.round(rgb[2])),
|
|
1447
|
+
area: Math.round(cand.area),
|
|
1448
|
+
});
|
|
1449
|
+
}
|
|
1450
|
+
return fills;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
/** Zhang-Suen 细化:把线压成 1px 骨架,用于「中心线」描线。 */
|
|
1454
|
+
function thinning(mask, w, h) {
|
|
1455
|
+
const img = Uint8Array.from(mask);
|
|
1456
|
+
const at = (x, y) => (x < 0 || y < 0 || x >= w || y >= h ? 0 : img[y * w + x]);
|
|
1457
|
+
let changed = true;
|
|
1458
|
+
let guard = 0;
|
|
1459
|
+
while (changed && guard < 40) {
|
|
1460
|
+
guard += 1;
|
|
1461
|
+
changed = false;
|
|
1462
|
+
for (let step = 0; step < 2; step += 1) {
|
|
1463
|
+
const del = [];
|
|
1464
|
+
for (let y = 1; y < h - 1; y += 1) {
|
|
1465
|
+
for (let x = 1; x < w - 1; x += 1) {
|
|
1466
|
+
if (!img[y * w + x]) continue;
|
|
1467
|
+
const p2 = at(x, y - 1);
|
|
1468
|
+
const p3 = at(x + 1, y - 1);
|
|
1469
|
+
const p4 = at(x + 1, y);
|
|
1470
|
+
const p5 = at(x + 1, y + 1);
|
|
1471
|
+
const p6 = at(x, y + 1);
|
|
1472
|
+
const p7 = at(x - 1, y + 1);
|
|
1473
|
+
const p8 = at(x - 1, y);
|
|
1474
|
+
const p9 = at(x - 1, y - 1);
|
|
1475
|
+
const B = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;
|
|
1476
|
+
if (B < 2 || B > 6) continue;
|
|
1477
|
+
const seq = [p2, p3, p4, p5, p6, p7, p8, p9, p2];
|
|
1478
|
+
let a = 0;
|
|
1479
|
+
for (let i = 0; i < 8; i += 1) if (seq[i] === 0 && seq[i + 1] === 1) a += 1;
|
|
1480
|
+
if (a !== 1) continue;
|
|
1481
|
+
if (step === 0) {
|
|
1482
|
+
if (p2 * p4 * p6 !== 0) continue;
|
|
1483
|
+
if (p4 * p6 * p8 !== 0) continue;
|
|
1484
|
+
} else {
|
|
1485
|
+
if (p2 * p4 * p8 !== 0) continue;
|
|
1486
|
+
if (p2 * p6 * p8 !== 0) continue;
|
|
1487
|
+
}
|
|
1488
|
+
del.push(y * w + x);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
if (del.length > 0) {
|
|
1492
|
+
changed = true;
|
|
1493
|
+
for (const p of del) img[p] = 0;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
return img;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
/** 骨架 → 折线:从端点走一遍,剩下的环再走一遍。 */
|
|
1501
|
+
function skeletonPaths(skel, w, h) {
|
|
1502
|
+
const deg = new Uint8Array(w * h);
|
|
1503
|
+
const N8 = [[-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0]];
|
|
1504
|
+
for (let y = 0; y < h; y += 1) {
|
|
1505
|
+
for (let x = 0; x < w; x += 1) {
|
|
1506
|
+
if (!skel[y * w + x]) continue;
|
|
1507
|
+
let d = 0;
|
|
1508
|
+
for (const [dx, dy] of N8) {
|
|
1509
|
+
const nx = x + dx;
|
|
1510
|
+
const ny = y + dy;
|
|
1511
|
+
if (nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
|
|
1512
|
+
if (skel[ny * w + nx]) d += 1;
|
|
1513
|
+
}
|
|
1514
|
+
deg[y * w + x] = d;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
const visited = new Uint8Array(w * h);
|
|
1518
|
+
const paths = [];
|
|
1519
|
+
const walk = (sx, sy) => {
|
|
1520
|
+
const path = [[sx, sy]];
|
|
1521
|
+
visited[sy * w + sx] = 1;
|
|
1522
|
+
let x = sx;
|
|
1523
|
+
let y = sy;
|
|
1524
|
+
for (;;) {
|
|
1525
|
+
let next = null;
|
|
1526
|
+
for (const [dx, dy] of N8) {
|
|
1527
|
+
const nx = x + dx;
|
|
1528
|
+
const ny = y + dy;
|
|
1529
|
+
if (nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
|
|
1530
|
+
const q = ny * w + nx;
|
|
1531
|
+
if (skel[q] && !visited[q]) {
|
|
1532
|
+
next = [nx, ny];
|
|
1533
|
+
break;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
if (!next) break;
|
|
1537
|
+
visited[next[1] * w + next[0]] = 1;
|
|
1538
|
+
path.push(next);
|
|
1539
|
+
x = next[0];
|
|
1540
|
+
y = next[1];
|
|
1541
|
+
}
|
|
1542
|
+
return path;
|
|
1543
|
+
};
|
|
1544
|
+
for (let y = 0; y < h; y += 1) {
|
|
1545
|
+
for (let x = 0; x < w; x += 1) {
|
|
1546
|
+
const p = y * w + x;
|
|
1547
|
+
if (skel[p] && deg[p] === 1 && !visited[p]) paths.push(walk(x, y));
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
for (let y = 0; y < h; y += 1) {
|
|
1551
|
+
for (let x = 0; x < w; x += 1) {
|
|
1552
|
+
const p = y * w + x;
|
|
1553
|
+
if (skel[p] && !visited[p]) paths.push(walk(x, y));
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
return paths;
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
/** 三次贝塞尔在 t 处的点。 */
|
|
1560
|
+
function bezierPoint(seg, t) {
|
|
1561
|
+
const p0 = seg[0];
|
|
1562
|
+
const c1 = seg[1];
|
|
1563
|
+
const c2 = seg[2];
|
|
1564
|
+
const p1 = seg[3];
|
|
1565
|
+
const mt = 1 - t;
|
|
1566
|
+
const b0 = mt * mt * mt;
|
|
1567
|
+
const b1 = 3 * t * mt * mt;
|
|
1568
|
+
const b2 = 3 * t * t * mt;
|
|
1569
|
+
const b3 = t * t * t;
|
|
1570
|
+
return [
|
|
1571
|
+
p0[0] * b0 + c1[0] * b1 + c2[0] * b2 + p1[0] * b3,
|
|
1572
|
+
p0[1] * b0 + c1[1] * b1 + c2[1] * b2 + p1[1] * b3,
|
|
1573
|
+
];
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
/**
|
|
1577
|
+
* 三次贝塞尔拟合(Schneider 的自动拟合思路):把一串点拟成尽量少的曲线段。
|
|
1578
|
+
* 比磨圆更根本——长弧能一条曲线走完,不再靠大量短线段凑。
|
|
1579
|
+
* 返回 [[p0, c1, c2, p1], …]。
|
|
1580
|
+
*/
|
|
1581
|
+
function fitBezier(points, tolerance) {
|
|
1582
|
+
const tol = Math.max(0.2, Number(tolerance) || 1);
|
|
1583
|
+
const pts = points.filter((p) => Array.isArray(p) && Number.isFinite(p[0]));
|
|
1584
|
+
const segs = [];
|
|
1585
|
+
if (pts.length < 2) return segs;
|
|
1586
|
+
const unit = (a, b) => {
|
|
1587
|
+
const dx = b[0] - a[0];
|
|
1588
|
+
const dy = b[1] - a[1];
|
|
1589
|
+
const n = Math.hypot(dx, dy) || 1;
|
|
1590
|
+
return [dx / n, dy / n];
|
|
1591
|
+
};
|
|
1592
|
+
const fitOne = (p, t1, t2) => {
|
|
1593
|
+
const last = p.length - 1;
|
|
1594
|
+
// 弦长参数化
|
|
1595
|
+
const u = [0];
|
|
1596
|
+
for (let i = 1; i <= last; i += 1) {
|
|
1597
|
+
u.push(u[i - 1] + (Math.hypot(p[i][0] - p[i - 1][0], p[i][1] - p[i - 1][1]) || 1e-4));
|
|
1598
|
+
}
|
|
1599
|
+
const total = u[last] || 1;
|
|
1600
|
+
for (let i = 0; i <= last; i += 1) u[i] /= total;
|
|
1601
|
+
const C = [[0, 0], [0, 0]];
|
|
1602
|
+
const X = [0, 0];
|
|
1603
|
+
for (let i = 0; i <= last; i += 1) {
|
|
1604
|
+
const t = u[i];
|
|
1605
|
+
const mt = 1 - t;
|
|
1606
|
+
const b0 = mt * mt * mt;
|
|
1607
|
+
const b1 = 3 * t * mt * mt;
|
|
1608
|
+
const b2 = 3 * t * t * mt;
|
|
1609
|
+
const b3 = t * t * t;
|
|
1610
|
+
const a1 = [t1[0] * b1, t1[1] * b1];
|
|
1611
|
+
const a2 = [t2[0] * b2, t2[1] * b2];
|
|
1612
|
+
C[0][0] += a1[0] * a1[0] + a1[1] * a1[1];
|
|
1613
|
+
C[0][1] += a1[0] * a2[0] + a1[1] * a2[1];
|
|
1614
|
+
C[1][0] = C[0][1];
|
|
1615
|
+
C[1][1] += a2[0] * a2[0] + a2[1] * a2[1];
|
|
1616
|
+
const px = p[i][0] - (p[0][0] * (b0 + b1) + p[last][0] * (b2 + b3));
|
|
1617
|
+
const py = p[i][1] - (p[0][1] * (b0 + b1) + p[last][1] * (b2 + b3));
|
|
1618
|
+
X[0] += a1[0] * px + a1[1] * py;
|
|
1619
|
+
X[1] += a2[0] * px + a2[1] * py;
|
|
1620
|
+
}
|
|
1621
|
+
const det = C[0][0] * C[1][1] - C[0][1] * C[1][0];
|
|
1622
|
+
let a1;
|
|
1623
|
+
let a2;
|
|
1624
|
+
if (Math.abs(det) > 1e-9) {
|
|
1625
|
+
a1 = (X[0] * C[1][1] - X[1] * C[0][1]) / det;
|
|
1626
|
+
a2 = (C[0][0] * X[1] - C[1][0] * X[0]) / det;
|
|
1627
|
+
} else {
|
|
1628
|
+
a1 = 0;
|
|
1629
|
+
a2 = 0;
|
|
1630
|
+
}
|
|
1631
|
+
const chord = Math.hypot(p[last][0] - p[0][0], p[last][1] - p[0][1]) || 1;
|
|
1632
|
+
if (!(a1 > chord * 0.02)) a1 = chord / 3;
|
|
1633
|
+
if (!(a2 > chord * 0.02)) a2 = chord / 3;
|
|
1634
|
+
const seg = [
|
|
1635
|
+
p[0],
|
|
1636
|
+
[p[0][0] + t1[0] * a1, p[0][1] + t1[1] * a1],
|
|
1637
|
+
[p[last][0] + t2[0] * a2, p[last][1] + t2[1] * a2],
|
|
1638
|
+
p[last],
|
|
1639
|
+
];
|
|
1640
|
+
// 取每段的采样误差,找最离谱的那点当切分位置
|
|
1641
|
+
let maxErr = 0;
|
|
1642
|
+
let maxIdx = Math.floor(last / 2);
|
|
1643
|
+
for (let i = 1; i < last; i += 1) {
|
|
1644
|
+
const q = bezierPoint(seg, u[i]);
|
|
1645
|
+
const d = Math.hypot(q[0] - p[i][0], q[1] - p[i][1]);
|
|
1646
|
+
if (d > maxErr) {
|
|
1647
|
+
maxErr = d;
|
|
1648
|
+
maxIdx = i;
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
return { seg, maxErr, maxIdx };
|
|
1652
|
+
};
|
|
1653
|
+
const fitRange = (s, e, t1, t2, depth) => {
|
|
1654
|
+
const p = pts.slice(s, e + 1);
|
|
1655
|
+
if (p.length === 2) {
|
|
1656
|
+
const c1 = [p[0][0] + (p[1][0] - p[0][0]) / 3, p[0][1] + (p[1][1] - p[0][1]) / 3];
|
|
1657
|
+
const c2 = [p[0][0] + (p[1][0] - p[0][0]) * 2 / 3, p[0][1] + (p[1][1] - p[0][1]) * 2 / 3];
|
|
1658
|
+
segs.push([p[0], c1, c2, p[1]]);
|
|
1659
|
+
return;
|
|
1660
|
+
}
|
|
1661
|
+
const r = fitOne(p, t1, t2);
|
|
1662
|
+
if (r.maxErr <= tol || p.length <= 3 || depth >= 12) {
|
|
1663
|
+
segs.push(r.seg);
|
|
1664
|
+
return;
|
|
1665
|
+
}
|
|
1666
|
+
const k = s + Math.max(1, Math.min(p.length - 2, r.maxIdx));
|
|
1667
|
+
const center = unit(pts[k - 1], pts[k + 1]);
|
|
1668
|
+
fitRange(s, k, t1, [-center[0], -center[1]], depth + 1);
|
|
1669
|
+
fitRange(k, e, [center[0], center[1]], t2, depth + 1);
|
|
1670
|
+
};
|
|
1671
|
+
fitRange(0, pts.length - 1, unit(pts[0], pts[1]), unit(pts[pts.length - 1], pts[pts.length - 2]), 0);
|
|
1672
|
+
return segs;
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
/** 把拟合出的曲线段采样回点串(落笔还是用点,笔刷引擎才吃得到压感)。 */
|
|
1676
|
+
function bezierSample(segs, step, cap) {
|
|
1677
|
+
const out = [];
|
|
1678
|
+
if (!Array.isArray(segs) || segs.length === 0) return out;
|
|
1679
|
+
const max = Math.max(8, Number(cap) || 400);
|
|
1680
|
+
for (const seg of segs) {
|
|
1681
|
+
const len = Math.hypot(seg[1][0] - seg[0][0], seg[1][1] - seg[0][1])
|
|
1682
|
+
+ Math.hypot(seg[2][0] - seg[1][0], seg[2][1] - seg[1][1])
|
|
1683
|
+
+ Math.hypot(seg[3][0] - seg[2][0], seg[3][1] - seg[2][1]);
|
|
1684
|
+
const n = Math.max(2, Math.min(64, Math.ceil(len / Math.max(1, Number(step) || 3))));
|
|
1685
|
+
for (let i = out.length ? 1 : 0; i <= n; i += 1) {
|
|
1686
|
+
const q = bezierPoint(seg, i / n);
|
|
1687
|
+
out.push([q[0], q[1]]);
|
|
1688
|
+
if (out.length >= max) return out;
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
return out;
|
|
1692
|
+
}
|
|
1693
|
+
|
|
1694
|
+
/**
|
|
1695
|
+
* 参考图 → 折线:掩码 → 模糊 → 轮廓 → 简化 → 过滤碎片。
|
|
1696
|
+
* 输出坐标是这块区域内的像素坐标(半像素精度),调用方自己加偏移。
|
|
1697
|
+
*/
|
|
1698
|
+
function traceInk(imageData, w, h, opts = {}) {
|
|
1699
|
+
const center = opts.mode === 'center';
|
|
1700
|
+
const tolerance = Math.max(0.5, Number(opts.tolerance) || (center ? 1.2 : 2.5));
|
|
1701
|
+
const minLength = Math.max(2, Number(opts.minLength) || 12);
|
|
1702
|
+
const maxLines = Math.max(1, Number(opts.maxLines) || 80);
|
|
1703
|
+
const maxPointsPerLine = Math.max(6, Number(opts.maxPointsPerLine) || 160);
|
|
1704
|
+
const smoothIters = Math.max(0, Math.min(3, opts.smooth === undefined ? 1 : Number(opts.smooth) || 0));
|
|
1705
|
+
const wantFit = opts.fit === true;
|
|
1706
|
+
const fitStep = Math.max(1.5, Number(opts.fitStep) || 3);
|
|
1707
|
+
const tolFit = Math.max(0.3, Number(opts.fitTolerance) || tolerance * 0.9);
|
|
1708
|
+
let lines;
|
|
1709
|
+
if (center) {
|
|
1710
|
+
// 中心线:先细化成 1px 骨架,再从端点走线
|
|
1711
|
+
const skel = thinning(inkMask(imageData, w, h, opts.threshold), w, h);
|
|
1712
|
+
lines = skeletonPaths(skel, w, h).map((pts) => pts.map((p) => [p[0] * 2, p[1] * 2]));
|
|
1713
|
+
} else {
|
|
1714
|
+
lines = stitchSegments(marchingSquares(blurMask(inkMask(imageData, w, h, opts.threshold), w, h), w, h));
|
|
1715
|
+
}
|
|
1716
|
+
const out = [];
|
|
1717
|
+
const closedFlags = [];
|
|
1718
|
+
let dropped = 0;
|
|
1719
|
+
for (const raw of lines) {
|
|
1720
|
+
const pts = raw.map((p) => [p[0] / 2, p[1] / 2]);
|
|
1721
|
+
// 闭合环去掉重复的末点(draw 会自己收尾)
|
|
1722
|
+
const a = pts[0];
|
|
1723
|
+
const b = pts[pts.length - 1];
|
|
1724
|
+
const closed = pts.length > 3 && Math.hypot(a[0] - b[0], a[1] - b[1]) < 1.5;
|
|
1725
|
+
if (closed) pts.pop();
|
|
1726
|
+
let len = 0;
|
|
1727
|
+
for (let i = 1; i < pts.length; i += 1) len += Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]);
|
|
1728
|
+
if (len < minLength) {
|
|
1729
|
+
dropped += 1;
|
|
1730
|
+
continue;
|
|
1731
|
+
}
|
|
1732
|
+
let simplified = rdp(pts, tolerance);
|
|
1733
|
+
// 贝塞尔拟合优先:长弧一条曲线走完,比磨圆更「像手画的」
|
|
1734
|
+
if (wantFit && simplified.length >= 3) {
|
|
1735
|
+
const segsFit = fitBezier(simplified, tolFit);
|
|
1736
|
+
const sampled = bezierSample(segsFit, fitStep, maxPointsPerLine * 2);
|
|
1737
|
+
if (sampled.length >= 2) {
|
|
1738
|
+
// 闭合环拟合容易外冲,夹回画布内,否则色块 bbox 会虚大
|
|
1739
|
+
simplified = sampled.map((p) => [
|
|
1740
|
+
Math.max(0, Math.min(w - 1, p[0])),
|
|
1741
|
+
Math.max(0, Math.min(h - 1, p[1])),
|
|
1742
|
+
]);
|
|
1743
|
+
}
|
|
1744
|
+
} else if (smoothIters > 0) {
|
|
1745
|
+
// 磨圆一遍再收紧:像素阶梯走出来的折角会被抹掉,线看起来才是「画」出来的
|
|
1746
|
+
simplified = smoothPath(simplified, closed, smoothIters);
|
|
1747
|
+
}
|
|
1748
|
+
if (simplified.length > maxPointsPerLine) {
|
|
1749
|
+
const stepN = Math.ceil(simplified.length / maxPointsPerLine);
|
|
1750
|
+
simplified = simplified.filter((_, i) => i % stepN === 0 || i === simplified.length - 1);
|
|
1751
|
+
}
|
|
1752
|
+
out.push(simplified.map((p) => [Number(p[0].toFixed(1)), Number(p[1].toFixed(1))]));
|
|
1753
|
+
closedFlags.push(closed);
|
|
1754
|
+
if (out.length >= maxLines) break;
|
|
1755
|
+
}
|
|
1756
|
+
const points = out.reduce((n, l) => n + l.length, 0);
|
|
1757
|
+
return { paths: out, closed: closedFlags, mode: center ? 'center' : 'edge', lines: out.length, points, dropped, tolerance, smooth: smoothIters, fit: wantFit, threshold: opts.threshold === undefined ? 0.45 : Number(opts.threshold) };
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
/** 主色聚类:真 k-means(初值取量化后占比最高的几档,10 轮迭代)。 */
|
|
1761
|
+
function kmeansColors(data, k, stepPx = 2) {
|
|
1762
|
+
const samples = [];
|
|
1763
|
+
for (let i = 0; i < data.length; i += 4 * stepPx) {
|
|
1764
|
+
if (data[i + 3] < 16) continue;
|
|
1765
|
+
samples.push([data[i], data[i + 1], data[i + 2]]);
|
|
1766
|
+
}
|
|
1767
|
+
if (samples.length === 0) throw new Error('这块区域里没有不透明的像素');
|
|
1768
|
+
const kk = Math.max(2, Math.min(8, Math.round(k)));
|
|
1769
|
+
// 初值:先按 5 位量化统计,取占比最高的 kk 档的均值
|
|
1770
|
+
const buckets = new Map();
|
|
1771
|
+
for (const s of samples) {
|
|
1772
|
+
const key = ((s[0] >> 3) << 10) | ((s[1] >> 3) << 5) | (s[2] >> 3);
|
|
1773
|
+
const cur = buckets.get(key) || { r: 0, g: 0, b: 0, n: 0 };
|
|
1774
|
+
cur.r += s[0];
|
|
1775
|
+
cur.g += s[1];
|
|
1776
|
+
cur.b += s[2];
|
|
1777
|
+
cur.n += 1;
|
|
1778
|
+
buckets.set(key, cur);
|
|
1779
|
+
}
|
|
1780
|
+
let centers = [...buckets.values()]
|
|
1781
|
+
.sort((a, b) => b.n - a.n)
|
|
1782
|
+
.slice(0, kk)
|
|
1783
|
+
.map((v) => [v.r / v.n, v.g / v.n, v.b / v.n]);
|
|
1784
|
+
while (centers.length < kk) centers.push([255, 255, 255]);
|
|
1785
|
+
for (let iter = 0; iter < 10; iter += 1) {
|
|
1786
|
+
const sums = centers.map(() => [0, 0, 0, 0]);
|
|
1787
|
+
for (const s of samples) {
|
|
1788
|
+
let best = 0;
|
|
1789
|
+
let bestD = Infinity;
|
|
1790
|
+
for (let c = 0; c < centers.length; c += 1) {
|
|
1791
|
+
const dr = s[0] - centers[c][0];
|
|
1792
|
+
const dg = s[1] - centers[c][1];
|
|
1793
|
+
const db = s[2] - centers[c][2];
|
|
1794
|
+
const d = dr * dr + dg * dg + db * db;
|
|
1795
|
+
if (d < bestD) {
|
|
1796
|
+
bestD = d;
|
|
1797
|
+
best = c;
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
const acc = sums[best];
|
|
1801
|
+
acc[0] += s[0];
|
|
1802
|
+
acc[1] += s[1];
|
|
1803
|
+
acc[2] += s[2];
|
|
1804
|
+
acc[3] += 1;
|
|
1805
|
+
}
|
|
1806
|
+
centers = sums.map((acc, i) => (acc[3] ? [acc[0] / acc[3], acc[1] / acc[3], acc[2] / acc[3]] : centers[i]));
|
|
1807
|
+
}
|
|
1808
|
+
// 用最终中心再数一遍归属,算占比
|
|
1809
|
+
const counts = centers.map(() => 0);
|
|
1810
|
+
for (const s of samples) {
|
|
1811
|
+
let best = 0;
|
|
1812
|
+
let bestD = Infinity;
|
|
1813
|
+
for (let c = 0; c < centers.length; c += 1) {
|
|
1814
|
+
const dr = s[0] - centers[c][0];
|
|
1815
|
+
const dg = s[1] - centers[c][1];
|
|
1816
|
+
const db = s[2] - centers[c][2];
|
|
1817
|
+
const d = dr * dr + dg * dg + db * db;
|
|
1818
|
+
if (d < bestD) {
|
|
1819
|
+
bestD = d;
|
|
1820
|
+
best = c;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
counts[best] += 1;
|
|
1824
|
+
}
|
|
1825
|
+
return centers
|
|
1826
|
+
.map((c, i) => ({
|
|
1827
|
+
hex: toHex(Math.round(c[0]), Math.round(c[1]), Math.round(c[2])),
|
|
1828
|
+
rgb: [Math.round(c[0]), Math.round(c[1]), Math.round(c[2])],
|
|
1829
|
+
ratio: Number(((counts[i] / samples.length) * 100).toFixed(1)),
|
|
1830
|
+
pixels: counts[i],
|
|
1831
|
+
}))
|
|
1832
|
+
.filter((c) => c.pixels > 0)
|
|
1833
|
+
.sort((a, b) => b.ratio - a.ratio);
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
/**
|
|
1837
|
+
* 宿主塞过来的像素任务:全部在这里用 <canvas> 算完再回传。
|
|
1838
|
+
* 宿主只搬运结果,不引任何图像解码依赖。
|
|
1839
|
+
*/
|
|
1840
|
+
function runPixelTask(job, doc) {
|
|
1841
|
+
if (!doc) return { ok: false, error: '文档还没加载' };
|
|
1842
|
+
const kind = job.kind;
|
|
1843
|
+
const params = job.params || {};
|
|
1844
|
+
|
|
1845
|
+
if (kind === 'crop' || kind === 'ref') {
|
|
1846
|
+
const rect = parseRectIn(params.rect, doc);
|
|
1847
|
+
const zoom = params.zoom === undefined ? 1 : Number(params.zoom);
|
|
1848
|
+
let src = null;
|
|
1849
|
+
let background = null;
|
|
1850
|
+
if (kind === 'crop') {
|
|
1851
|
+
const target = params.layer ? docOnlyLayer(doc, params.layer) : doc;
|
|
1852
|
+
src = surface(doc.width, doc.height);
|
|
1853
|
+
renderDoc(src, target, { withBackground: true });
|
|
1854
|
+
background = null;
|
|
1855
|
+
} else {
|
|
1856
|
+
const layer = params.layer
|
|
1857
|
+
? layersOf(doc).find((l) => l.image && (l.id === params.layer || l.name === params.layer))
|
|
1858
|
+
: refLayerOf(doc);
|
|
1859
|
+
if (!layer) throw new Error('画布上没有可用的参考图');
|
|
1860
|
+
src = surface(doc.width, doc.height);
|
|
1861
|
+
const g = src.getContext('2d');
|
|
1862
|
+
g.clearRect(0, 0, src.width, src.height);
|
|
1863
|
+
drawImageLayer(g, layer.image);
|
|
1864
|
+
}
|
|
1865
|
+
const out = cropCanvas(src, rect, zoom, background);
|
|
1866
|
+
return { ok: true, dataUrl: out.dataUrl, width: out.width, height: out.height, zoom: out.zoom, rect, layer: params.layer || null };
|
|
1867
|
+
}
|
|
1868
|
+
|
|
1869
|
+
if (kind === 'pick') {
|
|
1870
|
+
const x = Math.round(Number(params.x));
|
|
1871
|
+
const y = Math.round(Number(params.y));
|
|
1872
|
+
if (!Number.isFinite(x) || !Number.isFinite(y) || x < 0 || y < 0) throw new Error('pick 的坐标非法');
|
|
1873
|
+
const onCanvas = params.on === 'canvas';
|
|
1874
|
+
const srcDoc = onCanvas ? doc : (params.layer ? docOnlyLayer(doc, params.layer) : doc);
|
|
1875
|
+
const c = surface(doc.width, doc.height);
|
|
1876
|
+
renderDoc(c, srcDoc, { withBackground: onCanvas });
|
|
1877
|
+
const px = c.getContext('2d').getImageData(x, y, 1, 1).data;
|
|
1878
|
+
const alpha = Number((px[3] / 255).toFixed(3));
|
|
1879
|
+
return {
|
|
1880
|
+
ok: true,
|
|
1881
|
+
x,
|
|
1882
|
+
y,
|
|
1883
|
+
on: onCanvas ? 'canvas' : 'ref',
|
|
1884
|
+
hex: toHex(px[0], px[1], px[2]),
|
|
1885
|
+
rgb: [px[0], px[1], px[2]],
|
|
1886
|
+
alpha,
|
|
1887
|
+
ink: alpha > 0.15,
|
|
1888
|
+
};
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
if (kind === 'compare') {
|
|
1892
|
+
const layer = params.layer
|
|
1893
|
+
? layersOf(doc).find((l) => l.image && (l.id === params.layer || l.name === params.layer))
|
|
1894
|
+
: refLayerOf(doc);
|
|
1895
|
+
if (!layer) throw new Error('画布上没有可用的参考图:先 import 一张');
|
|
1896
|
+
const W = doc.width || 1024;
|
|
1897
|
+
const H = doc.height || 1024;
|
|
1898
|
+
const rect = parseRectIn(params.rect, doc);
|
|
1899
|
+
|
|
1900
|
+
const ref = surface(W, H);
|
|
1901
|
+
const rg = ref.getContext('2d');
|
|
1902
|
+
rg.clearRect(0, 0, W, H);
|
|
1903
|
+
drawImageLayer(rg, layer.image);
|
|
1904
|
+
|
|
1905
|
+
const drawn = surface(W, H);
|
|
1906
|
+
renderDoc(drawn, docWithoutImages(doc), { withBackground: false });
|
|
1907
|
+
|
|
1908
|
+
const rd = rg.getImageData(rect.x, rect.y, rect.w, rect.h).data;
|
|
1909
|
+
const dd = drawn.getContext('2d').getImageData(rect.x, rect.y, rect.w, rect.h).data;
|
|
1910
|
+
const diff = surface(rect.w, rect.h);
|
|
1911
|
+
const dg = diff.getContext('2d');
|
|
1912
|
+
const diffData = dg.createImageData(rect.w, rect.h);
|
|
1913
|
+
const out = diffData.data;
|
|
1914
|
+
|
|
1915
|
+
let refInk = 0;
|
|
1916
|
+
let drawInk = 0;
|
|
1917
|
+
let covered = 0;
|
|
1918
|
+
let extra = 0;
|
|
1919
|
+
let sumDiff = 0;
|
|
1920
|
+
let missedMinX = Infinity;
|
|
1921
|
+
let missedMinY = Infinity;
|
|
1922
|
+
let missedMaxX = -Infinity;
|
|
1923
|
+
let missedMaxY = -Infinity;
|
|
1924
|
+
// 32×32 的网格,用来告诉模型「哪一格还没画到」
|
|
1925
|
+
const GRID = 32;
|
|
1926
|
+
const cells = new Map();
|
|
1927
|
+
|
|
1928
|
+
for (let i = 0, p = 0; i < rd.length; i += 4, p += 1) {
|
|
1929
|
+
const ra = rd[i + 3];
|
|
1930
|
+
const da = dd[i + 3];
|
|
1931
|
+
const isRef = ra > 12;
|
|
1932
|
+
const isDraw = da > 12;
|
|
1933
|
+
const px = rect.x + (p % rect.w);
|
|
1934
|
+
const py = rect.y + Math.floor(p / rect.w);
|
|
1935
|
+
const o = i;
|
|
1936
|
+
if (isRef) refInk += 1;
|
|
1937
|
+
if (isDraw) drawInk += 1;
|
|
1938
|
+
if (isRef && isDraw) {
|
|
1939
|
+
covered += 1;
|
|
1940
|
+
sumDiff += (Math.abs(rd[i] - dd[i]) + Math.abs(rd[i + 1] - dd[i + 1]) + Math.abs(rd[i + 2] - dd[i + 2])) / 3;
|
|
1941
|
+
// 两边都有:按平均色显示成暗灰,方便看出形变
|
|
1942
|
+
out[o] = Math.round((rd[i] + dd[i]) / 2);
|
|
1943
|
+
out[o + 1] = Math.round((rd[i + 1] + dd[i + 1]) / 2);
|
|
1944
|
+
out[o + 2] = Math.round((rd[i + 2] + dd[i + 2]) / 2);
|
|
1945
|
+
out[o + 3] = 140;
|
|
1946
|
+
} else if (isRef) {
|
|
1947
|
+
// 参考有、你没画 = 品红(漏画)
|
|
1948
|
+
out[o] = 255;
|
|
1949
|
+
out[o + 1] = 0;
|
|
1950
|
+
out[o + 2] = 255;
|
|
1951
|
+
out[o + 3] = 235;
|
|
1952
|
+
if (px < missedMinX) missedMinX = px;
|
|
1953
|
+
if (py < missedMinY) missedMinY = py;
|
|
1954
|
+
if (px > missedMaxX) missedMaxX = px;
|
|
1955
|
+
if (py > missedMaxY) missedMaxY = py;
|
|
1956
|
+
const key = `${Math.floor(((px - rect.x) / rect.w) * GRID)},${Math.floor(((py - rect.y) / rect.h) * GRID)}`;
|
|
1957
|
+
const cell = cells.get(key) || { x0: Infinity, y0: Infinity, x1: -Infinity, y1: -Infinity, n: 0 };
|
|
1958
|
+
cell.n += 1;
|
|
1959
|
+
if (px < cell.x0) cell.x0 = px;
|
|
1960
|
+
if (py < cell.y0) cell.y0 = py;
|
|
1961
|
+
if (px > cell.x1) cell.x1 = px;
|
|
1962
|
+
if (py > cell.y1) cell.y1 = py;
|
|
1963
|
+
cells.set(key, cell);
|
|
1964
|
+
} else if (isDraw) {
|
|
1965
|
+
// 你画了、参考没有 = 青(多画)
|
|
1966
|
+
out[o] = 0;
|
|
1967
|
+
out[o + 1] = 210;
|
|
1968
|
+
out[o + 2] = 255;
|
|
1969
|
+
out[o + 3] = 200;
|
|
1970
|
+
extra += 1;
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
dg.putImageData(diffData, 0, 0);
|
|
1974
|
+
|
|
1975
|
+
const gaps = [...cells.values()].sort((a, b) => b.n - a.n).slice(0, 4).map((c) => ({
|
|
1976
|
+
x: Math.round(c.x0),
|
|
1977
|
+
y: Math.round(c.y0),
|
|
1978
|
+
w: Math.round(c.x1 - c.x0) + 1,
|
|
1979
|
+
h: Math.round(c.y1 - c.y0) + 1,
|
|
1980
|
+
missed: c.n,
|
|
1981
|
+
}));
|
|
1982
|
+
|
|
1983
|
+
const metrics = {
|
|
1984
|
+
rect,
|
|
1985
|
+
refInkPixels: refInk,
|
|
1986
|
+
drawnInkPixels: drawInk,
|
|
1987
|
+
coveredPixels: covered,
|
|
1988
|
+
extraPixels: extra,
|
|
1989
|
+
// 参考图的笔画里,有多少被你盖住了
|
|
1990
|
+
coveragePct: refInk ? Number(((covered / refInk) * 100).toFixed(1)) : 100,
|
|
1991
|
+
// 你画的东西里,有多少是参考图里没有的
|
|
1992
|
+
extraPct: drawInk ? Number(((extra / drawInk) * 100).toFixed(1)) : 0,
|
|
1993
|
+
meanColorDiff: covered ? Number((sumDiff / covered).toFixed(1)) : null,
|
|
1994
|
+
missedBounds: Number.isFinite(missedMinX)
|
|
1995
|
+
? { x: Math.round(missedMinX), y: Math.round(missedMinY), w: Math.round(missedMaxX - missedMinX) + 1, h: Math.round(missedMaxY - missedMinY) + 1 }
|
|
1996
|
+
: null,
|
|
1997
|
+
topGaps: gaps,
|
|
1998
|
+
};
|
|
1999
|
+
return { ok: true, dataUrl: diff.toDataURL('image/png'), rect, metrics };
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
if (kind === 'fill') {
|
|
2003
|
+
const W = doc.width || 1024;
|
|
2004
|
+
const H = doc.height || 1024;
|
|
2005
|
+
const rect = parseRectIn(params.rect || {}, doc);
|
|
2006
|
+
const sx = Math.round(Number(params.x));
|
|
2007
|
+
const sy = Math.round(Number(params.y));
|
|
2008
|
+
if (!Number.isFinite(sx) || !Number.isFinite(sy) || sx < 0 || sy < 0 || sx >= W || sy >= H) {
|
|
2009
|
+
throw new Error('fill 的种子点超出画布');
|
|
2010
|
+
}
|
|
2011
|
+
// 边界只认笔迹(不含已填的色块),不然第二次填色会被自己的色块挡住
|
|
2012
|
+
const barrierDoc = {
|
|
2013
|
+
...docWithoutImages(doc),
|
|
2014
|
+
layers: layersOf(doc).map((l) => ({
|
|
2015
|
+
...l,
|
|
2016
|
+
image: null,
|
|
2017
|
+
strokes: (l.strokes || []).filter((s) => s.mode !== 'fill'),
|
|
2018
|
+
})),
|
|
2019
|
+
};
|
|
2020
|
+
const lineCan = surface(W, H);
|
|
2021
|
+
renderDoc(lineCan, barrierDoc, { withBackground: false });
|
|
2022
|
+
const lineData = lineCan.getContext('2d').getImageData(0, 0, W, H).data;
|
|
2023
|
+
const barrier = new Uint8Array(W * H);
|
|
2024
|
+
for (let i = 0, p = 0; i < lineData.length; i += 4, p += 1) {
|
|
2025
|
+
if (lineData[i + 3] > 24) barrier[p] = 1;
|
|
2026
|
+
}
|
|
2027
|
+
const seed = sy * W + sx;
|
|
2028
|
+
if (barrier[seed]) throw new Error('种子点落在线上:给一个区域内部的点(配合 pick 找空白)');
|
|
2029
|
+
// 泛洪:四连通,限定在 rect 内
|
|
2030
|
+
const region = new Uint8Array(W * H);
|
|
2031
|
+
const stack = new Int32Array(W * H);
|
|
2032
|
+
let top = 0;
|
|
2033
|
+
stack[top] = seed;
|
|
2034
|
+
top += 1;
|
|
2035
|
+
region[seed] = 1;
|
|
2036
|
+
let area = 0;
|
|
2037
|
+
const inRect = (x, y) => x >= rect.x && y >= rect.y && x < rect.x + rect.w && y < rect.y + rect.h;
|
|
2038
|
+
while (top > 0) {
|
|
2039
|
+
top -= 1;
|
|
2040
|
+
const p = stack[top];
|
|
2041
|
+
area += 1;
|
|
2042
|
+
if (area > W * H * 0.62) throw new Error('这块没被线围住(填下去会把整片背景都涂了):先有线稿,或给 rect 缩小范围');
|
|
2043
|
+
const x = p % W;
|
|
2044
|
+
const y = (p - x) / W;
|
|
2045
|
+
const cand = [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]];
|
|
2046
|
+
for (const [nx, ny] of cand) {
|
|
2047
|
+
if (nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
|
|
2048
|
+
if (!inRect(nx, ny)) continue;
|
|
2049
|
+
const q = ny * W + nx;
|
|
2050
|
+
if (region[q] || barrier[q]) continue;
|
|
2051
|
+
region[q] = 1;
|
|
2052
|
+
if (top >= stack.length) break;
|
|
2053
|
+
stack[top] = q;
|
|
2054
|
+
top += 1;
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
// 区域边界 → 多边形
|
|
2058
|
+
const segs = marchingSquares(region, W, H);
|
|
2059
|
+
// 轮廓嵌套:偶数层是区域(要涂),奇数层是洞(evenodd 挖掉)
|
|
2060
|
+
const loops = stitchSegments(segs)
|
|
2061
|
+
.map((raw) => {
|
|
2062
|
+
const pts = raw.map((p) => [p[0] / 2, p[1] / 2]);
|
|
2063
|
+
// 闭环的首点会在末尾重复一次;首尾同点时 rdp 会整条塌成两点,先去掉重尾
|
|
2064
|
+
if (pts.length > 3 && Math.hypot(pts[0][0] - pts[pts.length - 1][0], pts[0][1] - pts[pts.length - 1][1]) < 1.5) pts.pop();
|
|
2065
|
+
return { pts: rdp(pts, 1.5) };
|
|
2066
|
+
})
|
|
2067
|
+
.map((v) => ({ ...v, area: polygonArea(v.pts) }))
|
|
2068
|
+
.filter((v) => v.area > 40)
|
|
2069
|
+
.sort((a, b) => b.area - a.area)
|
|
2070
|
+
.slice(0, 40);
|
|
2071
|
+
const parents = loops.map(() => -1);
|
|
2072
|
+
for (let i = 0; i < loops.length; i += 1) {
|
|
2073
|
+
const p0 = loops[i].pts[0];
|
|
2074
|
+
let best = -1;
|
|
2075
|
+
for (let j = 0; j < i; j += 1) {
|
|
2076
|
+
if (loops[j].area <= loops[i].area) continue;
|
|
2077
|
+
if (!pointInPolygon(p0[0], p0[1], loops[j].pts)) continue;
|
|
2078
|
+
if (best === -1 || loops[j].area < loops[best].area) best = j;
|
|
2079
|
+
}
|
|
2080
|
+
parents[i] = best;
|
|
2081
|
+
}
|
|
2082
|
+
const depths = loops.map((_, i) => {
|
|
2083
|
+
let d = 0;
|
|
2084
|
+
let p = parents[i];
|
|
2085
|
+
while (p !== -1 && d < 8) {
|
|
2086
|
+
d += 1;
|
|
2087
|
+
p = parents[p];
|
|
2088
|
+
}
|
|
2089
|
+
return d;
|
|
2090
|
+
});
|
|
2091
|
+
const polys = [];
|
|
2092
|
+
for (let i = 0; i < loops.length; i += 1) {
|
|
2093
|
+
if (depths[i] % 2 !== 0) continue;
|
|
2094
|
+
const holes = loops
|
|
2095
|
+
.filter((_, j) => parents[j] === i)
|
|
2096
|
+
.slice(0, 10)
|
|
2097
|
+
.map((h) => h.pts.map((p) => [Math.round(p[0]), Math.round(p[1])]));
|
|
2098
|
+
polys.push({
|
|
2099
|
+
points: loops[i].pts.map((p) => [Math.round(p[0]), Math.round(p[1])]),
|
|
2100
|
+
holes,
|
|
2101
|
+
area: Math.round(loops[i].area),
|
|
2102
|
+
});
|
|
2103
|
+
if (polys.length >= 12) break;
|
|
2104
|
+
}
|
|
2105
|
+
// 颜色:给了就用;否则取参考图该点颜色,映射到参考图主色里最接近的一档
|
|
2106
|
+
let color = params.color || null;
|
|
2107
|
+
if (!color) {
|
|
2108
|
+
const ref = refLayerOf(doc);
|
|
2109
|
+
if (ref) {
|
|
2110
|
+
const refCan = surface(W, H);
|
|
2111
|
+
const rg = refCan.getContext('2d');
|
|
2112
|
+
rg.clearRect(0, 0, W, H);
|
|
2113
|
+
drawImageLayer(rg, ref.image);
|
|
2114
|
+
const refData = rg.getImageData(0, 0, W, H).data;
|
|
2115
|
+
const sxp = refData[seed * 4 + 3] > 16
|
|
2116
|
+
? [refData[seed * 4], refData[seed * 4 + 1], refData[seed * 4 + 2]]
|
|
2117
|
+
: [255, 255, 255];
|
|
2118
|
+
let palette = [];
|
|
2119
|
+
try {
|
|
2120
|
+
palette = kmeansColors(refData, 6, 8);
|
|
2121
|
+
} catch {
|
|
2122
|
+
palette = [];
|
|
2123
|
+
}
|
|
2124
|
+
let best = null;
|
|
2125
|
+
let bestD = Infinity;
|
|
2126
|
+
for (const c of palette) {
|
|
2127
|
+
const d = (c.rgb[0] - sxp[0]) ** 2 + (c.rgb[1] - sxp[1]) ** 2 + (c.rgb[2] - sxp[2]) ** 2;
|
|
2128
|
+
if (d < bestD) {
|
|
2129
|
+
bestD = d;
|
|
2130
|
+
best = c;
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
color = best ? best.hex : toHex(sxp[0], sxp[1], sxp[2]);
|
|
2134
|
+
} else {
|
|
2135
|
+
const comp = surface(W, H);
|
|
2136
|
+
renderDoc(comp, doc, { withBackground: true });
|
|
2137
|
+
const px = comp.getContext('2d').getImageData(sx, sy, 1, 1).data;
|
|
2138
|
+
color = toHex(px[0], px[1], px[2]);
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
return {
|
|
2142
|
+
ok: true,
|
|
2143
|
+
seed: [sx, sy],
|
|
2144
|
+
area,
|
|
2145
|
+
color,
|
|
2146
|
+
polygons: polys,
|
|
2147
|
+
};
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
if (kind === 'copy') {
|
|
2151
|
+
const layer = params.layer
|
|
2152
|
+
? layersOf(doc).find((l) => l.image && (l.id === params.layer || l.name === params.layer))
|
|
2153
|
+
: refLayerOf(doc);
|
|
2154
|
+
if (!layer) throw new Error('画布上没有可用的参考图:先 import 一张');
|
|
2155
|
+
const W = doc.width || 1024;
|
|
2156
|
+
const H = doc.height || 1024;
|
|
2157
|
+
const c = surface(W, H);
|
|
2158
|
+
const g = c.getContext('2d');
|
|
2159
|
+
g.clearRect(0, 0, W, H);
|
|
2160
|
+
drawImageLayer(g, layer.image);
|
|
2161
|
+
const rect = parseRectIn(params.rect, doc);
|
|
2162
|
+
const img = g.getImageData(rect.x, rect.y, rect.w, rect.h);
|
|
2163
|
+
// 线稿墨迹占比低 → 中心线更贴原线宽;照片/色块占比高 → 沿边界
|
|
2164
|
+
let mode = params.mode === 'center' || params.mode === 'edge' ? params.mode : 'auto';
|
|
2165
|
+
let inkRatio = null;
|
|
2166
|
+
let mask = null;
|
|
2167
|
+
if (mode === 'auto') {
|
|
2168
|
+
mask = inkMask(img.data, rect.w, rect.h, params.threshold);
|
|
2169
|
+
let inkPx = 0;
|
|
2170
|
+
for (let i = 0; i < mask.length; i += 1) inkPx += mask[i];
|
|
2171
|
+
inkRatio = inkPx / (rect.w * rect.h);
|
|
2172
|
+
mode = inkRatio < 0.13 ? 'center' : 'edge';
|
|
2173
|
+
}
|
|
2174
|
+
const traced = traceInk(img.data, rect.w, rect.h, {
|
|
2175
|
+
mode,
|
|
2176
|
+
smooth: params.smooth === undefined ? 2 : params.smooth,
|
|
2177
|
+
fit: params.fit === undefined ? true : params.fit,
|
|
2178
|
+
fitStep: params.fitStep || 3,
|
|
2179
|
+
threshold: params.threshold,
|
|
2180
|
+
tolerance: params.tolerance,
|
|
2181
|
+
minLength: params.minLength === undefined ? 16 : params.minLength,
|
|
2182
|
+
maxLines: params.maxLines || 160,
|
|
2183
|
+
maxPointsPerLine: params.maxPointsPerLine || 120,
|
|
2184
|
+
});
|
|
2185
|
+
const palette = kmeansColors(img.data, Math.max(2, Math.min(8, Number(params.colors) || 6)), 3);
|
|
2186
|
+
const wantFill = params.style === 'flat' || params.style === 'anime';
|
|
2187
|
+
// 色块必须靠闭合轮廓;中心线模式下的路径是骨架,不闭合,所以填色另跑一遍边界追踪
|
|
2188
|
+
let edgeTrace = traced;
|
|
2189
|
+
if (wantFill && mode === 'center') {
|
|
2190
|
+
edgeTrace = traceInk(img.data, rect.w, rect.h, {
|
|
2191
|
+
mode: 'edge',
|
|
2192
|
+
smooth: params.smooth === undefined ? 2 : params.smooth,
|
|
2193
|
+
fit: false,
|
|
2194
|
+
threshold: params.threshold,
|
|
2195
|
+
tolerance: params.tolerance,
|
|
2196
|
+
minLength: params.minLength === undefined ? 16 : params.minLength,
|
|
2197
|
+
maxLines: params.maxLines || 160,
|
|
2198
|
+
maxPointsPerLine: params.maxPointsPerLine || 120,
|
|
2199
|
+
});
|
|
2200
|
+
}
|
|
2201
|
+
const fills = wantFill
|
|
2202
|
+
? regionFills(c, rect, edgeTrace, palette, { minFill: params.minFill, maxFills: params.maxFills || 40 })
|
|
2203
|
+
: [];
|
|
2204
|
+
const paths = traced.paths.map((line) => line.map((p) => [Math.round(rect.x + p[0]), Math.round(rect.y + p[1])]));
|
|
2205
|
+
// 每根线的墨宽:中心线模式下量出来就是原线宽,宿主照这个 size 落笔
|
|
2206
|
+
if (!mask) mask = inkMask(img.data, rect.w, rect.h, params.threshold);
|
|
2207
|
+
const widths = pathWidths(mask, rect.w, rect.h, traced.paths);
|
|
2208
|
+
const lineWidth = widths.length ? Number((widths.reduce((a, b) => a + b, 0) / widths.length).toFixed(2)) : null;
|
|
2209
|
+
return {
|
|
2210
|
+
ok: true,
|
|
2211
|
+
rect,
|
|
2212
|
+
paths,
|
|
2213
|
+
widths,
|
|
2214
|
+
lineWidth,
|
|
2215
|
+
closed: traced.closed,
|
|
2216
|
+
fills,
|
|
2217
|
+
palette,
|
|
2218
|
+
mode,
|
|
2219
|
+
smooth: traced.smooth,
|
|
2220
|
+
fit: traced.fit,
|
|
2221
|
+
inkRatio: inkRatio === null ? null : Number(inkRatio.toFixed(3)),
|
|
2222
|
+
lines: traced.lines,
|
|
2223
|
+
points: traced.points,
|
|
2224
|
+
dropped: traced.dropped,
|
|
2225
|
+
tolerance: traced.tolerance,
|
|
2226
|
+
threshold: traced.threshold,
|
|
2227
|
+
};
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
if (kind === 'trace') {
|
|
2231
|
+
const layer = params.layer
|
|
2232
|
+
? layersOf(doc).find((l) => l.image && (l.id === params.layer || l.name === params.layer))
|
|
2233
|
+
: refLayerOf(doc);
|
|
2234
|
+
if (!layer) throw new Error('画布上没有可用的参考图:先 import 一张');
|
|
2235
|
+
const W = doc.width || 1024;
|
|
2236
|
+
const H = doc.height || 1024;
|
|
2237
|
+
const c = surface(W, H);
|
|
2238
|
+
const g = c.getContext('2d');
|
|
2239
|
+
g.clearRect(0, 0, W, H);
|
|
2240
|
+
drawImageLayer(g, layer.image);
|
|
2241
|
+
const rect = parseRectIn(params.rect, doc);
|
|
2242
|
+
const img = g.getImageData(rect.x, rect.y, rect.w, rect.h);
|
|
2243
|
+
const t = traceInk(img.data, rect.w, rect.h, {
|
|
2244
|
+
mode: params.mode === 'center' ? 'center' : 'edge',
|
|
2245
|
+
smooth: params.smooth,
|
|
2246
|
+
fit: params.fit === true,
|
|
2247
|
+
fitStep: params.fitStep,
|
|
2248
|
+
threshold: params.threshold,
|
|
2249
|
+
tolerance: params.tolerance,
|
|
2250
|
+
minLength: params.minLength,
|
|
2251
|
+
maxLines: params.maxLines,
|
|
2252
|
+
});
|
|
2253
|
+
// 区域内的坐标 → 画布坐标,模型可以直接喂 draw
|
|
2254
|
+
const paths = t.paths.map((line) => line.map((p) => [Math.round(rect.x + p[0]), Math.round(rect.y + p[1])]));
|
|
2255
|
+
return { ok: true, rect, paths, lines: t.lines, points: t.points, dropped: t.dropped, tolerance: t.tolerance, smooth: t.smooth, fit: t.fit, threshold: t.threshold };
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
if (kind === 'palette') {
|
|
2259
|
+
const layer = params.layer
|
|
2260
|
+
? layersOf(doc).find((l) => l.image && (l.id === params.layer || l.name === params.layer))
|
|
2261
|
+
: refLayerOf(doc);
|
|
2262
|
+
if (!layer) throw new Error('画布上没有可用的参考图');
|
|
2263
|
+
const W = doc.width || 1024;
|
|
2264
|
+
const H = doc.height || 1024;
|
|
2265
|
+
const c = surface(W, H);
|
|
2266
|
+
const g = c.getContext('2d');
|
|
2267
|
+
g.clearRect(0, 0, W, H);
|
|
2268
|
+
drawImageLayer(g, layer.image);
|
|
2269
|
+
const rect = parseRectIn(params.rect, doc);
|
|
2270
|
+
const data = g.getImageData(rect.x, rect.y, rect.w, rect.h).data;
|
|
2271
|
+
const k = Math.max(2, Math.min(8, Math.round(Number(params.k) || 6)));
|
|
2272
|
+
// 真 k-means(初值取占比最高的几档,10 轮迭代),比纯量化更能抓住渐变
|
|
2273
|
+
const colors = kmeansColors(data, k);
|
|
2274
|
+
const sampled = colors.reduce((n, c) => n + c.pixels, 0);
|
|
2275
|
+
return { ok: true, colors, sampled, rect, method: 'kmeans' };
|
|
2276
|
+
}
|
|
2277
|
+
|
|
2278
|
+
throw new Error(`未知像素任务:${kind}`);
|
|
2279
|
+
}
|
|
2280
|
+
|
|
2281
|
+
/** 文档里所有笔迹按全局顺序拍平,回放和计数都用它。 */
|
|
2282
|
+
function flattenStrokes(doc) {
|
|
2283
|
+
const out = [];
|
|
2284
|
+
for (const layer of layersOf(doc)) {
|
|
2285
|
+
for (const s of layer.strokes || []) out.push({ layerId: layer.id, stroke: s });
|
|
2286
|
+
}
|
|
2287
|
+
out.sort((a, b) => (a.stroke.seq || 0) - (b.stroke.seq || 0));
|
|
2288
|
+
return out;
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2291
|
+
/** 前 n 笔的文档快照,用于回放逐帧渲染。 */
|
|
2292
|
+
function docAtStep(doc, entries, n) {
|
|
2293
|
+
const counts = new Map();
|
|
2294
|
+
for (let i = 0; i < n && i < entries.length; i += 1) {
|
|
2295
|
+
const id = entries[i].layerId;
|
|
2296
|
+
counts.set(id, (counts.get(id) || 0) + 1);
|
|
2297
|
+
}
|
|
2298
|
+
return {
|
|
2299
|
+
...doc,
|
|
2300
|
+
layers: layersOf(doc).map((layer) => ({
|
|
2301
|
+
...layer,
|
|
2302
|
+
strokes: (layer.strokes || []).slice(0, counts.get(layer.id) || 0),
|
|
2303
|
+
})),
|
|
2304
|
+
};
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
/** 形状工具:把起止点变成一条笔迹(矩形/椭圆用折线多点闭合)。 */
|
|
2308
|
+
function shapePoints(tool, a, b) {
|
|
2309
|
+
const p = (x, y, pr = 0.9) => [Math.round(x), Math.round(y), pr];
|
|
2310
|
+
if (tool === 'line') return [p(a[0], a[1]), p(b[0], b[1])];
|
|
2311
|
+
if (tool === 'rect') {
|
|
2312
|
+
const x1 = Math.min(a[0], b[0]);
|
|
2313
|
+
const y1 = Math.min(a[1], b[1]);
|
|
2314
|
+
const x2 = Math.max(a[0], b[0]);
|
|
2315
|
+
const y2 = Math.max(a[1], b[1]);
|
|
2316
|
+
return [p(x1, y1), p(x2, y1), p(x2, y2), p(x1, y2), p(x1, y1)];
|
|
2317
|
+
}
|
|
2318
|
+
// 椭圆:中心取两点中点,半轴取差
|
|
2319
|
+
const cx = (a[0] + b[0]) / 2;
|
|
2320
|
+
const cy = (a[1] + b[1]) / 2;
|
|
2321
|
+
const rx = Math.abs(b[0] - a[0]) / 2;
|
|
2322
|
+
const ry = Math.abs(b[1] - a[1]) / 2;
|
|
2323
|
+
const pts = [];
|
|
2324
|
+
const steps = 28;
|
|
2325
|
+
for (let i = 0; i <= steps; i += 1) {
|
|
2326
|
+
const ang = (Math.PI * 2 * i) / steps;
|
|
2327
|
+
pts.push(p(cx + rx * Math.cos(ang), cy + ry * Math.sin(ang)));
|
|
2328
|
+
}
|
|
2329
|
+
return pts;
|
|
2330
|
+
}
|
|
2331
|
+
|
|
2332
|
+
async function fetchDoc() {
|
|
2333
|
+
const r = await fetch(`${API}/doc`, { cache: 'no-store' });
|
|
2334
|
+
return r.json();
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
/** 把 File 读成 dataURL:导入图片、新建画布都走这条,不依赖本地路径。 */
|
|
2338
|
+
function readFileAsDataUrl(file) {
|
|
2339
|
+
return new Promise((resolve, reject) => {
|
|
2340
|
+
const fr = new FileReader();
|
|
2341
|
+
fr.onload = () => resolve(String(fr.result || ''));
|
|
2342
|
+
fr.onerror = () => reject(new Error('read failed'));
|
|
2343
|
+
fr.readAsDataURL(file);
|
|
2344
|
+
});
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
/** 弹窗卡片:盖住整个界面居中显示,点多选项时用它,比在窄列里挤一排按钮好认。 */
|
|
2348
|
+
function Sheet(props) {
|
|
2349
|
+
const t = props.t || ((k) => k);
|
|
2350
|
+
if (!props.open) return null;
|
|
2351
|
+
return h('div', {
|
|
2352
|
+
style: {
|
|
2353
|
+
position: 'fixed',
|
|
2354
|
+
inset: 0,
|
|
2355
|
+
zIndex: 80,
|
|
2356
|
+
display: 'flex',
|
|
2357
|
+
alignItems: 'center',
|
|
2358
|
+
justifyContent: 'center',
|
|
2359
|
+
padding: 24,
|
|
2360
|
+
background: 'var(--dsw-alias-bg-mask-2, rgba(0,0,0,0.38))',
|
|
2361
|
+
},
|
|
2362
|
+
onClick: (e) => {
|
|
2363
|
+
if (e.target === e.currentTarget) props.onClose();
|
|
2364
|
+
},
|
|
2365
|
+
},
|
|
2366
|
+
h('div', {
|
|
2367
|
+
style: {
|
|
2368
|
+
width: props.width || 460,
|
|
2369
|
+
maxWidth: '92vw',
|
|
2370
|
+
maxHeight: '80vh',
|
|
2371
|
+
overflowY: 'auto',
|
|
2372
|
+
background: 'var(--dsw-alias-bg-layer-1, #fff)',
|
|
2373
|
+
color: 'var(--dsw-alias-label-primary, inherit)',
|
|
2374
|
+
border: '1px solid var(--dsw-alias-border-l2, rgba(127,127,127,0.35))',
|
|
2375
|
+
borderRadius: 14,
|
|
2376
|
+
boxShadow: '0 24px 64px rgba(0,0,0,0.32)',
|
|
2377
|
+
padding: '14px 16px 18px',
|
|
2378
|
+
boxSizing: 'border-box',
|
|
2379
|
+
font: '13px/1.55 system-ui, "Microsoft YaHei", sans-serif',
|
|
2380
|
+
},
|
|
2381
|
+
},
|
|
2382
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 10, marginBottom: 12 } },
|
|
2383
|
+
h('span', { style: { fontWeight: 700, fontSize: 14 } }, props.title),
|
|
2384
|
+
h('button', {
|
|
2385
|
+
type: 'button',
|
|
2386
|
+
style: {
|
|
2387
|
+
marginLeft: 'auto',
|
|
2388
|
+
border: 'none',
|
|
2389
|
+
background: 'transparent',
|
|
2390
|
+
color: 'var(--dsw-alias-label-tertiary, inherit)',
|
|
2391
|
+
cursor: 'pointer',
|
|
2392
|
+
fontSize: 18,
|
|
2393
|
+
lineHeight: 1,
|
|
2394
|
+
padding: '0 2px',
|
|
2395
|
+
},
|
|
2396
|
+
onClick: props.onClose,
|
|
2397
|
+
}, '×'),
|
|
2398
|
+
),
|
|
2399
|
+
props.children,
|
|
2400
|
+
),
|
|
2401
|
+
);
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
/**
|
|
2405
|
+
* 笔刷预览:用这支笔画一条样笔(带压感变化),一眼就能看出颗粒、湿边、飞白的差别。
|
|
2406
|
+
* 灰阶固定色,不跟着当前颜色走,免得看半天认不出笔刷本身。
|
|
2407
|
+
*/
|
|
2408
|
+
function BrushPreview(props) {
|
|
2409
|
+
const ref = React.useRef(null);
|
|
2410
|
+
React.useEffect(() => {
|
|
2411
|
+
const canvas = ref.current;
|
|
2412
|
+
if (!canvas) return;
|
|
2413
|
+
const w = 96;
|
|
2414
|
+
const hgt = 24;
|
|
2415
|
+
const dpr = window.devicePixelRatio || 1;
|
|
2416
|
+
canvas.width = Math.round(w * dpr);
|
|
2417
|
+
canvas.height = Math.round(hgt * dpr);
|
|
2418
|
+
const ctx = canvas.getContext('2d');
|
|
2419
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
2420
|
+
ctx.clearRect(0, 0, w, hgt);
|
|
2421
|
+
const pts = [];
|
|
2422
|
+
for (let i = 0; i <= 26; i += 1) {
|
|
2423
|
+
const f = i / 26;
|
|
2424
|
+
pts.push([
|
|
2425
|
+
7 + f * (w - 14),
|
|
2426
|
+
hgt / 2 + Math.sin(f * Math.PI * 1.7) * 4.5,
|
|
2427
|
+
0.15 + 0.85 * Math.sin(f * Math.PI),
|
|
2428
|
+
]);
|
|
2429
|
+
}
|
|
2430
|
+
paintStroke(ctx, {
|
|
2431
|
+
id: `preview-${props.id}`,
|
|
2432
|
+
color: '#8a8a8a',
|
|
2433
|
+
size: 7,
|
|
2434
|
+
brush: props.id,
|
|
2435
|
+
taper: true,
|
|
2436
|
+
minSize: 0.25,
|
|
2437
|
+
opacity: 1,
|
|
2438
|
+
points: pts,
|
|
2439
|
+
});
|
|
2440
|
+
}, [props.id]);
|
|
2441
|
+
return h('canvas', {
|
|
2442
|
+
ref,
|
|
2443
|
+
style: { width: 96, height: 24, display: 'block', maxWidth: '100%' },
|
|
2444
|
+
});
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
/**
|
|
2448
|
+
* 图层缩略图:把那一层的笔迹按比例缩到 30px 里。
|
|
2449
|
+
* 只在「笔迹数/图片签名」变化时重画(笔迹只增不减,够用了),不然每帧整层重绘太贵。
|
|
2450
|
+
*/
|
|
2451
|
+
function LayerThumb(props) {
|
|
2452
|
+
const ref = React.useRef(null);
|
|
2453
|
+
const last = React.useRef('');
|
|
2454
|
+
React.useEffect(() => {
|
|
2455
|
+
const canvas = ref.current;
|
|
2456
|
+
const doc = props.docRef && props.docRef.current;
|
|
2457
|
+
if (!canvas || !doc) return;
|
|
2458
|
+
// 矢量层:签名(笔迹数)没变就不重画,免得每来一笔都把每一层重绘一遍
|
|
2459
|
+
if (props.sig === last.current && !String(props.sig).includes('img')) return;
|
|
2460
|
+
last.current = props.sig;
|
|
2461
|
+
const size = 30;
|
|
2462
|
+
const dpr = window.devicePixelRatio || 1;
|
|
2463
|
+
canvas.width = Math.round(size * dpr);
|
|
2464
|
+
canvas.height = Math.round(size * dpr);
|
|
2465
|
+
const ctx = canvas.getContext('2d');
|
|
2466
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
2467
|
+
ctx.clearRect(0, 0, size, size);
|
|
2468
|
+
const layer = layersOf(doc).find((l) => l.id === props.layerId);
|
|
2469
|
+
if (!layer) return;
|
|
2470
|
+
const w = doc.width || 1024;
|
|
2471
|
+
const hgt = doc.height || 1024;
|
|
2472
|
+
const s = Math.min(size / w, size / hgt);
|
|
2473
|
+
ctx.save();
|
|
2474
|
+
ctx.translate((size - w * s) / 2, (size - hgt * s) / 2);
|
|
2475
|
+
ctx.scale(s, s);
|
|
2476
|
+
if (layer.image && layer.image.file) drawImageLayer(ctx, layer.image);
|
|
2477
|
+
for (const stroke of layer.strokes || []) paintStroke(ctx, stroke);
|
|
2478
|
+
ctx.restore();
|
|
2479
|
+
}, [props.sig, props.layerId, props.docRef, props.rev]);
|
|
2480
|
+
return h('canvas', {
|
|
2481
|
+
ref,
|
|
2482
|
+
style: {
|
|
2483
|
+
width: 30,
|
|
2484
|
+
height: 30,
|
|
2485
|
+
flex: '0 0 30px',
|
|
2486
|
+
display: 'block',
|
|
2487
|
+
borderRadius: 4,
|
|
2488
|
+
background: 'var(--dsw-alias-bg-layer-3, rgba(127,127,127,0.16))',
|
|
2489
|
+
outline: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.22))',
|
|
2490
|
+
},
|
|
2491
|
+
});
|
|
2492
|
+
}
|
|
2493
|
+
|
|
2494
|
+
function CanvasPanel(props) {
|
|
2495
|
+
const t = props.t || ((k) => k);
|
|
2496
|
+
const canvasRef = React.useRef(null);
|
|
2497
|
+
const docRef = React.useRef(null);
|
|
2498
|
+
const revRef = React.useRef(0);
|
|
2499
|
+
const dragRef = React.useRef(null);
|
|
2500
|
+
const replayRef = React.useRef(null);
|
|
2501
|
+
const [revision, setRevision] = React.useState(0);
|
|
2502
|
+
const [error, setError] = React.useState(null);
|
|
2503
|
+
const [color, setColor] = React.useState('#111111');
|
|
2504
|
+
const [size, setSize] = React.useState(10);
|
|
2505
|
+
const [tool, setTool] = React.useState('brush');
|
|
2506
|
+
const [brush, setBrush] = React.useState('round');
|
|
2507
|
+
const [taper, setTaper] = React.useState(true);
|
|
2508
|
+
const [staleHost, setStaleHost] = React.useState(false);
|
|
2509
|
+
const [layerDock, setLayerDock] = React.useState(true);
|
|
2510
|
+
// 图层不透明度拖动时先用本地值显示,等宿主回包(700ms 轮询)再对齐,不然滑块会跳
|
|
2511
|
+
const [optOpacity, setOptOpacity] = React.useState(null);
|
|
2512
|
+
const [playing, setPlaying] = React.useState(false);
|
|
2513
|
+
/** 宿主查到的版本信息:有新版本时在标题旁边挂一枚提示。 */
|
|
2514
|
+
const [updateInfo, setUpdateInfo] = React.useState(null);
|
|
2515
|
+
const [applying, setApplying] = React.useState(false);
|
|
2516
|
+
const [applyNote, setApplyNote] = React.useState('');
|
|
2517
|
+
|
|
2518
|
+
/** 一键更新:宿主拿 pluginManager 按 tag 重装(git 安装也能收更新)。 */
|
|
2519
|
+
const applyUpdate = async () => {
|
|
2520
|
+
if (applying) return;
|
|
2521
|
+
setApplying(true);
|
|
2522
|
+
setApplyNote('');
|
|
2523
|
+
try {
|
|
2524
|
+
const r = await fetch(`${API}/update`, {
|
|
2525
|
+
method: 'POST',
|
|
2526
|
+
headers: { 'Content-Type': 'application/json' },
|
|
2527
|
+
body: JSON.stringify({ apply: true }),
|
|
2528
|
+
});
|
|
2529
|
+
const body = await r.json().catch(() => ({}));
|
|
2530
|
+
if (!r.ok || body.ok === false) {
|
|
2531
|
+
setApplyNote(`${t('updateFail')}:${body.error || r.status}`);
|
|
2532
|
+
return;
|
|
2533
|
+
}
|
|
2534
|
+
const done = body.apply && body.apply.message ? body.apply.message : t('updateDone');
|
|
2535
|
+
setApplyNote(done);
|
|
2536
|
+
if (body.apply && body.apply.upToDate) setUpdateInfo({ ...(body || {}), hasUpdate: false });
|
|
2537
|
+
} catch (err) {
|
|
2538
|
+
// 更新会触发重载/重启,请求可能被掐断,这不算失败
|
|
2539
|
+
setApplyNote(t('updateDone'));
|
|
2540
|
+
} finally {
|
|
2541
|
+
setApplying(false);
|
|
2542
|
+
}
|
|
2543
|
+
};
|
|
2544
|
+
|
|
2545
|
+
/** 手动查一次(/update?force=1),让「检查更新」这个动作有个看得见的结果。 */
|
|
2546
|
+
const [checking, setChecking] = React.useState(false);
|
|
2547
|
+
const [checkNote, setCheckNote] = React.useState('');
|
|
2548
|
+
const [localVer, setLocalVer] = React.useState('');
|
|
2549
|
+
const checkUpdateNow = async () => {
|
|
2550
|
+
if (checking) return;
|
|
2551
|
+
setChecking(true);
|
|
2552
|
+
setCheckNote('');
|
|
2553
|
+
try {
|
|
2554
|
+
const r = await fetch(`${API}/update?force=1`, { cache: 'no-store' });
|
|
2555
|
+
const body = await r.json().catch(() => ({}));
|
|
2556
|
+
if (!r.ok || body.ok === false) {
|
|
2557
|
+
setCheckNote(`${t('versionCheckFail')}:${body.error || r.status}`);
|
|
2558
|
+
return;
|
|
2559
|
+
}
|
|
2560
|
+
setUpdateInfo({ ...body, enabled: body.enabled !== false });
|
|
2561
|
+
} catch (err) {
|
|
2562
|
+
setCheckNote(`${t('versionCheckFail')}:${(err && err.message) || err}`);
|
|
2563
|
+
} finally {
|
|
2564
|
+
setChecking(false);
|
|
2565
|
+
}
|
|
2566
|
+
};
|
|
2567
|
+
const [meta, setMeta] = React.useState({ strokes: 0, redo: 0, layers: [], activeLayer: null, background: '#ffffff', width: 1024, height: 1024 });
|
|
2568
|
+
const viewportRef = React.useRef(null);
|
|
2569
|
+
const viewRef = React.useRef({ scale: 1, x: 0, y: 0 });
|
|
2570
|
+
const panRef = React.useRef(null);
|
|
2571
|
+
const spaceRef = React.useRef(false);
|
|
2572
|
+
const userViewRef = React.useRef(false);
|
|
2573
|
+
const fitDimsRef = React.useRef(null);
|
|
2574
|
+
const [view, setViewState] = React.useState({ scale: 1, x: 0, y: 0 });
|
|
2575
|
+
const [spaceHeld, setSpaceHeld] = React.useState(false);
|
|
2576
|
+
/** 整页模式里的弹窗卡片:tools / layers / canvas,null 表示都关着。 */
|
|
2577
|
+
const [sheet, setSheet] = React.useState(null);
|
|
2578
|
+
/** 导入图片:选完文件直接读成 dataURL 交给宿主,不依赖任何文件对话框 API。 */
|
|
2579
|
+
const [importNote, setImportNote] = React.useState(null);
|
|
2580
|
+
const importRef = React.useRef(null);
|
|
2581
|
+
/** 新建绘画:先问要不要存当前这幅,再选空白/图片。 */
|
|
2582
|
+
const [pendingSave, setPendingSave] = React.useState(false);
|
|
2583
|
+
const [newNote, setNewNote] = React.useState(null);
|
|
2584
|
+
const newImportRef = React.useRef(null);
|
|
2585
|
+
|
|
2586
|
+
// 顶栏「新建」按钮通过桥直接开这里的弹窗
|
|
2587
|
+
React.useEffect(() => {
|
|
2588
|
+
const b = props.bridge;
|
|
2589
|
+
if (!b) return undefined;
|
|
2590
|
+
b.current.newPaint = () => {
|
|
2591
|
+
setPendingSave(false);
|
|
2592
|
+
setNewNote(null);
|
|
2593
|
+
setSheet('newSave');
|
|
2594
|
+
};
|
|
2595
|
+
return () => {
|
|
2596
|
+
b.current.newPaint = null;
|
|
2597
|
+
};
|
|
2598
|
+
}, [props.bridge]);
|
|
2599
|
+
|
|
2600
|
+
// 只在打开画界的这个会话里把 paint_canvas 接上(宿主按会话绑定),
|
|
2601
|
+
// 免得别的会话(比如正在写代码的那个)被画布协议带偏。
|
|
2602
|
+
React.useEffect(() => {
|
|
2603
|
+
const sessionId = props.sessionId;
|
|
2604
|
+
if (!sessionId) return undefined;
|
|
2605
|
+
const attachBody = JSON.stringify({ sessionId, variant: props.variant || 'tab' });
|
|
2606
|
+
fetch(`${API}/attach`, {
|
|
2607
|
+
method: 'POST',
|
|
2608
|
+
headers: { 'Content-Type': 'application/json' },
|
|
2609
|
+
body: attachBody,
|
|
2610
|
+
}).catch(() => {});
|
|
2611
|
+
return () => {
|
|
2612
|
+
// 宿主侧有 15 秒宽限,切标签时的卸载不会真的解绑
|
|
2613
|
+
fetch(`${API}/detach`, {
|
|
2614
|
+
method: 'POST',
|
|
2615
|
+
headers: { 'Content-Type': 'application/json' },
|
|
2616
|
+
body: JSON.stringify({ sessionId }),
|
|
2617
|
+
}).catch(() => {});
|
|
2618
|
+
};
|
|
2619
|
+
}, [props.sessionId, props.variant]);
|
|
2620
|
+
|
|
2621
|
+
const setView = React.useCallback((next) => {
|
|
2622
|
+
viewRef.current = next;
|
|
2623
|
+
setViewState(next);
|
|
2624
|
+
}, []);
|
|
2625
|
+
|
|
2626
|
+
const readMeta = React.useCallback((doc) => {
|
|
2627
|
+
const layers = (doc.layers || []).map((l) => ({
|
|
2628
|
+
id: l.id,
|
|
2629
|
+
name: l.name,
|
|
2630
|
+
visible: l.visible !== false,
|
|
2631
|
+
opacity: l.opacity === undefined ? 1 : l.opacity,
|
|
2632
|
+
blend: l.blend || 'normal',
|
|
2633
|
+
count: (l.strokes || []).length,
|
|
2634
|
+
image: Boolean(l.image && l.image.file),
|
|
2635
|
+
}));
|
|
2636
|
+
setMeta({
|
|
2637
|
+
strokes: layers.reduce((n, l) => n + l.count, 0),
|
|
2638
|
+
redo: (doc.redo || []).length,
|
|
2639
|
+
layers,
|
|
2640
|
+
activeLayer: doc.activeLayer,
|
|
2641
|
+
background: doc.background || '#ffffff',
|
|
2642
|
+
width: doc.width || 1024,
|
|
2643
|
+
height: doc.height || 1024,
|
|
2644
|
+
});
|
|
2645
|
+
}, []);
|
|
2646
|
+
|
|
2647
|
+
const uploadPreview = React.useCallback(() => {
|
|
2648
|
+
const canvas = canvasRef.current;
|
|
2649
|
+
const doc = docRef.current;
|
|
2650
|
+
if (!canvas || !doc) return;
|
|
2651
|
+
try {
|
|
2652
|
+
const dataUrl = canvas.toDataURL('image/png');
|
|
2653
|
+
// 同一帧再渲染一遍透明底,导出时不用再往返
|
|
2654
|
+
const alphaCanvas = document.createElement('canvas');
|
|
2655
|
+
renderDoc(alphaCanvas, doc, { withBackground: false });
|
|
2656
|
+
const alphaDataUrl = alphaCanvas.toDataURL('image/png');
|
|
2657
|
+
fetch(`${API}/preview`, {
|
|
2658
|
+
method: 'POST',
|
|
2659
|
+
headers: { 'Content-Type': 'application/json' },
|
|
2660
|
+
body: JSON.stringify({ dataUrl, alphaDataUrl }),
|
|
2661
|
+
}).catch(() => {});
|
|
2662
|
+
} catch {
|
|
2663
|
+
/* 画布被污染或过大时忽略 */
|
|
2664
|
+
}
|
|
2665
|
+
}, []);
|
|
2666
|
+
|
|
2667
|
+
/** 适应窗口:整幅画缩进可视区并居中。 */
|
|
2668
|
+
const fitToDoc = React.useCallback((w, h) => {
|
|
2669
|
+
const el = viewportRef.current;
|
|
2670
|
+
if (!el || !w || !h) return;
|
|
2671
|
+
const vw = el.clientWidth;
|
|
2672
|
+
const vh = el.clientHeight;
|
|
2673
|
+
if (vw <= 0 || vh <= 0) return;
|
|
2674
|
+
const scale = Math.min(vw / w, vh / h) * 0.96;
|
|
2675
|
+
setView({ scale, x: (vw - w * scale) / 2, y: (vh - h * scale) / 2 });
|
|
2676
|
+
userViewRef.current = false;
|
|
2677
|
+
}, [setView]);
|
|
2678
|
+
|
|
2679
|
+
/** 以某点为锚缩放:鼠标下面那一点保持不动。 */
|
|
2680
|
+
const zoomAt = React.useCallback((clientX, clientY, factor) => {
|
|
2681
|
+
const el = viewportRef.current;
|
|
2682
|
+
if (!el) return;
|
|
2683
|
+
const rect = el.getBoundingClientRect();
|
|
2684
|
+
const cur = viewRef.current;
|
|
2685
|
+
const nextScale = Math.max(0.06, Math.min(16, cur.scale * factor));
|
|
2686
|
+
const atX = clientX - rect.left;
|
|
2687
|
+
const atY = clientY - rect.top;
|
|
2688
|
+
const px = (atX - cur.x) / cur.scale;
|
|
2689
|
+
const py = (atY - cur.y) / cur.scale;
|
|
2690
|
+
setView({ scale: nextScale, x: atX - px * nextScale, y: atY - py * nextScale });
|
|
2691
|
+
userViewRef.current = true;
|
|
2692
|
+
}, [setView]);
|
|
2693
|
+
|
|
2694
|
+
const zoomBy = (factor) => {
|
|
2695
|
+
const el = viewportRef.current;
|
|
2696
|
+
if (!el) return;
|
|
2697
|
+
const rect = el.getBoundingClientRect();
|
|
2698
|
+
zoomAt(rect.left + rect.width / 2, rect.top + rect.height / 2, factor);
|
|
2699
|
+
};
|
|
2700
|
+
|
|
2701
|
+
const zoomActual = () => {
|
|
2702
|
+
const el = viewportRef.current;
|
|
2703
|
+
const doc = docRef.current;
|
|
2704
|
+
if (!el || !doc) return;
|
|
2705
|
+
setView({ scale: 1, x: (el.clientWidth - doc.width) / 2, y: (el.clientHeight - doc.height) / 2 });
|
|
2706
|
+
userViewRef.current = true;
|
|
2707
|
+
};
|
|
2708
|
+
|
|
2709
|
+
// 滚轮缩放:必须非 passive 才能 preventDefault,所以自己挂原生监听
|
|
2710
|
+
React.useEffect(() => {
|
|
2711
|
+
const el = viewportRef.current;
|
|
2712
|
+
if (!el) return undefined;
|
|
2713
|
+
const onWheel = (e) => {
|
|
2714
|
+
e.preventDefault();
|
|
2715
|
+
zoomAt(e.clientX, e.clientY, e.deltaY < 0 ? 1.12 : 1 / 1.12);
|
|
2716
|
+
};
|
|
2717
|
+
el.addEventListener('wheel', onWheel, { passive: false });
|
|
2718
|
+
return () => el.removeEventListener('wheel', onWheel);
|
|
2719
|
+
}, [zoomAt]);
|
|
2720
|
+
|
|
2721
|
+
// 空格 = 临时抓手;在输入框里按空格不抢
|
|
2722
|
+
React.useEffect(() => {
|
|
2723
|
+
const isEditing = (target) => {
|
|
2724
|
+
const tag = (target && target.tagName) || '';
|
|
2725
|
+
return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || (target && target.isContentEditable);
|
|
2726
|
+
};
|
|
2727
|
+
const down = (e) => {
|
|
2728
|
+
if (e.code !== 'Space' || isEditing(e.target)) return;
|
|
2729
|
+
spaceRef.current = true;
|
|
2730
|
+
setSpaceHeld(true);
|
|
2731
|
+
e.preventDefault();
|
|
2732
|
+
};
|
|
2733
|
+
const up = (e) => {
|
|
2734
|
+
if (e.code !== 'Space') return;
|
|
2735
|
+
spaceRef.current = false;
|
|
2736
|
+
setSpaceHeld(false);
|
|
2737
|
+
};
|
|
2738
|
+
window.addEventListener('keydown', down);
|
|
2739
|
+
window.addEventListener('keyup', up);
|
|
2740
|
+
return () => {
|
|
2741
|
+
window.removeEventListener('keydown', down);
|
|
2742
|
+
window.removeEventListener('keyup', up);
|
|
2743
|
+
};
|
|
2744
|
+
}, []);
|
|
2745
|
+
|
|
2746
|
+
// 视口大小变化时,只要用户没手动缩放过就重新适应
|
|
2747
|
+
React.useEffect(() => {
|
|
2748
|
+
const el = viewportRef.current;
|
|
2749
|
+
if (!el || typeof ResizeObserver === 'undefined') return undefined;
|
|
2750
|
+
const ro = new ResizeObserver(() => {
|
|
2751
|
+
if (userViewRef.current) return;
|
|
2752
|
+
const doc = docRef.current;
|
|
2753
|
+
if (doc) fitToDoc(doc.width, doc.height);
|
|
2754
|
+
});
|
|
2755
|
+
ro.observe(el);
|
|
2756
|
+
return () => ro.disconnect();
|
|
2757
|
+
}, [fitToDoc]);
|
|
2758
|
+
|
|
2759
|
+
const onViewPointerDown = (evt) => {
|
|
2760
|
+
if (!(evt.button === 1 || spaceRef.current)) return;
|
|
2761
|
+
evt.preventDefault();
|
|
2762
|
+
evt.currentTarget.setPointerCapture?.(evt.pointerId);
|
|
2763
|
+
panRef.current = { id: evt.pointerId, sx: evt.clientX, sy: evt.clientY, base: viewRef.current };
|
|
2764
|
+
};
|
|
2765
|
+
|
|
2766
|
+
const onViewPointerMove = (evt) => {
|
|
2767
|
+
const pan = panRef.current;
|
|
2768
|
+
if (!pan || pan.id !== evt.pointerId) return;
|
|
2769
|
+
setView({
|
|
2770
|
+
scale: pan.base.scale,
|
|
2771
|
+
x: pan.base.x + (evt.clientX - pan.sx),
|
|
2772
|
+
y: pan.base.y + (evt.clientY - pan.sy),
|
|
2773
|
+
});
|
|
2774
|
+
userViewRef.current = true;
|
|
2775
|
+
};
|
|
2776
|
+
|
|
2777
|
+
const onViewPointerUp = (evt) => {
|
|
2778
|
+
if (panRef.current && panRef.current.id === evt.pointerId) panRef.current = null;
|
|
2779
|
+
};
|
|
2780
|
+
|
|
2781
|
+
// 初次加载 + 轮询宿主版本(用户或模型都可能改动文档)
|
|
2782
|
+
React.useEffect(() => {
|
|
2783
|
+
let alive = true;
|
|
2784
|
+
let timer = null;
|
|
2785
|
+
const tick = async () => {
|
|
2786
|
+
try {
|
|
2787
|
+
const body = await fetchDoc();
|
|
2788
|
+
if (!alive || !body.ok) return;
|
|
2789
|
+
// 宿主有没有 32 支笔刷:旧进程只认 5 支,落笔后笔刷会被丢掉,重画成圆头
|
|
2790
|
+
setStaleHost(!(Number(body.brushes) >= BRUSHES.length));
|
|
2791
|
+
setUpdateInfo(body.update || null);
|
|
2792
|
+
if (body.version) setLocalVer(String(body.version));
|
|
2793
|
+
docRef.current = body.doc;
|
|
2794
|
+
await ensureImages(body.doc);
|
|
2795
|
+
if (!alive) return;
|
|
2796
|
+
if (body.revision !== revRef.current) {
|
|
2797
|
+
revRef.current = body.revision;
|
|
2798
|
+
setRevision(body.revision);
|
|
2799
|
+
readMeta(body.doc);
|
|
2800
|
+
renderDoc(canvasRef.current, body.doc);
|
|
2801
|
+
setTimeout(uploadPreview, 300);
|
|
2802
|
+
// 第一次拿到文档、或画布尺寸变了,重新适应窗口
|
|
2803
|
+
const fit = fitDimsRef.current;
|
|
2804
|
+
if (!fit || fit.w !== body.doc.width || fit.h !== body.doc.height) {
|
|
2805
|
+
fitDimsRef.current = { w: body.doc.width, h: body.doc.height };
|
|
2806
|
+
fitToDoc(body.doc.width, body.doc.height);
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
setError(null);
|
|
2810
|
+
} catch {
|
|
2811
|
+
if (alive) setError(t('syncFailed'));
|
|
2812
|
+
} finally {
|
|
2813
|
+
if (alive) timer = setTimeout(tick, 700);
|
|
2814
|
+
}
|
|
2815
|
+
};
|
|
2816
|
+
tick();
|
|
2817
|
+
return () => {
|
|
2818
|
+
alive = false;
|
|
2819
|
+
if (timer) clearTimeout(timer);
|
|
2820
|
+
if (replayRef.current) clearTimeout(replayRef.current);
|
|
2821
|
+
};
|
|
2822
|
+
}, [fitToDoc, readMeta, t, uploadPreview]);
|
|
2823
|
+
|
|
2824
|
+
// 像素任务:宿主把裁切/取色/差分塞进队列,这里取走、算完回传。
|
|
2825
|
+
// 只在画界面板打开时存在,所以「模型要看得见」的前提是用户开着画界。
|
|
2826
|
+
React.useEffect(() => {
|
|
2827
|
+
let alive = true;
|
|
2828
|
+
let timer = null;
|
|
2829
|
+
const seen = [];
|
|
2830
|
+
const drain = async () => {
|
|
2831
|
+
try {
|
|
2832
|
+
if (docRef.current) {
|
|
2833
|
+
const res = await fetch(`${API}/jobs`, { cache: 'no-store' });
|
|
2834
|
+
const body = await res.json();
|
|
2835
|
+
for (const job of (body && body.jobs) || []) {
|
|
2836
|
+
if (seen.includes(job.id)) continue;
|
|
2837
|
+
seen.push(job.id);
|
|
2838
|
+
if (seen.length > 120) seen.shift();
|
|
2839
|
+
let result;
|
|
2840
|
+
try {
|
|
2841
|
+
result = runPixelTask(job, docRef.current);
|
|
2842
|
+
} catch (err) {
|
|
2843
|
+
result = { ok: false, error: String((err && err.message) || err) };
|
|
2844
|
+
}
|
|
2845
|
+
fetch(`${API}/job`, {
|
|
2846
|
+
method: 'POST',
|
|
2847
|
+
headers: { 'Content-Type': 'application/json' },
|
|
2848
|
+
body: JSON.stringify({ id: job.id, result }),
|
|
2849
|
+
}).catch(() => {});
|
|
2850
|
+
}
|
|
2851
|
+
}
|
|
2852
|
+
} catch {
|
|
2853
|
+
/* 宿主没开或网络抖动:下一轮再试 */
|
|
2854
|
+
}
|
|
2855
|
+
if (alive) timer = setTimeout(drain, 700);
|
|
2856
|
+
};
|
|
2857
|
+
drain();
|
|
2858
|
+
return () => {
|
|
2859
|
+
alive = false;
|
|
2860
|
+
if (timer) clearTimeout(timer);
|
|
2861
|
+
};
|
|
2862
|
+
}, []);
|
|
2863
|
+
|
|
2864
|
+
// 尺寸变化时重画(保证位图不被拉伸糊掉)
|
|
2865
|
+
React.useEffect(() => {
|
|
2866
|
+
if (docRef.current) renderDoc(canvasRef.current, docRef.current);
|
|
2867
|
+
}, [revision]);
|
|
2868
|
+
|
|
2869
|
+
const posOf = (evt) => {
|
|
2870
|
+
const canvas = canvasRef.current;
|
|
2871
|
+
const rect = canvas.getBoundingClientRect();
|
|
2872
|
+
const x = ((evt.clientX - rect.left) / rect.width) * canvas.width;
|
|
2873
|
+
const y = ((evt.clientY - rect.top) / rect.height) * canvas.height;
|
|
2874
|
+
const pressure = evt.pressure && evt.pressure > 0 ? evt.pressure : 0.55;
|
|
2875
|
+
return [Math.round(x), Math.round(y), Number(pressure.toFixed(3))];
|
|
2876
|
+
};
|
|
2877
|
+
|
|
2878
|
+
const tempStroke = () => {
|
|
2879
|
+
const drag = dragRef.current;
|
|
2880
|
+
if (!drag) return null;
|
|
2881
|
+
const base = {
|
|
2882
|
+
color,
|
|
2883
|
+
size,
|
|
2884
|
+
opacity: 1,
|
|
2885
|
+
mode: tool === 'eraser' ? 'eraser' : 'brush',
|
|
2886
|
+
brush: tool === 'eraser' ? 'round' : brush,
|
|
2887
|
+
};
|
|
2888
|
+
if (drag.mode === 'free') {
|
|
2889
|
+
if (drag.points.length < 2) return null;
|
|
2890
|
+
// 手绘才带笔锋与最小笔宽;形状工具保持等宽,不然矩形角会缺一块
|
|
2891
|
+
return { ...base, taper, minSize: 0.3, points: drag.points };
|
|
2892
|
+
}
|
|
2893
|
+
return { ...base, points: shapePoints(drag.tool, drag.start, drag.end) };
|
|
2894
|
+
};
|
|
2895
|
+
|
|
2896
|
+
// 手上空中的那一笔只画在屏幕上,不提交
|
|
2897
|
+
const showTemp = () => {
|
|
2898
|
+
const doc = docRef.current;
|
|
2899
|
+
const stroke = tempStroke();
|
|
2900
|
+
if (!doc || !stroke) return;
|
|
2901
|
+
const base = layersOf(doc);
|
|
2902
|
+
const activeId = doc.activeLayer || (base[base.length - 1] || {}).id;
|
|
2903
|
+
renderDoc(canvasRef.current, {
|
|
2904
|
+
...doc,
|
|
2905
|
+
layers: base.map((l) => (l.id === activeId ? { ...l, strokes: [...(l.strokes || []), stroke] } : l)),
|
|
2906
|
+
});
|
|
2907
|
+
};
|
|
2908
|
+
|
|
2909
|
+
const onPointerDown = (evt) => {
|
|
2910
|
+
if (playing || spaceRef.current || evt.button === 1) return;
|
|
2911
|
+
evt.currentTarget.setPointerCapture?.(evt.pointerId);
|
|
2912
|
+
const p = posOf(evt);
|
|
2913
|
+
if (tool === 'brush') {
|
|
2914
|
+
dragRef.current = { mode: 'free', points: [p] };
|
|
2915
|
+
} else {
|
|
2916
|
+
dragRef.current = { mode: 'shape', tool, start: p, end: p };
|
|
2917
|
+
}
|
|
2918
|
+
};
|
|
2919
|
+
|
|
2920
|
+
const onPointerMove = (evt) => {
|
|
2921
|
+
const drag = dragRef.current;
|
|
2922
|
+
if (!drag) return;
|
|
2923
|
+
const p = posOf(evt);
|
|
2924
|
+
if (drag.mode === 'free') {
|
|
2925
|
+
const last = drag.points[drag.points.length - 1];
|
|
2926
|
+
if (Math.hypot(p[0] - last[0], p[1] - last[1]) < 1.5) return;
|
|
2927
|
+
drag.points.push(p);
|
|
2928
|
+
if (drag.points.length % 4 === 0) showTemp();
|
|
2929
|
+
return;
|
|
2930
|
+
}
|
|
2931
|
+
drag.end = p;
|
|
2932
|
+
showTemp();
|
|
2933
|
+
};
|
|
2934
|
+
|
|
2935
|
+
const finish = async () => {
|
|
2936
|
+
const stroke = tempStroke();
|
|
2937
|
+
dragRef.current = null;
|
|
2938
|
+
const doc = docRef.current;
|
|
2939
|
+
if (!stroke) {
|
|
2940
|
+
if (doc) renderDoc(canvasRef.current, doc);
|
|
2941
|
+
return;
|
|
2942
|
+
}
|
|
2943
|
+
if (stroke.points.length < 2) {
|
|
2944
|
+
if (doc) renderDoc(canvasRef.current, doc);
|
|
2945
|
+
return;
|
|
2946
|
+
}
|
|
2947
|
+
try {
|
|
2948
|
+
await fetch(`${API}/stroke`, {
|
|
2949
|
+
method: 'POST',
|
|
2950
|
+
headers: { 'Content-Type': 'application/json' },
|
|
2951
|
+
body: JSON.stringify({ strokes: [stroke], layer: doc ? doc.activeLayer : undefined }),
|
|
2952
|
+
});
|
|
2953
|
+
} catch {
|
|
2954
|
+
setError(t('syncFailed'));
|
|
2955
|
+
}
|
|
2956
|
+
};
|
|
2957
|
+
|
|
2958
|
+
/** 新建一幅:save 决定先把当前这幅存成 PNG;带 image 就按图片尺寸建画布。 */
|
|
2959
|
+
const startNewPainting = async (opts = {}) => {
|
|
2960
|
+
setNewNote(t('page.new.creating'));
|
|
2961
|
+
try {
|
|
2962
|
+
const r = await fetch(`${API}/new`, {
|
|
2963
|
+
method: 'POST',
|
|
2964
|
+
headers: { 'Content-Type': 'application/json' },
|
|
2965
|
+
body: JSON.stringify({
|
|
2966
|
+
save: pendingSave === true,
|
|
2967
|
+
width: opts.width,
|
|
2968
|
+
height: opts.height,
|
|
2969
|
+
dataUrl: opts.image ? opts.image.dataUrl : undefined,
|
|
2970
|
+
name: opts.image ? opts.image.name : undefined,
|
|
2971
|
+
}),
|
|
2972
|
+
});
|
|
2973
|
+
const body = await r.json();
|
|
2974
|
+
if (body && body.ok) {
|
|
2975
|
+
setNewNote(null);
|
|
2976
|
+
setSheet(null);
|
|
2977
|
+
setPendingSave(false);
|
|
2978
|
+
userViewRef.current = false;
|
|
2979
|
+
// 尺寸变了,fitDimsRef 清掉,下一帧按新画布重新适应
|
|
2980
|
+
fitDimsRef.current = null;
|
|
2981
|
+
return;
|
|
2982
|
+
}
|
|
2983
|
+
setNewNote((body && body.error) || t('page.new.failed'));
|
|
2984
|
+
} catch {
|
|
2985
|
+
setNewNote(t('page.new.failed'));
|
|
2986
|
+
}
|
|
2987
|
+
};
|
|
2988
|
+
|
|
2989
|
+
/** 新建时选了一张图片:读出来直接按图片尺寸建画布。 */
|
|
2990
|
+
const startNewFromImage = async (evt) => {
|
|
2991
|
+
const input = evt.target;
|
|
2992
|
+
const file = input.files && input.files[0];
|
|
2993
|
+
if (!file) return;
|
|
2994
|
+
try {
|
|
2995
|
+
const dataUrl = await readFileAsDataUrl(file);
|
|
2996
|
+
await startNewPainting({
|
|
2997
|
+
withSave: true,
|
|
2998
|
+
image: { dataUrl, name: file.name.replace(/\.[^.]+$/, '') },
|
|
2999
|
+
});
|
|
3000
|
+
} catch {
|
|
3001
|
+
setNewNote(t('page.new.failed'));
|
|
3002
|
+
} finally {
|
|
3003
|
+
input.value = '';
|
|
3004
|
+
}
|
|
3005
|
+
};
|
|
3006
|
+
|
|
3007
|
+
const onOp = async (op, extra = {}) => {
|
|
3008
|
+
try {
|
|
3009
|
+
await fetch(`${API}/op`, {
|
|
3010
|
+
method: 'POST',
|
|
3011
|
+
headers: { 'Content-Type': 'application/json' },
|
|
3012
|
+
body: JSON.stringify({ op, ...extra }),
|
|
3013
|
+
});
|
|
3014
|
+
} catch {
|
|
3015
|
+
setError(t('syncFailed'));
|
|
3016
|
+
}
|
|
3017
|
+
};
|
|
3018
|
+
|
|
3019
|
+
/** 选一张本机图片 → 宿主存进 images/ → 压成最底下的图片层(图层 1)。 */
|
|
3020
|
+
const importImage = async (evt) => {
|
|
3021
|
+
const input = evt.target;
|
|
3022
|
+
const file = input.files && input.files[0];
|
|
3023
|
+
if (!file) return;
|
|
3024
|
+
setImportNote(t('page.importing'));
|
|
3025
|
+
try {
|
|
3026
|
+
const dataUrl = await readFileAsDataUrl(file);
|
|
3027
|
+
const r = await fetch(`${API}/import`, {
|
|
3028
|
+
method: 'POST',
|
|
3029
|
+
headers: { 'Content-Type': 'application/json' },
|
|
3030
|
+
body: JSON.stringify({
|
|
3031
|
+
dataUrl,
|
|
3032
|
+
name: file.name.replace(/\.[^.]+$/, ''),
|
|
3033
|
+
fit: 'contain',
|
|
3034
|
+
}),
|
|
3035
|
+
});
|
|
3036
|
+
const body = await r.json();
|
|
3037
|
+
setImportNote(body && body.ok
|
|
3038
|
+
? `${t('page.imported')} · ${body.layer.name}`
|
|
3039
|
+
: (body && body.error) || t('syncFailed'));
|
|
3040
|
+
} catch {
|
|
3041
|
+
setImportNote(t('syncFailed'));
|
|
3042
|
+
} finally {
|
|
3043
|
+
input.value = '';
|
|
3044
|
+
}
|
|
3045
|
+
};
|
|
3046
|
+
|
|
3047
|
+
// 回放:按全局顺序逐笔重画,节奏按笔数自适应
|
|
3048
|
+
const replay = () => {
|
|
3049
|
+
const doc = docRef.current;
|
|
3050
|
+
if (!doc || playing) return;
|
|
3051
|
+
const entries = flattenStrokes(doc);
|
|
3052
|
+
if (entries.length === 0) return;
|
|
3053
|
+
const step = Math.max(1, Math.ceil(entries.length / 90));
|
|
3054
|
+
let n = 0;
|
|
3055
|
+
setPlaying(true);
|
|
3056
|
+
renderDoc(canvasRef.current, docAtStep(doc, entries, 0));
|
|
3057
|
+
const tick = () => {
|
|
3058
|
+
n += step;
|
|
3059
|
+
if (n >= entries.length) {
|
|
3060
|
+
renderDoc(canvasRef.current, doc);
|
|
3061
|
+
replayRef.current = null;
|
|
3062
|
+
setPlaying(false);
|
|
3063
|
+
return;
|
|
3064
|
+
}
|
|
3065
|
+
renderDoc(canvasRef.current, docAtStep(doc, entries, n));
|
|
3066
|
+
replayRef.current = setTimeout(tick, 60);
|
|
3067
|
+
};
|
|
3068
|
+
replayRef.current = setTimeout(tick, 80);
|
|
3069
|
+
};
|
|
3070
|
+
|
|
3071
|
+
const btn = {
|
|
3072
|
+
padding: '3px 9px',
|
|
3073
|
+
borderRadius: 6,
|
|
3074
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
3075
|
+
background: 'transparent',
|
|
3076
|
+
color: 'inherit',
|
|
3077
|
+
cursor: 'pointer',
|
|
3078
|
+
font: 'inherit',
|
|
3079
|
+
};
|
|
3080
|
+
const btnOn = (active) => ({
|
|
3081
|
+
...btn,
|
|
3082
|
+
borderColor: active ? 'var(--dsw-alias-border-l3, rgba(127,127,127,0.65))' : undefined,
|
|
3083
|
+
background: active ? 'var(--dsw-alias-bg-layer-3, rgba(127,127,127,0.14))' : 'transparent',
|
|
3084
|
+
});
|
|
3085
|
+
const selectStyle = {
|
|
3086
|
+
padding: '2px 4px',
|
|
3087
|
+
borderRadius: 4,
|
|
3088
|
+
background: 'transparent',
|
|
3089
|
+
color: 'inherit',
|
|
3090
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
3091
|
+
font: 'inherit',
|
|
3092
|
+
};
|
|
3093
|
+
const barStyle = {
|
|
3094
|
+
display: 'flex',
|
|
3095
|
+
alignItems: 'center',
|
|
3096
|
+
gap: 8,
|
|
3097
|
+
padding: '6px 8px',
|
|
3098
|
+
borderBottom: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.2))',
|
|
3099
|
+
flex: '0 0 auto',
|
|
3100
|
+
flexWrap: 'wrap',
|
|
3101
|
+
};
|
|
3102
|
+
|
|
3103
|
+
const activeLayer = meta.layers.find((l) => l.id === meta.activeLayer) || meta.layers[meta.layers.length - 1] || null;
|
|
3104
|
+
const toolBtn = (id, label) => h('button', { type: 'button', style: btnOn(tool === id), onClick: () => setTool(id) }, label);
|
|
3105
|
+
|
|
3106
|
+
/* ---------- 可复用的几块,两种排布共用 ---------- */
|
|
3107
|
+
|
|
3108
|
+
const colorCtl = h('label', { style: { display: 'inline-flex', alignItems: 'center', gap: 4 } },
|
|
3109
|
+
t('color'),
|
|
3110
|
+
h('input', {
|
|
3111
|
+
type: 'color',
|
|
3112
|
+
value: color,
|
|
3113
|
+
onChange: (e) => setColor(e.target.value),
|
|
3114
|
+
style: { width: 26, height: 20, padding: 0, border: 'none', background: 'none' },
|
|
3115
|
+
}),
|
|
3116
|
+
);
|
|
3117
|
+
|
|
3118
|
+
const sizeCtl = h('label', { style: { display: 'inline-flex', alignItems: 'center', gap: 4 } },
|
|
3119
|
+
t('size'),
|
|
3120
|
+
h('input', {
|
|
3121
|
+
type: 'range',
|
|
3122
|
+
min: 1,
|
|
3123
|
+
max: 120,
|
|
3124
|
+
value: size,
|
|
3125
|
+
onChange: (e) => setSize(Number(e.target.value)),
|
|
3126
|
+
style: { width: 96 },
|
|
3127
|
+
}),
|
|
3128
|
+
h('span', { style: { minWidth: 22, textAlign: 'right' } }, String(size)),
|
|
3129
|
+
);
|
|
3130
|
+
|
|
3131
|
+
const toolCtl = h('div', { style: { display: 'inline-flex', gap: 3, flexWrap: 'wrap' } },
|
|
3132
|
+
toolBtn('brush', t('toolBrush')),
|
|
3133
|
+
toolBtn('eraser', t('toolEraser')),
|
|
3134
|
+
toolBtn('line', t('toolLine')),
|
|
3135
|
+
toolBtn('rect', t('toolRect')),
|
|
3136
|
+
toolBtn('ellipse', t('toolEllipse')),
|
|
3137
|
+
);
|
|
3138
|
+
|
|
3139
|
+
const opsCtl = h('div', { style: { display: 'inline-flex', gap: 6, flexWrap: 'wrap', alignItems: 'center' } },
|
|
3140
|
+
h('button', { type: 'button', style: btn, disabled: playing, onClick: () => onOp('undo') }, `↶ ${t('undo')}`),
|
|
3141
|
+
h('button', {
|
|
3142
|
+
type: 'button',
|
|
3143
|
+
style: btn,
|
|
3144
|
+
disabled: playing || meta.redo === 0,
|
|
3145
|
+
onClick: () => onOp('redo'),
|
|
3146
|
+
}, `↷ ${t('redo')}`),
|
|
3147
|
+
h('button', {
|
|
3148
|
+
type: 'button',
|
|
3149
|
+
style: btn,
|
|
3150
|
+
disabled: playing || meta.strokes === 0,
|
|
3151
|
+
onClick: replay,
|
|
3152
|
+
}, `▶ ${t('replay')}`),
|
|
3153
|
+
h('button', { type: 'button', style: btn, disabled: playing, onClick: () => onOp('clear') }, t('clear')),
|
|
3154
|
+
);
|
|
3155
|
+
|
|
3156
|
+
const layersBlock = (maxH) => h(React.Fragment, null,
|
|
3157
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 6, marginBottom: 4 } },
|
|
3158
|
+
h('span', { style: { opacity: 0.6 } }, t('layers')),
|
|
3159
|
+
h('button', { type: 'button', style: btn, onClick: () => onOp('layer.add', { name: null }) }, t('layerAdd')),
|
|
3160
|
+
meta.layers.length > 1 && activeLayer
|
|
3161
|
+
? h('button', { type: 'button', style: btn, onClick: () => onOp('layer.remove', { id: activeLayer.id }) }, t('layerRemove'))
|
|
3162
|
+
: null,
|
|
3163
|
+
),
|
|
3164
|
+
h('div', {
|
|
3165
|
+
style: {
|
|
3166
|
+
display: 'flex',
|
|
3167
|
+
flexDirection: 'column',
|
|
3168
|
+
gap: 2,
|
|
3169
|
+
maxHeight: maxH || 118,
|
|
3170
|
+
overflowY: 'auto',
|
|
3171
|
+
},
|
|
3172
|
+
},
|
|
3173
|
+
meta.layers.map((l) => h('div', {
|
|
3174
|
+
key: l.id,
|
|
3175
|
+
onClick: () => onOp('layer.set', { id: l.id, patch: { active: true } }),
|
|
3176
|
+
style: {
|
|
3177
|
+
display: 'flex',
|
|
3178
|
+
alignItems: 'center',
|
|
3179
|
+
gap: 6,
|
|
3180
|
+
padding: '2px 4px',
|
|
3181
|
+
borderRadius: 4,
|
|
3182
|
+
cursor: 'pointer',
|
|
3183
|
+
background: l.id === meta.activeLayer
|
|
3184
|
+
? 'var(--dsw-alias-bg-layer-3, rgba(127,127,127,0.14))'
|
|
3185
|
+
: 'transparent',
|
|
3186
|
+
},
|
|
3187
|
+
},
|
|
3188
|
+
h('button', {
|
|
3189
|
+
type: 'button',
|
|
3190
|
+
style: { ...btn, padding: '0 4px', border: 'none' },
|
|
3191
|
+
title: t('layerVisibility'),
|
|
3192
|
+
onClick: (e) => {
|
|
3193
|
+
e.stopPropagation();
|
|
3194
|
+
onOp('layer.set', { id: l.id, patch: { visible: !l.visible } });
|
|
3195
|
+
},
|
|
3196
|
+
}, l.visible ? '◉' : '○'),
|
|
3197
|
+
h('span', {
|
|
3198
|
+
style: {
|
|
3199
|
+
flex: '1 1 auto',
|
|
3200
|
+
overflow: 'hidden',
|
|
3201
|
+
textOverflow: 'ellipsis',
|
|
3202
|
+
whiteSpace: 'nowrap',
|
|
3203
|
+
opacity: l.visible ? 1 : 0.45,
|
|
3204
|
+
},
|
|
3205
|
+
}, l.name),
|
|
3206
|
+
l.image
|
|
3207
|
+
? h('span', { style: { opacity: 0.45, fontSize: 10 } }, 'IMG')
|
|
3208
|
+
: h('span', { style: { opacity: 0.45 } }, String(l.count)),
|
|
3209
|
+
)),
|
|
3210
|
+
),
|
|
3211
|
+
activeLayer
|
|
3212
|
+
? h('div', { style: { display: 'flex', alignItems: 'center', gap: 6, paddingTop: 6, flexWrap: 'wrap' } },
|
|
3213
|
+
h('span', { style: { opacity: 0.6 } }, t('opacity')),
|
|
3214
|
+
h('input', {
|
|
3215
|
+
type: 'range',
|
|
3216
|
+
min: 0,
|
|
3217
|
+
max: 1,
|
|
3218
|
+
step: 0.05,
|
|
3219
|
+
value: activeLayer.opacity,
|
|
3220
|
+
onChange: (e) => onOp('layer.set', { id: activeLayer.id, patch: { opacity: Number(e.target.value) } }),
|
|
3221
|
+
style: { width: 72 },
|
|
3222
|
+
}),
|
|
3223
|
+
h('select', {
|
|
3224
|
+
value: activeLayer.blend,
|
|
3225
|
+
onChange: (e) => onOp('layer.set', { id: activeLayer.id, patch: { blend: e.target.value } }),
|
|
3226
|
+
style: selectStyle,
|
|
3227
|
+
},
|
|
3228
|
+
h('option', { value: 'normal' }, t('blendNormal')),
|
|
3229
|
+
h('option', { value: 'multiply' }, t('blendMultiply')),
|
|
3230
|
+
),
|
|
3231
|
+
)
|
|
3232
|
+
: null,
|
|
3233
|
+
);
|
|
3234
|
+
|
|
3235
|
+
const viewportBlock = h('div', {
|
|
3236
|
+
ref: viewportRef,
|
|
3237
|
+
onPointerDown: onViewPointerDown,
|
|
3238
|
+
onPointerMove: onViewPointerMove,
|
|
3239
|
+
onPointerUp: onViewPointerUp,
|
|
3240
|
+
onPointerCancel: onViewPointerUp,
|
|
3241
|
+
style: {
|
|
3242
|
+
flex: '1 1 auto',
|
|
3243
|
+
minHeight: 0,
|
|
3244
|
+
position: 'relative',
|
|
3245
|
+
overflow: 'hidden',
|
|
3246
|
+
touchAction: 'none',
|
|
3247
|
+
background: 'var(--dsw-alias-bg-layer-2, rgba(127,127,127,0.14))',
|
|
3248
|
+
cursor: spaceHeld ? 'grab' : 'default',
|
|
3249
|
+
},
|
|
3250
|
+
},
|
|
3251
|
+
h('div', {
|
|
3252
|
+
style: {
|
|
3253
|
+
position: 'absolute',
|
|
3254
|
+
left: 0,
|
|
3255
|
+
top: 0,
|
|
3256
|
+
width: meta.width || 1024,
|
|
3257
|
+
height: meta.height || 1024,
|
|
3258
|
+
transform: `translate(${view.x}px, ${view.y}px) scale(${view.scale})`,
|
|
3259
|
+
transformOrigin: '0 0',
|
|
3260
|
+
willChange: 'transform',
|
|
3261
|
+
},
|
|
3262
|
+
},
|
|
3263
|
+
h('canvas', {
|
|
3264
|
+
ref: canvasRef,
|
|
3265
|
+
onPointerDown,
|
|
3266
|
+
onPointerMove,
|
|
3267
|
+
onPointerUp: finish,
|
|
3268
|
+
onPointerCancel: finish,
|
|
3269
|
+
onPointerLeave: (e) => { if (dragRef.current) finish(e); },
|
|
3270
|
+
style: {
|
|
3271
|
+
width: meta.width || 1024,
|
|
3272
|
+
height: meta.height || 1024,
|
|
3273
|
+
display: 'block',
|
|
3274
|
+
background: meta.background || '#ffffff',
|
|
3275
|
+
borderRadius: 2,
|
|
3276
|
+
boxShadow: '0 6px 28px rgba(0,0,0,0.35)',
|
|
3277
|
+
touchAction: 'none',
|
|
3278
|
+
cursor: playing ? 'progress' : (spaceHeld ? 'grab' : 'crosshair'),
|
|
3279
|
+
},
|
|
3280
|
+
}),
|
|
3281
|
+
),
|
|
3282
|
+
h('div', {
|
|
3283
|
+
title: t('panHint'),
|
|
3284
|
+
style: {
|
|
3285
|
+
position: 'absolute',
|
|
3286
|
+
right: 8,
|
|
3287
|
+
bottom: 8,
|
|
3288
|
+
display: 'flex',
|
|
3289
|
+
alignItems: 'center',
|
|
3290
|
+
gap: 2,
|
|
3291
|
+
padding: '3px 5px',
|
|
3292
|
+
borderRadius: 6,
|
|
3293
|
+
background: 'rgba(0,0,0,0.42)',
|
|
3294
|
+
color: '#f2f2f2',
|
|
3295
|
+
},
|
|
3296
|
+
},
|
|
3297
|
+
h('button', { type: 'button', style: { ...btn, border: 'none', padding: '1px 7px' }, onClick: () => zoomBy(1 / 1.2) }, '−'),
|
|
3298
|
+
h('span', {
|
|
3299
|
+
style: { minWidth: 42, textAlign: 'center', fontVariantNumeric: 'tabular-nums' },
|
|
3300
|
+
}, `${Math.round(view.scale * 100)}%`),
|
|
3301
|
+
h('button', { type: 'button', style: { ...btn, border: 'none', padding: '1px 7px' }, onClick: () => zoomBy(1.2) }, '+'),
|
|
3302
|
+
h('button', {
|
|
3303
|
+
type: 'button',
|
|
3304
|
+
style: { ...btn, border: 'none', padding: '1px 7px' },
|
|
3305
|
+
onClick: () => {
|
|
3306
|
+
const d = docRef.current;
|
|
3307
|
+
if (d) fitToDoc(d.width, d.height);
|
|
3308
|
+
},
|
|
3309
|
+
}, t('zoomFit')),
|
|
3310
|
+
h('button', { type: 'button', style: { ...btn, border: 'none', padding: '1px 7px' }, onClick: zoomActual }, t('zoomActual')),
|
|
3311
|
+
),
|
|
3312
|
+
);
|
|
3313
|
+
|
|
3314
|
+
/** 整页布局里的卡片壳。 */
|
|
3315
|
+
const card = (title, children, extra) => h('div', {
|
|
3316
|
+
style: {
|
|
3317
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.22))',
|
|
3318
|
+
borderRadius: 10,
|
|
3319
|
+
padding: '9px 10px 10px',
|
|
3320
|
+
background: 'var(--dsw-alias-bg-layer-3, rgba(127,127,127,0.05))',
|
|
3321
|
+
},
|
|
3322
|
+
},
|
|
3323
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 } },
|
|
3324
|
+
h('span', { style: { fontWeight: 600, opacity: 0.85 } }, title),
|
|
3325
|
+
extra ? h('span', { style: { marginLeft: 'auto', opacity: 0.45, fontSize: 11 } }, extra) : null,
|
|
3326
|
+
),
|
|
3327
|
+
children,
|
|
3328
|
+
);
|
|
3329
|
+
|
|
3330
|
+
/* ---------- 整页模式用的按钮与弹窗卡片 ---------- */
|
|
3331
|
+
const pill = {
|
|
3332
|
+
boxSizing: 'border-box',
|
|
3333
|
+
padding: '5px 10px',
|
|
3334
|
+
borderRadius: 8,
|
|
3335
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
3336
|
+
background: 'var(--dsw-alias-bg-layer-3, rgba(127,127,127,0.1))',
|
|
3337
|
+
color: 'var(--dsw-alias-label-primary, inherit)',
|
|
3338
|
+
cursor: 'pointer',
|
|
3339
|
+
font: 'inherit',
|
|
3340
|
+
display: 'inline-flex',
|
|
3341
|
+
alignItems: 'center',
|
|
3342
|
+
gap: 6,
|
|
3343
|
+
justifyContent: 'center',
|
|
3344
|
+
whiteSpace: 'nowrap',
|
|
3345
|
+
};
|
|
3346
|
+
const pillOn = (active) => ({
|
|
3347
|
+
...pill,
|
|
3348
|
+
borderColor: active ? 'var(--dsw-alias-brand-primary, #4c8bf5)' : 'var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
3349
|
+
background: active
|
|
3350
|
+
? 'var(--dsw-alias-button-ghost-active-fill, rgba(76,139,245,0.14))'
|
|
3351
|
+
: 'var(--dsw-alias-bg-layer-3, rgba(127,127,127,0.1))',
|
|
3352
|
+
color: active ? 'var(--dsw-alias-brand-primary, #4c8bf5)' : 'var(--dsw-alias-label-primary, inherit)',
|
|
3353
|
+
});
|
|
3354
|
+
|
|
3355
|
+
const TOOLS = [
|
|
3356
|
+
{ id: 'brush', label: t('toolBrush'), glyph: '✎' },
|
|
3357
|
+
{ id: 'eraser', label: t('toolEraser'), glyph: '⌫' },
|
|
3358
|
+
{ id: 'line', label: t('toolLine'), glyph: '╱' },
|
|
3359
|
+
{ id: 'rect', label: t('toolRect'), glyph: '▭' },
|
|
3360
|
+
{ id: 'ellipse', label: t('toolEllipse'), glyph: '◯' },
|
|
3361
|
+
];
|
|
3362
|
+
const currentTool = TOOLS.find((x) => x.id === tool) || TOOLS[0];
|
|
3363
|
+
|
|
3364
|
+
const currentBrush = BRUSH_BY_ID.get(brush) || BRUSH_BY_ID.get('round');
|
|
3365
|
+
const brushGroups = BRUSH_GROUPS.map((gid) => ({
|
|
3366
|
+
id: gid,
|
|
3367
|
+
label: t(`brushGroup.${gid}`),
|
|
3368
|
+
items: BRUSHES.filter((b) => b.group === gid).map((b) => BRUSH_BY_ID.get(b.id)),
|
|
3369
|
+
}));
|
|
3370
|
+
|
|
3371
|
+
/**
|
|
3372
|
+
* 侧边图层视窗:像画世界那样贴在画板右边,能收起成一条窄轨。
|
|
3373
|
+
* 展开时是「头部(层数/收起)+ 层列表(缩略图/眼睛/不透明度/混合)+ 底部(新建/删除)」。
|
|
3374
|
+
*/
|
|
3375
|
+
const layerDockBlock = h('div', {
|
|
3376
|
+
key: 'layer-dock',
|
|
3377
|
+
style: {
|
|
3378
|
+
flex: `0 0 ${layerDock ? 214 : 34}px`,
|
|
3379
|
+
width: layerDock ? 214 : 34,
|
|
3380
|
+
display: 'flex',
|
|
3381
|
+
flexDirection: 'column',
|
|
3382
|
+
minHeight: 0,
|
|
3383
|
+
overflow: 'hidden',
|
|
3384
|
+
background: 'var(--dsw-alias-bg-layer-1, #fff)',
|
|
3385
|
+
borderLeft: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.22))',
|
|
3386
|
+
transition: 'flex-basis 140ms ease, width 140ms ease',
|
|
3387
|
+
},
|
|
3388
|
+
},
|
|
3389
|
+
layerDock
|
|
3390
|
+
? [
|
|
3391
|
+
h('div', {
|
|
3392
|
+
key: 'head',
|
|
3393
|
+
style: {
|
|
3394
|
+
display: 'flex',
|
|
3395
|
+
alignItems: 'center',
|
|
3396
|
+
gap: 6,
|
|
3397
|
+
padding: '7px 8px',
|
|
3398
|
+
borderBottom: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.22))',
|
|
3399
|
+
},
|
|
3400
|
+
},
|
|
3401
|
+
h('span', { style: { fontWeight: 700, fontSize: 12 } }, t('layers')),
|
|
3402
|
+
h('span', {
|
|
3403
|
+
style: { fontSize: 11, color: 'var(--dsw-alias-label-tertiary, inherit)' },
|
|
3404
|
+
}, String(meta.layers.length)),
|
|
3405
|
+
h('button', {
|
|
3406
|
+
type: 'button',
|
|
3407
|
+
title: t('layerCollapse'),
|
|
3408
|
+
style: { ...pill, marginLeft: 'auto', padding: '1px 8px', fontSize: 12 },
|
|
3409
|
+
onClick: () => setLayerDock(false),
|
|
3410
|
+
}, '›'),
|
|
3411
|
+
),
|
|
3412
|
+
h('div', {
|
|
3413
|
+
key: 'list',
|
|
3414
|
+
style: {
|
|
3415
|
+
flex: '1 1 auto',
|
|
3416
|
+
minHeight: 0,
|
|
3417
|
+
overflowY: 'auto',
|
|
3418
|
+
padding: 6,
|
|
3419
|
+
display: 'flex',
|
|
3420
|
+
flexDirection: 'column',
|
|
3421
|
+
gap: 3,
|
|
3422
|
+
},
|
|
3423
|
+
},
|
|
3424
|
+
[...meta.layers].reverse().map((l) => {
|
|
3425
|
+
const isActive = l.id === meta.activeLayer;
|
|
3426
|
+
return h('div', {
|
|
3427
|
+
key: l.id,
|
|
3428
|
+
onClick: () => onOp('layer.set', { id: l.id, patch: { active: true } }),
|
|
3429
|
+
style: {
|
|
3430
|
+
display: 'flex',
|
|
3431
|
+
alignItems: 'center',
|
|
3432
|
+
gap: 6,
|
|
3433
|
+
padding: '4px 5px',
|
|
3434
|
+
borderRadius: 7,
|
|
3435
|
+
cursor: 'pointer',
|
|
3436
|
+
border: `1px solid ${isActive ? 'var(--dsw-alias-brand-primary, #4c8bf5)' : 'transparent'}`,
|
|
3437
|
+
background: isActive
|
|
3438
|
+
? 'var(--dsw-alias-button-ghost-active-fill, rgba(76,139,245,0.14))'
|
|
3439
|
+
: 'transparent',
|
|
3440
|
+
opacity: l.visible ? 1 : 0.55,
|
|
3441
|
+
},
|
|
3442
|
+
},
|
|
3443
|
+
h('button', {
|
|
3444
|
+
type: 'button',
|
|
3445
|
+
title: t('layerVisibility'),
|
|
3446
|
+
style: {
|
|
3447
|
+
border: 'none',
|
|
3448
|
+
background: 'transparent',
|
|
3449
|
+
color: 'var(--dsw-alias-label-secondary, inherit)',
|
|
3450
|
+
cursor: 'pointer',
|
|
3451
|
+
padding: 0,
|
|
3452
|
+
fontSize: 12,
|
|
3453
|
+
flex: '0 0 14px',
|
|
3454
|
+
},
|
|
3455
|
+
onClick: (e) => {
|
|
3456
|
+
e.stopPropagation();
|
|
3457
|
+
onOp('layer.set', { id: l.id, patch: { visible: !l.visible } });
|
|
3458
|
+
},
|
|
3459
|
+
}, l.visible ? '◉' : '○'),
|
|
3460
|
+
h(LayerThumb, { layerId: l.id, sig: `${l.count}:${l.image ? 'img' : 'vec'}`, docRef, rev: revision }),
|
|
3461
|
+
h('div', { style: { flex: '1 1 auto', minWidth: 0 } },
|
|
3462
|
+
h('div', {
|
|
3463
|
+
style: {
|
|
3464
|
+
fontSize: 11.5,
|
|
3465
|
+
fontWeight: isActive ? 600 : 400,
|
|
3466
|
+
overflow: 'hidden',
|
|
3467
|
+
textOverflow: 'ellipsis',
|
|
3468
|
+
whiteSpace: 'nowrap',
|
|
3469
|
+
},
|
|
3470
|
+
}, l.name),
|
|
3471
|
+
h('div', {
|
|
3472
|
+
style: {
|
|
3473
|
+
fontSize: 10,
|
|
3474
|
+
color: 'var(--dsw-alias-label-tertiary, inherit)',
|
|
3475
|
+
display: 'flex',
|
|
3476
|
+
gap: 6,
|
|
3477
|
+
overflow: 'hidden',
|
|
3478
|
+
whiteSpace: 'nowrap',
|
|
3479
|
+
},
|
|
3480
|
+
},
|
|
3481
|
+
h('span', null, l.image ? 'IMG' : `${l.count} ${t('strokes')}`),
|
|
3482
|
+
l.opacity < 1 ? h('span', null, `${Math.round(l.opacity * 100)}%`) : null,
|
|
3483
|
+
l.blend === 'multiply' ? h('span', null, t('blendMultiply')) : null,
|
|
3484
|
+
),
|
|
3485
|
+
),
|
|
3486
|
+
);
|
|
3487
|
+
}),
|
|
3488
|
+
),
|
|
3489
|
+
activeLayer
|
|
3490
|
+
? h('div', {
|
|
3491
|
+
key: 'foot',
|
|
3492
|
+
style: {
|
|
3493
|
+
borderTop: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.22))',
|
|
3494
|
+
padding: '7px 8px 9px',
|
|
3495
|
+
display: 'flex',
|
|
3496
|
+
flexDirection: 'column',
|
|
3497
|
+
gap: 6,
|
|
3498
|
+
},
|
|
3499
|
+
},
|
|
3500
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 6, fontSize: 11 } },
|
|
3501
|
+
h('span', { style: { color: 'var(--dsw-alias-label-tertiary, inherit)' } }, t('opacity')),
|
|
3502
|
+
h('input', {
|
|
3503
|
+
type: 'range',
|
|
3504
|
+
min: 0,
|
|
3505
|
+
max: 1,
|
|
3506
|
+
step: 0.05,
|
|
3507
|
+
value: optOpacity && optOpacity.id === activeLayer.id ? optOpacity.v : activeLayer.opacity,
|
|
3508
|
+
onChange: (e) => {
|
|
3509
|
+
const v = Number(e.target.value);
|
|
3510
|
+
setOptOpacity({ id: activeLayer.id, v });
|
|
3511
|
+
onOp('layer.set', { id: activeLayer.id, patch: { opacity: v } });
|
|
3512
|
+
},
|
|
3513
|
+
style: { flex: '1 1 auto', minWidth: 0, accentColor: 'var(--dsw-alias-brand-primary, #4c8bf5)' },
|
|
3514
|
+
}),
|
|
3515
|
+
h('span', {
|
|
3516
|
+
style: { minWidth: 30, textAlign: 'right', fontVariantNumeric: 'tabular-nums' },
|
|
3517
|
+
}, `${Math.round((optOpacity && optOpacity.id === activeLayer.id ? optOpacity.v : activeLayer.opacity) * 100)}%`),
|
|
3518
|
+
),
|
|
3519
|
+
h('select', {
|
|
3520
|
+
value: activeLayer.blend,
|
|
3521
|
+
onChange: (e) => onOp('layer.set', { id: activeLayer.id, patch: { blend: e.target.value } }),
|
|
3522
|
+
style: {
|
|
3523
|
+
width: '100%',
|
|
3524
|
+
boxSizing: 'border-box',
|
|
3525
|
+
borderRadius: 7,
|
|
3526
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
3527
|
+
background: 'var(--dsw-alias-bg-layer-3, rgba(127,127,127,0.1))',
|
|
3528
|
+
color: 'var(--dsw-alias-label-primary, inherit)',
|
|
3529
|
+
padding: '3px 6px',
|
|
3530
|
+
font: 'inherit',
|
|
3531
|
+
},
|
|
3532
|
+
},
|
|
3533
|
+
h('option', { value: 'normal' }, t('blendNormal')),
|
|
3534
|
+
h('option', { value: 'multiply' }, t('blendMultiply')),
|
|
3535
|
+
),
|
|
3536
|
+
h('div', { style: { display: 'flex', gap: 6 } },
|
|
3537
|
+
h('button', {
|
|
3538
|
+
type: 'button',
|
|
3539
|
+
style: { ...pill, flex: '1 1 auto', padding: '4px 8px' },
|
|
3540
|
+
onClick: () => onOp('layer.add', { name: null }),
|
|
3541
|
+
}, `+ ${t('layerAdd')}`),
|
|
3542
|
+
meta.layers.length > 1
|
|
3543
|
+
? h('button', {
|
|
3544
|
+
type: 'button',
|
|
3545
|
+
style: { ...pill, padding: '4px 8px' },
|
|
3546
|
+
onClick: () => onOp('layer.remove', { id: activeLayer.id }),
|
|
3547
|
+
}, t('layerRemove'))
|
|
3548
|
+
: null,
|
|
3549
|
+
),
|
|
3550
|
+
)
|
|
3551
|
+
: null,
|
|
3552
|
+
]
|
|
3553
|
+
: h('button', {
|
|
3554
|
+
type: 'button',
|
|
3555
|
+
title: t('layerExpand'),
|
|
3556
|
+
onClick: () => setLayerDock(true),
|
|
3557
|
+
style: {
|
|
3558
|
+
flex: '1 1 auto',
|
|
3559
|
+
display: 'flex',
|
|
3560
|
+
flexDirection: 'column',
|
|
3561
|
+
alignItems: 'center',
|
|
3562
|
+
gap: 8,
|
|
3563
|
+
padding: '10px 0',
|
|
3564
|
+
border: 'none',
|
|
3565
|
+
background: 'transparent',
|
|
3566
|
+
color: 'var(--dsw-alias-label-secondary, inherit)',
|
|
3567
|
+
cursor: 'pointer',
|
|
3568
|
+
},
|
|
3569
|
+
},
|
|
3570
|
+
h('span', { style: { fontSize: 14 } }, '▤'),
|
|
3571
|
+
h('span', {
|
|
3572
|
+
style: { writingMode: 'vertical-rl', fontSize: 11, letterSpacing: 1 },
|
|
3573
|
+
}, t('layers')),
|
|
3574
|
+
h('span', {
|
|
3575
|
+
style: { fontSize: 10, color: 'var(--dsw-alias-label-tertiary, inherit)' },
|
|
3576
|
+
}, String(meta.layers.length)),
|
|
3577
|
+
),
|
|
3578
|
+
);
|
|
3579
|
+
|
|
3580
|
+
// 整页模式:A1 画板在左,B1 画笔 / B2 编辑与画布在右;多选项都收进弹窗卡片
|
|
3581
|
+
if (props.variant === 'page') {
|
|
3582
|
+
const layerName = activeLayer ? activeLayer.name : '—';
|
|
3583
|
+
return h('div', {
|
|
3584
|
+
style: {
|
|
3585
|
+
display: 'flex',
|
|
3586
|
+
height: '100%',
|
|
3587
|
+
minHeight: 0,
|
|
3588
|
+
minWidth: 0,
|
|
3589
|
+
overflow: 'hidden',
|
|
3590
|
+
font: '12px/1.5 system-ui, "Microsoft YaHei", sans-serif',
|
|
3591
|
+
color: 'var(--dsw-alias-label-primary, inherit)',
|
|
3592
|
+
},
|
|
3593
|
+
},
|
|
3594
|
+
// A1:画板
|
|
3595
|
+
h('div', {
|
|
3596
|
+
style: {
|
|
3597
|
+
flex: '1 1 auto',
|
|
3598
|
+
minWidth: 0,
|
|
3599
|
+
minHeight: 260,
|
|
3600
|
+
display: 'flex',
|
|
3601
|
+
flexDirection: 'column',
|
|
3602
|
+
padding: 12,
|
|
3603
|
+
},
|
|
3604
|
+
},
|
|
3605
|
+
error
|
|
3606
|
+
? h('div', { style: { padding: '0 0 6px', color: 'var(--dsw-alias-label-error, #e5484d)' } }, error)
|
|
3607
|
+
: null,
|
|
3608
|
+
staleHost
|
|
3609
|
+
? h('div', {
|
|
3610
|
+
style: {
|
|
3611
|
+
marginBottom: 6,
|
|
3612
|
+
padding: '5px 8px',
|
|
3613
|
+
borderRadius: 8,
|
|
3614
|
+
fontSize: 11,
|
|
3615
|
+
color: 'var(--dsw-alias-label-error, #e5484d)',
|
|
3616
|
+
background: 'var(--dsw-alias-bg-layer-2, rgba(127,127,127,0.1))',
|
|
3617
|
+
},
|
|
3618
|
+
}, t('staleHost'))
|
|
3619
|
+
: null,
|
|
3620
|
+
updateInfo && updateInfo.hasUpdate
|
|
3621
|
+
? h('div', {
|
|
3622
|
+
style: {
|
|
3623
|
+
marginBottom: 6,
|
|
3624
|
+
padding: '5px 8px',
|
|
3625
|
+
borderRadius: 8,
|
|
3626
|
+
fontSize: 11,
|
|
3627
|
+
display: 'flex',
|
|
3628
|
+
gap: 8,
|
|
3629
|
+
alignItems: 'center',
|
|
3630
|
+
flexWrap: 'wrap',
|
|
3631
|
+
color: 'var(--dsw-alias-label-secondary, inherit)',
|
|
3632
|
+
background: 'var(--dsw-alias-bg-layer-2, rgba(127,127,127,0.1))',
|
|
3633
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.2))',
|
|
3634
|
+
},
|
|
3635
|
+
},
|
|
3636
|
+
h('span', null, `${t('updateTip')} ${updateInfo.latest}${updateInfo.local ? `(${t('updateNow')} ${updateInfo.local})` : ''}`),
|
|
3637
|
+
h('button', {
|
|
3638
|
+
type: 'button',
|
|
3639
|
+
onClick: applyUpdate,
|
|
3640
|
+
disabled: applying,
|
|
3641
|
+
style: {
|
|
3642
|
+
padding: '2px 8px',
|
|
3643
|
+
borderRadius: 6,
|
|
3644
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
3645
|
+
background: 'var(--dsw-alias-brand-primary, #4d6bfe)',
|
|
3646
|
+
color: '#fff',
|
|
3647
|
+
fontSize: 11,
|
|
3648
|
+
fontWeight: 600,
|
|
3649
|
+
cursor: applying ? 'default' : 'pointer',
|
|
3650
|
+
opacity: applying ? 0.7 : 1,
|
|
3651
|
+
},
|
|
3652
|
+
}, applying ? t('updateApplying') : t('updateApply')),
|
|
3653
|
+
updateInfo.url
|
|
3654
|
+
? h('a', {
|
|
3655
|
+
href: updateInfo.url,
|
|
3656
|
+
target: '_blank',
|
|
3657
|
+
rel: 'noreferrer',
|
|
3658
|
+
style: { color: 'var(--dsw-alias-brand-primary, inherit)', fontWeight: 600 },
|
|
3659
|
+
}, t('updateGo'))
|
|
3660
|
+
: null,
|
|
3661
|
+
applyNote
|
|
3662
|
+
? h('span', { style: { opacity: 0.8 } }, applyNote)
|
|
3663
|
+
: null)
|
|
3664
|
+
: null,
|
|
3665
|
+
h('div', {
|
|
3666
|
+
style: {
|
|
3667
|
+
flex: '1 1 auto',
|
|
3668
|
+
minHeight: 0,
|
|
3669
|
+
display: 'flex',
|
|
3670
|
+
borderRadius: 12,
|
|
3671
|
+
overflow: 'hidden',
|
|
3672
|
+
border: '1px solid var(--dsw-alias-border-l2, rgba(127,127,127,0.25))',
|
|
3673
|
+
},
|
|
3674
|
+
}, viewportBlock, layerDockBlock),
|
|
3675
|
+
),
|
|
3676
|
+
|
|
3677
|
+
// 右列
|
|
3678
|
+
h('aside', {
|
|
3679
|
+
style: {
|
|
3680
|
+
flex: '0 0 286px',
|
|
3681
|
+
width: 286,
|
|
3682
|
+
minHeight: 0,
|
|
3683
|
+
borderLeft: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.22))',
|
|
3684
|
+
padding: '12px 12px 20px',
|
|
3685
|
+
display: 'flex',
|
|
3686
|
+
flexDirection: 'column',
|
|
3687
|
+
gap: 10,
|
|
3688
|
+
overflowY: 'auto',
|
|
3689
|
+
boxSizing: 'border-box',
|
|
3690
|
+
},
|
|
3691
|
+
},
|
|
3692
|
+
// B1:画笔
|
|
3693
|
+
card(t('page.tools'), h(React.Fragment, null,
|
|
3694
|
+
h('div', { style: { display: 'flex', alignItems: 'stretch', gap: 8, marginBottom: 10 } },
|
|
3695
|
+
h('button', {
|
|
3696
|
+
type: 'button',
|
|
3697
|
+
style: { ...pill, flex: '1 1 auto', justifyContent: 'flex-start', padding: '7px 10px', fontSize: 13 },
|
|
3698
|
+
onClick: () => setSheet('tools'),
|
|
3699
|
+
},
|
|
3700
|
+
h('span', { style: { fontSize: 15, lineHeight: 1 } }, currentTool.glyph),
|
|
3701
|
+
h('span', { style: { fontWeight: 600 } }, currentTool.label),
|
|
3702
|
+
h('span', {
|
|
3703
|
+
style: { color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 11 },
|
|
3704
|
+
}, `· ${currentBrush.label}`),
|
|
3705
|
+
h('span', { style: { marginLeft: 'auto', color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 11 } }, '▾'),
|
|
3706
|
+
),
|
|
3707
|
+
h('label', {
|
|
3708
|
+
title: t('color'),
|
|
3709
|
+
style: {
|
|
3710
|
+
flex: '0 0 auto',
|
|
3711
|
+
width: 38,
|
|
3712
|
+
height: 34,
|
|
3713
|
+
borderRadius: 8,
|
|
3714
|
+
border: '1px solid var(--dsw-alias-border-l2, rgba(127,127,127,0.35))',
|
|
3715
|
+
background: color,
|
|
3716
|
+
cursor: 'pointer',
|
|
3717
|
+
position: 'relative',
|
|
3718
|
+
overflow: 'hidden',
|
|
3719
|
+
boxSizing: 'border-box',
|
|
3720
|
+
},
|
|
3721
|
+
},
|
|
3722
|
+
h('input', {
|
|
3723
|
+
type: 'color',
|
|
3724
|
+
value: color,
|
|
3725
|
+
onChange: (e) => setColor(e.target.value),
|
|
3726
|
+
style: { position: 'absolute', inset: 0, opacity: 0, cursor: 'pointer', width: '100%', height: '100%' },
|
|
3727
|
+
}),
|
|
3728
|
+
),
|
|
3729
|
+
),
|
|
3730
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10 } },
|
|
3731
|
+
h('span', { style: { color: 'var(--dsw-alias-label-tertiary, inherit)', flex: '0 0 auto' } }, t('size')),
|
|
3732
|
+
h('input', {
|
|
3733
|
+
type: 'range',
|
|
3734
|
+
min: 1,
|
|
3735
|
+
max: 120,
|
|
3736
|
+
value: size,
|
|
3737
|
+
onChange: (e) => setSize(Number(e.target.value)),
|
|
3738
|
+
style: { flex: '1 1 auto', minWidth: 0, accentColor: 'var(--dsw-alias-brand-primary, #4c8bf5)' },
|
|
3739
|
+
}),
|
|
3740
|
+
h('span', {
|
|
3741
|
+
style: {
|
|
3742
|
+
flex: '0 0 auto',
|
|
3743
|
+
minWidth: 26,
|
|
3744
|
+
textAlign: 'right',
|
|
3745
|
+
fontVariantNumeric: 'tabular-nums',
|
|
3746
|
+
fontWeight: 600,
|
|
3747
|
+
},
|
|
3748
|
+
}, String(size)),
|
|
3749
|
+
),
|
|
3750
|
+
h('button', {
|
|
3751
|
+
type: 'button',
|
|
3752
|
+
style: { ...pill, width: '100%', justifyContent: 'flex-start', padding: '7px 10px' },
|
|
3753
|
+
onClick: () => setLayerDock((v) => !v),
|
|
3754
|
+
},
|
|
3755
|
+
h('span', { style: { fontSize: 13 } }, '▤'),
|
|
3756
|
+
h('span', null, t('layers')),
|
|
3757
|
+
h('span', {
|
|
3758
|
+
style: { color: 'var(--dsw-alias-label-tertiary, inherit)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth: 130 },
|
|
3759
|
+
}, layerName),
|
|
3760
|
+
h('span', { style: { marginLeft: 'auto', color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 11 } },
|
|
3761
|
+
`${meta.layers.length} ${layerDock ? '▸' : '◂'}`),
|
|
3762
|
+
),
|
|
3763
|
+
), `${meta.strokes} ${t('strokes')}`),
|
|
3764
|
+
|
|
3765
|
+
// B2:编辑 + 画布
|
|
3766
|
+
card(t('page.edit'), h(React.Fragment, null,
|
|
3767
|
+
h('div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 6, marginBottom: 6 } },
|
|
3768
|
+
h('button', { type: 'button', style: { ...pill, opacity: playing ? 0.55 : 1 }, disabled: playing, onClick: () => onOp('undo') }, `↶ ${t('undo')}`),
|
|
3769
|
+
h('button', {
|
|
3770
|
+
type: 'button',
|
|
3771
|
+
style: { ...pill, opacity: (playing || meta.redo === 0) ? 0.55 : 1 },
|
|
3772
|
+
disabled: playing || meta.redo === 0,
|
|
3773
|
+
onClick: () => onOp('redo'),
|
|
3774
|
+
}, `↷ ${t('redo')}`),
|
|
3775
|
+
h('button', {
|
|
3776
|
+
type: 'button',
|
|
3777
|
+
style: { ...pill, opacity: (playing || meta.strokes === 0) ? 0.55 : 1 },
|
|
3778
|
+
disabled: playing || meta.strokes === 0,
|
|
3779
|
+
onClick: replay,
|
|
3780
|
+
}, `▶ ${t('replay')}`),
|
|
3781
|
+
h('button', { type: 'button', style: { ...pill, opacity: playing ? 0.55 : 1 }, disabled: playing, onClick: () => onOp('clear') }, `⌫ ${t('clear')}`),
|
|
3782
|
+
),
|
|
3783
|
+
h('button', {
|
|
3784
|
+
type: 'button',
|
|
3785
|
+
style: { ...pill, width: '100%', justifyContent: 'flex-start', padding: '7px 10px', marginTop: 2 },
|
|
3786
|
+
onClick: () => setSheet('canvas'),
|
|
3787
|
+
},
|
|
3788
|
+
h('span', { style: { fontSize: 13 } }, '⬒'),
|
|
3789
|
+
h('span', null, t('page.canvas')),
|
|
3790
|
+
h('span', {
|
|
3791
|
+
style: { marginLeft: 'auto', color: 'var(--dsw-alias-label-tertiary, inherit)', fontVariantNumeric: 'tabular-nums' },
|
|
3792
|
+
}, `${meta.width}×${meta.height}`),
|
|
3793
|
+
h('span', { style: { color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 11 } }, '▸'),
|
|
3794
|
+
),
|
|
3795
|
+
), `rev ${revision}`),
|
|
3796
|
+
|
|
3797
|
+
// B3:版本(自检结果一直看得见,不然「已是最新」跟「没查」没法区分)
|
|
3798
|
+
card(t('versionCard'), h(React.Fragment, null,
|
|
3799
|
+
h('div', {
|
|
3800
|
+
style: { display: 'flex', alignItems: 'baseline', gap: 6, fontSize: 11.5, marginBottom: 3 },
|
|
3801
|
+
},
|
|
3802
|
+
h('span', { style: { color: 'var(--dsw-alias-label-tertiary, inherit)' } }, t('versionCurrent')),
|
|
3803
|
+
h('span', {
|
|
3804
|
+
style: { fontWeight: 600, fontVariantNumeric: 'tabular-nums' },
|
|
3805
|
+
}, localVer || (updateInfo && updateInfo.local) || '—'),
|
|
3806
|
+
h('span', {
|
|
3807
|
+
style: { marginLeft: 'auto', fontSize: 11 },
|
|
3808
|
+
},
|
|
3809
|
+
updateInfo
|
|
3810
|
+
? (updateInfo.enabled === false
|
|
3811
|
+
? t('versionOff')
|
|
3812
|
+
: updateInfo.error
|
|
3813
|
+
? `${t('versionCheckFail')}`
|
|
3814
|
+
: updateInfo.hasUpdate
|
|
3815
|
+
? `${t('updateTip')} ${updateInfo.latest}`
|
|
3816
|
+
: t('versionUpToDate'))
|
|
3817
|
+
: t('versionNever')),
|
|
3818
|
+
),
|
|
3819
|
+
updateInfo && updateInfo.latest && !updateInfo.error
|
|
3820
|
+
? h('div', {
|
|
3821
|
+
style: {
|
|
3822
|
+
fontSize: 11,
|
|
3823
|
+
color: 'var(--dsw-alias-label-tertiary, inherit)',
|
|
3824
|
+
marginBottom: 6,
|
|
3825
|
+
fontVariantNumeric: 'tabular-nums',
|
|
3826
|
+
},
|
|
3827
|
+
}, `${t('versionLatest')} ${updateInfo.latest}${updateInfo.checkedAt ? ` · ${t('versionCheckedAt')} ${new Date(updateInfo.checkedAt).toLocaleTimeString()}` : ''}`)
|
|
3828
|
+
: null,
|
|
3829
|
+
h('div', { style: { display: 'flex', gap: 6, flexWrap: 'wrap' } },
|
|
3830
|
+
h('button', {
|
|
3831
|
+
type: 'button',
|
|
3832
|
+
style: { ...pill, flex: '1 1 auto', padding: '5px 8px', opacity: checking ? 0.6 : 1 },
|
|
3833
|
+
disabled: checking,
|
|
3834
|
+
onClick: checkUpdateNow,
|
|
3835
|
+
}, checking ? t('versionChecking') : t('versionCheckNow')),
|
|
3836
|
+
updateInfo && updateInfo.hasUpdate
|
|
3837
|
+
? h('button', {
|
|
3838
|
+
type: 'button',
|
|
3839
|
+
style: {
|
|
3840
|
+
...pill,
|
|
3841
|
+
flex: '1 1 auto',
|
|
3842
|
+
padding: '5px 8px',
|
|
3843
|
+
borderColor: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
3844
|
+
color: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
3845
|
+
fontWeight: 600,
|
|
3846
|
+
opacity: applying ? 0.7 : 1,
|
|
3847
|
+
},
|
|
3848
|
+
disabled: applying,
|
|
3849
|
+
onClick: applyUpdate,
|
|
3850
|
+
}, applying ? t('updateApplying') : t('updateApply'))
|
|
3851
|
+
: null,
|
|
3852
|
+
),
|
|
3853
|
+
checkNote
|
|
3854
|
+
? h('div', {
|
|
3855
|
+
style: { fontSize: 11, paddingTop: 5, color: 'var(--dsw-alias-label-error, #e5484d)' },
|
|
3856
|
+
}, checkNote)
|
|
3857
|
+
: null,
|
|
3858
|
+
updateInfo && updateInfo.error
|
|
3859
|
+
? h('div', {
|
|
3860
|
+
style: { fontSize: 10.5, paddingTop: 5, color: 'var(--dsw-alias-label-tertiary, inherit)', wordBreak: 'break-all' },
|
|
3861
|
+
}, String(updateInfo.error).slice(0, 160))
|
|
3862
|
+
: null,
|
|
3863
|
+
), updateInfo && updateInfo.enabled === false ? t('versionOff') : undefined),
|
|
3864
|
+
|
|
3865
|
+
h('div', {
|
|
3866
|
+
style: {
|
|
3867
|
+
padding: '2px 2px 0',
|
|
3868
|
+
color: 'var(--dsw-alias-label-caption, inherit)',
|
|
3869
|
+
fontSize: 11,
|
|
3870
|
+
lineHeight: 1.6,
|
|
3871
|
+
},
|
|
3872
|
+
}, t('panHint')),
|
|
3873
|
+
),
|
|
3874
|
+
|
|
3875
|
+
// 弹窗卡片:工具、图层、画布设置
|
|
3876
|
+
h(Sheet, {
|
|
3877
|
+
open: sheet === 'tools',
|
|
3878
|
+
title: t('page.tools'),
|
|
3879
|
+
width: 560,
|
|
3880
|
+
onClose: () => setSheet(null),
|
|
3881
|
+
},
|
|
3882
|
+
h('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(3, minmax(0, 1fr))', gap: 8 } },
|
|
3883
|
+
TOOLS.map((x) => h('button', {
|
|
3884
|
+
key: x.id,
|
|
3885
|
+
type: 'button',
|
|
3886
|
+
style: { ...pill, flexDirection: 'column', gap: 4, padding: '12px 8px', fontSize: 13 },
|
|
3887
|
+
onClick: () => {
|
|
3888
|
+
setTool(x.id);
|
|
3889
|
+
setSheet(null);
|
|
3890
|
+
},
|
|
3891
|
+
...(x.id === tool
|
|
3892
|
+
? {
|
|
3893
|
+
borderColor: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
3894
|
+
background: 'var(--dsw-alias-button-ghost-active-fill, rgba(76,139,245,0.14))',
|
|
3895
|
+
color: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
3896
|
+
}
|
|
3897
|
+
: null),
|
|
3898
|
+
},
|
|
3899
|
+
h('span', { style: { fontSize: 20, lineHeight: 1 } }, x.glyph),
|
|
3900
|
+
h('span', null, x.label),
|
|
3901
|
+
)),
|
|
3902
|
+
),
|
|
3903
|
+
h('div', {
|
|
3904
|
+
style: {
|
|
3905
|
+
marginTop: 14,
|
|
3906
|
+
paddingTop: 10,
|
|
3907
|
+
borderTop: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.22))',
|
|
3908
|
+
},
|
|
3909
|
+
},
|
|
3910
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 8, marginBottom: 8 } },
|
|
3911
|
+
h('span', { style: { fontWeight: 600 } }, t('brush')),
|
|
3912
|
+
h('span', {
|
|
3913
|
+
style: { color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 11 },
|
|
3914
|
+
}, t('brushHint')),
|
|
3915
|
+
),
|
|
3916
|
+
h('div', { style: { maxHeight: '52vh', overflowY: 'auto', paddingRight: 4 } },
|
|
3917
|
+
brushGroups.map((grp) => h('div', { key: grp.id, style: { marginBottom: 10 } },
|
|
3918
|
+
h('div', {
|
|
3919
|
+
style: {
|
|
3920
|
+
color: 'var(--dsw-alias-label-secondary, inherit)',
|
|
3921
|
+
fontSize: 11,
|
|
3922
|
+
fontWeight: 600,
|
|
3923
|
+
marginBottom: 5,
|
|
3924
|
+
},
|
|
3925
|
+
}, grp.label),
|
|
3926
|
+
h('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(4, minmax(0, 1fr))', gap: 6 } },
|
|
3927
|
+
grp.items.map((b) => h('button', {
|
|
3928
|
+
key: b.id,
|
|
3929
|
+
type: 'button',
|
|
3930
|
+
title: t(`brush.${b.id}`),
|
|
3931
|
+
style: {
|
|
3932
|
+
...pill,
|
|
3933
|
+
flexDirection: 'column',
|
|
3934
|
+
alignItems: 'stretch',
|
|
3935
|
+
gap: 3,
|
|
3936
|
+
padding: '5px 6px 4px',
|
|
3937
|
+
minWidth: 0,
|
|
3938
|
+
},
|
|
3939
|
+
onClick: () => {
|
|
3940
|
+
setBrush(b.id);
|
|
3941
|
+
setSheet(null);
|
|
3942
|
+
},
|
|
3943
|
+
...(b.id === brush
|
|
3944
|
+
? {
|
|
3945
|
+
borderColor: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
3946
|
+
color: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
3947
|
+
background: 'var(--dsw-alias-button-ghost-active-fill, rgba(76,139,245,0.14))',
|
|
3948
|
+
}
|
|
3949
|
+
: null),
|
|
3950
|
+
},
|
|
3951
|
+
h(BrushPreview, { id: b.id }),
|
|
3952
|
+
h('span', {
|
|
3953
|
+
style: { fontSize: 11, textAlign: 'center', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
3954
|
+
}, t(`brush.${b.id}`)),
|
|
3955
|
+
)),
|
|
3956
|
+
),
|
|
3957
|
+
)),
|
|
3958
|
+
),
|
|
3959
|
+
h('label', {
|
|
3960
|
+
style: {
|
|
3961
|
+
display: 'flex',
|
|
3962
|
+
alignItems: 'center',
|
|
3963
|
+
gap: 8,
|
|
3964
|
+
paddingTop: 8,
|
|
3965
|
+
cursor: 'pointer',
|
|
3966
|
+
fontSize: 12,
|
|
3967
|
+
},
|
|
3968
|
+
},
|
|
3969
|
+
h('input', {
|
|
3970
|
+
type: 'checkbox',
|
|
3971
|
+
checked: taper,
|
|
3972
|
+
onChange: (e) => setTaper(e.target.checked),
|
|
3973
|
+
style: { accentColor: 'var(--dsw-alias-brand-primary, #4c8bf5)' },
|
|
3974
|
+
}),
|
|
3975
|
+
h('span', null, t('taper')),
|
|
3976
|
+
h('span', {
|
|
3977
|
+
style: { color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 11 },
|
|
3978
|
+
}, t('taperHint')),
|
|
3979
|
+
),
|
|
3980
|
+
),
|
|
3981
|
+
),
|
|
3982
|
+
|
|
3983
|
+
h(Sheet, {
|
|
3984
|
+
open: sheet === 'layers',
|
|
3985
|
+
title: t('layers'),
|
|
3986
|
+
width: 460,
|
|
3987
|
+
onClose: () => setSheet(null),
|
|
3988
|
+
},
|
|
3989
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10, flexWrap: 'wrap' } },
|
|
3990
|
+
h('button', {
|
|
3991
|
+
type: 'button',
|
|
3992
|
+
style: pill,
|
|
3993
|
+
onClick: () => {
|
|
3994
|
+
if (importRef.current) importRef.current.click();
|
|
3995
|
+
},
|
|
3996
|
+
}, `🖼 ${t('page.importLayer')}`),
|
|
3997
|
+
h('input', {
|
|
3998
|
+
ref: importRef,
|
|
3999
|
+
type: 'file',
|
|
4000
|
+
accept: 'image/png,image/jpeg,image/webp,image/gif',
|
|
4001
|
+
style: { display: 'none' },
|
|
4002
|
+
onChange: importImage,
|
|
4003
|
+
}),
|
|
4004
|
+
importNote
|
|
4005
|
+
? h('span', {
|
|
4006
|
+
style: {
|
|
4007
|
+
color: 'var(--dsw-alias-label-tertiary, inherit)',
|
|
4008
|
+
fontSize: 11,
|
|
4009
|
+
overflow: 'hidden',
|
|
4010
|
+
textOverflow: 'ellipsis',
|
|
4011
|
+
whiteSpace: 'nowrap',
|
|
4012
|
+
maxWidth: 260,
|
|
4013
|
+
},
|
|
4014
|
+
}, importNote)
|
|
4015
|
+
: null,
|
|
4016
|
+
),
|
|
4017
|
+
layersBlock(240),
|
|
4018
|
+
h('div', { style: { paddingTop: 10, color: 'var(--dsw-alias-label-caption, inherit)', fontSize: 11 } },
|
|
4019
|
+
t('page.layerHint')),
|
|
4020
|
+
),
|
|
4021
|
+
|
|
4022
|
+
h(Sheet, {
|
|
4023
|
+
open: sheet === 'canvas',
|
|
4024
|
+
title: t('page.canvas'),
|
|
4025
|
+
width: 560,
|
|
4026
|
+
onClose: () => setSheet(null),
|
|
4027
|
+
},
|
|
4028
|
+
h(SettingsSection, { t, compact: true }),
|
|
4029
|
+
),
|
|
4030
|
+
|
|
4031
|
+
// 新建第一步:先问当前这幅要不要留一份
|
|
4032
|
+
h(Sheet, {
|
|
4033
|
+
open: sheet === 'newSave',
|
|
4034
|
+
title: t('page.new.title'),
|
|
4035
|
+
width: 440,
|
|
4036
|
+
onClose: () => setSheet(null),
|
|
4037
|
+
},
|
|
4038
|
+
h('div', { style: { fontWeight: 600, marginBottom: 4 } }, t('page.new.saveQ')),
|
|
4039
|
+
h('div', {
|
|
4040
|
+
style: { color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 12, marginBottom: 16 },
|
|
4041
|
+
}, t('page.new.saveHint')),
|
|
4042
|
+
h('div', { style: { display: 'flex', gap: 8, justifyContent: 'flex-end', flexWrap: 'wrap' } },
|
|
4043
|
+
h('button', { type: 'button', style: pill, onClick: () => setSheet(null) }, t('page.new.cancel')),
|
|
4044
|
+
h('button', {
|
|
4045
|
+
type: 'button',
|
|
4046
|
+
style: pill,
|
|
4047
|
+
onClick: () => {
|
|
4048
|
+
setPendingSave(false);
|
|
4049
|
+
setNewNote(null);
|
|
4050
|
+
setSheet('newPick');
|
|
4051
|
+
},
|
|
4052
|
+
}, t('page.new.skipSave')),
|
|
4053
|
+
h('button', {
|
|
4054
|
+
type: 'button',
|
|
4055
|
+
style: {
|
|
4056
|
+
...pill,
|
|
4057
|
+
borderColor: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
4058
|
+
color: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
4059
|
+
fontWeight: 600,
|
|
4060
|
+
},
|
|
4061
|
+
onClick: () => {
|
|
4062
|
+
setPendingSave(true);
|
|
4063
|
+
setNewNote(null);
|
|
4064
|
+
setSheet('newPick');
|
|
4065
|
+
},
|
|
4066
|
+
}, t('page.new.saveAnd')),
|
|
4067
|
+
),
|
|
4068
|
+
),
|
|
4069
|
+
|
|
4070
|
+
// 新建第二步:空白画布(挑尺寸)或导入图片(按图片尺寸建)
|
|
4071
|
+
h(Sheet, {
|
|
4072
|
+
open: sheet === 'newPick',
|
|
4073
|
+
title: t('page.new.pick'),
|
|
4074
|
+
width: 520,
|
|
4075
|
+
onClose: () => setSheet(null),
|
|
4076
|
+
},
|
|
4077
|
+
pendingSave
|
|
4078
|
+
? h('div', {
|
|
4079
|
+
style: {
|
|
4080
|
+
marginBottom: 10,
|
|
4081
|
+
padding: '6px 10px',
|
|
4082
|
+
borderRadius: 8,
|
|
4083
|
+
fontSize: 12,
|
|
4084
|
+
background: 'var(--dsw-alias-button-ghost-active-fill, rgba(76,139,245,0.12))',
|
|
4085
|
+
color: 'var(--dsw-alias-brand-primary, #4c8bf5)',
|
|
4086
|
+
},
|
|
4087
|
+
}, t('page.new.willSave'))
|
|
4088
|
+
: null,
|
|
4089
|
+
h('div', {
|
|
4090
|
+
style: {
|
|
4091
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.28))',
|
|
4092
|
+
borderRadius: 10,
|
|
4093
|
+
padding: '10px 12px 12px',
|
|
4094
|
+
marginBottom: 10,
|
|
4095
|
+
},
|
|
4096
|
+
},
|
|
4097
|
+
h('div', { style: { fontWeight: 600, marginBottom: 4 } }, t('page.new.blank')),
|
|
4098
|
+
h('div', {
|
|
4099
|
+
style: { color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 11, marginBottom: 8 },
|
|
4100
|
+
}, t('page.new.blankHint')),
|
|
4101
|
+
h('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(3, minmax(0, 1fr))', gap: 6 } },
|
|
4102
|
+
['1-1', '4-3', '16-9', '9-16', 'a4'].map((id) => CANVAS_PRESETS.find((p) => p.id === id)).filter(Boolean).map((p) => h('button', {
|
|
4103
|
+
key: p.id,
|
|
4104
|
+
type: 'button',
|
|
4105
|
+
style: { ...pill, flexDirection: 'column', gap: 2, padding: '8px 6px' },
|
|
4106
|
+
onClick: () => startNewPainting({ width: p.w, height: p.h }),
|
|
4107
|
+
},
|
|
4108
|
+
h('span', { style: { fontWeight: 600 } }, p.label),
|
|
4109
|
+
h('span', { style: { fontSize: 10, color: 'var(--dsw-alias-label-caption, inherit)' } }, `${p.w}×${p.h}`),
|
|
4110
|
+
)),
|
|
4111
|
+
h('button', {
|
|
4112
|
+
type: 'button',
|
|
4113
|
+
style: { ...pill, flexDirection: 'column', gap: 2, padding: '8px 6px' },
|
|
4114
|
+
onClick: () => startNewPainting({ width: meta.width, height: meta.height }),
|
|
4115
|
+
},
|
|
4116
|
+
h('span', { style: { fontWeight: 600 } }, t('page.new.currentSize')),
|
|
4117
|
+
h('span', { style: { fontSize: 10, color: 'var(--dsw-alias-label-caption, inherit)' } }, `${meta.width}×${meta.height}`),
|
|
4118
|
+
),
|
|
4119
|
+
),
|
|
4120
|
+
h('div', { style: { fontSize: 11, paddingTop: 8 } },
|
|
4121
|
+
h('button', {
|
|
4122
|
+
type: 'button',
|
|
4123
|
+
style: { ...pill, padding: '4px 9px' },
|
|
4124
|
+
onClick: () => setSheet('canvas'),
|
|
4125
|
+
}, t('page.new.moreSizes')),
|
|
4126
|
+
),
|
|
4127
|
+
),
|
|
4128
|
+
h('div', {
|
|
4129
|
+
style: {
|
|
4130
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.28))',
|
|
4131
|
+
borderRadius: 10,
|
|
4132
|
+
padding: '10px 12px 12px',
|
|
4133
|
+
},
|
|
4134
|
+
},
|
|
4135
|
+
h('div', { style: { fontWeight: 600, marginBottom: 4 } }, t('page.new.fromImage')),
|
|
4136
|
+
h('div', {
|
|
4137
|
+
style: { color: 'var(--dsw-alias-label-tertiary, inherit)', fontSize: 11, marginBottom: 8 },
|
|
4138
|
+
}, t('page.new.fromImageHint')),
|
|
4139
|
+
h('button', {
|
|
4140
|
+
type: 'button',
|
|
4141
|
+
style: pill,
|
|
4142
|
+
onClick: () => {
|
|
4143
|
+
if (newImportRef.current) newImportRef.current.click();
|
|
4144
|
+
},
|
|
4145
|
+
}, `🖼 ${t('page.new.chooseImage')}`),
|
|
4146
|
+
h('input', {
|
|
4147
|
+
ref: newImportRef,
|
|
4148
|
+
type: 'file',
|
|
4149
|
+
accept: 'image/png,image/jpeg,image/webp,image/gif',
|
|
4150
|
+
style: { display: 'none' },
|
|
4151
|
+
onChange: startNewFromImage,
|
|
4152
|
+
}),
|
|
4153
|
+
),
|
|
4154
|
+
newNote
|
|
4155
|
+
? h('div', {
|
|
4156
|
+
style: { paddingTop: 10, fontSize: 12, color: 'var(--dsw-alias-label-secondary, inherit)' },
|
|
4157
|
+
}, newNote)
|
|
4158
|
+
: null,
|
|
4159
|
+
), );
|
|
4160
|
+
}
|
|
4161
|
+
// 右栏模式:原来的窄条排布
|
|
4162
|
+
return h('div', {
|
|
4163
|
+
style: {
|
|
4164
|
+
display: 'flex',
|
|
4165
|
+
flexDirection: 'column',
|
|
4166
|
+
height: '100%',
|
|
4167
|
+
minHeight: 0,
|
|
4168
|
+
font: '12px/1.4 system-ui, "Microsoft YaHei", sans-serif',
|
|
4169
|
+
color: 'var(--dsw-alias-label-primary, #eee)',
|
|
4170
|
+
},
|
|
4171
|
+
},
|
|
4172
|
+
h('div', { style: barStyle },
|
|
4173
|
+
colorCtl,
|
|
4174
|
+
sizeCtl,
|
|
4175
|
+
toolCtl,
|
|
4176
|
+
h('span', { style: { marginLeft: 'auto', opacity: 0.6 } }, `${meta.strokes} ${t('strokes')}`),
|
|
4177
|
+
),
|
|
4178
|
+
h('div', { style: { ...barStyle, borderBottom: 'none', paddingTop: 0 } },
|
|
4179
|
+
opsCtl,
|
|
4180
|
+
h('span', { style: { marginLeft: 'auto', opacity: 0.45 } }, `rev ${revision}${localVer ? ` · v${localVer}` : ''}`),
|
|
4181
|
+
),
|
|
4182
|
+
h('div', {
|
|
4183
|
+
style: {
|
|
4184
|
+
borderBottom: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.2))',
|
|
4185
|
+
padding: '6px 8px',
|
|
4186
|
+
flex: '0 0 auto',
|
|
4187
|
+
},
|
|
4188
|
+
}, layersBlock(118)),
|
|
4189
|
+
error
|
|
4190
|
+
? h('div', { style: { padding: '4px 8px', color: '#e5484d' } }, error)
|
|
4191
|
+
: null,
|
|
4192
|
+
viewportBlock,
|
|
4193
|
+
);
|
|
4194
|
+
}
|
|
4195
|
+
|
|
4196
|
+
/** 设置里的画布页:尺寸、背景、缓存目录、保存路径,外加把当前画布另存成 PNG。 */
|
|
4197
|
+
function SettingsSection(props) {
|
|
4198
|
+
const t = props.t || ((k) => k);
|
|
4199
|
+
const compact = Boolean(props.compact);
|
|
4200
|
+
const openFn = props.onOpenTab || props.onOpenCanvas;
|
|
4201
|
+
const [state, setState] = React.useState(null);
|
|
4202
|
+
const [note, setNote] = React.useState(null);
|
|
4203
|
+
const [busy, setBusy] = React.useState(false);
|
|
4204
|
+
const [copied, setCopied] = React.useState(null);
|
|
4205
|
+
const [alpha, setAlpha] = React.useState(false);
|
|
4206
|
+
|
|
4207
|
+
const read = React.useCallback(async () => {
|
|
4208
|
+
try {
|
|
4209
|
+
const r = await fetch(`${API}/config`, { cache: 'no-store' });
|
|
4210
|
+
const body = await r.json();
|
|
4211
|
+
if (body && body.ok) {
|
|
4212
|
+
setState({
|
|
4213
|
+
width: body.width,
|
|
4214
|
+
height: body.height,
|
|
4215
|
+
background: body.background,
|
|
4216
|
+
cacheDir: body.cacheDir || '',
|
|
4217
|
+
saveDir: body.saveDir || '',
|
|
4218
|
+
defaults: body.defaults || {},
|
|
4219
|
+
});
|
|
4220
|
+
return;
|
|
4221
|
+
}
|
|
4222
|
+
setNote((body && body.error) || t('settings.unreachable'));
|
|
4223
|
+
} catch {
|
|
4224
|
+
setNote(t('settings.unreachable'));
|
|
4225
|
+
}
|
|
4226
|
+
}, [t]);
|
|
4227
|
+
|
|
4228
|
+
React.useEffect(() => {
|
|
4229
|
+
read();
|
|
4230
|
+
}, [read]);
|
|
4231
|
+
|
|
4232
|
+
const patch = (key, value) => setState((prev) => (prev ? { ...prev, [key]: value } : prev));
|
|
4233
|
+
|
|
4234
|
+
const save = async (values) => {
|
|
4235
|
+
if (!values) return;
|
|
4236
|
+
setBusy(true);
|
|
4237
|
+
setNote(null);
|
|
4238
|
+
try {
|
|
4239
|
+
const r = await fetch(`${API}/config`, {
|
|
4240
|
+
method: 'POST',
|
|
4241
|
+
headers: { 'Content-Type': 'application/json' },
|
|
4242
|
+
body: JSON.stringify({
|
|
4243
|
+
width: values.width,
|
|
4244
|
+
height: values.height,
|
|
4245
|
+
background: values.background,
|
|
4246
|
+
cacheDir: values.cacheDir,
|
|
4247
|
+
saveDir: values.saveDir,
|
|
4248
|
+
}),
|
|
4249
|
+
});
|
|
4250
|
+
const body = await r.json();
|
|
4251
|
+
if (body && body.ok) {
|
|
4252
|
+
setState((prev) => (prev
|
|
4253
|
+
? {
|
|
4254
|
+
...prev,
|
|
4255
|
+
width: body.width,
|
|
4256
|
+
height: body.height,
|
|
4257
|
+
background: body.background,
|
|
4258
|
+
cacheDir: body.cacheDir || prev.cacheDir,
|
|
4259
|
+
saveDir: body.saveDir || prev.saveDir,
|
|
4260
|
+
}
|
|
4261
|
+
: prev));
|
|
4262
|
+
setNote(t('settings.saved'));
|
|
4263
|
+
} else {
|
|
4264
|
+
setNote((body && body.error) || t('settings.unreachable'));
|
|
4265
|
+
}
|
|
4266
|
+
} catch {
|
|
4267
|
+
setNote(t('settings.unreachable'));
|
|
4268
|
+
} finally {
|
|
4269
|
+
setBusy(false);
|
|
4270
|
+
}
|
|
4271
|
+
};
|
|
4272
|
+
|
|
4273
|
+
/** 点预设就立刻改尺寸,不用再按保存。 */
|
|
4274
|
+
const applyPreset = (preset) => {
|
|
4275
|
+
if (!state) return;
|
|
4276
|
+
const next = { ...state, width: preset.w, height: preset.h };
|
|
4277
|
+
setState(next);
|
|
4278
|
+
save(next);
|
|
4279
|
+
};
|
|
4280
|
+
|
|
4281
|
+
const exportPng = async () => {
|
|
4282
|
+
setBusy(true);
|
|
4283
|
+
setNote(null);
|
|
4284
|
+
try {
|
|
4285
|
+
const r = await fetch(`${API}/export`, {
|
|
4286
|
+
method: 'POST',
|
|
4287
|
+
headers: { 'Content-Type': 'application/json' },
|
|
4288
|
+
body: JSON.stringify({ transparent: alpha }),
|
|
4289
|
+
});
|
|
4290
|
+
const body = await r.json();
|
|
4291
|
+
if (body && body.ok) setNote(`${t('settings.exported')} ${body.path}`);
|
|
4292
|
+
else setNote((body && body.error) || t('settings.unreachable'));
|
|
4293
|
+
} catch {
|
|
4294
|
+
setNote(t('settings.unreachable'));
|
|
4295
|
+
} finally {
|
|
4296
|
+
setBusy(false);
|
|
4297
|
+
}
|
|
4298
|
+
};
|
|
4299
|
+
|
|
4300
|
+
const copy = (value, key) => {
|
|
4301
|
+
if (!value) return;
|
|
4302
|
+
try {
|
|
4303
|
+
navigator.clipboard.writeText(value).then(() => {
|
|
4304
|
+
setCopied(key);
|
|
4305
|
+
setTimeout(() => setCopied(null), 1500);
|
|
4306
|
+
}).catch(() => {});
|
|
4307
|
+
} catch {
|
|
4308
|
+
/* 剪贴板不可用时安静跳过 */
|
|
4309
|
+
}
|
|
4310
|
+
};
|
|
4311
|
+
|
|
4312
|
+
const field = {
|
|
4313
|
+
width: 92,
|
|
4314
|
+
padding: '5px 8px',
|
|
4315
|
+
borderRadius: 6,
|
|
4316
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
4317
|
+
background: 'transparent',
|
|
4318
|
+
color: 'inherit',
|
|
4319
|
+
font: 'inherit',
|
|
4320
|
+
};
|
|
4321
|
+
const pathField = {
|
|
4322
|
+
...field,
|
|
4323
|
+
flex: '1 1 auto',
|
|
4324
|
+
minWidth: 0,
|
|
4325
|
+
width: 'auto',
|
|
4326
|
+
fontFamily: 'ui-monospace, monospace',
|
|
4327
|
+
fontSize: 12,
|
|
4328
|
+
};
|
|
4329
|
+
const rowStyle = { display: 'flex', alignItems: 'center', gap: 10, padding: '9px 0' };
|
|
4330
|
+
const labelStyle = { minWidth: 84, opacity: 0.75 };
|
|
4331
|
+
const btn = {
|
|
4332
|
+
padding: '5px 12px',
|
|
4333
|
+
borderRadius: 6,
|
|
4334
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
4335
|
+
background: 'transparent',
|
|
4336
|
+
color: 'inherit',
|
|
4337
|
+
font: 'inherit',
|
|
4338
|
+
cursor: 'pointer',
|
|
4339
|
+
whiteSpace: 'nowrap',
|
|
4340
|
+
};
|
|
4341
|
+
const unit = { opacity: 0.5, minWidth: 18 };
|
|
4342
|
+
|
|
4343
|
+
const pathRow = (labelKey, valueKey, hintKey) => h('div', { style: { padding: '9px 0' } },
|
|
4344
|
+
h('div', { style: { opacity: 0.75, marginBottom: 4 } }, t(labelKey)),
|
|
4345
|
+
h('div', { style: { display: 'flex', gap: 6, alignItems: 'center' } },
|
|
4346
|
+
h('input', {
|
|
4347
|
+
type: 'text',
|
|
4348
|
+
spellCheck: false,
|
|
4349
|
+
value: state[valueKey] || '',
|
|
4350
|
+
style: pathField,
|
|
4351
|
+
onChange: (e) => patch(valueKey, e.target.value),
|
|
4352
|
+
placeholder: t('settings.pathHint'),
|
|
4353
|
+
}),
|
|
4354
|
+
h('button', { type: 'button', style: btn, onClick: () => copy(state[valueKey], valueKey) },
|
|
4355
|
+
copied === valueKey ? t('settings.copied') : t('settings.copy')),
|
|
4356
|
+
),
|
|
4357
|
+
h('div', { style: { opacity: 0.5, fontSize: 12, paddingTop: 3 } }, t(hintKey)),
|
|
4358
|
+
);
|
|
4359
|
+
|
|
4360
|
+
return h('div', {
|
|
4361
|
+
style: {
|
|
4362
|
+
padding: compact ? '0' : '4px 12px 20px',
|
|
4363
|
+
maxWidth: compact ? 'none' : 620,
|
|
4364
|
+
width: '100%',
|
|
4365
|
+
boxSizing: 'border-box',
|
|
4366
|
+
font: `${compact ? '12px' : '13px'}/1.5 system-ui, "Microsoft YaHei", sans-serif`,
|
|
4367
|
+
color: 'var(--dsw-alias-label-primary, inherit)',
|
|
4368
|
+
},
|
|
4369
|
+
},
|
|
4370
|
+
compact
|
|
4371
|
+
? (openFn
|
|
4372
|
+
? h('div', { style: { marginBottom: 8 } },
|
|
4373
|
+
h('button', {
|
|
4374
|
+
type: 'button',
|
|
4375
|
+
style: { ...btn, padding: '3px 10px', opacity: 0.8 },
|
|
4376
|
+
onClick: openFn,
|
|
4377
|
+
}, t('page.tab')))
|
|
4378
|
+
: null)
|
|
4379
|
+
: h('div', { style: { fontWeight: 600, marginBottom: 2 } }, t('title')),
|
|
4380
|
+
compact
|
|
4381
|
+
? null
|
|
4382
|
+
: h('div', { style: { opacity: 0.6, marginBottom: 12 } }, t('settings.subtitle')),
|
|
4383
|
+
!compact && openFn
|
|
4384
|
+
? h('div', { style: { marginBottom: 12 } },
|
|
4385
|
+
h('button', {
|
|
4386
|
+
type: 'button',
|
|
4387
|
+
style: { ...btn, padding: '6px 12px', fontWeight: 600 },
|
|
4388
|
+
onClick: openFn,
|
|
4389
|
+
}, `🎨 ${t('open')}`))
|
|
4390
|
+
: null,
|
|
4391
|
+
|
|
4392
|
+
state === null
|
|
4393
|
+
? h('div', { style: { opacity: 0.6, padding: '6px 0' } }, note || t('loading'))
|
|
4394
|
+
: h(React.Fragment, null,
|
|
4395
|
+
h('div', { style: { paddingBottom: 4 } },
|
|
4396
|
+
h('div', { style: { opacity: 0.75, marginBottom: 6 } }, t('settings.presets')),
|
|
4397
|
+
...['ratio', 'paper', 'pixel'].map((group) => h('div', { key: group, style: { marginBottom: 8 } },
|
|
4398
|
+
h('div', { style: { opacity: 0.5, fontSize: 11, marginBottom: 4 } },
|
|
4399
|
+
t(group === 'ratio' ? 'settings.presetRatio' : group === 'paper' ? 'settings.presetPaper' : 'settings.presetPixel')),
|
|
4400
|
+
h('div', {
|
|
4401
|
+
style: { display: 'grid', gridTemplateColumns: 'repeat(3, minmax(0, 1fr))', gap: 6 },
|
|
4402
|
+
},
|
|
4403
|
+
CANVAS_PRESETS.filter((p) => p.group === group).map((p) => {
|
|
4404
|
+
const active = String(state.width) === String(p.w) && String(state.height) === String(p.h);
|
|
4405
|
+
return h('button', {
|
|
4406
|
+
key: p.id,
|
|
4407
|
+
type: 'button',
|
|
4408
|
+
title: `${p.w}×${p.h}`,
|
|
4409
|
+
disabled: busy,
|
|
4410
|
+
onClick: () => applyPreset(p),
|
|
4411
|
+
style: {
|
|
4412
|
+
...btn,
|
|
4413
|
+
padding: '5px 4px',
|
|
4414
|
+
textAlign: 'left',
|
|
4415
|
+
borderColor: active
|
|
4416
|
+
? 'var(--dsw-alias-border-l3, rgba(127,127,127,0.7))'
|
|
4417
|
+
: 'var(--dsw-alias-border-l1, rgba(127,127,127,0.28))',
|
|
4418
|
+
background: active
|
|
4419
|
+
? 'var(--dsw-alias-bg-layer-3, rgba(127,127,127,0.14))'
|
|
4420
|
+
: 'transparent',
|
|
4421
|
+
lineHeight: 1.25,
|
|
4422
|
+
},
|
|
4423
|
+
},
|
|
4424
|
+
h('div', { style: { fontWeight: 600 } }, p.label),
|
|
4425
|
+
h('div', { style: { opacity: 0.45, fontSize: 10 } }, `${p.w}×${p.h}`),
|
|
4426
|
+
);
|
|
4427
|
+
}),
|
|
4428
|
+
),
|
|
4429
|
+
)),
|
|
4430
|
+
h('div', { style: { opacity: 0.5, fontSize: 11, paddingBottom: 4 } }, t('settings.presetHint')),
|
|
4431
|
+
),
|
|
4432
|
+
h('div', { style: rowStyle },
|
|
4433
|
+
h('span', { style: labelStyle }, t('settings.size')),
|
|
4434
|
+
h('input', {
|
|
4435
|
+
type: 'number',
|
|
4436
|
+
min: 16,
|
|
4437
|
+
max: 4096,
|
|
4438
|
+
step: 1,
|
|
4439
|
+
value: state.width,
|
|
4440
|
+
style: field,
|
|
4441
|
+
onChange: (e) => patch('width', e.target.value),
|
|
4442
|
+
}),
|
|
4443
|
+
h('span', { style: unit }, '×'),
|
|
4444
|
+
h('input', {
|
|
4445
|
+
type: 'number',
|
|
4446
|
+
min: 16,
|
|
4447
|
+
max: 4096,
|
|
4448
|
+
step: 1,
|
|
4449
|
+
value: state.height,
|
|
4450
|
+
style: field,
|
|
4451
|
+
onChange: (e) => patch('height', e.target.value),
|
|
4452
|
+
}),
|
|
4453
|
+
h('span', { style: { opacity: 0.5 } }, 'px'),
|
|
4454
|
+
),
|
|
4455
|
+
h('div', { style: rowStyle },
|
|
4456
|
+
h('span', { style: labelStyle }, t('settings.background')),
|
|
4457
|
+
h('input', {
|
|
4458
|
+
type: 'color',
|
|
4459
|
+
value: state.background || '#ffffff',
|
|
4460
|
+
onChange: (e) => patch('background', e.target.value),
|
|
4461
|
+
style: { width: 34, height: 24, padding: 0, border: 'none', background: 'none' },
|
|
4462
|
+
}),
|
|
4463
|
+
h('span', { style: { opacity: 0.5, fontFamily: 'ui-monospace, monospace' } }, state.background || '#ffffff'),
|
|
4464
|
+
),
|
|
4465
|
+
pathRow('settings.dir', 'cacheDir', 'settings.dirHint'),
|
|
4466
|
+
pathRow('settings.saveDir', 'saveDir', 'settings.saveHint'),
|
|
4467
|
+
h('div', { style: { opacity: 0.55, fontSize: 12, paddingTop: 2 } }, t('settings.hint')),
|
|
4468
|
+
h('label', { style: { display: 'flex', alignItems: 'center', gap: 6, paddingTop: 10 } },
|
|
4469
|
+
h('input', {
|
|
4470
|
+
type: 'checkbox',
|
|
4471
|
+
checked: alpha,
|
|
4472
|
+
onChange: (e) => setAlpha(e.target.checked),
|
|
4473
|
+
}),
|
|
4474
|
+
h('span', null, t('settings.alpha')),
|
|
4475
|
+
h('span', { style: { opacity: 0.5, fontSize: 12 } }, t('settings.alphaHint')),
|
|
4476
|
+
),
|
|
4477
|
+
h('div', { style: { display: 'flex', gap: 8, paddingTop: 14, flexWrap: 'wrap' } },
|
|
4478
|
+
h('button', {
|
|
4479
|
+
type: 'button',
|
|
4480
|
+
style: { ...btn, fontWeight: 600 },
|
|
4481
|
+
disabled: busy,
|
|
4482
|
+
onClick: () => save(state),
|
|
4483
|
+
}, t('settings.save')),
|
|
4484
|
+
h('button', {
|
|
4485
|
+
type: 'button',
|
|
4486
|
+
style: btn,
|
|
4487
|
+
disabled: busy,
|
|
4488
|
+
onClick: () => {
|
|
4489
|
+
const defaults = state.defaults || {};
|
|
4490
|
+
const reset = {
|
|
4491
|
+
width: defaults.width || 1024,
|
|
4492
|
+
height: defaults.height || 1024,
|
|
4493
|
+
background: defaults.background || '#ffffff',
|
|
4494
|
+
cacheDir: state.cacheDir,
|
|
4495
|
+
saveDir: state.saveDir,
|
|
4496
|
+
};
|
|
4497
|
+
setState((prev) => (prev ? { ...prev, ...reset } : prev));
|
|
4498
|
+
save(reset);
|
|
4499
|
+
},
|
|
4500
|
+
}, t('settings.reset')),
|
|
4501
|
+
h('button', {
|
|
4502
|
+
type: 'button',
|
|
4503
|
+
style: btn,
|
|
4504
|
+
disabled: busy,
|
|
4505
|
+
onClick: exportPng,
|
|
4506
|
+
}, t('settings.export')),
|
|
4507
|
+
),
|
|
4508
|
+
note
|
|
4509
|
+
? h('div', {
|
|
4510
|
+
style: {
|
|
4511
|
+
paddingTop: 10,
|
|
4512
|
+
fontSize: 12,
|
|
4513
|
+
wordBreak: 'break-all',
|
|
4514
|
+
opacity: 0.8,
|
|
4515
|
+
},
|
|
4516
|
+
}, note)
|
|
4517
|
+
: null,
|
|
4518
|
+
),
|
|
4519
|
+
);
|
|
4520
|
+
}
|
|
4521
|
+
|
|
4522
|
+
/** 整页模式的入口:C1 标识条 + 下面一块大画板与右列工具。 */
|
|
4523
|
+
function WorkspacePage(props) {
|
|
4524
|
+
const t = props.t || ((k) => k);
|
|
4525
|
+
const onBack = props.onBack;
|
|
4526
|
+
const onOpenTab = props.onOpenTab;
|
|
4527
|
+
/** 顶栏按钮要能开到 CanvasPanel 里的弹窗,用这个桥传一下。 */
|
|
4528
|
+
const bridge = React.useRef({});
|
|
4529
|
+
const headBtn = {
|
|
4530
|
+
padding: '4px 10px',
|
|
4531
|
+
borderRadius: 6,
|
|
4532
|
+
border: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.3))',
|
|
4533
|
+
background: 'transparent',
|
|
4534
|
+
color: 'inherit',
|
|
4535
|
+
font: 'inherit',
|
|
4536
|
+
cursor: 'pointer',
|
|
4537
|
+
whiteSpace: 'nowrap',
|
|
4538
|
+
};
|
|
4539
|
+
return h('div', {
|
|
4540
|
+
style: {
|
|
4541
|
+
height: '100%',
|
|
4542
|
+
minHeight: 0,
|
|
4543
|
+
display: 'flex',
|
|
4544
|
+
flexDirection: 'column',
|
|
4545
|
+
color: 'var(--dsw-alias-label-primary, inherit)',
|
|
4546
|
+
},
|
|
4547
|
+
},
|
|
4548
|
+
// C1:左上角标识
|
|
4549
|
+
h('div', {
|
|
4550
|
+
style: {
|
|
4551
|
+
flex: '0 0 auto',
|
|
4552
|
+
display: 'flex',
|
|
4553
|
+
alignItems: 'center',
|
|
4554
|
+
gap: 10,
|
|
4555
|
+
padding: '0 14px',
|
|
4556
|
+
height: 46,
|
|
4557
|
+
borderBottom: '1px solid var(--dsw-alias-border-l1, rgba(127,127,127,0.2))',
|
|
4558
|
+
},
|
|
4559
|
+
},
|
|
4560
|
+
h('span', {
|
|
4561
|
+
style: { fontSize: 13, letterSpacing: 0.3, display: 'inline-flex', alignItems: 'baseline', gap: 1 },
|
|
4562
|
+
},
|
|
4563
|
+
h('span', { style: { opacity: 0.45, fontFamily: 'ui-monospace, monospace' } }, 'xenra.'),
|
|
4564
|
+
h('span', { style: { fontWeight: 700 } }, t('title')),
|
|
4565
|
+
),
|
|
4566
|
+
h('span', {
|
|
4567
|
+
style: { opacity: 0.38, fontSize: 11, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
4568
|
+
}, t('page.hint')),
|
|
4569
|
+
h('div', { style: { marginLeft: 'auto', display: 'flex', gap: 8, flex: '0 0 auto' } },
|
|
4570
|
+
h('button', {
|
|
4571
|
+
type: 'button',
|
|
4572
|
+
style: { ...headBtn, fontWeight: 600 },
|
|
4573
|
+
onClick: () => {
|
|
4574
|
+
const fn = bridge.current && bridge.current.newPaint;
|
|
4575
|
+
if (typeof fn === 'function') fn();
|
|
4576
|
+
},
|
|
4577
|
+
}, `+ ${t('page.new')}`),
|
|
4578
|
+
onOpenTab
|
|
4579
|
+
? h('button', { type: 'button', style: headBtn, onClick: onOpenTab }, t('page.tab'))
|
|
4580
|
+
: null,
|
|
4581
|
+
onBack
|
|
4582
|
+
? h('button', { type: 'button', style: headBtn, onClick: onBack }, `← ${t('page.back')}`)
|
|
4583
|
+
: null,
|
|
4584
|
+
),
|
|
4585
|
+
),
|
|
4586
|
+
// A1 画板 + 右列 B1/B2 都由 CanvasPanel 的整页排布给出
|
|
4587
|
+
h('div', { style: { flex: '1 1 auto', minHeight: 0, minWidth: 0 } },
|
|
4588
|
+
h(CanvasPanel, { ...props, t, variant: 'page', onOpenTab, bridge }),
|
|
4589
|
+
),
|
|
4590
|
+
);
|
|
4591
|
+
}
|
|
4592
|
+
|
|
4593
|
+
/** 左栏导航里的图标:一块画板加一支笔。 */
|
|
4594
|
+
function PanelIcon(props) {
|
|
4595
|
+
const size = (props && props.size) || 16;
|
|
4596
|
+
return h('svg', {
|
|
4597
|
+
width: size,
|
|
4598
|
+
height: size,
|
|
4599
|
+
viewBox: '0 0 24 24',
|
|
4600
|
+
fill: 'none',
|
|
4601
|
+
stroke: 'currentColor',
|
|
4602
|
+
'aria-hidden': true,
|
|
4603
|
+
},
|
|
4604
|
+
h('rect', { x: 3, y: 4.5, width: 18, height: 14, rx: 3, strokeWidth: 1.3, opacity: 0.8 }),
|
|
4605
|
+
h('path', { d: 'M6.8 15.2l3.4-3.6 2.2 2.2 2.6-3.2 2.4 3.1', strokeWidth: 1.3, strokeLinecap: 'round' }),
|
|
4606
|
+
h('circle', { cx: 8.4, cy: 8.2, r: 1.2, fill: 'currentColor', stroke: 'none' }),
|
|
4607
|
+
);
|
|
4608
|
+
}
|
|
4609
|
+
|
|
4610
|
+
/** 引导页条目的图标:一块画布加一支笔,线条跟随主题色。 */
|
|
4611
|
+
function GuideArtwork() {
|
|
4612
|
+
return h('svg', {
|
|
4613
|
+
width: 18,
|
|
4614
|
+
height: 18,
|
|
4615
|
+
viewBox: '0 0 24 24',
|
|
4616
|
+
fill: 'none',
|
|
4617
|
+
stroke: 'currentColor',
|
|
4618
|
+
'aria-hidden': true,
|
|
4619
|
+
},
|
|
4620
|
+
h('rect', { x: 3, y: 4.5, width: 18, height: 13, rx: 3, strokeWidth: 1.3, opacity: 0.75 }),
|
|
4621
|
+
h('path', { d: 'M6.5 15.5c2.3-4.4 4.3-6.5 8-7.2', strokeWidth: 1.7, strokeLinecap: 'round' }),
|
|
4622
|
+
h('circle', { cx: 16.2, cy: 8.1, r: 1.5, fill: 'currentColor', stroke: 'none' }),
|
|
4623
|
+
);
|
|
4624
|
+
}
|
|
4625
|
+
|
|
4626
|
+
return {
|
|
4627
|
+
inject: ['slots', 'locale', 'sidebarRight', 'sidebarRightTabs'],
|
|
4628
|
+
// 纯算法露出,供离线自测(不影响装载)
|
|
4629
|
+
__pixel: { inkMask, blurMask, rdp, marchingSquares, stitchSegments, traceInk, kmeansColors, polygonArea, pointInPolygon, pathWidths, regionFills, thinning, skeletonPaths, chaikin, smoothPath, curvePts, fitBezier, bezierSample, bezierPoint, brushes: [...BRUSH_BY_ID.values()] },
|
|
4630
|
+
apply(ctx) {
|
|
4631
|
+
let t = (k) => k;
|
|
4632
|
+
try {
|
|
4633
|
+
ctx.effect(() => ctx.locale.register(NS, DICT));
|
|
4634
|
+
t = ctx.locale.bind(NS);
|
|
4635
|
+
} catch {
|
|
4636
|
+
t = (k) => k;
|
|
4637
|
+
}
|
|
4638
|
+
|
|
4639
|
+
// 右栏「开始」页的入口行:guide 条目是引导页唯一的来源,没有它就不会出现在列表里。
|
|
4640
|
+
ctx.effect(() => ctx.sidebarRightTabs.register({
|
|
4641
|
+
id: TAB_ID,
|
|
4642
|
+
kind: TAB_KIND,
|
|
4643
|
+
multiple: false,
|
|
4644
|
+
priority: 'builtin',
|
|
4645
|
+
title: () => t('title'),
|
|
4646
|
+
guide: [{
|
|
4647
|
+
id: 'open',
|
|
4648
|
+
order: 15,
|
|
4649
|
+
title: () => t('guide.title'),
|
|
4650
|
+
description: () => t('guide.description'),
|
|
4651
|
+
icon: GuideArtwork,
|
|
4652
|
+
}],
|
|
4653
|
+
}));
|
|
4654
|
+
|
|
4655
|
+
ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register({
|
|
4656
|
+
name: 'sidebar.right.pane.tab',
|
|
4657
|
+
key: TAB_ID,
|
|
4658
|
+
}, (props) => h(CanvasPanel, { ...props, t })));
|
|
4659
|
+
|
|
4660
|
+
ctx.slots.inject('sidebar.right.pane.tab.title', () => ctx.slots.register({
|
|
4661
|
+
name: 'sidebar.right.pane.tab.title',
|
|
4662
|
+
key: TAB_ID,
|
|
4663
|
+
}, () => t('title')));
|
|
4664
|
+
|
|
4665
|
+
// 左栏导航多一行「画界」(排在插件、自动化任务之后):点它就把中栏切成整页画板。
|
|
4666
|
+
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
|
|
4667
|
+
name: 'sidebar.panellist',
|
|
4668
|
+
id: PANEL_ID,
|
|
4669
|
+
order: 20,
|
|
4670
|
+
label: () => t('title'),
|
|
4671
|
+
locale: NS,
|
|
4672
|
+
}, PanelIcon));
|
|
4673
|
+
|
|
4674
|
+
// 中栏的整页内容:main 是 keyed slot,key 必须和 panellist 的 id 一致,宿主才会切过去。
|
|
4675
|
+
ctx.slots.inject('main', () => ctx.slots.register({
|
|
4676
|
+
name: 'main',
|
|
4677
|
+
key: PANEL_ID,
|
|
4678
|
+
locale: NS,
|
|
4679
|
+
inject: () => {
|
|
4680
|
+
const safe = (fn) => () => {
|
|
4681
|
+
try {
|
|
4682
|
+
fn();
|
|
4683
|
+
} catch {
|
|
4684
|
+
/* 没有 layout / sidebarRight 时静默 */
|
|
4685
|
+
}
|
|
4686
|
+
};
|
|
4687
|
+
return {
|
|
4688
|
+
onBack: safe(() => ctx.layout.selectPanel(null)),
|
|
4689
|
+
onOpenTab: safe(() => ctx.sidebarRight.openTab(TAB_KIND, { params: {} })),
|
|
4690
|
+
};
|
|
4691
|
+
},
|
|
4692
|
+
}, (props) => h(WorkspacePage, { ...props, t })));
|
|
4693
|
+
},
|
|
4694
|
+
};
|
|
4695
|
+
},
|
|
4696
|
+
});
|
|
4697
|
+
|