@yft-design/engine 0.0.4 → 0.0.5

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.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * 单应性(透视变换)数学——智能对象置入层 quad 专用(批次 363 自主仓
3
+ * extension/object/FabricSmartObject.ts 迁居,逐式 verbatim)。
4
+ *
5
+ * PSD placed layer 的 smartObject 元数据携带 8 元组 quad(nonAffineTransform
6
+ * 优先、transform 兜底),描述子文档在父文档中的四角投射——超出仿射的透视
7
+ * 形变是渲染域词汇:后续 PS 智能对象新形态(变形/网格变换,warpvas 同族)
8
+ * 与 GPU 原生透视渲染(render/,免预光栅的非破坏性编辑)均复用本模块。
9
+ * 纯函数无 DOM,jsdom 可测。
10
+ */
11
+ /** 平面点(PSD 父文档绝对坐标系或 quad 局部系) */
12
+ export interface SmartObjectPoint {
13
+ x: number;
14
+ y: number;
15
+ }
16
+ /** 置入层四角 quad:tl/tr/br/bl(PSD transform 8 元组按序展开) */
17
+ export interface SmartObjectQuad {
18
+ tl: SmartObjectPoint;
19
+ tr: SmartObjectPoint;
20
+ br: SmartObjectPoint;
21
+ bl: SmartObjectPoint;
22
+ }
23
+ /** 单位正方形 → quad 的单应矩阵(齐次坐标:分母 g·x + h·y + 1) */
24
+ export interface Homography {
25
+ a: number;
26
+ b: number;
27
+ c: number;
28
+ d: number;
29
+ e: number;
30
+ f: number;
31
+ g: number;
32
+ h: number;
33
+ }
34
+ /** PSD 置入层 transform/nonAffineTransform 8 元组 → quad;长度不足或含非有限数判无效(producer 脏数据不进渲染) */
35
+ export declare const getSmartObjectTransformQuad: (transform?: number[]) => SmartObjectQuad | undefined;
36
+ /** quad 的轴对齐外接框(尺寸向上取整,退化零轴钳到 1——光栅画布最小尺寸) */
37
+ export declare const getQuadBounds: (quad: SmartObjectQuad) => {
38
+ minX: number;
39
+ minY: number;
40
+ width: number;
41
+ height: number;
42
+ };
43
+ /** quad 整体平移(投射到 quadBounds 局部系时取负偏移) */
44
+ export declare const offsetQuad: (quad: SmartObjectQuad, offsetX: number, offsetY: number) => SmartObjectQuad;
45
+ /** quad 是否退化成仿射(br ≈ tr + bl − tl,容差 0.01)——仿射走 setTransform 直绘,透视走逐像素反投射 */
46
+ export declare const isAffineQuad: (quad: SmartObjectQuad) => boolean;
47
+ /** 单位正方形 → quad 的单应求解(仿射特例 g=h=0 短路;奇异分母判无效) */
48
+ export declare const getSquareToQuadHomography: (quad: SmartObjectQuad) => Homography | undefined;
49
+ /** 单应正向投射:源平面 (x, y) → 目标平面点(x/y 通常取归一化 [0,1]) */
50
+ export declare const projectPoint: (homography: Homography, x: number, y: number) => {
51
+ x: number;
52
+ y: number;
53
+ };
54
+ /** 单应求逆(行列式/齐次分量退化判无效)——透视光栅逐像素反投射用 */
55
+ export declare const invertHomography: (homography: Homography) => Homography | undefined;
@@ -0,0 +1,123 @@
1
+ /**
2
+ * 单应性(透视变换)数学——智能对象置入层 quad 专用(批次 363 自主仓
3
+ * extension/object/FabricSmartObject.ts 迁居,逐式 verbatim)。
4
+ *
5
+ * PSD placed layer 的 smartObject 元数据携带 8 元组 quad(nonAffineTransform
6
+ * 优先、transform 兜底),描述子文档在父文档中的四角投射——超出仿射的透视
7
+ * 形变是渲染域词汇:后续 PS 智能对象新形态(变形/网格变换,warpvas 同族)
8
+ * 与 GPU 原生透视渲染(render/,免预光栅的非破坏性编辑)均复用本模块。
9
+ * 纯函数无 DOM,jsdom 可测。
10
+ */
11
+ const EPSILON = 0.000001;
12
+ /** PSD 置入层 transform/nonAffineTransform 8 元组 → quad;长度不足或含非有限数判无效(producer 脏数据不进渲染) */
13
+ export const getSmartObjectTransformQuad = (transform) => {
14
+ if (!transform || transform.length < 8 || transform.some((item) => typeof item !== 'number' || !Number.isFinite(item)))
15
+ return undefined;
16
+ return {
17
+ tl: { x: transform[0], y: transform[1] },
18
+ tr: { x: transform[2], y: transform[3] },
19
+ br: { x: transform[4], y: transform[5] },
20
+ bl: { x: transform[6], y: transform[7] }
21
+ };
22
+ };
23
+ /** quad 的轴对齐外接框(尺寸向上取整,退化零轴钳到 1——光栅画布最小尺寸) */
24
+ export const getQuadBounds = (quad) => {
25
+ const points = [quad.tl, quad.tr, quad.br, quad.bl];
26
+ const minX = Math.min(...points.map((point) => point.x));
27
+ const minY = Math.min(...points.map((point) => point.y));
28
+ const maxX = Math.max(...points.map((point) => point.x));
29
+ const maxY = Math.max(...points.map((point) => point.y));
30
+ return {
31
+ minX,
32
+ minY,
33
+ width: Math.max(1, Math.ceil(maxX - minX)),
34
+ height: Math.max(1, Math.ceil(maxY - minY))
35
+ };
36
+ };
37
+ /** quad 整体平移(投射到 quadBounds 局部系时取负偏移) */
38
+ export const offsetQuad = (quad, offsetX, offsetY) => {
39
+ return {
40
+ tl: { x: quad.tl.x + offsetX, y: quad.tl.y + offsetY },
41
+ tr: { x: quad.tr.x + offsetX, y: quad.tr.y + offsetY },
42
+ br: { x: quad.br.x + offsetX, y: quad.br.y + offsetY },
43
+ bl: { x: quad.bl.x + offsetX, y: quad.bl.y + offsetY }
44
+ };
45
+ };
46
+ /** quad 是否退化成仿射(br ≈ tr + bl − tl,容差 0.01)——仿射走 setTransform 直绘,透视走逐像素反投射 */
47
+ export const isAffineQuad = (quad) => {
48
+ const expectedBrX = quad.tr.x + quad.bl.x - quad.tl.x;
49
+ const expectedBrY = quad.tr.y + quad.bl.y - quad.tl.y;
50
+ return Math.abs(expectedBrX - quad.br.x) < 0.01 && Math.abs(expectedBrY - quad.br.y) < 0.01;
51
+ };
52
+ /** 单位正方形 → quad 的单应求解(仿射特例 g=h=0 短路;奇异分母判无效) */
53
+ export const getSquareToQuadHomography = (quad) => {
54
+ const sx = quad.tl.x - quad.tr.x + quad.br.x - quad.bl.x;
55
+ const sy = quad.tl.y - quad.tr.y + quad.br.y - quad.bl.y;
56
+ if (Math.abs(sx) < EPSILON && Math.abs(sy) < EPSILON) {
57
+ return {
58
+ a: quad.tr.x - quad.tl.x,
59
+ b: quad.bl.x - quad.tl.x,
60
+ c: quad.tl.x,
61
+ d: quad.tr.y - quad.tl.y,
62
+ e: quad.bl.y - quad.tl.y,
63
+ f: quad.tl.y,
64
+ g: 0,
65
+ h: 0
66
+ };
67
+ }
68
+ const dx1 = quad.tr.x - quad.br.x;
69
+ const dy1 = quad.tr.y - quad.br.y;
70
+ const dx2 = quad.bl.x - quad.br.x;
71
+ const dy2 = quad.bl.y - quad.br.y;
72
+ const denominator = dx1 * dy2 - dx2 * dy1;
73
+ if (Math.abs(denominator) < EPSILON)
74
+ return undefined;
75
+ const g = (sx * dy2 - dx2 * sy) / denominator;
76
+ const h = (dx1 * sy - sx * dy1) / denominator;
77
+ return {
78
+ a: quad.tr.x - quad.tl.x + g * quad.tr.x,
79
+ b: quad.bl.x - quad.tl.x + h * quad.bl.x,
80
+ c: quad.tl.x,
81
+ d: quad.tr.y - quad.tl.y + g * quad.tr.y,
82
+ e: quad.bl.y - quad.tl.y + h * quad.bl.y,
83
+ f: quad.tl.y,
84
+ g,
85
+ h
86
+ };
87
+ };
88
+ /** 单应正向投射:源平面 (x, y) → 目标平面点(x/y 通常取归一化 [0,1]) */
89
+ export const projectPoint = (homography, x, y) => {
90
+ const denominator = homography.g * x + homography.h * y + 1;
91
+ return {
92
+ x: (homography.a * x + homography.b * y + homography.c) / denominator,
93
+ y: (homography.d * x + homography.e * y + homography.f) / denominator
94
+ };
95
+ };
96
+ /** 单应求逆(行列式/齐次分量退化判无效)——透视光栅逐像素反投射用 */
97
+ export const invertHomography = (homography) => {
98
+ const { a, b, c, d, e, f, g, h } = homography;
99
+ const determinant = a * (e - f * h) - b * (d - f * g) + c * (d * h - e * g);
100
+ if (Math.abs(determinant) < EPSILON)
101
+ return undefined;
102
+ const m00 = (e - f * h) / determinant;
103
+ const m01 = (c * h - b) / determinant;
104
+ const m02 = (b * f - c * e) / determinant;
105
+ const m10 = (f * g - d) / determinant;
106
+ const m11 = (a - c * g) / determinant;
107
+ const m12 = (c * d - a * f) / determinant;
108
+ const m20 = (d * h - e * g) / determinant;
109
+ const m21 = (b * g - a * h) / determinant;
110
+ const m22 = (a * e - b * d) / determinant;
111
+ if (Math.abs(m22) < EPSILON)
112
+ return undefined;
113
+ return {
114
+ a: m00 / m22,
115
+ b: m01 / m22,
116
+ c: m02 / m22,
117
+ d: m10 / m22,
118
+ e: m11 / m22,
119
+ f: m12 / m22,
120
+ g: m20 / m22,
121
+ h: m21 / m22
122
+ };
123
+ };
package/dist/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export type { GradientFiller, PatternFiller, TFiller, Control } from './model/fi
14
14
  export { CompatPoint, Point, type PointLike } from './core/point';
