fabricjs-document-engine 1.1.0 → 1.3.0
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 +70 -1
- package/README.zh-CN.md +71 -1
- package/dist/assets/asset-pipeline.cjs +10 -8
- package/dist/assets/asset-pipeline.d.cts +1 -1
- package/dist/assets/asset-pipeline.d.ts +1 -1
- package/dist/assets/asset-pipeline.js +10 -8
- package/dist/assets/image-check.cjs +4 -3
- package/dist/assets/image-check.d.cts +1 -1
- package/dist/assets/image-check.d.ts +1 -1
- package/dist/assets/image-check.js +4 -3
- package/dist/commands/clipboard.cjs +5 -0
- package/dist/commands/clipboard.js +5 -0
- package/dist/commands/text-edit.cjs +223 -0
- package/dist/commands/text-edit.d.cts +23 -0
- package/dist/commands/text-edit.d.ts +23 -0
- package/dist/commands/text-edit.js +218 -0
- package/dist/document/document-format.d.cts +15 -0
- package/dist/document/document-format.d.ts +15 -0
- package/dist/document/validate-document.cjs +8 -0
- package/dist/document/validate-document.js +8 -0
- package/dist/engine/create-document-engine.cjs +115 -21
- package/dist/engine/create-document-engine.d.cts +24 -6
- package/dist/engine/create-document-engine.d.ts +24 -6
- package/dist/engine/create-document-engine.js +116 -22
- package/dist/engine/errors.d.cts +1 -1
- package/dist/engine/errors.d.ts +1 -1
- package/dist/export/export-options.cjs +3 -0
- package/dist/export/export-options.d.cts +6 -0
- package/dist/export/export-options.d.ts +6 -0
- package/dist/export/export-options.js +3 -0
- package/dist/export/preflight-export.d.cts +1 -1
- package/dist/export/preflight-export.d.ts +1 -1
- package/dist/export/render-export.cjs +42 -4
- package/dist/export/render-export.js +43 -5
- package/dist/export/svg/clip-masks.cjs +39 -0
- package/dist/export/svg/clip-masks.js +37 -0
- package/dist/export/svg/embed-assets.cjs +1 -0
- package/dist/export/svg/embed-assets.js +1 -0
- package/dist/export/svg/raster-object.cjs +82 -0
- package/dist/export/svg/raster-object.js +81 -0
- package/dist/{pdf → export/svg}/text-decorations.cjs +3 -3
- package/dist/{pdf → export/svg}/text-decorations.js +3 -3
- package/dist/export/svg/text-on-path.cjs +0 -10
- package/dist/export/svg/text-on-path.js +1 -10
- package/dist/fabric/fabric-adapter.cjs +5 -3
- package/dist/fabric/fabric-adapter.js +5 -3
- package/dist/fabric/object-registry.cjs +7 -1
- package/dist/fabric/object-registry.js +7 -1
- package/dist/fabric/page-state.cjs +69 -0
- package/dist/fabric/page-state.js +64 -0
- package/dist/filters/filter-worker.cjs +281 -0
- package/dist/filters/filter-worker.d.cts +26 -0
- package/dist/filters/filter-worker.d.ts +26 -0
- package/dist/filters/filter-worker.js +281 -0
- package/dist/filters/pipeline.cjs +91 -0
- package/dist/filters/pipeline.d.cts +35 -0
- package/dist/filters/pipeline.d.ts +35 -0
- package/dist/filters/pipeline.js +87 -0
- package/dist/filters/worker-entry.cjs +78 -0
- package/dist/filters/worker-entry.d.cts +29 -0
- package/dist/filters/worker-entry.d.ts +29 -0
- package/dist/filters/worker-entry.js +78 -0
- package/dist/filters.cjs +9 -0
- package/dist/filters.d.cts +3 -0
- package/dist/filters.d.ts +3 -0
- package/dist/filters.js +3 -0
- package/dist/history/apply-state.cjs +19 -2
- package/dist/history/apply-state.js +19 -2
- package/dist/history/create-history.cjs +53 -13
- package/dist/history/create-history.d.cts +8 -0
- package/dist/history/create-history.d.ts +8 -0
- package/dist/history/create-history.js +53 -13
- package/dist/history/snapshot.cjs +10 -1
- package/dist/history/snapshot.js +10 -2
- package/dist/import/svg-import.cjs +102 -0
- package/dist/import/svg-import.d.cts +1 -0
- package/dist/import/svg-import.d.ts +1 -0
- package/dist/import/svg-import.js +103 -1
- package/dist/index.cjs +7 -0
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +2 -1
- package/dist/migrations/migrate-document.cjs +2 -1
- package/dist/migrations/migrate-document.js +2 -1
- package/dist/pdf/export-pdf.cjs +8 -67
- package/dist/pdf/export-pdf.js +8 -67
- package/dist/performance/batch.cjs +31 -0
- package/dist/performance/batch.d.cts +4 -0
- package/dist/performance/batch.d.ts +4 -0
- package/dist/performance/batch.js +31 -0
- package/dist/performance/dirty-regions.cjs +378 -0
- package/dist/performance/dirty-regions.d.cts +23 -0
- package/dist/performance/dirty-regions.d.ts +23 -0
- package/dist/performance/dirty-regions.js +378 -0
- package/dist/performance/monitor.cjs +60 -0
- package/dist/performance/monitor.d.cts +20 -0
- package/dist/performance/monitor.d.ts +20 -0
- package/dist/performance/monitor.js +60 -0
- package/dist/performance/spatial-index.cjs +78 -0
- package/dist/performance/spatial-index.d.cts +21 -0
- package/dist/performance/spatial-index.d.ts +21 -0
- package/dist/performance/spatial-index.js +78 -0
- package/dist/performance.cjs +9 -0
- package/dist/performance.d.cts +5 -0
- package/dist/performance.d.ts +5 -0
- package/dist/performance.js +5 -0
- package/dist/recovery/recovery-controller.cjs +148 -29
- package/dist/recovery/recovery-controller.d.cts +8 -0
- package/dist/recovery/recovery-controller.d.ts +8 -0
- package/dist/recovery/recovery-controller.js +148 -29
- package/dist/security/content-limits.cjs +43 -3
- package/dist/security/content-limits.d.cts +8 -0
- package/dist/security/content-limits.d.ts +8 -0
- package/dist/security/content-limits.js +39 -4
- package/dist/text/bounded-textbox.cjs +306 -0
- package/dist/text/bounded-textbox.d.cts +53 -0
- package/dist/text/bounded-textbox.d.ts +53 -0
- package/dist/text/bounded-textbox.js +306 -0
- package/dist/text/mobile-input.cjs +146 -0
- package/dist/text/mobile-input.d.cts +19 -0
- package/dist/text/mobile-input.d.ts +19 -0
- package/dist/text/mobile-input.js +145 -0
- package/dist/text/register.cjs +17 -0
- package/dist/text/register.d.cts +5 -0
- package/dist/text/register.d.ts +5 -0
- package/dist/text/register.js +16 -0
- package/dist/text/shaping.cjs +125 -0
- package/dist/text/shaping.d.cts +28 -0
- package/dist/text/shaping.d.ts +28 -0
- package/dist/text/shaping.js +121 -0
- package/dist/text/vertical-orientation.cjs +192 -0
- package/dist/text/vertical-orientation.d.cts +10 -0
- package/dist/text/vertical-orientation.d.ts +10 -0
- package/dist/text/vertical-orientation.js +190 -0
- package/dist/text/vertical-text.cjs +336 -0
- package/dist/text/vertical-text.d.cts +54 -0
- package/dist/text/vertical-text.d.ts +54 -0
- package/dist/text/vertical-text.js +336 -0
- package/dist/text.cjs +21 -0
- package/dist/text.d.cts +7 -0
- package/dist/text.d.ts +7 -0
- package/dist/text.js +7 -0
- package/dist/util/concurrency.cjs +16 -3
- package/dist/util/concurrency.js +16 -3
- package/package.json +31 -1
- package/schema/document-v1.schema.json +5 -1
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
const require_spatial_index = require("./spatial-index.cjs");
|
|
2
|
+
//#region src/performance/dirty-regions.ts
|
|
3
|
+
const UNBOUNDED_COMPOSITES = /* @__PURE__ */ new Set([
|
|
4
|
+
"source-in",
|
|
5
|
+
"source-out",
|
|
6
|
+
"destination-in",
|
|
7
|
+
"destination-atop",
|
|
8
|
+
"copy"
|
|
9
|
+
]);
|
|
10
|
+
function union(first, second) {
|
|
11
|
+
const left = Math.min(first.left, second.left);
|
|
12
|
+
const top = Math.min(first.top, second.top);
|
|
13
|
+
return {
|
|
14
|
+
left,
|
|
15
|
+
top,
|
|
16
|
+
width: Math.max(first.left + first.width, second.left + second.width) - left,
|
|
17
|
+
height: Math.max(first.top + first.height, second.top + second.height) - top
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function intersects(first, second, gap = 0) {
|
|
21
|
+
return first.left <= second.left + second.width + gap && second.left <= first.left + first.width + gap && first.top <= second.top + second.height + gap && second.top <= first.top + first.height + gap;
|
|
22
|
+
}
|
|
23
|
+
const area = (rect) => Math.max(0, rect.width) * Math.max(0, rect.height);
|
|
24
|
+
function overhang(object) {
|
|
25
|
+
const self = object;
|
|
26
|
+
let x = 0;
|
|
27
|
+
let y = 0;
|
|
28
|
+
if (self.stroke && self.strokeWidth) x = y = self.strokeWidth * Math.max(1, self.strokeMiterLimit ?? 4) / 2;
|
|
29
|
+
if (typeof self.fontSize === "number" && typeof self.text === "string") {
|
|
30
|
+
x = Math.max(x, self.fontSize * .5);
|
|
31
|
+
y = Math.max(y, self.fontSize * .5);
|
|
32
|
+
}
|
|
33
|
+
for (const child of self._objects ?? []) {
|
|
34
|
+
const inner = overhang(child);
|
|
35
|
+
const childShadow = shadowReach(child);
|
|
36
|
+
x = Math.max(x, inner.x + childShadow.x);
|
|
37
|
+
y = Math.max(y, inner.y + childShadow.y);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
x,
|
|
41
|
+
y
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function shadowReach(object) {
|
|
45
|
+
const shadow = object.shadow;
|
|
46
|
+
if (!shadow) return {
|
|
47
|
+
x: 0,
|
|
48
|
+
y: 0
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
x: Math.abs(shadow.offsetX) + shadow.blur * 2,
|
|
52
|
+
y: Math.abs(shadow.offsetY) + shadow.blur * 2
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function drawnBounds(object) {
|
|
56
|
+
object.setCoords();
|
|
57
|
+
const box = object.getBoundingRect();
|
|
58
|
+
const scaling = object.getObjectScaling();
|
|
59
|
+
const scaleX = Math.abs(scaling.x);
|
|
60
|
+
const scaleY = Math.abs(scaling.y);
|
|
61
|
+
const reach = overhang(object);
|
|
62
|
+
const shadow = shadowReach(object);
|
|
63
|
+
const nonScaling = Boolean(object.shadow?.nonScaling);
|
|
64
|
+
const grow = Math.max(reach.x * scaleX, reach.y * scaleY) + Math.max(shadow.x, shadow.y) * (nonScaling ? 1 : Math.max(scaleX, scaleY));
|
|
65
|
+
let bounds = {
|
|
66
|
+
left: box.left - grow,
|
|
67
|
+
top: box.top - grow,
|
|
68
|
+
width: box.width + grow * 2,
|
|
69
|
+
height: box.height + grow * 2
|
|
70
|
+
};
|
|
71
|
+
for (const child of object._objects ?? []) bounds = union(bounds, drawnBounds(child));
|
|
72
|
+
return bounds;
|
|
73
|
+
}
|
|
74
|
+
function fingerprint(object, index, into = []) {
|
|
75
|
+
const self = object;
|
|
76
|
+
const matrix = object.calcTransformMatrix();
|
|
77
|
+
const shadow = self.shadow;
|
|
78
|
+
into.push(index, matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], self.width, self.height, self.visible, self.opacity, self.strokeWidth, self.globalCompositeOperation, self._element, self.clipPath, shadow, shadow?.blur, shadow?.offsetX, shadow?.offsetY);
|
|
79
|
+
self._objects?.forEach((child, position) => fingerprint(child, position, into));
|
|
80
|
+
return into;
|
|
81
|
+
}
|
|
82
|
+
function isDirty(object) {
|
|
83
|
+
const self = object;
|
|
84
|
+
return Boolean(self.dirty) || (self._objects ?? []).some(isDirty);
|
|
85
|
+
}
|
|
86
|
+
function sameFingerprint(first, second) {
|
|
87
|
+
if (first.length !== second.length) return false;
|
|
88
|
+
for (let index = 0; index < first.length; index += 1) if (first[index] !== second[index]) return false;
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
function toViewport(rect, matrix) {
|
|
92
|
+
const [a, b, c, d, e, f] = matrix;
|
|
93
|
+
const xs = [];
|
|
94
|
+
const ys = [];
|
|
95
|
+
for (const [x, y] of [
|
|
96
|
+
[rect.left, rect.top],
|
|
97
|
+
[rect.left + rect.width, rect.top],
|
|
98
|
+
[rect.left, rect.top + rect.height],
|
|
99
|
+
[rect.left + rect.width, rect.top + rect.height]
|
|
100
|
+
]) {
|
|
101
|
+
xs.push(a * x + c * y + e);
|
|
102
|
+
ys.push(b * x + d * y + f);
|
|
103
|
+
}
|
|
104
|
+
const left = Math.min(...xs);
|
|
105
|
+
const top = Math.min(...ys);
|
|
106
|
+
return {
|
|
107
|
+
left,
|
|
108
|
+
top,
|
|
109
|
+
width: Math.max(...xs) - left,
|
|
110
|
+
height: Math.max(...ys) - top
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function toScene(rect, matrix) {
|
|
114
|
+
const [a, b, c, d, e, f] = matrix;
|
|
115
|
+
const determinant = a * d - b * c;
|
|
116
|
+
return toViewport(rect, [
|
|
117
|
+
d / determinant,
|
|
118
|
+
-b / determinant,
|
|
119
|
+
-c / determinant,
|
|
120
|
+
a / determinant,
|
|
121
|
+
(c * f - d * e) / determinant,
|
|
122
|
+
(b * e - a * f) / determinant
|
|
123
|
+
]);
|
|
124
|
+
}
|
|
125
|
+
function controlsArea(canvas) {
|
|
126
|
+
const active = canvas.getActiveObject?.();
|
|
127
|
+
if (!active) return {
|
|
128
|
+
key: [],
|
|
129
|
+
rect: void 0
|
|
130
|
+
};
|
|
131
|
+
const self = active;
|
|
132
|
+
const points = Object.values(self.oCoords ?? {});
|
|
133
|
+
if (points.length === 0 || !self.hasControls && !self.hasBorders) return {
|
|
134
|
+
key: [active],
|
|
135
|
+
rect: void 0
|
|
136
|
+
};
|
|
137
|
+
const reach = Math.max(self.cornerSize ?? 13, self.touchCornerSize ?? 0) / 2 + (self.padding ?? 0) + 4;
|
|
138
|
+
let left = Infinity;
|
|
139
|
+
let top = Infinity;
|
|
140
|
+
let right = -Infinity;
|
|
141
|
+
let bottom = -Infinity;
|
|
142
|
+
const key = [
|
|
143
|
+
active,
|
|
144
|
+
self.hasControls,
|
|
145
|
+
self.hasBorders
|
|
146
|
+
];
|
|
147
|
+
for (const point of points) {
|
|
148
|
+
left = Math.min(left, point.x);
|
|
149
|
+
top = Math.min(top, point.y);
|
|
150
|
+
right = Math.max(right, point.x);
|
|
151
|
+
bottom = Math.max(bottom, point.y);
|
|
152
|
+
key.push(point.x, point.y);
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
key,
|
|
156
|
+
rect: {
|
|
157
|
+
left: left - reach,
|
|
158
|
+
top: top - reach,
|
|
159
|
+
width: right - left + reach * 2,
|
|
160
|
+
height: bottom - top + reach * 2
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function mergeRects(rects, limit) {
|
|
165
|
+
if (rects.length > 64) return [rects.reduce(union)];
|
|
166
|
+
const merged = [...rects];
|
|
167
|
+
let changed = true;
|
|
168
|
+
while (changed) {
|
|
169
|
+
changed = false;
|
|
170
|
+
for (let first = 0; first < merged.length && !changed; first += 1) for (let second = first + 1; second < merged.length; second += 1) if (intersects(merged[first], merged[second], 8)) {
|
|
171
|
+
merged[first] = union(merged[first], merged[second]);
|
|
172
|
+
merged.splice(second, 1);
|
|
173
|
+
changed = true;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
while (merged.length > limit) {
|
|
178
|
+
let best = [0, 1];
|
|
179
|
+
let bestGrowth = Infinity;
|
|
180
|
+
for (let first = 0; first < merged.length; first += 1) for (let second = first + 1; second < merged.length; second += 1) {
|
|
181
|
+
const growth = area(union(merged[first], merged[second])) - area(merged[first]) - area(merged[second]);
|
|
182
|
+
if (growth < bestGrowth) {
|
|
183
|
+
bestGrowth = growth;
|
|
184
|
+
best = [first, second];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
merged[best[0]] = union(merged[best[0]], merged[best[1]]);
|
|
188
|
+
merged.splice(best[1], 1);
|
|
189
|
+
}
|
|
190
|
+
return merged;
|
|
191
|
+
}
|
|
192
|
+
function enableDirtyRegionRendering(canvas, options = {}) {
|
|
193
|
+
const self = canvas;
|
|
194
|
+
const fullRatio = options.fullRedrawRatio ?? .4;
|
|
195
|
+
const maxRegions = Math.max(1, options.maxRegions ?? 4);
|
|
196
|
+
const index = require_spatial_index.createSpatialIndex({ cellSize: options.cellSize ?? 256 });
|
|
197
|
+
const states = /* @__PURE__ */ new Map();
|
|
198
|
+
const unbounded = /* @__PURE__ */ new Set();
|
|
199
|
+
const original = self.renderCanvas;
|
|
200
|
+
const hadOwn = Object.prototype.hasOwnProperty.call(canvas, "renderCanvas");
|
|
201
|
+
const stats = {
|
|
202
|
+
frames: 0,
|
|
203
|
+
fullFrames: 0,
|
|
204
|
+
skippedFrames: 0,
|
|
205
|
+
lastObjectsDrawn: 0,
|
|
206
|
+
lastAreaRatio: 0,
|
|
207
|
+
lastRegions: []
|
|
208
|
+
};
|
|
209
|
+
let frame = 0;
|
|
210
|
+
let forceFull = true;
|
|
211
|
+
let lastPage = [];
|
|
212
|
+
let lastControls = {
|
|
213
|
+
key: [],
|
|
214
|
+
rect: void 0
|
|
215
|
+
};
|
|
216
|
+
const pageKey = () => [
|
|
217
|
+
...self.viewportTransform,
|
|
218
|
+
self.width,
|
|
219
|
+
self.height,
|
|
220
|
+
self.getRetinaScaling(),
|
|
221
|
+
self.backgroundColor,
|
|
222
|
+
self.backgroundImage,
|
|
223
|
+
self.backgroundImage?.dirty,
|
|
224
|
+
self.overlayColor,
|
|
225
|
+
self.overlayImage,
|
|
226
|
+
self.overlayImage?.dirty,
|
|
227
|
+
self.clipPath,
|
|
228
|
+
self.clipPath?.dirty
|
|
229
|
+
];
|
|
230
|
+
function track(object, position) {
|
|
231
|
+
const print = fingerprint(object, position);
|
|
232
|
+
const state = states.get(object);
|
|
233
|
+
if (state) {
|
|
234
|
+
state.seen = frame;
|
|
235
|
+
if (!isDirty(object) && sameFingerprint(state.fingerprint, print)) return [];
|
|
236
|
+
}
|
|
237
|
+
const bounds = drawnBounds(object);
|
|
238
|
+
states.set(object, {
|
|
239
|
+
fingerprint: print,
|
|
240
|
+
bounds,
|
|
241
|
+
seen: frame
|
|
242
|
+
});
|
|
243
|
+
index.set(object, bounds);
|
|
244
|
+
const composite = object.globalCompositeOperation ?? "source-over";
|
|
245
|
+
if (UNBOUNDED_COMPOSITES.has(composite)) unbounded.add(object);
|
|
246
|
+
else unbounded.delete(object);
|
|
247
|
+
return state ? [state.bounds, bounds] : [bounds];
|
|
248
|
+
}
|
|
249
|
+
function drawFull(ctx, objects) {
|
|
250
|
+
stats.fullFrames += 1;
|
|
251
|
+
stats.lastObjectsDrawn = objects.length;
|
|
252
|
+
stats.lastAreaRatio = 1;
|
|
253
|
+
stats.lastRegions = [];
|
|
254
|
+
original.call(canvas, ctx, objects);
|
|
255
|
+
}
|
|
256
|
+
function renderCanvas(ctx, objects) {
|
|
257
|
+
if (ctx !== self.getContext()) {
|
|
258
|
+
original.call(canvas, ctx, objects);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
frame += 1;
|
|
262
|
+
stats.frames += 1;
|
|
263
|
+
const changed = [];
|
|
264
|
+
self._objects.forEach((object, position) => changed.push(...track(object, position)));
|
|
265
|
+
for (const [object, state] of states) {
|
|
266
|
+
if (state.seen === frame) continue;
|
|
267
|
+
changed.push(state.bounds);
|
|
268
|
+
states.delete(object);
|
|
269
|
+
index.delete(object);
|
|
270
|
+
unbounded.delete(object);
|
|
271
|
+
}
|
|
272
|
+
const page = pageKey();
|
|
273
|
+
const controls = controlsArea(self);
|
|
274
|
+
const pageChanged = !sameFingerprint(page, lastPage) || page.length !== lastPage.length;
|
|
275
|
+
const controlsChanged = !sameFingerprint(controls.key, lastControls.key) || controls.key.length !== lastControls.key.length;
|
|
276
|
+
const viewportChanged = [];
|
|
277
|
+
if (controlsChanged) {
|
|
278
|
+
if (lastControls.rect) viewportChanged.push(lastControls.rect);
|
|
279
|
+
if (controls.rect) viewportChanged.push(controls.rect);
|
|
280
|
+
const before = lastControls.key[0];
|
|
281
|
+
const after = controls.key[0];
|
|
282
|
+
for (const object of [before, after]) {
|
|
283
|
+
const state = object ? states.get(object) : void 0;
|
|
284
|
+
if (state) changed.push(state.bounds);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
lastPage = page;
|
|
288
|
+
lastControls = controls;
|
|
289
|
+
if (forceFull || pageChanged) {
|
|
290
|
+
forceFull = false;
|
|
291
|
+
drawFull(ctx, objects);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const matrix = self.viewportTransform;
|
|
295
|
+
const retina = self.getRetinaScaling();
|
|
296
|
+
const canvasRect = {
|
|
297
|
+
left: 0,
|
|
298
|
+
top: 0,
|
|
299
|
+
width: self.width,
|
|
300
|
+
height: self.height
|
|
301
|
+
};
|
|
302
|
+
const regions = [];
|
|
303
|
+
for (const rect of [...changed.map((scene) => toViewport(scene, matrix)), ...viewportChanged]) {
|
|
304
|
+
const pad = 2 / retina;
|
|
305
|
+
const left = Math.max(0, Math.floor((rect.left - pad) * retina) / retina);
|
|
306
|
+
const top = Math.max(0, Math.floor((rect.top - pad) * retina) / retina);
|
|
307
|
+
const right = Math.min(self.width, Math.ceil((rect.left + rect.width + pad) * retina) / retina);
|
|
308
|
+
const bottom = Math.min(self.height, Math.ceil((rect.top + rect.height + pad) * retina) / retina);
|
|
309
|
+
if (right > left && bottom > top) regions.push({
|
|
310
|
+
left,
|
|
311
|
+
top,
|
|
312
|
+
width: right - left,
|
|
313
|
+
height: bottom - top
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
if (regions.length === 0) {
|
|
317
|
+
stats.skippedFrames += 1;
|
|
318
|
+
stats.lastObjectsDrawn = 0;
|
|
319
|
+
stats.lastAreaRatio = 0;
|
|
320
|
+
stats.lastRegions = [];
|
|
321
|
+
self.fire("before:render", { ctx });
|
|
322
|
+
self.fire("after:render", { ctx });
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
const merged = mergeRects(regions, maxRegions);
|
|
326
|
+
const drawnArea = merged.reduce((sum, rect) => sum + area(rect), 0);
|
|
327
|
+
if (drawnArea > area(canvasRect) * fullRatio) {
|
|
328
|
+
drawFull(ctx, objects);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const wanted = new Set(unbounded);
|
|
332
|
+
for (const rect of merged) {
|
|
333
|
+
const pad = 2 / retina;
|
|
334
|
+
const scene = toScene({
|
|
335
|
+
left: rect.left - pad,
|
|
336
|
+
top: rect.top - pad,
|
|
337
|
+
width: rect.width + pad * 2,
|
|
338
|
+
height: rect.height + pad * 2
|
|
339
|
+
}, matrix);
|
|
340
|
+
index.query(scene).forEach((object) => {
|
|
341
|
+
const bounds = states.get(object)?.bounds;
|
|
342
|
+
if (!bounds || intersects(bounds, scene)) wanted.add(object);
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
const toDraw = objects.filter((object) => wanted.has(object));
|
|
346
|
+
stats.lastObjectsDrawn = toDraw.length;
|
|
347
|
+
stats.lastAreaRatio = drawnArea / area(canvasRect);
|
|
348
|
+
stats.lastRegions = merged;
|
|
349
|
+
ctx.save();
|
|
350
|
+
ctx.beginPath();
|
|
351
|
+
for (const rect of merged) ctx.rect(rect.left, rect.top, rect.width, rect.height);
|
|
352
|
+
ctx.clip();
|
|
353
|
+
try {
|
|
354
|
+
original.call(canvas, ctx, toDraw);
|
|
355
|
+
} finally {
|
|
356
|
+
ctx.restore();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
self.renderCanvas = renderCanvas;
|
|
360
|
+
return {
|
|
361
|
+
invalidate() {
|
|
362
|
+
forceFull = true;
|
|
363
|
+
},
|
|
364
|
+
stats: () => ({
|
|
365
|
+
...stats,
|
|
366
|
+
lastRegions: [...stats.lastRegions]
|
|
367
|
+
}),
|
|
368
|
+
disable() {
|
|
369
|
+
if (self.renderCanvas !== renderCanvas) return;
|
|
370
|
+
if (hadOwn) self.renderCanvas = original;
|
|
371
|
+
else delete canvas.renderCanvas;
|
|
372
|
+
index.clear();
|
|
373
|
+
states.clear();
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
//#endregion
|
|
378
|
+
exports.enableDirtyRegionRendering = enableDirtyRegionRendering;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Rect } from "./spatial-index.cjs";
|
|
2
|
+
import { StaticCanvas } from "fabric";
|
|
3
|
+
//#region src/performance/dirty-regions.d.ts
|
|
4
|
+
export interface DirtyRegionOptions {
|
|
5
|
+
fullRedrawRatio?: number;
|
|
6
|
+
maxRegions?: number;
|
|
7
|
+
cellSize?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface DirtyRegionStats {
|
|
10
|
+
frames: number;
|
|
11
|
+
fullFrames: number;
|
|
12
|
+
skippedFrames: number;
|
|
13
|
+
lastObjectsDrawn: number;
|
|
14
|
+
lastAreaRatio: number;
|
|
15
|
+
lastRegions: Rect[];
|
|
16
|
+
}
|
|
17
|
+
export interface DirtyRegionRenderer {
|
|
18
|
+
invalidate(): void;
|
|
19
|
+
stats(): DirtyRegionStats;
|
|
20
|
+
disable(): void;
|
|
21
|
+
}
|
|
22
|
+
export declare function enableDirtyRegionRendering(canvas: StaticCanvas, options?: DirtyRegionOptions): DirtyRegionRenderer;
|
|
23
|
+
//#endregion
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Rect as Rect$1 } from "./spatial-index.js";
|
|
2
|
+
import { StaticCanvas } from "fabric";
|
|
3
|
+
//#region src/performance/dirty-regions.d.ts
|
|
4
|
+
export interface DirtyRegionOptions {
|
|
5
|
+
fullRedrawRatio?: number;
|
|
6
|
+
maxRegions?: number;
|
|
7
|
+
cellSize?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface DirtyRegionStats {
|
|
10
|
+
frames: number;
|
|
11
|
+
fullFrames: number;
|
|
12
|
+
skippedFrames: number;
|
|
13
|
+
lastObjectsDrawn: number;
|
|
14
|
+
lastAreaRatio: number;
|
|
15
|
+
lastRegions: Rect$1[];
|
|
16
|
+
}
|
|
17
|
+
export interface DirtyRegionRenderer {
|
|
18
|
+
invalidate(): void;
|
|
19
|
+
stats(): DirtyRegionStats;
|
|
20
|
+
disable(): void;
|
|
21
|
+
}
|
|
22
|
+
export declare function enableDirtyRegionRendering(canvas: StaticCanvas, options?: DirtyRegionOptions): DirtyRegionRenderer;
|
|
23
|
+
//#endregion
|