@yft-design/engine 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -2
- package/dist/EngineCanvas.d.ts +500 -0
- package/dist/EngineCanvas.js +927 -0
- package/dist/core/canvasPerf.d.ts +23 -0
- package/dist/core/canvasPerf.js +170 -0
- package/dist/core/color.d.ts +52 -0
- package/dist/core/color.js +107 -0
- package/dist/core/distanceField.d.ts +15 -0
- package/dist/core/distanceField.js +92 -0
- package/dist/core/events.d.ts +34 -0
- package/dist/core/events.js +52 -0
- package/dist/core/gradient.d.ts +97 -0
- package/dist/core/gradient.js +175 -0
- package/dist/core/imageFilters.d.ts +150 -0
- package/dist/core/imageFilters.js +330 -0
- package/dist/core/mat2d.d.ts +151 -0
- package/dist/core/mat2d.js +241 -0
- package/dist/core/pathData.d.ts +53 -0
- package/dist/core/pathData.js +926 -0
- package/dist/core/pattern.d.ts +38 -0
- package/dist/core/pattern.js +40 -0
- package/dist/core/point.d.ts +59 -0
- package/dist/core/point.js +119 -0
- package/dist/core/psdEffects.d.ts +126 -0
- package/dist/core/psdEffects.js +158 -0
- package/dist/core/roundedRect.d.ts +62 -0
- package/dist/core/roundedRect.js +163 -0
- package/dist/core/shadow.d.ts +51 -0
- package/dist/core/shadow.js +47 -0
- package/dist/core/snap.d.ts +232 -0
- package/dist/core/snap.js +793 -0
- package/dist/core/stroke.d.ts +41 -0
- package/dist/core/stroke.js +44 -0
- package/dist/core/triangulate.d.ts +24 -0
- package/dist/core/triangulate.js +118 -0
- package/dist/debug/canvasInspection.d.ts +92 -0
- package/dist/debug/canvasInspection.js +113 -0
- package/dist/debug/pixelDiff.d.ts +22 -0
- package/dist/debug/pixelDiff.js +64 -0
- package/dist/debug/seed.d.ts +473 -0
- package/dist/debug/seed.js +3803 -0
- package/dist/decoders/gif2sprite.d.ts +9 -0
- package/dist/decoders/gif2sprite.js +84 -0
- package/dist/decoders/webp2sprite.d.ts +22 -0
- package/dist/decoders/webp2sprite.js +133 -0
- package/dist/engineFlag.d.ts +72 -0
- package/dist/engineFlag.js +86 -0
- package/dist/export/SceneExporter.d.ts +92 -0
- package/dist/export/SceneExporter.js +280 -0
- package/dist/export/glExport.d.ts +66 -0
- package/dist/export/glExport.js +79 -0
- package/dist/export/svgExport.d.ts +277 -0
- package/dist/export/svgExport.js +1790 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +48 -0
- package/dist/interact/animationPlayback.d.ts +125 -0
- package/dist/interact/animationPlayback.js +344 -0
- package/dist/interact/canvasInteractions.d.ts +50 -0
- package/dist/interact/canvasInteractions.js +150 -0
- package/dist/interact/cropSession.d.ts +123 -0
- package/dist/interact/cropSession.js +526 -0
- package/dist/interact/dblClick.d.ts +51 -0
- package/dist/interact/dblClick.js +106 -0
- package/dist/interact/expandSession.d.ts +107 -0
- package/dist/interact/expandSession.js +281 -0
- package/dist/interact/hotkeyActions.d.ts +16 -0
- package/dist/interact/hotkeyActions.js +41 -0
- package/dist/interact/pathSession.d.ts +141 -0
- package/dist/interact/pathSession.js +480 -0
- package/dist/interact/pointer.d.ts +106 -0
- package/dist/interact/pointer.js +1594 -0
- package/dist/interact/pointerTablePuzzle.d.ts +100 -0
- package/dist/interact/pointerTablePuzzle.js +358 -0
- package/dist/interact/pointerTransform.d.ts +86 -0
- package/dist/interact/pointerTransform.js +440 -0
- package/dist/interact/pointerTypes.d.ts +577 -0
- package/dist/interact/pointerTypes.js +96 -0
- package/dist/interact/snapGuide.d.ts +127 -0
- package/dist/interact/snapGuide.js +376 -0
- package/dist/interact/tableCellEditing.d.ts +61 -0
- package/dist/interact/tableCellEditing.js +198 -0
- package/dist/interact/tableInteraction.d.ts +55 -0
- package/dist/interact/tableInteraction.js +212 -0
- package/dist/interact/textEditing.d.ts +77 -0
- package/dist/interact/textEditing.js +410 -0
- package/dist/interact/warpFacade.d.ts +36 -0
- package/dist/interact/warpFacade.js +51 -0
- package/dist/interact/warpSession.d.ts +191 -0
- package/dist/interact/warpSession.js +582 -0
- package/dist/interact/wheelScroll.d.ts +62 -0
- package/dist/interact/wheelScroll.js +72 -0
- package/dist/model/alphaHit.d.ts +59 -0
- package/dist/model/alphaHit.js +262 -0
- package/dist/model/animation.d.ts +228 -0
- package/dist/model/animation.js +510 -0
- package/dist/model/arcLayout.d.ts +54 -0
- package/dist/model/arcLayout.js +307 -0
- package/dist/model/arcTextTransform.d.ts +7 -0
- package/dist/model/arcTextTransform.js +87 -0
- package/dist/model/compatEvents.d.ts +135 -0
- package/dist/model/compatEvents.js +73 -0
- package/dist/model/compatObject.d.ts +716 -0
- package/dist/model/compatObject.js +1579 -0
- package/dist/model/cropEdit.d.ts +118 -0
- package/dist/model/cropEdit.js +497 -0
- package/dist/model/document.d.ts +294 -0
- package/dist/model/document.js +572 -0
- package/dist/model/expandEdit.d.ts +93 -0
- package/dist/model/expandEdit.js +161 -0
- package/dist/model/fabricPrimitives.d.ts +156 -0
- package/dist/model/fabricPrimitives.js +9 -0
- package/dist/model/filler.d.ts +53 -0
- package/dist/model/filler.js +1 -0
- package/dist/model/groupAssemble.d.ts +7 -0
- package/dist/model/groupAssemble.js +98 -0
- package/dist/model/groupChildMove.d.ts +46 -0
- package/dist/model/groupChildMove.js +145 -0
- package/dist/model/groupResize.d.ts +78 -0
- package/dist/model/groupResize.js +108 -0
- package/dist/model/handles.d.ts +40 -0
- package/dist/model/handles.js +29 -0
- package/dist/model/imageMaskBake.d.ts +75 -0
- package/dist/model/imageMaskBake.js +136 -0
- package/dist/model/lineConstruct.d.ts +13 -0
- package/dist/model/lineConstruct.js +79 -0
- package/dist/model/maskCropEdit.d.ts +64 -0
- package/dist/model/maskCropEdit.js +90 -0
- package/dist/model/pathBackground.d.ts +51 -0
- package/dist/model/pathBackground.js +196 -0
- package/dist/model/pathConstruct.d.ts +27 -0
- package/dist/model/pathConstruct.js +55 -0
- package/dist/model/pathEdit.d.ts +114 -0
- package/dist/model/pathEdit.js +378 -0
- package/dist/model/puzzleEdit.d.ts +136 -0
- package/dist/model/puzzleEdit.js +742 -0
- package/dist/model/puzzleLayout.d.ts +91 -0
- package/dist/model/puzzleLayout.js +143 -0
- package/dist/model/scene.d.ts +1004 -0
- package/dist/model/scene.js +2841 -0
- package/dist/model/schema.d.ts +290 -0
- package/dist/model/schema.js +13 -0
- package/dist/model/shapePath.d.ts +14 -0
- package/dist/model/shapePath.js +136 -0
- package/dist/model/tableEdit.d.ts +196 -0
- package/dist/model/tableEdit.js +751 -0
- package/dist/model/tableFacade.d.ts +6 -0
- package/dist/model/tableFacade.js +808 -0
- package/dist/model/tableLayout.d.ts +85 -0
- package/dist/model/tableLayout.js +193 -0
- package/dist/model/textLayout.d.ts +357 -0
- package/dist/model/textLayout.js +822 -0
- package/dist/render/GLBlend.d.ts +40 -0
- package/dist/render/GLBlend.js +270 -0
- package/dist/render/GLContext.d.ts +25 -0
- package/dist/render/GLContext.js +48 -0
- package/dist/render/GLIsolate.d.ts +46 -0
- package/dist/render/GLIsolate.js +188 -0
- package/dist/render/GLTextureCache.d.ts +86 -0
- package/dist/render/GLTextureCache.js +187 -0
- package/dist/render/GlyphAtlas.d.ts +151 -0
- package/dist/render/GlyphAtlas.js +404 -0
- package/dist/render/MeshBatch.d.ts +123 -0
- package/dist/render/MeshBatch.js +416 -0
- package/dist/render/QuadBatch.d.ts +138 -0
- package/dist/render/QuadBatch.js +490 -0
- package/dist/render/SceneRenderer.d.ts +186 -0
- package/dist/render/SceneRenderer.js +700 -0
- package/dist/render/SourceManager.d.ts +121 -0
- package/dist/render/SourceManager.js +505 -0
- package/dist/render/TextRenderer.d.ts +140 -0
- package/dist/render/TextRenderer.js +795 -0
- package/dist/render/arcText.d.ts +40 -0
- package/dist/render/arcText.js +187 -0
- package/dist/render/batchOrder.d.ts +105 -0
- package/dist/render/batchOrder.js +168 -0
- package/dist/render/blendMode.d.ts +35 -0
- package/dist/render/blendMode.js +142 -0
- package/dist/render/clipMaskPainter.d.ts +17 -0
- package/dist/render/clipMaskPainter.js +121 -0
- package/dist/render/fontInvalidation.d.ts +25 -0
- package/dist/render/fontInvalidation.js +77 -0
- package/dist/render/fontInvalidationController.d.ts +76 -0
- package/dist/render/fontInvalidationController.js +114 -0
- package/dist/render/frameOverlay.d.ts +62 -0
- package/dist/render/frameOverlay.js +124 -0
- package/dist/render/framePipeline.d.ts +99 -0
- package/dist/render/framePipeline.js +95 -0
- package/dist/render/gpuFilterPasses.d.ts +25 -0
- package/dist/render/gpuFilterPasses.js +883 -0
- package/dist/render/gpuFx.d.ts +136 -0
- package/dist/render/gpuFx.js +614 -0
- package/dist/render/gpuImageFilters.d.ts +59 -0
- package/dist/render/gpuImageFilters.js +289 -0
- package/dist/render/gpuShadow.d.ts +52 -0
- package/dist/render/gpuShadow.js +289 -0
- package/dist/render/gradientRamp.d.ts +45 -0
- package/dist/render/gradientRamp.js +74 -0
- package/dist/render/imageEffects.d.ts +123 -0
- package/dist/render/imageEffects.js +390 -0
- package/dist/render/maskRender.d.ts +92 -0
- package/dist/render/maskRender.js +224 -0
- package/dist/render/progressiveLoad.d.ts +45 -0
- package/dist/render/progressiveLoad.js +86 -0
- package/dist/render/psdEffectPainter.d.ts +34 -0
- package/dist/render/psdEffectPainter.js +309 -0
- package/dist/render/puzzleClamp.d.ts +28 -0
- package/dist/render/puzzleClamp.js +30 -0
- package/dist/render/retainedFrame.d.ts +85 -0
- package/dist/render/retainedFrame.js +193 -0
- package/dist/render/ruler.d.ts +90 -0
- package/dist/render/ruler.js +287 -0
- package/dist/render/scrollText.d.ts +65 -0
- package/dist/render/scrollText.js +261 -0
- package/dist/render/selectionOverlay.d.ts +318 -0
- package/dist/render/selectionOverlay.js +921 -0
- package/dist/render/shaders.d.ts +38 -0
- package/dist/render/shaders.js +314 -0
- package/dist/render/shadowBake.d.ts +43 -0
- package/dist/render/shadowBake.js +202 -0
- package/dist/render/shadowPainter.d.ts +13 -0
- package/dist/render/shadowPainter.js +80 -0
- package/dist/render/sharedGpuPainters.d.ts +25 -0
- package/dist/render/sharedGpuPainters.js +40 -0
- package/dist/render/sourceHydration.d.ts +12 -0
- package/dist/render/sourceHydration.js +48 -0
- package/dist/render/strokeBake.d.ts +85 -0
- package/dist/render/strokeBake.js +515 -0
- package/dist/render/strokePainter.d.ts +28 -0
- package/dist/render/strokePainter.js +119 -0
- package/dist/render/strokeSdf.d.ts +35 -0
- package/dist/render/strokeSdf.js +74 -0
- package/dist/render/svgRaster.d.ts +46 -0
- package/dist/render/svgRaster.js +71 -0
- package/dist/render/tableOverlay.d.ts +38 -0
- package/dist/render/tableOverlay.js +233 -0
- package/dist/render/textHighlight.d.ts +117 -0
- package/dist/render/textHighlight.js +380 -0
- package/dist/render/textureUpload.d.ts +18 -0
- package/dist/render/textureUpload.js +26 -0
- package/dist/render/verticalText.d.ts +38 -0
- package/dist/render/verticalText.js +63 -0
- package/dist/render/warpText.d.ts +58 -0
- package/dist/render/warpText.js +380 -0
- package/dist/render/workspaceBackdrop.d.ts +35 -0
- package/dist/render/workspaceBackdrop.js +71 -0
- package/dist/render/workspaceChecker.d.ts +54 -0
- package/dist/render/workspaceChecker.js +170 -0
- package/dist/render/workspaceGlow.d.ts +51 -0
- package/dist/render/workspaceGlow.js +171 -0
- package/package.json +39 -3
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 形状/路径族描边烘焙(main-webgl2 Phase 3 P3-2 + P3-5c 对齐扩展)。
|
|
3
|
+
*
|
|
4
|
+
* **strokeAlignment OUTER/INNER(应用定制,FabricPath._renderStroke 技巧)**:
|
|
5
|
+
* - OUTER:双倍宽中心描边 → destination-out 用对象填充擦内侧(半透明填充按
|
|
6
|
+
* 其 alpha 比例残留内半;空填充时 clip + 描边色擦)→ 净效果只剩外半带。
|
|
7
|
+
* main 随后重画填充——引擎填充走网格管线,烘焙里跳过重画(等价:擦净的
|
|
8
|
+
* 内半透出网格填充)
|
|
9
|
+
* - INNER:clip 到填充路径内 → 双倍宽描边只显内半
|
|
10
|
+
* 对齐语义全部在 pen 空间(strokeUniform 补偿后)执行,与 main 同序。
|
|
11
|
+
*
|
|
12
|
+
* fabric 的描边语义全部由 canvas2d stroke 算子承载(_renderStroke:lineWidth/
|
|
13
|
+
* lineCap/lineJoin/miterLimit/setLineDash)。**strokeUniform 的真实语义**
|
|
14
|
+
* (canvas 路径存储语义——坐标在构造时变换进存储空间,之后的 CTM 只影响笔):
|
|
15
|
+
* fabric 在路径构造完成后才 ctx.scale(1/scale),故几何照常缩放、**笔宽在设备
|
|
16
|
+
* 空间恒定**——不是几何反向补偿(首版照字面实现导致 uniform 对象描边整体
|
|
17
|
+
* 缩小一圈)。引擎用 beginPath 构造(同 fabric:路径随构造矩阵落位)+ 补偿后
|
|
18
|
+
* 的 CTM 出笔,逐算子同机制;quad 线性恒等平移到对象中心(P3-4/P3-5 同款)。
|
|
19
|
+
*
|
|
20
|
+
* 几何口径(中心原点,与 mesh 相同):
|
|
21
|
+
* - Path:归一化命令(fabric simple 形态)− pathOffset(getBoundsOfPath 中心)
|
|
22
|
+
* - polygon/polyline:points − bbox 中心;**fabric Polyline 怪癖照抄**——
|
|
23
|
+
* moveTo(p0) 后先 lineTo(p0) 再 lineTo(p1..)(零长段影响虚线相位/圆帽点)
|
|
24
|
+
* - ellipse/circle:ctx.ellipse
|
|
25
|
+
* - rect:rect 路径
|
|
26
|
+
*/
|
|
27
|
+
import { parseStrokeDecl } from '../core/stroke';
|
|
28
|
+
import { getBoundsOfPath, parsePathData } from '../core/pathData';
|
|
29
|
+
import { multiply } from '../core/mat2d';
|
|
30
|
+
import { rectCornerRadii, traceRoundedRectPath } from '../core/roundedRect';
|
|
31
|
+
/** 描边在对象线性变换下的包围盒半扩展(斜接尖峰可达 width×miterLimit/2) */
|
|
32
|
+
const strokeMargin = (decl, node) => {
|
|
33
|
+
const m = node.matrix;
|
|
34
|
+
const maxScale = Math.max(Math.hypot(m[0], m[1]), Math.hypot(m[2], m[3]));
|
|
35
|
+
return Math.ceil((decl.width * Math.max(1, decl.miterLimit) * (decl.uniform ? 1 : maxScale)) / 2 + 4);
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* 描边烘焙面积预算(画布像素,2026-10-09):光栅+上传实测 ~8µs/px,超预算
|
|
39
|
+
* 的单张烘焙即手势 settle 期的主线程长任务——3000×2000 工作区描边在 zoom 8
|
|
40
|
+
* 下按旧 8192 边长守卫烘出 8192×5476(45M px = 179MB 纹理,单次上传 164ms+
|
|
41
|
+
* 光栅 ~240ms)。6.25M px(≈2560²,25MB):1× 下工作区级大元素(6M px)不
|
|
42
|
+
* 降档保锐利;高倍时降档 GL 上采样(s<k 自动放弃像素对齐),高倍缩放观感
|
|
43
|
+
* 里描边边缘略软——优先于 settle 期数百 ms 停顿。桶 4 字形/PSD 同类预算
|
|
44
|
+
* 先例见 STROKE_BAKE_PIXEL_BUDGET(EDT 逐像素更贵,预算更小)。
|
|
45
|
+
*/
|
|
46
|
+
const MAX_STROKE_BAKE_AREA_PX = 6_250_000;
|
|
47
|
+
/**
|
|
48
|
+
* 光栅 scale = 设备 scale(zoom×dpr)经边长(8192)与面积双守卫降档:
|
|
49
|
+
* 面积守卫防超大元素的单一烘焙构成秒级停顿/巨纹显存(见上方预算注),
|
|
50
|
+
* s<k 时 strokeSnapOrigin 对齐自动放弃(调用方既有分支)。
|
|
51
|
+
*/
|
|
52
|
+
export const strokeRasterScale = (k, sceneW, sceneH) => Math.min(k, 8192 / sceneW, 8192 / sceneH, Math.sqrt(MAX_STROKE_BAKE_AREA_PX / (sceneW * sceneH)));
|
|
53
|
+
/**
|
|
54
|
+
* 烘焙窗口左上角的设备像素对齐(1px 边框的清晰度关键):把窗口平移吸附到
|
|
55
|
+
* 整数设备像素——烘焙 texel 与屏幕像素 1:1,canvas AA 成为唯一一次 AA
|
|
56
|
+
* (与 fabric 直绘同模型)。不对齐时亚像素平移经 GL 双线性把 1px 描边抹成
|
|
57
|
+
* 2-3px 软带(烘焙 AA + 采样 AA 双重抗锯齿;表格内边框两格叠加后肉眼即
|
|
58
|
+
* 「分层」,2026-09-20)。烘焙 quad 恒轴对齐(对象旋转烘在窗口内部),
|
|
59
|
+
* 任意旋转/缩放都可对齐;对齐量 ≤ 半设备像素,视觉零位移。
|
|
60
|
+
*/
|
|
61
|
+
export const strokeSnapOrigin = (m, zoom, dpr, vpt, sceneW, sceneH) => {
|
|
62
|
+
const k = zoom * dpr;
|
|
63
|
+
const cx = vpt[4] * dpr;
|
|
64
|
+
const cy = vpt[5] * dpr;
|
|
65
|
+
return {
|
|
66
|
+
lx: (Math.round((m[4] - sceneW / 2) * k + cx) - cx) / k,
|
|
67
|
+
ly: (Math.round((m[5] - sceneH / 2) * k + cy) - cy) / k
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* 形状族的中心原点几何路径 → ctx beginPath 构造(构造时 CTM 决定路径落位)。
|
|
72
|
+
* 类型键大小写归一比对:引擎 raw 两形态并存(store 装载小写 / fabric 构造器
|
|
73
|
+
* toObject 大写——线段工具经 compat 桥的 'Polyline' 曾因只匹配小写静默无描边),
|
|
74
|
+
* 与 ShapeNode.shapeKind / fabricReference 的归一口径一致。
|
|
75
|
+
*/
|
|
76
|
+
export const traceShapeStrokePath = (ctx, node) => {
|
|
77
|
+
const raw = node.raw;
|
|
78
|
+
const type = typeof raw.type === 'string' ? raw.type.toLowerCase() : '';
|
|
79
|
+
ctx.beginPath();
|
|
80
|
+
if (type === 'path') {
|
|
81
|
+
const commands = parsePathData(raw.path);
|
|
82
|
+
if (!commands)
|
|
83
|
+
return false;
|
|
84
|
+
const bounds = getBoundsOfPath(commands);
|
|
85
|
+
if (!bounds)
|
|
86
|
+
return false;
|
|
87
|
+
const cx = (bounds.left + bounds.right) / 2;
|
|
88
|
+
const cy = (bounds.top + bounds.bottom) / 2;
|
|
89
|
+
for (const cmd of commands) {
|
|
90
|
+
switch (cmd[0]) {
|
|
91
|
+
case 'M':
|
|
92
|
+
ctx.moveTo(cmd[1] - cx, cmd[2] - cy);
|
|
93
|
+
break;
|
|
94
|
+
case 'L':
|
|
95
|
+
ctx.lineTo(cmd[1] - cx, cmd[2] - cy);
|
|
96
|
+
break;
|
|
97
|
+
case 'C':
|
|
98
|
+
ctx.bezierCurveTo(cmd[1] - cx, cmd[2] - cy, cmd[3] - cx, cmd[4] - cy, cmd[5] - cx, cmd[6] - cy);
|
|
99
|
+
break;
|
|
100
|
+
case 'Q':
|
|
101
|
+
ctx.quadraticCurveTo(cmd[1] - cx, cmd[2] - cy, cmd[3] - cx, cmd[4] - cy);
|
|
102
|
+
break;
|
|
103
|
+
case 'Z':
|
|
104
|
+
ctx.closePath();
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (type === 'polygon' || type === 'polyline') {
|
|
111
|
+
const points = Array.isArray(raw.points) ? raw.points : [];
|
|
112
|
+
if (points.length < 2)
|
|
113
|
+
return false;
|
|
114
|
+
let minX = Infinity;
|
|
115
|
+
let minY = Infinity;
|
|
116
|
+
let maxX = -Infinity;
|
|
117
|
+
let maxY = -Infinity;
|
|
118
|
+
for (const q of points) {
|
|
119
|
+
if (minX > q.x)
|
|
120
|
+
minX = q.x;
|
|
121
|
+
if (minY > q.y)
|
|
122
|
+
minY = q.y;
|
|
123
|
+
if (maxX < q.x)
|
|
124
|
+
maxX = q.x;
|
|
125
|
+
if (maxY < q.y)
|
|
126
|
+
maxY = q.y;
|
|
127
|
+
}
|
|
128
|
+
const cx = (minX + maxX) / 2;
|
|
129
|
+
const cy = (minY + maxY) / 2;
|
|
130
|
+
// fabric Polyline._render 同构:moveTo(p0) 后先 lineTo(p0)(零长段——
|
|
131
|
+
// round 帽虚线会在 p0 画点、相位对齐 fabric)
|
|
132
|
+
ctx.moveTo(points[0].x - cx, points[0].y - cy);
|
|
133
|
+
for (let i = 0; i < points.length; i++)
|
|
134
|
+
ctx.lineTo(points[i].x - cx, points[i].y - cy);
|
|
135
|
+
if (raw.type === 'polygon')
|
|
136
|
+
ctx.closePath();
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
if (type === 'triangle') {
|
|
140
|
+
// fabric Triangle:points 不持久化,几何由 width/height 合成(本地系
|
|
141
|
+
// (w/2,0)→(w,h)→(0,h),scene.trianglePoints/svgExport 同源)——bbox 中心
|
|
142
|
+
// (w/2,h/2) 归一到中心原点(与填充网格 buildPolygonMesh 同口径)
|
|
143
|
+
const w = node.dims.x;
|
|
144
|
+
const h = node.dims.y;
|
|
145
|
+
if (!(w > 0) || !(h > 0))
|
|
146
|
+
return false;
|
|
147
|
+
ctx.moveTo(0, -h / 2);
|
|
148
|
+
ctx.lineTo(w / 2, h / 2);
|
|
149
|
+
ctx.lineTo(-w / 2, h / 2);
|
|
150
|
+
ctx.closePath();
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
if (type === 'ellipse' || type === 'circle') {
|
|
154
|
+
const rx = (type === 'circle' ? Number(raw.radius) : Number(raw.rx)) || 0;
|
|
155
|
+
const ry = (type === 'circle' ? Number(raw.radius) : Number(raw.ry)) || 0;
|
|
156
|
+
if (!(rx > 0) || !(ry > 0))
|
|
157
|
+
return false;
|
|
158
|
+
ctx.ellipse(0, 0, rx, ry, 0, 0, Math.PI * 2);
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
if (type === 'line' || type === 'referenceline') {
|
|
162
|
+
// 线族:x1..y2 即中心相对坐标(calcLinePoints 恒等映射),开放路径
|
|
163
|
+
const x1 = Number(raw.x1);
|
|
164
|
+
const y1 = Number(raw.y1);
|
|
165
|
+
const x2 = Number(raw.x2);
|
|
166
|
+
const y2 = Number(raw.y2);
|
|
167
|
+
if (!Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x2) || !Number.isFinite(y2))
|
|
168
|
+
return false;
|
|
169
|
+
ctx.moveTo(x1, y1);
|
|
170
|
+
ctx.lineTo(x2, y2);
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
if (type === 'rect' || type === 'fabricrect') {
|
|
174
|
+
const w = node.dims.x;
|
|
175
|
+
const h = node.dims.y;
|
|
176
|
+
if (!(w > 0) || !(h > 0))
|
|
177
|
+
return false;
|
|
178
|
+
// 圆角(批次 98):fabric Rect._render 的 kappa 贝塞尔逐式——描边几何/
|
|
179
|
+
// clipPath CPU 剪影与 quad SDF 主渲染同一轮廓
|
|
180
|
+
const radii = rectCornerRadii(raw, w, h);
|
|
181
|
+
if (radii)
|
|
182
|
+
traceRoundedRectPath(ctx, w, h, radii.rx, radii.ry);
|
|
183
|
+
else
|
|
184
|
+
ctx.rect(-w / 2, -h / 2, w, h);
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
return false;
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* 描边烘焙(场景空间,光栅 scale = 设备 scale):Path2D 按对象矩阵落位
|
|
191
|
+
* (strokeUniform 时矩阵后接 scale(1/scale) 补偿——fabric _renderStroke
|
|
192
|
+
* 同序),canvas stroke 一次。烘焙分辨率 = zoom×dpr,texel 与屏幕像素
|
|
193
|
+
* 1:1(strokeSnapOrigin 吸附窗口左上角)——canvas AA 即唯一一次 AA;
|
|
194
|
+
* 笔宽/虚线相位在 pen 空间自动同倍缩放,fabric 语义不变。
|
|
195
|
+
*/
|
|
196
|
+
export const bakeNodeStroke = (node, view) => {
|
|
197
|
+
const decl = parseStrokeDecl(node.raw);
|
|
198
|
+
if (!decl)
|
|
199
|
+
return null;
|
|
200
|
+
const type = typeof node.raw.type === 'string' ? node.raw.type.toLowerCase() : '';
|
|
201
|
+
if (type === 'arrow' || type === 'dimension') {
|
|
202
|
+
// 线族装饰同主路径模型设备分辨率烘焙(zoom×dpr 光栅 + 窗口左上角设备像素
|
|
203
|
+
// 吸附)。首版维持 1× 的判据(2026-09-23「杆宽 ≥2px/文本不敏感」)失准:
|
|
204
|
+
// 箭头三角头两条斜边长,dpr 1.25~2 屏上 GL 双线性上采样把边缘抹成 3-4px
|
|
205
|
+
// 软带,用户可见「箭头不清楚」(2026-09-25);导出链 multiplier>1 同病
|
|
206
|
+
// (applyExportViewport 期 host.zoom 跟随 multiplier,签名 k 连动自动重烘)
|
|
207
|
+
return bakeLineDecoration(node, decl, view);
|
|
208
|
+
}
|
|
209
|
+
const extentX = Math.abs(node.matrix[0]) * node.dims.x + Math.abs(node.matrix[2]) * node.dims.y;
|
|
210
|
+
const extentY = Math.abs(node.matrix[1]) * node.dims.x + Math.abs(node.matrix[3]) * node.dims.y;
|
|
211
|
+
const margin = strokeMargin(decl, node);
|
|
212
|
+
const sceneW = extentX + margin * 2;
|
|
213
|
+
const sceneH = extentY + margin * 2;
|
|
214
|
+
if (!(sceneW > 0) || !(sceneH > 0) || sceneW > 8192 || sceneH > 8192)
|
|
215
|
+
return null;
|
|
216
|
+
const k = view ? view.zoom * view.dpr : 1;
|
|
217
|
+
const s = strokeRasterScale(k, sceneW, sceneH);
|
|
218
|
+
const w = Math.ceil(sceneW * s);
|
|
219
|
+
const h = Math.ceil(sceneH * s);
|
|
220
|
+
if (!(w > 0) || !(h > 0))
|
|
221
|
+
return null;
|
|
222
|
+
const canvas = document.createElement('canvas');
|
|
223
|
+
canvas.width = w;
|
|
224
|
+
canvas.height = h;
|
|
225
|
+
const ctx = canvas.getContext('2d');
|
|
226
|
+
if (!ctx)
|
|
227
|
+
return null;
|
|
228
|
+
const m = node.matrix;
|
|
229
|
+
const snap = view && s === k ? strokeSnapOrigin(m, view.zoom, view.dpr, view.vpt, sceneW, sceneH) : null;
|
|
230
|
+
const lx = snap ? snap.lx : m[4] - sceneW / 2;
|
|
231
|
+
const ly = snap ? snap.ly : m[5] - sceneH / 2;
|
|
232
|
+
// quad 场景尺寸按实际画布反推(ceil 尾纹素吸收进 quad,左上角恒 = lx)
|
|
233
|
+
const qw = w / s;
|
|
234
|
+
const qh = h / s;
|
|
235
|
+
// 1) 路径构造矩阵(场景窗口左上角平移进画布 × 光栅 scale):canvas 路径
|
|
236
|
+
// 坐标在构造时变换进存储空间——几何随对象矩阵缩放/旋转,且以设备分辨率落位
|
|
237
|
+
const tm = [m[0] * s, m[1] * s, m[2] * s, m[3] * s, (m[4] - lx) * s, (m[5] - ly) * s];
|
|
238
|
+
ctx.setTransform(tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]);
|
|
239
|
+
if (!traceShapeStrokePath(ctx, node))
|
|
240
|
+
return null;
|
|
241
|
+
// 2) 出笔矩阵:strokeUniform 时矩阵后接 scale(1/scale)——只影响笔宽
|
|
242
|
+
// (路径已构造完成),设备空间笔宽恒定(fabric _renderStroke 同序同机制)
|
|
243
|
+
let pen = tm;
|
|
244
|
+
if (decl.uniform) {
|
|
245
|
+
const sx = Math.hypot(m[0], m[1]) || 1;
|
|
246
|
+
const sy = Math.hypot(m[2], m[3]) || 1;
|
|
247
|
+
pen = multiply(tm, [1 / sx, 0, 0, 1 / sy, 0, 0]);
|
|
248
|
+
}
|
|
249
|
+
ctx.setTransform(pen[0], pen[1], pen[2], pen[3], pen[4], pen[5]);
|
|
250
|
+
ctx.lineWidth = decl.width;
|
|
251
|
+
ctx.lineCap = decl.lineCap;
|
|
252
|
+
ctx.lineDashOffset = decl.dashOffset;
|
|
253
|
+
ctx.lineJoin = decl.lineJoin;
|
|
254
|
+
ctx.miterLimit = decl.miterLimit;
|
|
255
|
+
if (decl.dashArray)
|
|
256
|
+
ctx.setLineDash(decl.dashArray);
|
|
257
|
+
ctx.strokeStyle = decl.color;
|
|
258
|
+
if (decl.alignment === 'outside' || decl.alignment === 'inside') {
|
|
259
|
+
// 应用定制对齐:双倍宽中心带 +(擦内侧 | clip 内侧)
|
|
260
|
+
ctx.lineWidth = decl.width * 2;
|
|
261
|
+
if (decl.alignment === 'inside') {
|
|
262
|
+
ctx.clip();
|
|
263
|
+
ctx.stroke();
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
ctx.stroke();
|
|
267
|
+
// destination-out 擦内侧:填充色(含渐变)决定擦除比例;空填充时
|
|
268
|
+
// main 用 clip + 残留的描边色填(不透明全擦)
|
|
269
|
+
ctx.globalCompositeOperation = 'destination-out';
|
|
270
|
+
const fill = fillStyleForNode(node, ctx);
|
|
271
|
+
const hasFill = typeof node.raw.fill === 'string' && node.raw.fill !== '' && node.raw.fill !== 'transparent';
|
|
272
|
+
if (!hasFill)
|
|
273
|
+
ctx.clip();
|
|
274
|
+
ctx.fillStyle = fill;
|
|
275
|
+
ctx.fill();
|
|
276
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
277
|
+
// main 在此重画填充——引擎填充在网格层,跳过(等价性见文件头注)
|
|
278
|
+
}
|
|
279
|
+
return { canvas, w: qw, h: qh, lx, ly, sceneW, sceneH, snapped: !!snap };
|
|
280
|
+
}
|
|
281
|
+
ctx.stroke();
|
|
282
|
+
return { canvas, w: qw, h: qh, lx, ly, sceneW, sceneH, snapped: !!snap };
|
|
283
|
+
};
|
|
284
|
+
/** 节点填充的 canvas 色(擦除比例/投影剪影用;渐变走原生构造,Pattern 罕见回落描边色) */
|
|
285
|
+
export const fillStyleForNode = (node, ctx) => {
|
|
286
|
+
const raw = node.raw.fill;
|
|
287
|
+
if (typeof raw === 'string' && raw)
|
|
288
|
+
return raw;
|
|
289
|
+
if (raw && typeof raw === 'object' && raw.type === 'linear') {
|
|
290
|
+
const o = raw;
|
|
291
|
+
const c = o.coords ?? {};
|
|
292
|
+
const w = node.dims.x;
|
|
293
|
+
const h = node.dims.y;
|
|
294
|
+
const m11 = o.gradientUnits === 'percentage' ? w : 1;
|
|
295
|
+
const m22 = o.gradientUnits === 'percentage' ? h : 1;
|
|
296
|
+
const g = ctx.createLinearGradient((c.x1 ?? 0) * m11 + (o.offsetX ?? 0), (c.y1 ?? 0) * m22 + (o.offsetY ?? 0), (c.x2 ?? 0) * m11 + (o.offsetX ?? 0), (c.y2 ?? 0) * m22 + (o.offsetY ?? 0));
|
|
297
|
+
for (const stop of o.colorStops ?? []) {
|
|
298
|
+
if (typeof stop?.offset === 'number' && typeof stop?.color === 'string')
|
|
299
|
+
g.addColorStop(Math.min(1, Math.max(0, stop.offset)), stop.color);
|
|
300
|
+
}
|
|
301
|
+
return g;
|
|
302
|
+
}
|
|
303
|
+
return declFallbackStroke(node);
|
|
304
|
+
};
|
|
305
|
+
const declFallbackStroke = (node) => {
|
|
306
|
+
const stroke = node.raw.stroke;
|
|
307
|
+
return typeof stroke === 'string' && stroke ? stroke : '#000';
|
|
308
|
+
};
|
|
309
|
+
/** 线端点(中心相对坐标) */
|
|
310
|
+
const linePoints = (node) => {
|
|
311
|
+
const x1 = Number(node.raw.x1);
|
|
312
|
+
const y1 = Number(node.raw.y1);
|
|
313
|
+
const x2 = Number(node.raw.x2);
|
|
314
|
+
const y2 = Number(node.raw.y2);
|
|
315
|
+
if (!Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x2) || !Number.isFinite(y2))
|
|
316
|
+
return null;
|
|
317
|
+
return { x1, y1, x2, y2 };
|
|
318
|
+
};
|
|
319
|
+
/**
|
|
320
|
+
* 线族装饰烘焙(P3-6b):Arrow(缩短杆 + 实心三角头,main Arrow._render 同构
|
|
321
|
+
* ——lineWidth 直设、虚线原样、**无 strokeUniform 补偿**、头尺寸 = 预设 ×
|
|
322
|
+
* √strokeWidth)与 Dimension(整线 _renderStroke 全语义 + 端刻 round 帽 +
|
|
323
|
+
* 中点文本框)。文本宽度用浏览器 measureText(与 main 同源)。
|
|
324
|
+
*
|
|
325
|
+
* 光栅 scale = zoom×dpr(主路径同款,2026-09-25 箭头模糊收口):全部几何
|
|
326
|
+
* (杆/三角头/端刻/文本)都在 tm 变换下以局部坐标绘制,变换乘 s 即设备分辨率
|
|
327
|
+
* 落位,笔宽/字号在 pen 空间自动同倍缩放;超大窗 8192 守卫降档(放弃对齐
|
|
328
|
+
* 仍可渲染)。无 view(单测桩)时 s=1 恒等旧行为。
|
|
329
|
+
*/
|
|
330
|
+
const bakeLineDecoration = (node, decl, view) => {
|
|
331
|
+
const p = linePoints(node);
|
|
332
|
+
if (!p)
|
|
333
|
+
return null;
|
|
334
|
+
const raw = node.raw;
|
|
335
|
+
// 类型键小写归一(批次46 勘注:raw 两形态并存——store 装载小写 / fabric
|
|
336
|
+
// 构造器 toObject 大写,外层分支已归一,内层箭头/标注分派同口径)
|
|
337
|
+
const rawType = typeof raw.type === 'string' ? raw.type.toLowerCase() : '';
|
|
338
|
+
const isArrow = rawType === 'arrow';
|
|
339
|
+
const strokeWidth = Number(raw.strokeWidth) || 1;
|
|
340
|
+
const stroke = typeof raw.stroke === 'string' ? raw.stroke : isArrow ? '#000000' : '#000000';
|
|
341
|
+
const dash = Array.isArray(raw.strokeDashArray) ? raw.strokeDashArray : null;
|
|
342
|
+
// 烘焙边距:箭头高度/宽度 + 描边 + 标注文本框(offset + 文本对角)
|
|
343
|
+
let margin = Math.ceil(strokeWidth * 2 + 8);
|
|
344
|
+
let textInfo = null;
|
|
345
|
+
const arrowW = Number(raw.arrowWidth) || 15;
|
|
346
|
+
const arrowH = Number(raw.arrowHeight) || 22.5;
|
|
347
|
+
const arrowType = raw.arrowType === 'start' || raw.arrowType === 'both' ? raw.arrowType : raw.arrowType === 'end' ? 'end' : 'end';
|
|
348
|
+
const sizeScale = Math.sqrt(strokeWidth);
|
|
349
|
+
const headW = arrowW * sizeScale;
|
|
350
|
+
const headH = arrowH * sizeScale;
|
|
351
|
+
if (isArrow) {
|
|
352
|
+
margin = Math.ceil(margin + headH + headW);
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
// Dimension 文本几何(_getTextLayout 同构)
|
|
356
|
+
const fontPx = Number(raw.textFontSize) || 50;
|
|
357
|
+
const text = typeof raw.text === 'string' && raw.text.trim() ? raw.text : '50cm';
|
|
358
|
+
const pad = Number(raw.textPadding) || 4;
|
|
359
|
+
const font = `${fontPx}px Arial, sans-serif`;
|
|
360
|
+
const textW = measureWidth(font, text) + pad * 2;
|
|
361
|
+
const angle = Math.atan2(p.y2 - p.y1, p.x2 - p.x1);
|
|
362
|
+
const nx = -Math.sin(angle);
|
|
363
|
+
const ny = Math.cos(angle);
|
|
364
|
+
const offset = fontPx + strokeWidth / 2 + 5;
|
|
365
|
+
textInfo = {
|
|
366
|
+
text,
|
|
367
|
+
fontPx,
|
|
368
|
+
w: textW,
|
|
369
|
+
h: fontPx + pad * 2,
|
|
370
|
+
cx: (p.x1 + p.x2) / 2 + nx * offset,
|
|
371
|
+
cy: (p.y1 + p.y2) / 2 + ny * offset,
|
|
372
|
+
angle: raw.textFollowLine === false ? 0 : angle,
|
|
373
|
+
color: typeof raw.textColor === 'string' ? raw.textColor : '#333333',
|
|
374
|
+
bg: typeof raw.textBackgroundColor === 'string' ? raw.textBackgroundColor : '#ffffff',
|
|
375
|
+
pad,
|
|
376
|
+
follow: raw.textFollowLine !== false
|
|
377
|
+
};
|
|
378
|
+
margin = Math.ceil(margin + offset + Math.max(textW, fontPx * 2) * 0.75);
|
|
379
|
+
}
|
|
380
|
+
const extentX = Math.abs(node.matrix[0]) * node.dims.x + Math.abs(node.matrix[2]) * node.dims.y;
|
|
381
|
+
const extentY = Math.abs(node.matrix[1]) * node.dims.x + Math.abs(node.matrix[3]) * node.dims.y;
|
|
382
|
+
const sceneW = extentX + margin * 2;
|
|
383
|
+
const sceneH = extentY + margin * 2;
|
|
384
|
+
if (!(sceneW > 0) || !(sceneH > 0) || sceneW > 8192 || sceneH > 8192)
|
|
385
|
+
return null;
|
|
386
|
+
const k = view ? view.zoom * view.dpr : 1;
|
|
387
|
+
const s = strokeRasterScale(k, sceneW, sceneH);
|
|
388
|
+
const w = Math.ceil(sceneW * s);
|
|
389
|
+
const h = Math.ceil(sceneH * s);
|
|
390
|
+
if (!(w > 0) || !(h > 0))
|
|
391
|
+
return null;
|
|
392
|
+
const canvas = document.createElement('canvas');
|
|
393
|
+
canvas.width = w;
|
|
394
|
+
canvas.height = h;
|
|
395
|
+
const ctx = canvas.getContext('2d');
|
|
396
|
+
if (!ctx)
|
|
397
|
+
return null;
|
|
398
|
+
const m4 = node.matrix;
|
|
399
|
+
const snap = view && s === k ? strokeSnapOrigin(m4, view.zoom, view.dpr, view.vpt, sceneW, sceneH) : null;
|
|
400
|
+
const lx = snap ? snap.lx : m4[4] - sceneW / 2;
|
|
401
|
+
const ly = snap ? snap.ly : m4[5] - sceneH / 2;
|
|
402
|
+
// 路径构造矩阵:几何随对象矩阵 × 光栅 s 以设备分辨率落位;平移 = 窗口左上角
|
|
403
|
+
// (对齐后)到对象中心——ceil 尾纹素吸收进 quad(主路径同款)
|
|
404
|
+
const tm = [m4[0] * s, m4[1] * s, m4[2] * s, m4[3] * s, (m4[4] - lx) * s, (m4[5] - ly) * s];
|
|
405
|
+
ctx.setTransform(tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]);
|
|
406
|
+
const dx = p.x2 - p.x1;
|
|
407
|
+
const dy = p.y2 - p.y1;
|
|
408
|
+
const angle = Math.atan2(dy, dx);
|
|
409
|
+
const cos = Math.cos(angle);
|
|
410
|
+
const sin = Math.sin(angle);
|
|
411
|
+
if (isArrow) {
|
|
412
|
+
const atStart = arrowType === 'start' || arrowType === 'both';
|
|
413
|
+
const atEnd = arrowType === 'end' || arrowType === 'both';
|
|
414
|
+
const ax1 = p.x1 + cos * (atStart ? headH : 0);
|
|
415
|
+
const ay1 = p.y1 + sin * (atStart ? headH : 0);
|
|
416
|
+
const ax2 = p.x2 - cos * (atEnd ? headH : 0);
|
|
417
|
+
const ay2 = p.y2 - sin * (atEnd ? headH : 0);
|
|
418
|
+
ctx.lineWidth = strokeWidth;
|
|
419
|
+
ctx.strokeStyle = stroke;
|
|
420
|
+
if (dash && dash.length > 0)
|
|
421
|
+
ctx.setLineDash(dash);
|
|
422
|
+
else
|
|
423
|
+
ctx.setLineDash([]);
|
|
424
|
+
ctx.beginPath();
|
|
425
|
+
ctx.moveTo(ax1, ay1);
|
|
426
|
+
ctx.lineTo(ax2, ay2);
|
|
427
|
+
ctx.stroke();
|
|
428
|
+
ctx.setLineDash([]);
|
|
429
|
+
// 箭头三角(tip 在端点,向后 headH,±headW/2)
|
|
430
|
+
const head = (tipX, tipY, dirAngle) => {
|
|
431
|
+
ctx.save();
|
|
432
|
+
ctx.translate(tipX, tipY);
|
|
433
|
+
ctx.rotate(dirAngle);
|
|
434
|
+
ctx.beginPath();
|
|
435
|
+
ctx.moveTo(0, 0);
|
|
436
|
+
ctx.lineTo(-headH, -headW / 2);
|
|
437
|
+
ctx.lineTo(-headH, headW / 2);
|
|
438
|
+
ctx.closePath();
|
|
439
|
+
ctx.fillStyle = stroke;
|
|
440
|
+
ctx.fill();
|
|
441
|
+
ctx.restore();
|
|
442
|
+
};
|
|
443
|
+
if (atEnd)
|
|
444
|
+
head(p.x2, p.y2, angle);
|
|
445
|
+
if (atStart)
|
|
446
|
+
head(p.x1, p.y1, angle + Math.PI);
|
|
447
|
+
return { canvas, w: w / s, h: h / s, lx, ly, sceneW, sceneH, snapped: !!snap };
|
|
448
|
+
}
|
|
449
|
+
// Dimension:整线(_renderStroke 全语义——uniform 补偿只影响笔)
|
|
450
|
+
ctx.beginPath();
|
|
451
|
+
ctx.moveTo(p.x1, p.y1);
|
|
452
|
+
ctx.lineTo(p.x2, p.y2);
|
|
453
|
+
let pen = tm;
|
|
454
|
+
if (decl.uniform) {
|
|
455
|
+
const sx = Math.hypot(m4[0], m4[1]) || 1;
|
|
456
|
+
const sy = Math.hypot(m4[2], m4[3]) || 1;
|
|
457
|
+
pen = multiply(tm, [1 / sx, 0, 0, 1 / sy, 0, 0]);
|
|
458
|
+
}
|
|
459
|
+
ctx.setTransform(pen[0], pen[1], pen[2], pen[3], pen[4], pen[5]);
|
|
460
|
+
ctx.lineWidth = decl.width;
|
|
461
|
+
ctx.lineCap = decl.lineCap;
|
|
462
|
+
ctx.lineDashOffset = decl.dashOffset;
|
|
463
|
+
ctx.lineJoin = decl.lineJoin;
|
|
464
|
+
ctx.miterLimit = decl.miterLimit;
|
|
465
|
+
if (decl.dashArray)
|
|
466
|
+
ctx.setLineDash(decl.dashArray);
|
|
467
|
+
ctx.strokeStyle = stroke;
|
|
468
|
+
ctx.stroke();
|
|
469
|
+
// 端刻(局部空间、round 帽)
|
|
470
|
+
ctx.setTransform(tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]);
|
|
471
|
+
const nx = -sin;
|
|
472
|
+
const ny = cos;
|
|
473
|
+
const half = (Number(raw.textFontSize) || 50) / 2;
|
|
474
|
+
ctx.lineWidth = strokeWidth;
|
|
475
|
+
ctx.lineCap = 'round';
|
|
476
|
+
if (decl.dashArray)
|
|
477
|
+
ctx.setLineDash([]);
|
|
478
|
+
ctx.strokeStyle = stroke;
|
|
479
|
+
for (const [ex, ey] of [
|
|
480
|
+
[p.x1, p.y1],
|
|
481
|
+
[p.x2, p.y2]
|
|
482
|
+
]) {
|
|
483
|
+
ctx.beginPath();
|
|
484
|
+
ctx.moveTo(ex - nx * half, ey - ny * half);
|
|
485
|
+
ctx.lineTo(ex + nx * half, ey + ny * half);
|
|
486
|
+
ctx.stroke();
|
|
487
|
+
}
|
|
488
|
+
// 中点文本
|
|
489
|
+
if (textInfo) {
|
|
490
|
+
ctx.save();
|
|
491
|
+
ctx.translate(textInfo.cx, textInfo.cy);
|
|
492
|
+
if (textInfo.follow)
|
|
493
|
+
ctx.rotate(textInfo.angle);
|
|
494
|
+
ctx.font = `${textInfo.fontPx}px Arial, sans-serif`;
|
|
495
|
+
ctx.textAlign = 'center';
|
|
496
|
+
ctx.textBaseline = 'middle';
|
|
497
|
+
if (textInfo.bg) {
|
|
498
|
+
ctx.fillStyle = textInfo.bg;
|
|
499
|
+
ctx.fillRect(-textInfo.w / 2, -textInfo.h / 2, textInfo.w, textInfo.h);
|
|
500
|
+
}
|
|
501
|
+
ctx.fillStyle = textInfo.color;
|
|
502
|
+
ctx.fillText(textInfo.text, 0, 0);
|
|
503
|
+
ctx.restore();
|
|
504
|
+
}
|
|
505
|
+
return { canvas, w: w / s, h: h / s, lx, ly, sceneW, sceneH, snapped: !!snap };
|
|
506
|
+
};
|
|
507
|
+
/** 浏览器文本测量(main 的 measureSvgTextWidth 同源)。导出供 LineNode 命中
|
|
508
|
+
* 复用(2026-09-25 装饰命中:标注文本框宽与烘焙同源,命中/渲染口径一致) */
|
|
509
|
+
export const measureWidth = (font, text) => {
|
|
510
|
+
const m = document.createElement('canvas').getContext('2d');
|
|
511
|
+
if (!m)
|
|
512
|
+
return text.length * 50;
|
|
513
|
+
m.font = font;
|
|
514
|
+
return m.measureText(text).width;
|
|
515
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SceneNode } from '../model/scene';
|
|
2
|
+
import type { FrameState, SceneRendererHost } from './SceneRenderer';
|
|
3
|
+
export declare class StrokePainter {
|
|
4
|
+
private readonly host;
|
|
5
|
+
private readonly frame;
|
|
6
|
+
/** 描边烘焙缓存(P3-2,场景空间):sigBase = 声明 + 矩阵 + 几何源(type/path/
|
|
7
|
+
* points/rx)+ 尺寸(内容变化即失效立即重烘);k = 光栅 scale(zoom×dpr)——
|
|
8
|
+
* 仅它或吸附 lx/ly 翻转变化时走 stale 延迟重烘(重烘+重传是重操作,须避开
|
|
9
|
+
* 缩放/平移手势,见 noteVptChange)。lx/ly = 对齐后铺放左上角(snapped 时随
|
|
10
|
+
* vpt 平移跨半像素界翻转同样延迟重烘,sceneW/H 供确定性重算) */
|
|
11
|
+
private strokeBakes;
|
|
12
|
+
/** 最近一次视口写点时刻(stale 判定的手势窗口;-Infinity = 从未手势——
|
|
13
|
+
* 0 会在钟起点被误判窗口内)与 settle 定时器 */
|
|
14
|
+
private vptChangedAt;
|
|
15
|
+
private strokeSettleTimer;
|
|
16
|
+
constructor(host: SceneRendererHost, frame: FrameState);
|
|
17
|
+
/**
|
|
18
|
+
* 视口变化通知(缩放/平移写点均调——描边的光栅 scale 与设备像素吸附都随
|
|
19
|
+
* vpt 变化):记录时刻 + FX_SETTLE_MS settle 定时器——持续手势期间零重烘焙
|
|
20
|
+
* (重烘+重传是重操作,卡手势不可接受),停止后由 settle 帧按每帧预算分帧
|
|
21
|
+
* 补齐(frame.bakeDeadline/bakeMore)。与 PsdEffectPainter.noteVptScaleChange
|
|
22
|
+
* 同协议(其注释含协议全貌)。
|
|
23
|
+
*/
|
|
24
|
+
noteVptChange(): void;
|
|
25
|
+
render(node: SceneNode): void;
|
|
26
|
+
/** 失效(context lost 与字体迟到同口径——线族 Dimension 中点文本走 measureText):烘焙整删 */
|
|
27
|
+
invalidate(): void;
|
|
28
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 节点描边渲染画家(从 SceneRenderer 拆出)。
|
|
3
|
+
*
|
|
4
|
+
* 节点描边(P3-2):场景空间烘焙纹理(fabric _renderStroke 同算子),quad
|
|
5
|
+
* 线性恒等按烘焙窗口左上角铺放。缓存签名 = 描边声明 + 矩阵 + 几何源字段
|
|
6
|
+
* (path/points/rx/尺寸)——内容/几何变化随签名失效立即重烘(正确性优先)。
|
|
7
|
+
*
|
|
8
|
+
* 设备像素对齐(2026-09-20,表格边框「分层」反馈):光栅 scale = zoom×dpr,
|
|
9
|
+
* 烘焙窗口左上角吸附整数设备像素(strokeSnapOrigin)——texel 与屏幕像素
|
|
10
|
+
* 1:1,canvas AA 是唯一一次 AA(fabric 直绘同模型)。此前 1× 烘焙 + 亚像素
|
|
11
|
+
* quad 平移经 GL 双线性二次抗锯齿,1px 描边被抹成 2-3px 软带。
|
|
12
|
+
*
|
|
13
|
+
* 缩放/平移的 stale 延迟重烘(2026-10-09「缩放变大特别卡顿」止血):光栅
|
|
14
|
+
* scale 取连续 zoom×dpr 意味着每格滚轮全量重烘+重传(153 描边形状 1→8×
|
|
15
|
+
* 扫频实测每步 ~450ms 主线程阻塞,CPU 采样 texImage2D 52%+setTransform 26%)。
|
|
16
|
+
* 接入 PsdEffectPainter 同款协议:视口写点(缩放/平移,含吸附 lx/ly 跨半
|
|
17
|
+
* 像素界翻转)记手势窗口(FX_SETTLE_MS),窗口内与帧预算耗尽时沿用 stale
|
|
18
|
+
* 烘焙(quad 场景空间、几何不变,仅 AA 暂糊)+ bakeMore 续帧;手势停止后
|
|
19
|
+
* 由 settle 帧按每帧烘焙预算逐节点补齐精确 k。导出链(SceneExporter)按
|
|
20
|
+
* bakeMore 多轮重渲,终轮恒为新鲜烘焙。
|
|
21
|
+
*/
|
|
22
|
+
import { strokeKey } from '../core/stroke';
|
|
23
|
+
import { strokeSdfParams } from './strokeSdf';
|
|
24
|
+
import { bakeNodeStroke, strokeSnapOrigin } from './strokeBake';
|
|
25
|
+
import { createTexture2D } from './textureUpload';
|
|
26
|
+
import { FX_SETTLE_MS } from './psdEffectPainter';
|
|
27
|
+
export class StrokePainter {
|
|
28
|
+
host;
|
|
29
|
+
frame;
|
|
30
|
+
/** 描边烘焙缓存(P3-2,场景空间):sigBase = 声明 + 矩阵 + 几何源(type/path/
|
|
31
|
+
* points/rx)+ 尺寸(内容变化即失效立即重烘);k = 光栅 scale(zoom×dpr)——
|
|
32
|
+
* 仅它或吸附 lx/ly 翻转变化时走 stale 延迟重烘(重烘+重传是重操作,须避开
|
|
33
|
+
* 缩放/平移手势,见 noteVptChange)。lx/ly = 对齐后铺放左上角(snapped 时随
|
|
34
|
+
* vpt 平移跨半像素界翻转同样延迟重烘,sceneW/H 供确定性重算) */
|
|
35
|
+
strokeBakes = new WeakMap();
|
|
36
|
+
/** 最近一次视口写点时刻(stale 判定的手势窗口;-Infinity = 从未手势——
|
|
37
|
+
* 0 会在钟起点被误判窗口内)与 settle 定时器 */
|
|
38
|
+
vptChangedAt = -Infinity;
|
|
39
|
+
strokeSettleTimer = null;
|
|
40
|
+
constructor(host, frame) {
|
|
41
|
+
this.host = host;
|
|
42
|
+
this.frame = frame;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 视口变化通知(缩放/平移写点均调——描边的光栅 scale 与设备像素吸附都随
|
|
46
|
+
* vpt 变化):记录时刻 + FX_SETTLE_MS settle 定时器——持续手势期间零重烘焙
|
|
47
|
+
* (重烘+重传是重操作,卡手势不可接受),停止后由 settle 帧按每帧预算分帧
|
|
48
|
+
* 补齐(frame.bakeDeadline/bakeMore)。与 PsdEffectPainter.noteVptScaleChange
|
|
49
|
+
* 同协议(其注释含协议全貌)。
|
|
50
|
+
*/
|
|
51
|
+
noteVptChange() {
|
|
52
|
+
this.vptChangedAt = performance.now();
|
|
53
|
+
if (this.strokeSettleTimer !== null)
|
|
54
|
+
clearTimeout(this.strokeSettleTimer);
|
|
55
|
+
this.strokeSettleTimer = window.setTimeout(() => {
|
|
56
|
+
this.strokeSettleTimer = null;
|
|
57
|
+
this.host.requestRenderAll();
|
|
58
|
+
}, FX_SETTLE_MS);
|
|
59
|
+
}
|
|
60
|
+
render(node) {
|
|
61
|
+
const decl = node.strokeDecl;
|
|
62
|
+
if (!decl)
|
|
63
|
+
return;
|
|
64
|
+
// GS 描边 SDF 直绘(2026-10-09):rect/line/ellipse 合形态走解析式直绘
|
|
65
|
+
// (分辨率无关,缩放/平移零重烘零重传);不合形态回落下方烘焙链(stale
|
|
66
|
+
// 协议原样)。owner=node——增量上传的等价性判定吃打包槽位字面值
|
|
67
|
+
const sdf = strokeSdfParams(node, decl, this.host.zoom, this.host.dpr);
|
|
68
|
+
if (sdf) {
|
|
69
|
+
const c = decl.rgba;
|
|
70
|
+
this.host.batch.pushQuad(node.matrix, sdf.quadW, sdf.quadH, [c[0], c[1], c[2], c[3] * node.renderOpacity], null, sdf.shape, [sdf.halfW, sdf.code], node);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const raw = node.raw;
|
|
74
|
+
const m = node.matrix;
|
|
75
|
+
const geoKey = JSON.stringify([raw.type, raw.path, raw.points, raw.rx, raw.ry, raw.radius, node.dims.x, node.dims.y]);
|
|
76
|
+
const k = (this.host.zoom * this.host.dpr).toFixed(4);
|
|
77
|
+
const sigBase = [strokeKey(decl), m.join(','), geoKey].join(';');
|
|
78
|
+
let bake = this.strokeBakes.get(node);
|
|
79
|
+
if (bake && bake.sigBase !== sigBase) {
|
|
80
|
+
// 内容/声明/几何/矩阵变化:立即重烘(正确性优先,编辑场景单节点可承受)
|
|
81
|
+
this.host.gl.deleteTexture(bake.tex);
|
|
82
|
+
bake = undefined;
|
|
83
|
+
}
|
|
84
|
+
else if (bake) {
|
|
85
|
+
// 仅缩放链变化(k 不同,或 k 同而吸附目标随 vpt 平移跨半像素界翻转):
|
|
86
|
+
// 手势窗口内与帧预算耗尽时沿用 stale(几何不变,AA 暂糊),settle 后补齐
|
|
87
|
+
const snap = bake.k === k && bake.snapped ? strokeSnapOrigin(m, this.host.zoom, this.host.dpr, this.host.viewportTransform, bake.sceneW, bake.sceneH) : null;
|
|
88
|
+
if (bake.k !== k || (snap && (snap.lx !== bake.lx || snap.ly !== bake.ly))) {
|
|
89
|
+
const now = performance.now();
|
|
90
|
+
// 导出窗口(applyExportViewport 接管视口)不受手势门:k 换档立即新鲜烘焙
|
|
91
|
+
const gesturing = !this.host.isExporting() && (now - this.vptChangedAt < FX_SETTLE_MS || this.strokeSettleTimer !== null);
|
|
92
|
+
if (gesturing || now > this.frame.bakeDeadline) {
|
|
93
|
+
this.frame.bakeMore = true;
|
|
94
|
+
this.host.batch.pushQuad([1, 0, 0, 1, bake.lx + bake.w / 2, bake.ly + bake.h / 2], bake.w, bake.h, [1, 1, 1, node.renderOpacity], bake.tex);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.host.gl.deleteTexture(bake.tex);
|
|
98
|
+
bake = undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!bake) {
|
|
102
|
+
const result = bakeNodeStroke(node, { zoom: this.host.zoom, dpr: this.host.dpr, vpt: this.host.viewportTransform });
|
|
103
|
+
if (!result) {
|
|
104
|
+
this.strokeBakes.set(node, undefined);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const tex = createTexture2D(this.host.gl, result.canvas);
|
|
108
|
+
if (!tex)
|
|
109
|
+
return;
|
|
110
|
+
bake = { sigBase, k, tex, w: result.w, h: result.h, lx: result.lx, ly: result.ly, sceneW: result.sceneW, sceneH: result.sceneH, snapped: result.snapped };
|
|
111
|
+
this.strokeBakes.set(node, bake);
|
|
112
|
+
}
|
|
113
|
+
this.host.batch.pushQuad([1, 0, 0, 1, bake.lx + bake.w / 2, bake.ly + bake.h / 2], bake.w, bake.h, [1, 1, 1, node.renderOpacity], bake.tex);
|
|
114
|
+
}
|
|
115
|
+
/** 失效(context lost 与字体迟到同口径——线族 Dimension 中点文本走 measureText):烘焙整删 */
|
|
116
|
+
invalidate() {
|
|
117
|
+
this.strokeBakes = new WeakMap();
|
|
118
|
+
}
|
|
119
|
+
}
|