15
15
  export { buildEngineShapePath, type EngineShapePathResult } from './model/shapePath';
16
16
  export { imageMaskDeclOf, imageMaskOffset, maskOutsideRects, maskPixelsToAlpha, type ImageMaskDecl } from './model/imageMaskBake';
17
+ export { renderPlacedSmartObjectLayer, type SmartObjectPlacedDecl, type SmartObjectRenderOptions, type SmartObjectRenderResult } from './model/smartObjectBake';
17
18
  export { attachTableFacade } from './model/tableFacade';
18
19
  export { deriveArcCharTransformations } from './model/arcTextTransform';
19
20
  export { CompatObject, wrapCompatObject, findParentGroupOf, type CompatObjectContext } from './model/compatObject';
package/dist/index.js CHANGED
@@ -15,6 +15,10 @@ export { buildEngineShapePath } from './model/shapePath';
15
15
  // image 级图层蒙版烘焙纯函数(批次 103 装载烘焙口径;主仓 PDF worker 导出侧
16
16
  // 同式消费——decl 校验/灰度→alpha/offset 回退/矩形外涂黑区,parity 一源)
17
17
  export { imageMaskDeclOf, imageMaskOffset, maskOutsideRects, maskPixelsToAlpha } from './model/imageMaskBake';
18
+ // 智能对象置入层透视光栅烘焙(批次 363 自主仓 extension/object/FabricSmartObject.ts 迁居;
19
+ // 主仓 template store 智能对象同步管线消费——parity 一源。后续 PS 智能对象新形态
20
+ // (变形/网格变换)在引擎侧统一承接,GPU 原生透视渲染就绪后经 render/ 落地)
21
+ export { renderPlacedSmartObjectLayer } from './model/smartObjectBake';
18
22
  export { attachTableFacade } from './model/tableFacade';
