@yft-design/engine 0.0.14 → 0.0.16

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.
@@ -8,8 +8,20 @@
8
8
  * - 退化输入(<3 点 / 零面积)返回 [];共线点直接剔除不产三角形
9
9
  * - 自相交多边形无正确解(canvas nonzero 规则与三角化不可比),
10
10
  * 守卫耗尽后回退扇形三角化——保证终止与索引合法,仅病态数据触发
11
- * - 不支持带洞多边形(clipPath/复合路径属 Phase 4 议题,届时评估 earcut;
12
- * 本实现零依赖是为并行开发期不动 lockfile 的取舍)
11
+ * - 不支持带洞多边形(clipPath/复合路径由 pathData 的洞桥接预先合成简单
12
+ * 多边形;本实现零依赖是为并行开发期不动 lockfile 的取舍——批次 415 起
13
+ * 大环依赖 rbush(引擎运行时既有依赖,document 命中索引同源))
14
+ * - 批次 415:大环 rbush 点索引加速。占据检验(每耳候选 × 其余顶点包含
15
+ * 测试)是 O(n²) 主项——PDF 解析的长曲线 path(数千指令 flatten 后数万
16
+ * 顶点)单环全扫可达秒级,多页模板装载建景实测 15~18s 的 97%。索引后每
17
+ * 耳只测耳 bbox±EPS 内的存活顶点:点在耳三角形内/其边界段上必先落于耳
18
+ * bbox——候选集收缩是数学等价而非近似,谓词与失效语义逐字保留,输出与
19
+ * 全扫逐位一致(spec 内置旧实现副本随机对拍锁定)。
20
+ * 病态实录(批次 415 迭代):先试 uniform grid 失败——PDF 路径顶点全分布
21
+ * 在一维轮廓上,均匀 2D 格的边界格密度远超 n/格数 的均匀假设;洞桥接的
22
+ * 细长跨越耳(对角线远端相连)bbox 横跨大半多边形,「格数×平均密度」的
23
+ * 估计式降级两头挨打(不降级=格框架反噬、降级=全扫基数),真机 15.5s
24
+ * 不可救。rbush 查询返回真实覆盖点集而非估计,两种病态同时消解。
13
25
  */
14
26
  export interface Vec2 {
15
27
  x: number;
@@ -19,6 +31,7 @@ export interface Vec2 {
19
31
  export declare const polygonArea: (points: ArrayLike<Vec2>) => number;
20
32
  /**
21
33
  * 三角化简单多边形,返回三角形索引三元组数组(展开为 [i0,i1,i2, i0,i1,i2, ...])。
22
- * 时间 O(n²) 最坏(耳检验 × 扫描),shape 点数量级(几十~几百)下无感。
34
+ * 时间 O(n²) 最坏(耳检验 × 扫描);≥ INDEX_MIN_VERTICES 顶点自动启用 rbush
35
+ * 点索引,占据检验降为每耳 ~O(耳 bbox 内存活顶点)。
23
36
  */
24
37
  export declare const triangulatePolygon: (points: ArrayLike<Vec2>) => number[];
@@ -8,10 +8,25 @@
8
8
  * - 退化输入(<3 点 / 零面积)返回 [];共线点直接剔除不产三角形
9
9
  * - 自相交多边形无正确解(canvas nonzero 规则与三角化不可比),
10
10
  * 守卫耗尽后回退扇形三角化——保证终止与索引合法,仅病态数据触发
11
- * - 不支持带洞多边形(clipPath/复合路径属 Phase 4 议题,届时评估 earcut;
12
- * 本实现零依赖是为并行开发期不动 lockfile 的取舍)
11
+ * - 不支持带洞多边形(clipPath/复合路径由 pathData 的洞桥接预先合成简单
12
+ * 多边形;本实现零依赖是为并行开发期不动 lockfile 的取舍——批次 415 起
13
+ * 大环依赖 rbush(引擎运行时既有依赖,document 命中索引同源))
14
+ * - 批次 415:大环 rbush 点索引加速。占据检验(每耳候选 × 其余顶点包含
15
+ * 测试)是 O(n²) 主项——PDF 解析的长曲线 path(数千指令 flatten 后数万
16
+ * 顶点)单环全扫可达秒级,多页模板装载建景实测 15~18s 的 97%。索引后每
17
+ * 耳只测耳 bbox±EPS 内的存活顶点:点在耳三角形内/其边界段上必先落于耳
18
+ * bbox——候选集收缩是数学等价而非近似,谓词与失效语义逐字保留,输出与
19
+ * 全扫逐位一致(spec 内置旧实现副本随机对拍锁定)。
20
+ * 病态实录(批次 415 迭代):先试 uniform grid 失败——PDF 路径顶点全分布
21
+ * 在一维轮廓上,均匀 2D 格的边界格密度远超 n/格数 的均匀假设;洞桥接的
22
+ * 细长跨越耳(对角线远端相连)bbox 横跨大半多边形,「格数×平均密度」的
23
+ * 估计式降级两头挨打(不降级=格框架反噬、降级=全扫基数),真机 15.5s
24
+ * 不可救。rbush 查询返回真实覆盖点集而非估计,两种病态同时消解。
13
25
  */
26
+ import RBush from 'rbush';
14
27
  const EPS = 1e-12;
28
+ /** 索引加速阈值:小于该顶点数走全扫(建树成本在小环上不回本) */
29
+ const INDEX_MIN_VERTICES = 96;
15
30
  /** 叉积 (b−a)×(p−a):>0 = p 在 a→b 左侧(屏幕 y 向下时为顺时针侧) */
16
31
  const cross = (ax, ay, bx, by, px, py) => (bx - ax) * (py - ay) - (by - ay) * (px - ax);
17
32
  /** q 落在 a→b 线段范围内(边值 e 为该边叉积,≈0 时按点积区间判定) */
@@ -36,9 +51,30 @@ export const polygonArea = (points) => {
36
51
  }
37
52
  return area / 2;
38
53
  };
54
+ /**
55
+ * 耳候选占据检验(单点):其余顶点严格在耳内、或落在耳边界线段范围内才算
56
+ * 占据。P3-1b 洞桥接引入重合副本顶点与贴边点——早先的含边保守判定把它们
57
+ * 全当占据,整圈无耳耗尽守卫回退扇形(洞被盖住);纯严格内部又放行线段上
58
+ * 的点、产出退化三角。线延长线(段范围外)上的点不阻塞。与耳顶点重合的
59
+ * 点(桥接的 P/M 副本)不构成占据——它就是顶点自身。
60
+ */
61
+ const pointOccupiesEar = (points, qIndex, a, b, c) => {
62
+ const q = points[qIndex];
63
+ if ((Math.abs(q.x - a.x) < EPS && Math.abs(q.y - a.y) < EPS) || (Math.abs(q.x - b.x) < EPS && Math.abs(q.y - b.y) < EPS) || (Math.abs(q.x - c.x) < EPS && Math.abs(q.y - c.y) < EPS)) {
64
+ return false;
65
+ }
66
+ const e1 = cross(a.x, a.y, b.x, b.y, q.x, q.y);
67
+ const e2 = cross(b.x, b.y, c.x, c.y, q.x, q.y);
68
+ const e3 = cross(c.x, c.y, a.x, a.y, q.x, q.y);
69
+ if ((e1 > EPS && e2 > EPS && e3 > EPS) || (e1 < -EPS && e2 < -EPS && e3 < -EPS)) {
70
+ return true;
71
+ }
72
+ return blocksOnSegment(a, b, q, e1) || blocksOnSegment(b, c, q, e2) || blocksOnSegment(c, a, q, e3);
73
+ };
39
74
  /**
40
75
  * 三角化简单多边形,返回三角形索引三元组数组(展开为 [i0,i1,i2, i0,i1,i2, ...])。
41
- * 时间 O(n²) 最坏(耳检验 × 扫描),shape 点数量级(几十~几百)下无感。
76
+ * 时间 O(n²) 最坏(耳检验 × 扫描);≥ INDEX_MIN_VERTICES 顶点自动启用 rbush
77
+ * 点索引,占据检验降为每耳 ~O(耳 bbox 内存活顶点)。
42
78
  */
43
79
  export const triangulatePolygon = (points) => {
44
80
  const n = points.length;
@@ -57,9 +93,34 @@ export const triangulatePolygon = (points) => {
57
93
  // 守卫:耳检验失败轮转上限(自相交多边形可能整圈无耳),耗尽回退扇形
58
94
  let guard = remaining * remaining + 16;
59
95
  let i = 0;
96
+ // rbush 点索引(批次 415):点零尺寸 bbox + 原始下标,load 批建。删点不
97
+ // remove(rbush.remove 清子树重插,逐耳删点是 _all 全树遍历灾难——真机
98
+ // 16s 实录)——存活位标记,查询候选过滤,中期冗余 ≤2× 可接受
99
+ let tree = null;
100
+ let items = null;
101
+ let alive = null;
102
+ if (n >= INDEX_MIN_VERTICES) {
103
+ tree = new RBush();
104
+ items = new Array(n);
105
+ for (let k = 0; k < n; k++) {
106
+ const p = points[k];
107
+ items[k] = { minX: p.x, minY: p.y, maxX: p.x, maxY: p.y, v: k };
108
+ }
109
+ tree.load(items);
110
+ alive = new Uint8Array(n).fill(1);
111
+ }
112
+ // 死锁检测(批次 415 补三):自交环(洞桥接残余形态)上几乎每耳都被占据,
113
+ // 主循环空转到守卫耗尽才兜底——每空转迭代还带一次占据检验,n² 轮转 × O(n)
114
+ // 检验 = O(n³),千点级单环实测秒级(多页 PDF 模板装载 15s 的真凶)。
115
+ // 等价提前退出:转满一圈(remaining 次迭代)无任何删点 ⟹ (order, remaining,
116
+ // i) 状态完全复原(无删点时每迭代 i++,环绕归位)⟹ 恒空转到守卫耗尽走同
117
+ // 一兜底——此处直接 break 进兜底,输出逐位一致
118
+ let staleIterations = 0;
60
119
  while (remaining > 3 && guard-- > 0) {
61
120
  if (i >= remaining)
62
121
  i = 0;
122
+ if (++staleIterations > remaining)
123
+ break;
63
124
  const i0 = order[i];
64
125
  const i1 = order[(i + 1) % remaining];
65
126
  const i2 = order[(i + 2) % remaining];
@@ -68,40 +129,55 @@ export const triangulatePolygon = (points) => {
68
129
  const c = points[i2];
69
130
  const crossVal = cross(a.x, a.y, b.x, b.y, c.x, c.y);
70
131
  if (crossVal > EPS) {
71
- // 凸耳候选:其余顶点严格在耳内、或落在耳边界线段范围内才算占据。P3-1b
72
- // 洞桥接引入重合副本顶点与贴边点——早先的含边保守判定把它们全当占据,
73
- // 整圈无耳耗尽守卫回退扇形(洞被盖住);纯严格内部又放行线段上的点、
74
- // 产出退化三角。线延长线(段范围外)上的点不阻塞
132
+ // 凸耳候选:其余存活顶点均不占据才剪
75
133
  let occupied = false;
76
- for (let j = 3; j < remaining; j++) {
77
- const q = points[order[(i + j) % remaining]];
78
- // 与耳顶点重合的点(桥接的 P/M 副本)不构成占据——它就是顶点自身
79
- if ((Math.abs(q.x - a.x) < EPS && Math.abs(q.y - a.y) < EPS) || (Math.abs(q.x - b.x) < EPS && Math.abs(q.y - b.y) < EPS) || (Math.abs(q.x - c.x) < EPS && Math.abs(q.y - c.y) < EPS)) {
80
- continue;
134
+ if (tree && items) {
135
+ // 耳 bbox±EPS 查询:占据者(严格内部或骑边)必在其中(rbush 精确
136
+ // bbox 相交,无均匀密度假设——细长耳/轮廓密集分布两病态同解);
137
+ // 骑边恰在边界的浮点余量由 ±EPS 承接,与谓词自身容差同口径
138
+ const earMinX = Math.min(a.x, b.x, c.x) - EPS;
139
+ const earMaxX = Math.max(a.x, b.x, c.x) + EPS;
140
+ const earMinY = Math.min(a.y, b.y, c.y) - EPS;
141
+ const earMaxY = Math.max(a.y, b.y, c.y) + EPS;
142
+ const hits = tree.search({ minX: earMinX, minY: earMinY, maxX: earMaxX, maxY: earMaxY });
143
+ for (let h = 0; h < hits.length; h++) {
144
+ const v = hits[h].v;
145
+ if (!alive[v])
146
+ continue;
147
+ if (v === i0 || v === i1 || v === i2)
148
+ continue;
149
+ if (pointOccupiesEar(points, v, a, b, c)) {
150
+ occupied = true;
151
+ break;
152
+ }
81
153
  }
82
- const e1 = cross(a.x, a.y, b.x, b.y, q.x, q.y);
83
- const e2 = cross(b.x, b.y, c.x, c.y, q.x, q.y);
84
- const e3 = cross(c.x, c.y, a.x, a.y, q.x, q.y);
85
- if ((e1 > EPS && e2 > EPS && e3 > EPS) || (e1 < -EPS && e2 < -EPS && e3 < -EPS)) {
86
- occupied = true;
87
- break;
88
- }
89
- if (blocksOnSegment(a, b, q, e1) || blocksOnSegment(b, c, q, e2) || blocksOnSegment(c, a, q, e3)) {
90
- occupied = true;
91
- break;
154
+ }
155
+ else {
156
+ for (let j = 3; j < remaining; j++) {
157
+ const v = order[(i + j) % remaining];
158
+ if (pointOccupiesEar(points, v, a, b, c)) {
159
+ occupied = true;
160
+ break;
161
+ }
92
162
  }
93
163
  }
94
164
  if (!occupied) {
95
165
  triangles.push(i0, i1, i2);
96
- order.splice((i + 1) % remaining, 1);
166
+ const removed = order.splice((i + 1) % remaining, 1)[0];
97
167
  remaining--;
168
+ if (alive)
169
+ alive[removed] = 0;
170
+ staleIterations = 0;
98
171
  continue;
99
172
  }
100
173
  }
101
174
  else if (crossVal >= -EPS) {
102
175
  // 共线零面积顶点:直接剔除,防止扫描卡死
103
- order.splice((i + 1) % remaining, 1);
176
+ const removed = order.splice((i + 1) % remaining, 1)[0];
104
177
  remaining--;
178
+ if (alive)
179
+ alive[removed] = 0;
180
+ staleIterations = 0;
105
181
  continue;
106
182
  }
107
183
  i++;
@@ -13,7 +13,7 @@
13
13
  import { objectCenter } from '../core/mat2d';
14
14
  import { AnimationPlayer, buildSequentialPlayback, buildSinglePreviewPlan, computeTotalDuration, evaluateSequentialAt, evaluateScrollTextOffset, evaluateSinglePreviewAt, getScrollTextDuration, readSerializedAnimates } from '../model/animation';
15
15
  import { WORKSPACE_ID } from '../model/scene';
16
- import { AnimatedNode, GroupNode, TextNode } from '../model/scene';
16
+ import { AnimatedNode, GroupNode, MaskNode, TextNode } from '../model/scene';
17
17
  import { scrollTrackInput } from '../render/scrollText';
18
18
  export class AnimationPlaybackController {
19
19
  host;
@@ -55,6 +55,8 @@ export class AnimationPlaybackController {
55
55
  for (const node of this.host.document.nodes) {
56
56
  if (node instanceof AnimatedNode)
57
57
  node.play();
58
+ else if (node instanceof MaskNode)
59
+ node.playClipMedia();
58
60
  }
59
61
  this.syncVideosToTime(this.player.time, true);
60
62
  this.applyScrollTextOffsets(this.player.time);
@@ -65,6 +67,8 @@ export class AnimationPlaybackController {
65
67
  for (const node of this.host.document.nodes) {
66
68
  if (node instanceof AnimatedNode)
67
69
  node.pause();
70
+ else if (node instanceof MaskNode)
71
+ node.pauseClipMedia();
68
72
  }
69
73
  this.host.requestRenderAll();
70
74
  }
@@ -80,6 +84,8 @@ export class AnimationPlaybackController {
80
84
  for (const node of this.host.document.nodes) {
81
85
  if (node instanceof AnimatedNode)
82
86
  node.stop();
87
+ else if (node instanceof MaskNode)
88
+ node.syncClipMediaToTime(0, false);
83
89
  }
84
90
  this.host.requestRenderAll();
85
91
  }
@@ -322,6 +328,12 @@ export class AnimationPlaybackController {
322
328
  for (const n of nodes) {
323
329
  if (n instanceof AnimatedNode)
324
330
  durations.push(n.mediaDuration);
331
+ else if (n instanceof MaskNode) {
332
+ // 蒙版动态 clip 视频(VideoPlay 预览时间轴):时长缺席则总时长 0,
333
+ // 播放立即 ended、进度条不可拖(用户报「视频蒙版预览不显示」的连坐面)
334
+ if (n.clipMediaDuration > 0)
335
+ durations.push(n.clipMediaDuration);
336
+ }
325
337
  else if (n instanceof GroupNode)
326
338
  collect(n.children);
327
339
  }
@@ -335,6 +347,8 @@ export class AnimationPlaybackController {
335
347
  for (const n of nodes) {
336
348
  if (n instanceof AnimatedNode)
337
349
  n.syncVideoToTime(time, playing);
350
+ else if (n instanceof MaskNode)
351
+ n.syncClipMediaToTime(time, playing);
338
352
  else if (n instanceof GroupNode)
339
353
  walk(n.children);
340
354
  }
@@ -102,6 +102,11 @@ export class TextEditingController {
102
102
  const s = Math.min(sel.start, sel.end);
103
103
  const e = Math.max(sel.start, sel.end);
104
104
  const nodeMatrix = node.matrix;
105
+ // 屏幕恒定宽/厚的世界量除数:节点矩阵 x 向线缩放(√(m0²+m1²),旋转载荷下
106
+ // 仍为纯缩放长度)。表格格内编辑 overlay 继承表级 scaleX/scaleY——不除尽
107
+ // 时表放大 N 倍光标屏幕宽随之 N 倍(「合并格输入光标非常粗」报障根因;
108
+ // 合并只是把用户带进格内编辑,普通格同粗)。k=0(退化矩阵)兜 1 防除零
109
+ const nodeScaleX = Math.hypot(nodeMatrix[0], nodeMatrix[1]) || 1;
105
110
  const quads = [];
106
111
  for (const line of layout.lines) {
107
112
  const ls = line.styleMap.flatStart;
@@ -128,8 +133,9 @@ export class TextEditingController {
128
133
  const coord = node.cursorCoord(s);
129
134
  if (coord) {
130
135
  // 屏幕 2px 恒定宽(px/zoom,selectionOverlay 同约定)——不可设世界尺寸
131
- // 下限:高倍 zoom 下 2/zoom<1 被钳位后屏幕宽 = zoom px,光标随缩放变粗
132
- const caretWidth = 2 / this.host.zoom;
136
+ // 下限:高倍 zoom 下 2/zoom<1 被钳位后屏幕宽 = zoom px,光标随缩放变粗。
137
+ // 再除节点矩阵缩放:quad 经 nodeMatrix 入屏,缩放不除尽则屏幕宽=2×scaleX
138
+ const caretWidth = 2 / this.host.zoom / nodeScaleX;
133
139
  quads.push({
134
140
  matrix: multiply(nodeMatrix, [1, 0, 0, 1, coord.x + caretWidth / 2, coord.y + coord.height / 2]),
135
141
  dims: { x: caretWidth, y: coord.height },
@@ -154,7 +160,7 @@ export class TextEditingController {
154
160
  const underlineY = baselineY + node.textStyle.fontSize * 0.14;
155
161
  quads.push({
156
162
  matrix: multiply(nodeMatrix, [1, 0, 0, 1, (x0 + x1) / 2, underlineY]),
157
- dims: { x: Math.max(0, x1 - x0), y: 1.5 / this.host.zoom },
163
+ dims: { x: Math.max(0, x1 - x0), y: 1.5 / this.host.zoom / nodeScaleX },
158
164
  color: CARET_COLOR
159
165
  });
160
166
  }
@@ -469,8 +469,16 @@ export declare const isStrokeLineNode: (node: SceneNode) => boolean;
469
469
  */
470
470
  export declare class PathNode extends ShapeNode {
471
471
  private _pathGeo;
472
+ /** 填充多边形(洞合成)与三角网格:三层懒(批次 415)——建景只付
473
+ * parse+flatten+dims(dims/命中必需);洞合成与三角化是 PDF 长 path 的
474
+ * 秒级主项,懒到首渲染(shapeMesh 读点全在渲染/烘焙期)。失效键与
475
+ * _pathGeo 同源(path/fillRule),一次清三层 */
476
+ private _fillPolys;
477
+ private _pathMesh;
472
478
  set(props: Partial<SerializedNode>): void;
473
479
  private get pathGeometry();
480
+ /** 洞合成(桥接多边形集):纯函数重算可缓存,raw.fillRule 读位同旧实现 */
481
+ private get fillPolygons();
474
482
  get shapeMesh(): ShapeMesh | null;
475
483
  get dims(): {
476
484
  x: number;
@@ -821,7 +829,8 @@ export declare class MaskNode extends SceneNode {
821
829
  private _clipImageEl;
822
830
  constructor(raw: SerializedNode);
823
831
  /** clipImage 源元素(加载完成挂载;dims 恒由序列化 width/height 决定,
824
- * 挂载不触几何失效——烘焙缓存签名含 src 自然失效) */
832
+ * 挂载不触几何失效——烘焙缓存签名含 src 自然失效;保留式帧染脏仍须显式
833
+ * 登记——装载期外(reloadMaskClipImage 运行时换图)快照重放不会重画本节点) */
825
834
  get clipImageEl(): HTMLImageElement | null;
826
835
  set clipImageEl(img: HTMLImageElement | null);
827
836
  /** clipImage 的序列化声明(无 src 的坏声明按无填充图处理) */
@@ -854,6 +863,17 @@ export declare class MaskNode extends SceneNode {
854
863
  /** 释放动态源(场景重建/dispose):视频暂停即可(元素由 SourceManager 建、
855
864
  * 场景重建即弃;不 pause 会后台占解码流);精灵图共享缓存不释放 */
856
865
  detachDynamicClip(): void;
866
+ /** clip 视频时长(秒;未挂载/非视频 0——时间轴总时长与 seek 钳制消费)。
867
+ * 元素未挂载时回落序列化 duration(装载链 fire-and-forget,play() 常早于
868
+ * attach——无回落则总时长 0、播放立即 ended,VideoPlay 预览时间轴瘫痪) */
869
+ get clipMediaDuration(): number;
870
+ /** clip 视频续播(playAnimations 编排;attach 时的自动播不经过时间轴) */
871
+ playClipMedia(): void;
872
+ /** clip 视频暂停(pauseAnimations 编排) */
873
+ pauseClipMedia(): void;
874
+ /** clip 视频对齐时间轴时刻(seekAnimations/播放对齐;镜像 syncVideoToTime:
875
+ * 先停再定位,playing 决定是否续播——loop 由元素自身 loop 属性承载) */
876
+ syncClipMediaToTime(time: number, playing: boolean): void;
857
877
  }
858
878
  /**
859
879
  * 拼图节点(P4-5b(2/2)):应用 FabricPuzzle 的引擎侧同构(GroupNode 子类)。
@@ -1017,29 +1017,15 @@ const buildPolygonMesh = (rawPoints) => {
1017
1017
  return idx.length > 0 ? { verts, triangles: new Uint32Array(idx) } : null;
1018
1018
  };
1019
1019
  /**
1020
- * 路径数据 → 中心系网格(P3-6a):parsePathData → flatten →
1021
- * contoursToFillPolygons(按序列化 fillRule,缺省 nonzero——fabric Path 默认)
1022
- * → 逐多边形耳切 → 合并。顶点 − bbox 中心 = fabric pathOffset 语义
1023
- * (_calcBoundsFromPath 精确极值中心)。
1020
+ * 桥接多边形集 → 中心系三角网格(ShapeMesh)。退化输入返回 null。
1021
+ * 批次 415 自 buildPathGeometry 拆出:顶点平铺 + 逐多边形耳切 + 偏移合并。
1024
1022
  */
1025
- const buildPathGeometry = (rawPath, fillRule) => {
1026
- const commands = parsePathData(rawPath);
1027
- if (!commands)
1028
- return { mesh: null, contours: [], dims: { x: 0, y: 0 } };
1029
- const bounds = getBoundsOfPath(commands);
1030
- const contours = flattenPathCommands(commands);
1031
- if (!bounds)
1032
- return { mesh: null, contours, dims: { x: 0, y: 0 } };
1033
- const cx = (bounds.left + bounds.right) / 2;
1034
- const cy = (bounds.top + bounds.bottom) / 2;
1035
- // 轮廓平移到中心系(命中按 canvas fill 偶奇/非零口径在原始轮廓上算,中心系等价)
1036
- const centered = contours.map((c) => ({ closed: c.closed, points: c.points.map((p) => ({ x: p.x - cx, y: p.y - cy })) }));
1037
- const polys = contoursToFillPolygons(centered, fillRule);
1023
+ const buildFillMesh = (polys) => {
1038
1024
  let vertCount = 0;
1039
1025
  for (const poly of polys)
1040
1026
  vertCount += poly.length;
1041
1027
  if (vertCount < 3)
1042
- return { mesh: null, contours: centered, dims: { x: bounds.right - bounds.left, y: bounds.bottom - bounds.top } };
1028
+ return null;
1043
1029
  const verts = new Float32Array(vertCount * 2);
1044
1030
  const tris = [];
1045
1031
  let offset = 0;
@@ -1052,11 +1038,27 @@ const buildPathGeometry = (rawPath, fillRule) => {
1052
1038
  tris.push(t + offset);
1053
1039
  offset += poly.length;
1054
1040
  }
1055
- return {
1056
- mesh: tris.length > 0 ? { verts, triangles: new Uint32Array(tris) } : null,
1057
- contours: centered,
1058
- dims: { x: bounds.right - bounds.left, y: bounds.bottom - bounds.top }
1059
- };
1041
+ return tris.length > 0 ? { verts, triangles: new Uint32Array(tris) } : null;
1042
+ };
1043
+ /**
1044
+ * 路径数据 → 建景期几何(P3-6a):parsePathData → flatten → 中心系轮廓。
1045
+ * 顶点 − bbox 中心 = fabric pathOffset 语义(_calcBoundsFromPath 精确极值中心)。
1046
+ * 填充多边形/网格的生成链(contoursToFillPolygons → 耳切)见 buildFillMesh——
1047
+ * 批次 415 起 lazy 到首渲染,建景不再为其付费。
1048
+ */
1049
+ const buildPathGeometry = (rawPath) => {
1050
+ const commands = parsePathData(rawPath);
1051
+ if (!commands)
1052
+ return { contours: [], dims: { x: 0, y: 0 } };
1053
+ const bounds = getBoundsOfPath(commands);
1054
+ const contours = flattenPathCommands(commands);
1055
+ if (!bounds)
1056
+ return { contours, dims: { x: 0, y: 0 } };
1057
+ const cx = (bounds.left + bounds.right) / 2;
1058
+ const cy = (bounds.top + bounds.bottom) / 2;
1059
+ // 轮廓平移到中心系(命中按 canvas fill 偶奇/非零口径在原始轮廓上算,中心系等价)
1060
+ const centered = contours.map((c) => ({ closed: c.closed, points: c.points.map((p) => ({ x: p.x - cx, y: p.y - cy })) }));
1061
+ return { contours: centered, dims: { x: bounds.right - bounds.left, y: bounds.bottom - bounds.top } };
1060
1062
  };
1061
1063
  /**
1062
1064
  * 填充形状节点(Phase 3 P3-1):ellipse/circle/polygon/polyline。
@@ -1319,27 +1321,44 @@ const polylineStrokeHit = (rawPoints, local, closed, r) => {
1319
1321
  */
1320
1322
  export class PathNode extends ShapeNode {
1321
1323
  _pathGeo;
1324
+ /** 填充多边形(洞合成)与三角网格:三层懒(批次 415)——建景只付
1325
+ * parse+flatten+dims(dims/命中必需);洞合成与三角化是 PDF 长 path 的
1326
+ * 秒级主项,懒到首渲染(shapeMesh 读点全在渲染/烘焙期)。失效键与
1327
+ * _pathGeo 同源(path/fillRule),一次清三层 */
1328
+ _fillPolys;
1329
+ _pathMesh;
1322
1330
  set(props) {
1323
1331
  super.set(props);
1324
- // 路径几何化(parse+flatten+三角化)只依赖 path/fillRule(批次 221);
1332
+ // 路径几何化(parse+flatten/洞合成/三角化)只依赖 path/fillRule(批次 221);
1325
1333
  // 纯姿态写入(拖拽/缩放手势)保缓存——万级 stress 多选 50% path 的主卡顿源。
1326
1334
  // 批次 337 收窄为按键存在性精确失效:app setCanvasObject 的选中链惰性规范化
1327
1335
  // 键(setObjectEffects 每帧直写 effectFill/effectStroke/effectShadow、
1328
1336
  // maskURL——TextNode 位图转正防线已同族豁免)不参与几何,但「非姿态键即
1329
1337
  // 失效」的旧口径把它们全算进去了——1565 子路径级 PSD 网格 path 单次重建
1330
1338
  // ~500ms,拖拽/抬手被逐帧重算(真机 LoAF onUp 481ms 主源)
1331
- if ('path' in props || 'fillRule' in props)
1339
+ if ('path' in props || 'fillRule' in props) {
1332
1340
  this._pathGeo = undefined;
1341
+ this._fillPolys = undefined;
1342
+ this._pathMesh = undefined;
1343
+ }
1333
1344
  }
1334
1345
  get pathGeometry() {
1335
- if (this._pathGeo === undefined) {
1346
+ if (this._pathGeo === undefined)
1347
+ this._pathGeo = buildPathGeometry(this.raw.path);
1348
+ return this._pathGeo;
1349
+ }
1350
+ /** 洞合成(桥接多边形集):纯函数重算可缓存,raw.fillRule 读位同旧实现 */
1351
+ get fillPolygons() {
1352
+ if (this._fillPolys === undefined) {
1336
1353
  const rule = this.raw.fillRule === 'evenodd' ? 'evenodd' : 'nonzero';
1337
- this._pathGeo = buildPathGeometry(this.raw.path, rule);
1354
+ this._fillPolys = contoursToFillPolygons(this.pathGeometry.contours, rule);
1338
1355
  }
1339
- return this._pathGeo;
1356
+ return this._fillPolys;
1340
1357
  }
1341
1358
  get shapeMesh() {
1342
- return this.pathGeometry.mesh;
1359
+ if (this._pathMesh === undefined)
1360
+ this._pathMesh = buildFillMesh(this.fillPolygons);
1361
+ return this._pathMesh;
1343
1362
  }
1344
1363
  get dims() {
1345
1364
  // 几何化缓存直取(批次 221):此前每次访问都 parsePathData+getBoundsOfPath——
@@ -1572,8 +1591,11 @@ export class AnimatedNode extends SceneNode {
1572
1591
  this._anchorTime = performance.now();
1573
1592
  this._playing = sprite.framesLength > 1;
1574
1593
  this.blitFrame();
1575
- // dims 兜底从 0 变为帧尺寸,几何缓存随之失效(同 imageEl setter 语义)
1594
+ // dims 兜底从 0 变为帧尺寸,几何缓存随之失效(同 imageEl setter 语义);
1595
+ // 保留式帧染脏:装载链 fire-and-forget,attach 晚于首帧快照时不登记则
1596
+ // 重放永远跳过本节点(蒙版动态 clip 同款配方)
1576
1597
  this.invalidateGeometryCache();
1598
+ this._renderNotify?.();
1577
1599
  }
1578
1600
  /** 挂载视频元素(loadeddata 后由 EngineCanvas 调用):事件接线 + 自动播一遍 */
1579
1601
  attachVideo(el, onNewFrame) {
@@ -1598,6 +1620,8 @@ export class AnimatedNode extends SceneNode {
1598
1620
  }
1599
1621
  });
1600
1622
  this.invalidateGeometryCache();
1623
+ // 保留式帧染脏(同 attachSprite 批注:attach 晚于首帧快照的静态场景防冻结)
1624
+ this._renderNotify?.();
1601
1625
  el.play().catch(() => {
1602
1626
  /* 自动播放被策略拒绝(muted+playsInline 下罕见):定格首帧 */
1603
1627
  });
@@ -1715,7 +1739,11 @@ export class AnimatedNode extends SceneNode {
1715
1739
  get mediaDuration() {
1716
1740
  if (this.animatedKind === 'video') {
1717
1741
  const d = this._videoEl?.duration;
1718
- return d && Number.isFinite(d) ? d : 0;
1742
+ if (d && Number.isFinite(d))
1743
+ return d;
1744
+ // 序列化 duration 回落(挂载前 play() 同款口径,见 MaskNode.clipMediaDuration)
1745
+ const raw = Number(this.raw.duration);
1746
+ return Number.isFinite(raw) && raw > 0 ? raw : 0;
1719
1747
  }
1720
1748
  const sprite = this._sprite;
1721
1749
  if (!sprite)
@@ -2251,12 +2279,14 @@ export class MaskNode extends SceneNode {
2251
2279
  super(raw);
2252
2280
  }
2253
2281
  /** clipImage 源元素(加载完成挂载;dims 恒由序列化 width/height 决定,
2254
- * 挂载不触几何失效——烘焙缓存签名含 src 自然失效) */
2282
+ * 挂载不触几何失效——烘焙缓存签名含 src 自然失效;保留式帧染脏仍须显式
2283
+ * 登记——装载期外(reloadMaskClipImage 运行时换图)快照重放不会重画本节点) */
2255
2284
  get clipImageEl() {
2256
2285
  return this._clipImageEl;
2257
2286
  }
2258
2287
  set clipImageEl(img) {
2259
2288
  this._clipImageEl = img;
2289
+ this._renderNotify?.();
2260
2290
  }
2261
2291
  /** clipImage 的序列化声明(无 src 的坏声明按无填充图处理) */
2262
2292
  get clipImageRaw() {
@@ -2312,6 +2342,11 @@ export class MaskNode extends SceneNode {
2312
2342
  this._clipPlaying = sprite.framesLength > 1;
2313
2343
  this.clipDynamicVersion++;
2314
2344
  this.blitClipFrame();
2345
+ // 保留式帧染脏:装载链 fire-and-forget(SwiftShader 准死锁口径),attach 常
2346
+ // 晚于首帧快照——不登记则快照重放永远跳过本节点(VideoPlay 静态预览蒙版
2347
+ // 不显示的根因)。重推一次后:播放中 clipAnimating → animationActive 门
2348
+ // 自续全量帧;静止即定格新帧内容
2349
+ this._renderNotify?.();
2315
2350
  }
2316
2351
  /** 挂载视频元素:seeked/pause 补传帧版本 + 自动播(应用 renderDynamicElement
2317
2352
  * 的 videoElement.play 同语义;muted+playsInline 由 SourceManager 建) */
@@ -2320,12 +2355,16 @@ export class MaskNode extends SceneNode {
2320
2355
  this._clipOnNewFrame = onNewFrame ?? null;
2321
2356
  const markFrame = () => {
2322
2357
  this.clipDynamicVersion++;
2358
+ // play/pause/seeked 事件驱动的帧版本变化同样须染脏——静态期(快照有效、
2359
+ // 非播放中)事件到达时无登记则画面钉在旧帧
2360
+ this._renderNotify?.();
2323
2361
  this._clipOnNewFrame?.();
2324
2362
  };
2325
2363
  el.addEventListener('seeked', markFrame);
2326
2364
  el.addEventListener('pause', markFrame);
2327
2365
  el.addEventListener('play', markFrame);
2328
2366
  this.clipDynamicVersion++;
2367
+ this._renderNotify?.();
2329
2368
  el.play().catch(() => {
2330
2369
  /* 自动播放被策略拒绝(muted+playsInline 下罕见):定格首帧 */
2331
2370
  });
@@ -2390,6 +2429,55 @@ export class MaskNode extends SceneNode {
2390
2429
  this._clipVideoEl = null;
2391
2430
  this._clipSprite = null;
2392
2431
  this._clipFrameCanvas = null;
2432
+ this._renderNotify?.();
2433
+ }
2434
+ // ── 蒙版动态 clip 的播放编排面(VideoPlay 预览时间轴接入)──
2435
+ // 口径镜像 AnimatedNode.play/pause/syncVideoToTime/mediaDuration:仅视频
2436
+ //(GIF 帧机由渲染期 tick + animationActive 自续排驱动,编排器无需接管其
2437
+ // 播放态;时长计入同 AnimatedNode gif 的 sprite 口径)
2438
+ /** clip 视频时长(秒;未挂载/非视频 0——时间轴总时长与 seek 钳制消费)。
2439
+ * 元素未挂载时回落序列化 duration(装载链 fire-and-forget,play() 常早于
2440
+ * attach——无回落则总时长 0、播放立即 ended,VideoPlay 预览时间轴瘫痪) */
2441
+ get clipMediaDuration() {
2442
+ if (this.clipDynamicKind !== 'video')
2443
+ return 0;
2444
+ const d = this._clipVideoEl?.duration;
2445
+ if (d && Number.isFinite(d))
2446
+ return d;
2447
+ const raw = Number(this.clipImageRaw?.duration);
2448
+ return Number.isFinite(raw) && raw > 0 ? raw : 0;
2449
+ }
2450
+ /** clip 视频续播(playAnimations 编排;attach 时的自动播不经过时间轴) */
2451
+ playClipMedia() {
2452
+ if (this.clipDynamicKind !== 'video')
2453
+ return;
2454
+ this._clipVideoEl?.play().catch(() => undefined);
2455
+ }
2456
+ /** clip 视频暂停(pauseAnimations 编排) */
2457
+ pauseClipMedia() {
2458
+ if (this.clipDynamicKind !== 'video')
2459
+ return;
2460
+ this._clipVideoEl?.pause();
2461
+ }
2462
+ /** clip 视频对齐时间轴时刻(seekAnimations/播放对齐;镜像 syncVideoToTime:
2463
+ * 先停再定位,playing 决定是否续播——loop 由元素自身 loop 属性承载) */
2464
+ syncClipMediaToTime(time, playing) {
2465
+ if (this.clipDynamicKind !== 'video')
2466
+ return;
2467
+ const el = this._clipVideoEl;
2468
+ if (!el)
2469
+ return;
2470
+ el.pause();
2471
+ const duration = el.duration;
2472
+ const next = Number.isFinite(duration) && duration > 0 ? Math.min(Math.max(time, 0), duration) : Math.max(time, 0);
2473
+ try {
2474
+ el.currentTime = next;
2475
+ }
2476
+ catch {
2477
+ /* noop */
2478
+ }
2479
+ if (playing)
2480
+ el.play().catch(() => undefined);
2393
2481
  }
2394
2482
  }
2395
2483
  /**
@@ -1,3 +1,17 @@
1
+ /**
2
+ * 场景渲染器(从 EngineCanvas 拆出,main-webgl2 重构;烘焙族再外拆)。
3
+ *
4
+ * 本文件是逐节点渲染分派核心(组递归/clipPath 隔离/文本/形状/线族/动画族/
5
+ * 图片族)与组隔离(FBO 合成)+ 拼图钳制状态。四族烘焙已外拆为独立画家
6
+ * (各自持 WeakMap 缓存与失效口径):ShadowPainter(原生阴影)/
7
+ * PsdEffectPainter(PS 图层样式)/ StrokePainter(描边)/ ClipMaskPainter
8
+ * (clipPath 剪影);双消费的 GPU 画家(GpuFxPainter/GpuImageFilterChain)
9
+ * 归 SharedGpuPainters 共享持有(防双实例重复占显存)。跨层读取(zoom/dpr/
10
+ * vpt/画布尺寸/编辑态)经 SceneRendererHost 注入;帧级状态(PSD 烘焙预算、
11
+ * 动画自续标记)挂在 frame 上,由 EngineCanvas.render 每帧 beginFrame 复位、
12
+ * 渲染尾部消费——不做参数穿透(renderSceneNode↔renderIsolatedGroup 互递归)。
13
+ */
14
+ import { parseColor } from '../core/color';
1
15
  import { renderViewportOf } from './renderViewport';
2
16
  import { gradientSpaceMatrix } from '../core/gradient';
3
17
  import { patternSpaceMatrix, repeatFlags } from '../core/pattern';
@@ -642,7 +656,13 @@ export class SceneRenderer {
642
656
  if (!(x > 0) || !(y > 0))
643
657
  return;
644
658
  const opacity = node.renderOpacity;
645
- this.host.batch.pushQuad(node.matrix, x, y, [IMAGE_PLACEHOLDER_COLOR[0], IMAGE_PLACEHOLDER_COLOR[1], IMAGE_PLACEHOLDER_COLOR[2], IMAGE_PLACEHOLDER_COLOR[3] * opacity], null, [0, 0, 1, 1], this.quadRadii(node), node);
659
+ // 批次 415 补四(用户点名):占位优先节点自带 fill——PDF 解析的图片对象
660
+ // 恒带 fill(图片区域终态由位图呈现,fill 仅装载期占位借色,图源就绪即被
661
+ // 位图替换);透明/缺省/病态形态(批次 341 毒化守卫→透明)回落中性灰。
662
+ // parseColor 的非串守卫使渐变/图案对象形态安全走回落
663
+ const fill = parseColor(node.raw.fill);
664
+ const base = fill[3] > 0 ? fill : IMAGE_PLACEHOLDER_COLOR;
665
+ this.host.batch.pushQuad(node.matrix, x, y, [base[0], base[1], base[2], base[3] * opacity], null, [0, 0, 1, 1], this.quadRadii(node), node);
646
666
  }
647
667
  /**
648
668
  * quad 圆角解析(批次 98):rect 族走 rectCornerRadii(FabricRect 族 radius
@@ -20,6 +20,7 @@
20
20
  */
21
21
  import { effectiveShadow } from '../core/shadow';
22
22
  import { fillStyleForNode, traceShapeStrokePath } from './strokeBake';
23
+ import { suspendScissor } from './screenClip';
23
24
  /** 全屏三角(与 gpuShadow 相同的 trick,自持一份避免批次 1 回归面) */
24
25
  const VERT_TRI = `#version 300 es
25
26
  out vec2 vUv;
@@ -468,6 +469,11 @@ export class GpuFxPainter {
468
469
  this.maskMsaa = { fb, rb, samples, w, h };
469
470
  }
470
471
  const msaa = this.maskMsaa;
472
+ // MSAA 解析中转 RT:纯分配(无绘制),前移到绘制段之前——绘制段起豁
473
+ // 页剪裁后中间不再有 return 路径(免豁免泄漏,批次 414)
474
+ const resolveRt = this.ensureTarget(gl, 6, w, h);
475
+ if (!resolveRt)
476
+ return null;
471
477
  // 网格 VBO(rect 现构;形状族按 ShapeMesh 对象身份缓存——set() 重建即失效)
472
478
  let meshObj = mesh;
473
479
  if (!meshObj && rectDims) {
@@ -477,12 +483,25 @@ export class GpuFxPainter {
477
483
  }
478
484
  if (!meshObj)
479
485
  return null;
486
+ // ELEMENT_ARRAY_BUFFER 绑定属于 VAO 状态;首次上传网格前就隔离 VAO,
487
+ // 否则会覆盖调用方 QuadBatch 的 Uint16 索引绑定,后续图片退化为零面积三角形。
488
+ const vao = gl.createVertexArray();
489
+ if (!vao)
490
+ return null;
491
+ gl.bindVertexArray(vao);
480
492
  let bufs = this.meshBufs.get(meshObj);
481
493
  if (!bufs) {
482
494
  const vbo = gl.createBuffer();
483
495
  const ibo = gl.createBuffer();
484
- if (!vbo || !ibo)
496
+ if (!vbo || !ibo) {
497
+ if (vbo)
498
+ gl.deleteBuffer(vbo);
499
+ if (ibo)
500
+ gl.deleteBuffer(ibo);
501
+ gl.bindVertexArray(null);
502
+ gl.deleteVertexArray(vao);
485
503
  return null;
504
+ }
486
505
  gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
487
506
  gl.bufferData(gl.ARRAY_BUFFER, meshObj.verts, gl.STATIC_DRAW);
488
507
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo);
@@ -490,15 +509,14 @@ export class GpuFxPainter {
490
509
  bufs = { vbo, ibo, count: meshObj.triangles.length };
491
510
  this.meshBufs.set(meshObj, bufs);
492
511
  }
493
- const vao = gl.createVertexArray();
494
- if (!vao)
495
- return null;
496
- gl.bindVertexArray(vao);
497
512
  gl.bindBuffer(gl.ARRAY_BUFFER, bufs.vbo);
498
513
  const loc = gl.getAttribLocation(this.maskProg, 'aPos');
499
514
  gl.enableVertexAttribArray(loc);
500
515
  gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
501
516
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufs.ibo);
517
+ // 页剪裁豁免(批次 414):下方 MSAA 绘制/blit/pass 全在本内容尺寸坐标
518
+ // 系——多页帧 scissor 盒投到这里是随机区域(screenClip 头注)
519
+ const resumeScissor = suspendScissor(gl);
502
520
  gl.bindFramebuffer(gl.FRAMEBUFFER, msaa.fb);
503
521
  gl.viewport(0, 0, w, h);
504
522
  gl.clearColor(0, 0, 0, 0);
@@ -514,9 +532,6 @@ export class GpuFxPainter {
514
532
  // MSAA 解析必须 1:1 blit(缩放解析是 GL 非法操作——曾一步缩放 blit 静默
515
533
  // no-op 致蒙版全空):先 1:1 NEAREST 解析到 2× 中转纹理,再非 MSAA 缩放
516
534
  // blit(LINEAR 下采样)到 1× 蒙版纹理
517
- const resolveRt = this.ensureTarget(gl, 6, w, h);
518
- if (!resolveRt)
519
- return null;
520
535
  gl.bindFramebuffer(gl.READ_FRAMEBUFFER, msaa.fb);
521
536
  gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, resolveRt.fb);
522
537
  gl.blitFramebuffer(0, 0, w, h, 0, 0, w, h, gl.COLOR_BUFFER_BIT, gl.NEAREST);
@@ -534,6 +549,7 @@ export class GpuFxPainter {
534
549
  gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
535
550
  gl.viewport(0, 0, viewportW, viewportH);
536
551
  this.restore(gl);
552
+ resumeScissor();
537
553
  return this.maskRt.tex;
538
554
  }
539
555
  /**
@@ -574,6 +590,9 @@ export class GpuFxPainter {
574
590
  const rt = this.slot(gl, node, options.sig, 'shadow', cw, ch);
575
591
  if (!rt)
576
592
  return null;
593
+ // 页剪裁豁免(批次 414):累积清空起全部 pass 在本内容尺寸坐标系
594
+ //(screenClip 头注;单页/导出态零直通)
595
+ const resumeScissor = suspendScissor(gl);
577
596
  // 清空累积
578
597
  gl.bindFramebuffer(gl.FRAMEBUFFER, accum.fb);
579
598
  gl.viewport(0, 0, cw, ch);
@@ -582,8 +601,14 @@ export class GpuFxPainter {
582
601
  for (const decl of decls) {
583
602
  const eff = pathMode ? { blur: decl.blur, offX: decl.offsetX, offY: decl.offsetY } : effectiveShadow(asDecl(decl), scaleX, scaleY);
584
603
  const blurred = this.blurChain(gl, srcTex, cw, ch, (eff.blur * rs) / 2, 0, 1, 2);
585
- if (!blurred)
604
+ // 出口状态归还(批次 414 一并收口:此处 return 曾泄 fb 绑定/viewport/
605
+ // blend/页剪裁——后续节点成片画错的同类面)
606
+ if (!blurred) {
607
+ gl.viewport(0, 0, options.viewportW, options.viewportH);
608
+ this.restore(gl);
609
+ resumeScissor();
586
610
  return null;
611
+ }
587
612
  // PS④(Normal):阴影层合成(canvas filter drop-shadow 的阴影部分)
588
613
  this.pass(gl, this.tintProg, accum, (p) => {
589
614
  gl.activeTexture(gl.TEXTURE0);
@@ -614,6 +639,7 @@ export class GpuFxPainter {
614
639
  });
615
640
  gl.viewport(0, 0, options.viewportW, options.viewportH);
616
641
  this.restore(gl);
642
+ resumeScissor();
617
643
  return { tex: rt.tex, w: cw, h: ch };
618
644
  }
619
645
  /**
@@ -630,6 +656,8 @@ export class GpuFxPainter {
630
656
  if (!rt)
631
657
  return null;
632
658
  const s = sourceTex;
659
+ // 页剪裁豁免(批次 414):pass 在本内容尺寸坐标系(screenClip 头注)
660
+ const resumeScissor = suspendScissor(gl);
633
661
  this.pass(gl, this.fillProg, rt, (p) => {
634
662
  gl.activeTexture(gl.TEXTURE0);
635
663
  gl.bindTexture(gl.TEXTURE_2D, s);
@@ -638,6 +666,7 @@ export class GpuFxPainter {
638
666
  });
639
667
  gl.viewport(0, 0, viewportW, viewportH);
640
668
  this.restore(gl);
669
+ resumeScissor();
641
670
  return rt.tex;
642
671
  }
643
672
  }
@@ -27,6 +27,7 @@
27
27
  */
28
28
  import { imageFiltersKey } from '../core/imageFilters';
29
29
  import { buildPasses, VERTEX_SOURCE } from './gpuFilterPasses';
30
+ import { suspendScissor } from './screenClip';
30
31
  /** 图片滤镜 GPU 链:程序缓存 + FBO ping-pong + 按节点签名缓存输出纹理 */
31
32
  export class GpuImageFilterChain {
32
33
  gl;
@@ -109,6 +110,10 @@ export class GpuImageFilterChain {
109
110
  if (!texA || !texB || !fboA || !fboB)
110
111
  return null;
111
112
  const quad = this.ensureQuad();
113
+ // 页剪裁豁免(批次 414):下方 pass 循环在本内容尺寸坐标系画进 FBO——
114
+ // 多页帧 scissor 盒投到这里是随机区域;此处起豁、恢复段复位(其间无
115
+ // early return,screenClip 头注)
116
+ const resumeScissor = suspendScissor(gl);
112
117
  let input = sourceTex;
113
118
  let outTex = texA;
114
119
  let outFbo = fboA;
@@ -145,6 +150,7 @@ export class GpuImageFilterChain {
145
150
  }
146
151
  const finalTex = input;
147
152
  // ---- 状态恢复 ----
153
+ resumeScissor();
148
154
  gl.bindVertexArray(prevVao);
149
155
  gl.bindFramebuffer(gl.FRAMEBUFFER, prevFbo);
150
156
  gl.viewport(prevViewport[0], prevViewport[1], prevViewport[2], prevViewport[3]);
@@ -23,6 +23,7 @@ import { effectiveShadow, shadowMargin } from '../core/shadow';
23
23
  import { AnimatedNode } from '../model/scene';
24
24
  import { drawSilhouette, linearExtent } from './shadowBake';
25
25
  import { createTexture2D } from './textureUpload';
26
+ import { suspendScissor } from './screenClip';
26
27
  /** 全屏三角(box/final 两 pass 的几何,vUv 在可见屏幕区恰好插值 0..1) */
27
28
  const VERT_TRI = `#version 300 es
28
29
  out vec2 vUv;
@@ -251,6 +252,9 @@ export class GpuShadowBaker {
251
252
  if (!(w > 0) || !(h > 0) || w > 8192 || h > 8192)
252
253
  return null;
253
254
  const sig = bakeSig(node, decl, w, h);
255
+ // 页剪裁豁免(批次 414):pass 全在本内容尺寸坐标系——多页帧的 scissor
256
+ // 盒投到这里是随机区域,进出豁免(screenClip 头注;单页/导出态零直通)
257
+ const resumeScissor = suspendScissor(gl);
254
258
  try {
255
259
  let entry = this.targets.get(node);
256
260
  if (entry && entry.sig !== sig) {
@@ -317,6 +321,7 @@ export class GpuShadowBaker {
317
321
  // 恢复 blend(QuadBatch 只在构造时开启一次——曾泄漏 disabled 状态)
318
322
  gl.enable(gl.BLEND);
319
323
  gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
324
+ resumeScissor();
320
325
  }
321
326
  }
322
327
  }
@@ -185,6 +185,7 @@ export declare class MultiPageController {
185
185
  }
186
186
  /** 多页分段帧(EngineCanvas.render 的 count>1 分支体;单页路径不经此)。
187
187
  * 相位序对齐单页帧:棋盘格(清屏后首笔,逐页)→ 逐页场景推入(页间
188
- * flushAll + begin 换装)→ 全页脏集出清 → 多洞遮罩 → 活动页覆盖层
189
- * (finishOverlay——OverlayTailHost 注入活动页 doc)。 */
188
+ * flushAll + begin 换装 + 页剪裁 scissor——内容不出页,批次 414)→ 全页
189
+ * 脏集出清 → 多洞遮罩 → 活动页覆盖层(finishOverlay——OverlayTailHost
190
+ * 注入活动页 doc)。 */
190
191
  export declare function renderMultiPageFrame(controller: MultiPageController, host: MultiPageFrameHost): void;
@@ -29,6 +29,7 @@ import { EngineDocument } from '../model/document';
29
29
  import { WORKSPACE_IDS, WORKSPACE_ID } from '../model/scene';
30
30
  import { invert, multiply, sceneToClipMatrix, transformPoint } from '../core/mat2d';
31
31
  import { renderSceneNodes } from './framePipeline';
32
+ import { applyScissor, clearScissor, pageClipRect } from './screenClip';
32
33
  import { buildHoverOverlay } from './selectionOverlay';
33
34
  /**
34
35
  * 页间视觉间隙(CSS 像素,屏幕恒定):2026-10-15 实测 canva.cn 桌面编辑器,
@@ -328,8 +329,9 @@ function nodesBoundingBox(doc) {
328
329
  }
329
330
  /** 多页分段帧(EngineCanvas.render 的 count>1 分支体;单页路径不经此)。
330
331
  * 相位序对齐单页帧:棋盘格(清屏后首笔,逐页)→ 逐页场景推入(页间
331
- * flushAll + begin 换装)→ 全页脏集出清 → 多洞遮罩 → 活动页覆盖层
332
- * (finishOverlay——OverlayTailHost 注入活动页 doc)。 */
332
+ * flushAll + begin 换装 + 页剪裁 scissor——内容不出页,批次 414)→ 全页
333
+ * 脏集出清 → 多洞遮罩 → 活动页覆盖层(finishOverlay——OverlayTailHost
334
+ * 注入活动页 doc)。 */
333
335
  export function renderMultiPageFrame(controller, host) {
334
336
  controller.layout();
335
337
  const inv = invert(host.vpt);
@@ -352,18 +354,32 @@ export function renderMultiPageFrame(controller, host) {
352
354
  host.workspaceBackdrop.drawChecker(host.gl, canvasView, viewportParams, rects);
353
355
  const penScale = host.zoom * host.dpr;
354
356
  const expand = host.expandUnderlay();
355
- for (const page of visible) {
357
+ try {
358
+ for (const page of visible) {
359
+ // 前页延迟命令先出清——彼时 scissor 仍是前页盒(本页盒换装前把前页
360
+ // 命令画完,内容不出页的时序关键:push 期烘焙豁免与隔离边界 flush
361
+ // 也都发生在当前页盒之下)
362
+ host.batchOrder.flushAll();
363
+ // 页剪裁:本页全部绘制(含越界几何/屏幕尺寸隔离混合合成)钳在本页
364
+ // 矩形内——邻页矩形是多洞遮罩的洞,遮罩救不了越界内容(screenClip
365
+ // 头注);零尺寸盒 = 预筛 margin 余量下的视口外沿页,合法全弃置
366
+ applyScissor(host.gl, pageClipRect(page.rect, host.vpt, host.cssWidth, host.cssHeight, host.dpr));
367
+ const pageViewport = controller.pageVpt(host.vpt, page.index);
368
+ const pageView = sceneToClipMatrix(pageViewport, host.cssWidth, host.cssHeight, host.dpr);
369
+ host.batch.begin(pageView, penScale);
370
+ host.meshBatch.begin(pageView);
371
+ host.batch.deferDraws = true;
372
+ host.meshBatch.deferDraws = true;
373
+ host.batchOrder.reset();
374
+ host.withPageViewport(pageViewport, () => renderSceneNodes(host.batch, host.renderer, page.entry.doc.nodes, expand, page.entry.doc.nodes.length));
375
+ }
356
376
  host.batchOrder.flushAll();
357
- const pageViewport = controller.pageVpt(host.vpt, page.index);
358
- const pageView = sceneToClipMatrix(pageViewport, host.cssWidth, host.cssHeight, host.dpr);
359
- host.batch.begin(pageView, penScale);
360
- host.meshBatch.begin(pageView);
361
- host.batch.deferDraws = true;
362
- host.meshBatch.deferDraws = true;
363
- host.batchOrder.reset();
364
- host.withPageViewport(pageViewport, () => renderSceneNodes(host.batch, host.renderer, page.entry.doc.nodes, expand, page.entry.doc.nodes.length));
365
377
  }
366
- host.batchOrder.flushAll();
378
+ finally {
379
+ // 遮罩(全视口灰底+发光)/覆盖层(选框/hover/编辑光标)不裁——单页态
380
+ // 「内容被盖、选框可见」语义的多页推广;finally 保证中途异常不泄全局态
381
+ clearScissor(host.gl);
382
+ }
367
383
  controller.drainAll();
368
384
  host.workspaceBackdrop.drawBackdrop(host.gl, canvasView, viewportParams, rects);
369
385
  const activeView = sceneToClipMatrix(controller.pageVpt(host.vpt, controller.activeIndex), host.cssWidth, host.cssHeight, host.dpr);
@@ -0,0 +1,47 @@
1
+ /**
2
+ * 屏幕域页剪裁(方案 D 多页「内容不出页」,批次 414)。
3
+ *
4
+ * 多页分段帧逐页换装视口但不裁几何——节点越出本页工作区矩形的部分会画进
5
+ * 页间 gap 与邻页矩形;gap 段随后被不透明多洞灰遮罩盖住(不可见),而邻页
6
+ * 矩形是遮罩的洞,越界内容就此漏显在他人画板里(「元素跨两页显示」病灶——
7
+ * 跨页拖入置顶已定归属语义,渲染面须执行同一语义:元素只在归属页可见)。
8
+ * 单页态既有语义本就是「工作区外内容被遮罩盖住、只留覆盖层选框」——本模块
9
+ * 把它推广到多页:页段渲染期开 GL scissor,把本页全部绘制(含越界几何与
10
+ * 隔离/混合全屏合成)钳在本页矩形的帧缓冲像素内;遮罩/覆盖层相位关回。
11
+ *
12
+ * 坐标口径:编辑器 vpt 无旋转,页矩形(绝对场景系)经 vpt 映射为轴对齐
13
+ * CSS 矩形,再 ×dpr、y 翻转(GL 原点左下)得帧缓冲 scissor 盒;外向取整
14
+ * (floor/ceil)保边沿内容像素,与视口求交,空交集 = 零尺寸盒(全弃置,
15
+ * 预筛 margin 余量下的视口外沿页)。
16
+ *
17
+ * 烘焙豁免(suspendScissor):scissor 是上下文级状态,作用于一切帧缓冲的
18
+ * draw/clear/blit/像素拷贝。GPU 烘焙族(GpuShadowBaker/GpuFxPainter 三入口/
19
+ * GpuImageFilterChain)在自身内容坐标系的内容尺寸 FBO 里跑 pass——页矩形投
20
+ * 到那边是随机区域,不豁免即剪坏烘焙纹理。屏幕尺寸 FBO(GLIsolate 组隔离/
21
+ * GLBlend 混合)与页同屏同系:不豁免恰是把组内容一并钳在页内(组越页同裁)
22
+ * ——故不在豁免之列。CPU 烘焙(2D canvas → texImage 上传)不受 scissor 影响
23
+ * 天然豁免(maskRender/strokePainter/shadowPainter CPU 支/psdEffectPainter/
24
+ * GlyphAtlas 全族)。
25
+ */
26
+ import { type Mat } from '../core/mat2d';
27
+ /** 帧缓冲像素矩形(GL scissor 盒;w/h=0 = 空交集全弃置) */
28
+ export interface FbClipRect {
29
+ x: number;
30
+ y: number;
31
+ w: number;
32
+ h: number;
33
+ }
34
+ /** 场景矩形 → 帧缓冲 scissor 盒(vpt 无旋转假设下两角即轴对齐;空交集零尺寸) */
35
+ export declare function pageClipRect(sceneRect: {
36
+ left: number;
37
+ top: number;
38
+ width: number;
39
+ height: number;
40
+ }, vpt: Mat, cssWidth: number, cssHeight: number, dpr: number): FbClipRect;
41
+ /** 开启并设置页剪裁(零尺寸盒合法——本页绘制全弃置) */
42
+ export declare function applyScissor(gl: WebGL2RenderingContext, rect: FbClipRect): void;
43
+ /** 关闭页剪裁(帧尾 finally/遮罩与覆盖层相位前) */
44
+ export declare function clearScissor(gl: WebGL2RenderingContext): void;
45
+ /** 烘焙豁免:记录并关闭 scissor,返回恢复函数(幂等;出口/finally 调用)。
46
+ * 未开启时零成本直通(单页态/导出链恒不开启)。 */
47
+ export declare function suspendScissor(gl: WebGL2RenderingContext): () => void;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * 屏幕域页剪裁(方案 D 多页「内容不出页」,批次 414)。
3
+ *
4
+ * 多页分段帧逐页换装视口但不裁几何——节点越出本页工作区矩形的部分会画进
5
+ * 页间 gap 与邻页矩形;gap 段随后被不透明多洞灰遮罩盖住(不可见),而邻页
6
+ * 矩形是遮罩的洞,越界内容就此漏显在他人画板里(「元素跨两页显示」病灶——
7
+ * 跨页拖入置顶已定归属语义,渲染面须执行同一语义:元素只在归属页可见)。
8
+ * 单页态既有语义本就是「工作区外内容被遮罩盖住、只留覆盖层选框」——本模块
9
+ * 把它推广到多页:页段渲染期开 GL scissor,把本页全部绘制(含越界几何与
10
+ * 隔离/混合全屏合成)钳在本页矩形的帧缓冲像素内;遮罩/覆盖层相位关回。
11
+ *
12
+ * 坐标口径:编辑器 vpt 无旋转,页矩形(绝对场景系)经 vpt 映射为轴对齐
13
+ * CSS 矩形,再 ×dpr、y 翻转(GL 原点左下)得帧缓冲 scissor 盒;外向取整
14
+ * (floor/ceil)保边沿内容像素,与视口求交,空交集 = 零尺寸盒(全弃置,
15
+ * 预筛 margin 余量下的视口外沿页)。
16
+ *
17
+ * 烘焙豁免(suspendScissor):scissor 是上下文级状态,作用于一切帧缓冲的
18
+ * draw/clear/blit/像素拷贝。GPU 烘焙族(GpuShadowBaker/GpuFxPainter 三入口/
19
+ * GpuImageFilterChain)在自身内容坐标系的内容尺寸 FBO 里跑 pass——页矩形投
20
+ * 到那边是随机区域,不豁免即剪坏烘焙纹理。屏幕尺寸 FBO(GLIsolate 组隔离/
21
+ * GLBlend 混合)与页同屏同系:不豁免恰是把组内容一并钳在页内(组越页同裁)
22
+ * ——故不在豁免之列。CPU 烘焙(2D canvas → texImage 上传)不受 scissor 影响
23
+ * 天然豁免(maskRender/strokePainter/shadowPainter CPU 支/psdEffectPainter/
24
+ * GlyphAtlas 全族)。
25
+ */
26
+ import { transformPoint } from '../core/mat2d';
27
+ /** 场景矩形 → 帧缓冲 scissor 盒(vpt 无旋转假设下两角即轴对齐;空交集零尺寸) */
28
+ export function pageClipRect(sceneRect, vpt, cssWidth, cssHeight, dpr) {
29
+ const p1 = transformPoint(vpt, sceneRect.left, sceneRect.top);
30
+ const p2 = transformPoint(vpt, sceneRect.left + sceneRect.width, sceneRect.top + sceneRect.height);
31
+ const x0 = Math.floor(Math.min(p1.x, p2.x) * dpr);
32
+ const x1 = Math.ceil(Math.max(p1.x, p2.x) * dpr);
33
+ const yTop = Math.min(p1.y, p2.y);
34
+ const yBottom = Math.max(p1.y, p2.y);
35
+ // y 翻转:CSS 顶左原点 → GL 底左原点(帧缓冲底边 = cssHeight − 矩形下沿)
36
+ const y0 = Math.floor((cssHeight - yBottom) * dpr);
37
+ const y1 = Math.ceil((cssHeight - yTop) * dpr);
38
+ const cx0 = Math.max(0, x0);
39
+ const cy0 = Math.max(0, y0);
40
+ const cx1 = Math.min(Math.ceil(cssWidth * dpr), x1);
41
+ const cy1 = Math.min(Math.ceil(cssHeight * dpr), y1);
42
+ return { x: cx0, y: cy0, w: Math.max(0, cx1 - cx0), h: Math.max(0, cy1 - cy0) };
43
+ }
44
+ /** 开启并设置页剪裁(零尺寸盒合法——本页绘制全弃置) */
45
+ export function applyScissor(gl, rect) {
46
+ gl.enable(gl.SCISSOR_TEST);
47
+ gl.scissor(rect.x, rect.y, rect.w, rect.h);
48
+ }
49
+ /** 关闭页剪裁(帧尾 finally/遮罩与覆盖层相位前) */
50
+ export function clearScissor(gl) {
51
+ gl.disable(gl.SCISSOR_TEST);
52
+ }
53
+ /** 烘焙豁免:记录并关闭 scissor,返回恢复函数(幂等;出口/finally 调用)。
54
+ * 未开启时零成本直通(单页态/导出链恒不开启)。 */
55
+ export function suspendScissor(gl) {
56
+ const wasEnabled = gl.isEnabled(gl.SCISSOR_TEST);
57
+ if (wasEnabled)
58
+ gl.disable(gl.SCISSOR_TEST);
59
+ let resumed = false;
60
+ return () => {
61
+ if (resumed)
62
+ return;
63
+ resumed = true;
64
+ if (wasEnabled)
65
+ gl.enable(gl.SCISSOR_TEST);
66
+ };
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yft-design/engine",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "description": "yft-design 的 WebGL2 画布引擎:接受 fabric toObject 格式 JSON,输出 GL 画布 / 位图(任意倍率 FBO 读回)/ SVG(纯数据序列化器)",
5
5
  "repository": {
6
6
  "type": "git",