19
23
  export { deriveArcCharTransformations } from './model/arcTextTransform';
20
24
  export { CompatObject, wrapCompatObject, findParentGroupOf } from './model/compatObject';
@@ -0,0 +1,31 @@
1
+ /** 置入层 smartObject 装载消费字段子集(应用 SmartObjectInfo 的读位投影;多余字段自然兼容) */
2
+ export interface SmartObjectPlacedDecl {
3
+ width?: number;
4
+ height?: number;
5
+ transform?: number[];
6
+ nonAffineTransform?: number[];
7
+ }
8
+ export interface SmartObjectRenderOptions {
9
+ quality?: number;
10
+ format?: 'webp' | 'png';
11
+ maxGridSize?: number;
12
+ objectBounds?: {
13
+ left: number;
14
+ top: number;
15
+ width: number;
16
+ height: number;
17
+ };
18
+ }
19
+ /** 置入层光栅结果(src 写回对象即普通图片——引擎/fabric 双渲染路径同口径显示) */
20
+ export interface SmartObjectRenderResult {
21
+ src: string;
22
+ width: number;
23
+ height: number;
24
+ sourceWidth: number;
25
+ sourceHeight: number;
26
+ }
27
+ /**
28
+ * 置入层智能对象光栅:子文档合成画布经 smartObject quad(仿射/透视)烘焙成普通图片。
29
+ * 无 quad(无变换元数据)时源画布原样编码返回。
30
+ */
31
+ export declare const renderPlacedSmartObjectLayer: (sourceCanvas: HTMLCanvasElement, smartObject: SmartObjectPlacedDecl | undefined, options?: SmartObjectRenderOptions) => Promise<SmartObjectRenderResult>;
@@ -0,0 +1,295 @@
1
+ /**
2
+ * 智能对象置入层透视光栅烘焙(批次 363 自主仓 extension/object/FabricSmartObject.ts 迁居,
3
+ * 逐式 verbatim、仅剥 import.meta.env.DEV 调试日志——引擎构建无 vite env 注入)。
4
+ *
5
+ * PSD placed layer 的 smartObject 元数据携带 8 元组 quad(nonAffineTransform 优先、
6
+ * transform 兜底)——超出仿射的透视形变 fabric 时代无法渲染,主仓把子文档合成画布
7
+ * 经 quad 预光栅成普通图片 src 写回对象(template store 智能对象同步管线唯一消费方,
8
+ * 批次 257/258 建链)。迁居引擎后 parity 一源(同 imageMaskBake 先例):后续 PS 智能
9
+ * 对象新形态(变形/网格变换,warpvas 同族)在引擎侧统一承接;GPU 原生透视渲染
10
+ * (非破坏性编辑,免预光栅)就绪后经 render/ 落地,quad 数学同源复用 core/homography。
11
+ *
12
+ * 口径(迁居零行为变化):
13
+ * - 仿射 quad(br ≈ tr + bl − tl)→ setTransform 直绘;
14
+ * - 透视 quad → 逐像素反投射双线性采样;无 2d 上下文/奇异单应回退网格三角剖分
15
+ * (drawAffineTriangle 逐格 clip + setTransform 近似);
16
+ * - objectBounds 在 quad 外接框内 → 裁剪出 object 可见子区域;跨坐标系(object 在
17
+ * group 内或经 workspace 偏移)→ 整 quad 缩放到 object 尺寸;
18
+ * - 编码优先 canvas.toBlob(Chromium 后台线程编码,大画布 webp 同步编码实测阻塞
19
+ * 主线程 ~1s;主线程只剩 FileReader 读 base64),不可用/返回空回退同步 toDataURL。
20
+ */
21
+ import { getQuadBounds, getSmartObjectTransformQuad, getSquareToQuadHomography, invertHomography, isAffineQuad, offsetQuad, projectPoint } from '../core/homography';
22
+ const DEFAULT_GRID_SIZE = 32;
23
+ const MAX_GRID_COUNT = 120;
24
+ const EPSILON = 0.000001;
25
+ const createCanvasElement = () => {
26
+ const element = document.createElement('canvas');
27
+ if (!element || typeof element.getContext === 'undefined') {
28
+ throw new Error('Failed to create `canvas` element');
29
+ }
30
+ return element;
31
+ };
32
+ const copyCanvasElement = (canvas) => {
33
+ const newCanvas = createCanvasElement();
34
+ newCanvas.width = canvas.width;
35
+ newCanvas.height = canvas.height;
36
+ newCanvas.getContext('2d', { willReadFrequently: true })?.drawImage(canvas, 0, 0);
37
+ return newCanvas;
38
+ };
39
+ const toDataURL = (canvasEl, format, quality) => canvasEl.toDataURL?.(`image/${format}`, quality);
40
+ const readBlobAsDataURL = (blob) => new Promise((resolve, reject) => {
41
+ const reader = new FileReader();
42
+ reader.onload = () => resolve(String(reader.result));
43
+ reader.onerror = () => reject(reader.error);
44
+ reader.readAsDataURL(blob);
45
+ });
46
+ /** toDataURL 的异步版:优先 canvas.toBlob(离线程编码),不可用/返回空回退同步 toDataURL */
47
+ const toDataURLAsync = async (canvasEl, format, quality) => {
48
+ try {
49
+ const blob = await new Promise((resolve) => canvasEl.toBlob(resolve, `image/${format}`, quality));
50
+ if (blob)
51
+ return await readBlobAsDataURL(blob);
52
+ }
53
+ catch {
54
+ // 跨域污染等异常走同步兜底(同样会抛,由调用方处理)
55
+ }
56
+ return toDataURL(canvasEl, format, quality);
57
+ };
58
+ /** 源画布重采样到 smartObject 记录的文档精确尺寸(canvas 像素可能受 retina 缩放影响) */
59
+ const createLogicalSourceCanvas = (sourceCanvas, width, height) => {
60
+ const canvasWidth = Math.max(1, sourceCanvas.width);
61
+ const canvasHeight = Math.max(1, sourceCanvas.height);
62
+ const logicalWidth = Math.max(1, Math.round(width));
63
+ const logicalHeight = Math.max(1, Math.round(height));
64
+ if (Math.abs(canvasWidth - logicalWidth) < EPSILON && Math.abs(canvasHeight - logicalHeight) < EPSILON) {
65
+ return sourceCanvas;
66
+ }
67
+ const logicalCanvas = document.createElement('canvas');
68
+ logicalCanvas.width = logicalWidth;
69
+ logicalCanvas.height = logicalHeight;
70
+ const ctx = logicalCanvas.getContext('2d');
71
+ if (!ctx)
72
+ return sourceCanvas;
73
+ ctx.imageSmoothingEnabled = true;
74
+ ctx.imageSmoothingQuality = 'high';
75
+ ctx.drawImage(sourceCanvas, 0, 0, canvasWidth, canvasHeight, 0, 0, logicalWidth, logicalHeight);
76
+ return logicalCanvas;
77
+ };
78
+ const sampleBilinear = (sourceData, width, height, x, y) => {
79
+ const sx = Math.max(0, Math.min(width - 1, x));
80
+ const sy = Math.max(0, Math.min(height - 1, y));
81
+ const x0 = Math.floor(sx);
82
+ const y0 = Math.floor(sy);
83
+ const x1 = Math.min(width - 1, x0 + 1);
84
+ const y1 = Math.min(height - 1, y0 + 1);
85
+ const tx = sx - x0;
86
+ const ty = sy - y0;
87
+ const i00 = (y0 * width + x0) * 4;
88
+ const i10 = (y0 * width + x1) * 4;
89
+ const i01 = (y1 * width + x0) * 4;
90
+ const i11 = (y1 * width + x1) * 4;
91
+ const top = 1 - ty;
92
+ const left = 1 - tx;
93
+ const w00 = left * top;
94
+ const w10 = tx * top;
95
+ const w01 = left * ty;
96
+ const w11 = tx * ty;
97
+ return [
98
+ sourceData[i00] * w00 + sourceData[i10] * w10 + sourceData[i01] * w01 + sourceData[i11] * w11,
99
+ sourceData[i00 + 1] * w00 + sourceData[i10 + 1] * w10 + sourceData[i01 + 1] * w01 + sourceData[i11 + 1] * w11,
100
+ sourceData[i00 + 2] * w00 + sourceData[i10 + 2] * w10 + sourceData[i01 + 2] * w01 + sourceData[i11 + 2] * w11,
101
+ sourceData[i00 + 3] * w00 + sourceData[i10 + 3] * w10 + sourceData[i01 + 3] * w01 + sourceData[i11 + 3] * w11
102
+ ];
103
+ };
104
+ /** 逐像素反投射透视光栅:输出画布每像素经逆单应映射回源图双线性采样(真透视,非网格近似) */
105
+ const renderPerspectivePixels = (ctx, sourceCanvas, quad) => {
106
+ const homography = getSquareToQuadHomography(quad);
107
+ const inverseHomography = homography ? invertHomography(homography) : undefined;
108
+ if (!inverseHomography) {
109
+ renderPerspectiveMesh(ctx, sourceCanvas, quad, DEFAULT_GRID_SIZE);
110
+ return;
111
+ }
112
+ const sourceWidth = Math.max(1, sourceCanvas.width);
113
+ const sourceHeight = Math.max(1, sourceCanvas.height);
114
+ const sourceCtx = sourceCanvas.getContext('2d', { willReadFrequently: true });
115
+ if (!sourceCtx) {
116
+ renderPerspectiveMesh(ctx, sourceCanvas, quad, DEFAULT_GRID_SIZE);
117
+ return;
118
+ }
119
+ const sourceImageData = sourceCtx.getImageData(0, 0, sourceWidth, sourceHeight);
120
+ const outputImageData = ctx.createImageData(ctx.canvas.width, ctx.canvas.height);
121
+ const outputData = outputImageData.data;
122
+ for (let y = 0; y < ctx.canvas.height; y++) {
123
+ for (let x = 0; x < ctx.canvas.width; x++) {
124
+ const sourcePoint = projectPoint(inverseHomography, x + 0.5, y + 0.5);
125
+ if (sourcePoint.x < 0 || sourcePoint.x > 1 || sourcePoint.y < 0 || sourcePoint.y > 1) {
126
+ continue;
127
+ }
128
+ const sourceX = sourcePoint.x * (sourceWidth - 1);
129
+ const sourceY = sourcePoint.y * (sourceHeight - 1);
130
+ const rgba = sampleBilinear(sourceImageData.data, sourceWidth, sourceHeight, sourceX, sourceY);
131
+ const index = (y * ctx.canvas.width + x) * 4;
132
+ outputData[index] = rgba[0];
133
+ outputData[index + 1] = rgba[1];
134
+ outputData[index + 2] = rgba[2];
135
+ outputData[index + 3] = rgba[3];
136
+ }
137
+ }
138
+ ctx.putImageData(outputImageData, 0, 0);
139
+ };
140
+ /** 仿射三角剖分近似透视:源/目标三角形三点定仿射,clip 后 setTransform 绘源图 */
141
+ const drawAffineTriangle = (ctx, sourceCanvas, source, destination) => {
142
+ const [s0, s1, s2] = source;
143
+ const [d0, d1, d2] = destination;
144
+ const denominator = s0.x * (s1.y - s2.y) + s1.x * (s2.y - s0.y) + s2.x * (s0.y - s1.y);
145
+ if (Math.abs(denominator) < EPSILON)
146
+ return;
147
+ const a = (d0.x * (s1.y - s2.y) + d1.x * (s2.y - s0.y) + d2.x * (s0.y - s1.y)) / denominator;
148
+ const b = (d0.y * (s1.y - s2.y) + d1.y * (s2.y - s0.y) + d2.y * (s0.y - s1.y)) / denominator;
149
+ const c = (d0.x * (s2.x - s1.x) + d1.x * (s0.x - s2.x) + d2.x * (s1.x - s0.x)) / denominator;
150
+ const d = (d0.y * (s2.x - s1.x) + d1.y * (s0.x - s2.x) + d2.y * (s1.x - s0.x)) / denominator;
151
+ const e = (d0.x * (s1.x * s2.y - s2.x * s1.y) + d1.x * (s2.x * s0.y - s0.x * s2.y) + d2.x * (s0.x * s1.y - s1.x * s0.y)) / denominator;
152
+ const f = (d0.y * (s1.x * s2.y - s2.x * s1.y) + d1.y * (s2.x * s0.y - s0.x * s2.y) + d2.y * (s0.x * s1.y - s1.x * s0.y)) / denominator;
153
+ ctx.save();
154
+ ctx.beginPath();
155
+ ctx.moveTo(d0.x, d0.y);
156
+ ctx.lineTo(d1.x, d1.y);
157
+ ctx.lineTo(d2.x, d2.y);
158
+ ctx.closePath();
159
+ ctx.clip();
160
+ ctx.setTransform(a, b, c, d, e, f);
161
+ ctx.drawImage(sourceCanvas, 0, 0);
162
+ ctx.restore();
163
+ };
164
+ /** 网格三角剖分兜底(无 2d 读位/奇异单应时):源面按 maxGridSize 分格逐格双三角仿射近似 */
165
+ const renderPerspectiveMesh = (ctx, sourceCanvas, quad, maxGridSize) => {
166
+ const homography = getSquareToQuadHomography(quad);
167
+ if (!homography) {
168
+ ctx.drawImage(sourceCanvas, 0, 0, ctx.canvas.width, ctx.canvas.height);
169
+ return;
170
+ }
171
+ const sourceWidth = Math.max(1, sourceCanvas.width);
172
+ const sourceHeight = Math.max(1, sourceCanvas.height);
173
+ const columns = Math.min(MAX_GRID_COUNT, Math.max(1, Math.ceil(sourceWidth / maxGridSize)));
174
+ const rows = Math.min(MAX_GRID_COUNT, Math.max(1, Math.ceil(sourceHeight / maxGridSize)));
175
+ for (let row = 0; row < rows; row++) {
176
+ const y0 = row / rows;
177
+ const y1 = (row + 1) / rows;
178
+ const sy0 = y0 * sourceHeight;
179
+ const sy1 = y1 * sourceHeight;
180
+ for (let col = 0; col < columns; col++) {
181
+ const x0 = col / columns;
182
+ const x1 = (col + 1) / columns;
183
+ const sx0 = x0 * sourceWidth;
184
+ const sx1 = x1 * sourceWidth;
185
+ const p00 = projectPoint(homography, x0, y0);
186
+ const p10 = projectPoint(homography, x1, y0);
187
+ const p11 = projectPoint(homography, x1, y1);
188
+ const p01 = projectPoint(homography, x0, y1);
189
+ drawAffineTriangle(ctx, sourceCanvas, [
190
+ { x: sx0, y: sy0 },
191
+ { x: sx1, y: sy0 },
192
+ { x: sx1, y: sy1 }
193
+ ], [p00, p10, p11]);
194
+ drawAffineTriangle(ctx, sourceCanvas, [
195
+ { x: sx0, y: sy0 },
196
+ { x: sx1, y: sy1 },
197
+ { x: sx0, y: sy1 }
198
+ ], [p00, p11, p01]);
199
+ }
200
+ }
201
+ };
202
+ /**
203
+ * 置入层智能对象光栅:子文档合成画布经 smartObject quad(仿射/透视)烘焙成普通图片。
204
+ * 无 quad(无变换元数据)时源画布原样编码返回。
205
+ */
206
+ export const renderPlacedSmartObjectLayer = async (sourceCanvas, smartObject, options = {}) => {
207
+ const quality = options.quality ?? 0.92;
208
+ const format = options.format || 'webp';
209
+ const canvasWidth = Math.max(1, sourceCanvas.width);
210
+ const canvasHeight = Math.max(1, sourceCanvas.height);
211
+ // 优先使用 smartObject 存储的内部文档尺寸,而非 canvas 像素尺寸
212
+ // canvas 尺寸可能受 retina 缩放等因素影响,而 smartObject.width/height 是 PSD 中记录的精确值
213
+ const sourceWidth = Math.max(1, smartObject?.width || canvasWidth);
214
+ const sourceHeight = Math.max(1, smartObject?.height || canvasHeight);
215
+ const logicalSourceCanvas = createLogicalSourceCanvas(sourceCanvas, sourceWidth, sourceHeight);
216
+ const quad = getSmartObjectTransformQuad(smartObject?.nonAffineTransform || smartObject?.transform);
217
+ if (!quad) {
218
+ const canvas = copyCanvasElement(sourceCanvas);
219
+ return {
220
+ src: await toDataURLAsync(canvas, format, quality),
221
+ width: canvasWidth,
222
+ height: canvasHeight,
223
+ sourceWidth,
224
+ sourceHeight
225
+ };
226
+ }
227
+ const quadBounds = getQuadBounds(quad);
228
+ const localQuad = offsetQuad(quad, -quadBounds.minX, -quadBounds.minY);
229
+ const objectBounds = options.objectBounds;
230
+ // 在 quadBounds 尺寸的 canvas 上完整渲染
231
+ const quadCanvas = document.createElement('canvas');
232
+ quadCanvas.width = quadBounds.width;
233
+ quadCanvas.height = quadBounds.height;
234
+ const ctx = quadCanvas.getContext('2d');
235
+ if (!ctx)
236
+ throw new Error('无法创建智能对象渲染画布');
237
+ ctx.imageSmoothingEnabled = true;
238
+ ctx.imageSmoothingQuality = 'high';
239
+ if (isAffineQuad(localQuad)) {
240
+ const a = (localQuad.tr.x - localQuad.tl.x) / sourceWidth;
241
+ const b = (localQuad.tr.y - localQuad.tl.y) / sourceWidth;
242
+ const c = (localQuad.bl.x - localQuad.tl.x) / sourceHeight;
243
+ const d = (localQuad.bl.y - localQuad.tl.y) / sourceHeight;
244
+ const e = localQuad.tl.x;
245
+ const f = localQuad.tl.y;
246
+ ctx.setTransform(a, b, c, d, e, f);
247
+ ctx.drawImage(logicalSourceCanvas, 0, 0);
248
+ }
249
+ else {
250
+ renderPerspectivePixels(ctx, logicalSourceCanvas, localQuad);
251
+ }
252
+ // 决定输出策略:裁剪还是缩放
253
+ let outputCanvas;
254
+ if (objectBounds) {
255
+ const cropX = Math.round(objectBounds.left - quadBounds.minX);
256
+ const cropY = Math.round(objectBounds.top - quadBounds.minY);
257
+ const cropW = Math.max(1, Math.round(objectBounds.width));
258
+ const cropH = Math.max(1, Math.round(objectBounds.height));
259
+ // 判断 object 是否在 quad 内部(同一坐标系下)
260
+ const canCrop = cropX >= 0 && cropY >= 0 && cropX + cropW <= quadBounds.width + 1 && cropY + cropH <= quadBounds.height + 1;
261
+ if (canCrop) {
262
+ // object 是 quad 的子区域 → 裁剪出 object 可见部分
263
+ outputCanvas = document.createElement('canvas');
264
+ outputCanvas.width = cropW;
265
+ outputCanvas.height = cropH;
266
+ const cropCtx = outputCanvas.getContext('2d');
267
+ if (cropCtx) {
268
+ cropCtx.drawImage(quadCanvas, cropX, cropY, cropW, cropH, 0, 0, cropW, cropH);
269
+ }
270
+ }
271
+ else {
272
+ // 坐标不在同一空间(object 在 group 内或经过 workspace 偏移)→ 缩放整个 quad 到 object 尺寸
273
+ outputCanvas = document.createElement('canvas');
274
+ outputCanvas.width = cropW;
275
+ outputCanvas.height = cropH;
276
+ const scaleCtx = outputCanvas.getContext('2d');
277
+ if (scaleCtx) {
278
+ scaleCtx.imageSmoothingEnabled = true;
279
+ scaleCtx.imageSmoothingQuality = 'high';
280
+ scaleCtx.drawImage(quadCanvas, 0, 0, quadBounds.width, quadBounds.height, 0, 0, cropW, cropH);
281
+ }
282
+ }
283
+ }
284
+ else {
285
+ outputCanvas = quadCanvas;
286
+ }
287
+ return {
288
+ // toBlob 离线程编码:2267×2333 webp 同步编码实测阻塞主线程 ~1s,异步化后主线程只剩 FileReader
289
+ src: await toDataURLAsync(outputCanvas, format, quality),
290
+ width: outputCanvas.width,
291
+ height: outputCanvas.height,
292
+ sourceWidth,
293
+ sourceHeight
294
+ };
295
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yft-design/engine",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "yft-design 的 WebGL2 画布引擎:接受 fabric toObject 格式 JSON,输出 GL 画布 / 位图(任意倍率 FBO 读回)/ SVG(纯数据序列化器)",
5
5
  "repository": {
6
6
  "type": "git",