@leafer-in/export 1.6.7 → 1.8.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/dist/export.cjs +108 -100
- package/dist/export.esm.js +109 -101
- package/dist/export.esm.min.js +1 -1
- package/dist/export.esm.min.js.map +1 -1
- package/dist/export.js +108 -100
- package/dist/export.min.cjs +1 -1
- package/dist/export.min.cjs.map +1 -1
- package/dist/export.min.js +1 -1
- package/dist/export.min.js.map +1 -1
- package/package.json +4 -4
- package/src/export.ts +107 -99
- package/src/trim.ts +5 -2
package/dist/export.cjs
CHANGED
|
@@ -48,123 +48,131 @@ function getTrimBounds(canvas) {
|
|
|
48
48
|
index++;
|
|
49
49
|
}
|
|
50
50
|
const bounds = new draw.Bounds();
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
if (pointBounds) {
|
|
52
|
+
toBounds(pointBounds, bounds);
|
|
53
|
+
bounds.scale(1 / canvas.pixelRatio).ceil();
|
|
54
|
+
}
|
|
55
|
+
return bounds;
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
const ExportModule = {
|
|
56
59
|
syncExport(leaf, filename, options) {
|
|
57
|
-
|
|
60
|
+
draw.Export.running = true;
|
|
58
61
|
let result;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
else if (fileType === 'svg') {
|
|
69
|
-
isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
|
|
70
|
-
result = { data: isDownload ? true : leaf.toSVG() };
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
|
|
74
|
-
const { worldTransform, isLeafer, leafer, isFrame } = leaf;
|
|
75
|
-
const { slice, clip, trim, screenshot, padding, onCanvas } = options;
|
|
76
|
-
const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
|
|
77
|
-
const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
|
|
78
|
-
const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
|
|
79
|
-
const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
|
|
80
|
-
if (screenshot) {
|
|
81
|
-
renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
|
|
62
|
+
try {
|
|
63
|
+
const fileType = draw.FileHelper.fileType(filename);
|
|
64
|
+
const isDownload = filename.includes('.');
|
|
65
|
+
options = draw.FileHelper.getExportOptions(options);
|
|
66
|
+
const { toURL } = draw.Platform;
|
|
67
|
+
const { download } = draw.Platform.origin;
|
|
68
|
+
if (fileType === 'json') {
|
|
69
|
+
isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
|
|
70
|
+
result = { data: isDownload ? true : leaf.toJSON(options.json) };
|
|
82
71
|
}
|
|
83
|
-
else {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
scaleY = worldTransform.scaleY;
|
|
87
|
-
switch (relative) {
|
|
88
|
-
case 'inner':
|
|
89
|
-
matrix.set(worldTransform);
|
|
90
|
-
break;
|
|
91
|
-
case 'local':
|
|
92
|
-
matrix.set(worldTransform).divide(leaf.localTransform);
|
|
93
|
-
scaleX /= leaf.scaleX;
|
|
94
|
-
scaleY /= leaf.scaleY;
|
|
95
|
-
break;
|
|
96
|
-
case 'world':
|
|
97
|
-
scaleX = 1;
|
|
98
|
-
scaleY = 1;
|
|
99
|
-
break;
|
|
100
|
-
case 'page':
|
|
101
|
-
relative = leafer || leaf;
|
|
102
|
-
default:
|
|
103
|
-
matrix.set(worldTransform).divide(leaf.getTransform(relative));
|
|
104
|
-
const l = relative.worldTransform;
|
|
105
|
-
scaleX /= scaleX / l.scaleX;
|
|
106
|
-
scaleY /= scaleY / l.scaleY;
|
|
107
|
-
}
|
|
108
|
-
renderBounds = leaf.getBounds('render', relative);
|
|
109
|
-
}
|
|
110
|
-
const scaleData = { scaleX: 1, scaleY: 1 };
|
|
111
|
-
draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
|
|
112
|
-
let pixelRatio = options.pixelRatio || 1;
|
|
113
|
-
let { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
|
|
114
|
-
if (clip)
|
|
115
|
-
x += clip.x, y += clip.y, width = clip.width, height = clip.height;
|
|
116
|
-
const renderOptions = { exporting: true, matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
|
|
117
|
-
let canvas = draw.Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
|
|
118
|
-
let sliceLeaf;
|
|
119
|
-
if (slice) {
|
|
120
|
-
sliceLeaf = leaf;
|
|
121
|
-
sliceLeaf.__worldOpacity = 0;
|
|
122
|
-
leaf = leafer || leaf;
|
|
123
|
-
renderOptions.bounds = canvas.bounds;
|
|
124
|
-
}
|
|
125
|
-
canvas.save();
|
|
126
|
-
if (isFrame && fill !== undefined) {
|
|
127
|
-
const oldFill = leaf.get('fill');
|
|
128
|
-
leaf.fill = '';
|
|
129
|
-
leaf.__render(canvas, renderOptions);
|
|
130
|
-
leaf.fill = oldFill;
|
|
72
|
+
else if (fileType === 'svg') {
|
|
73
|
+
isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
|
|
74
|
+
result = { data: isDownload ? true : leaf.toSVG() };
|
|
131
75
|
}
|
|
132
76
|
else {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
77
|
+
let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
|
|
78
|
+
const { worldTransform, isLeafer, leafer, isFrame } = leaf;
|
|
79
|
+
const { slice, clip, trim, screenshot, padding, onCanvas } = options;
|
|
80
|
+
const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
|
|
81
|
+
const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
|
|
82
|
+
const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
|
|
83
|
+
const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
|
|
84
|
+
if (screenshot) {
|
|
85
|
+
renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
let relative = options.relative || (isLeafer ? 'inner' : 'local');
|
|
89
|
+
scaleX = worldTransform.scaleX;
|
|
90
|
+
scaleY = worldTransform.scaleY;
|
|
91
|
+
switch (relative) {
|
|
92
|
+
case 'inner':
|
|
93
|
+
matrix.set(worldTransform);
|
|
94
|
+
break;
|
|
95
|
+
case 'local':
|
|
96
|
+
matrix.set(worldTransform).divide(leaf.localTransform);
|
|
97
|
+
scaleX /= leaf.scaleX;
|
|
98
|
+
scaleY /= leaf.scaleY;
|
|
99
|
+
break;
|
|
100
|
+
case 'world':
|
|
101
|
+
scaleX = 1;
|
|
102
|
+
scaleY = 1;
|
|
103
|
+
break;
|
|
104
|
+
case 'page':
|
|
105
|
+
relative = leafer || leaf;
|
|
106
|
+
default:
|
|
107
|
+
matrix.set(worldTransform).divide(leaf.getTransform(relative));
|
|
108
|
+
const l = relative.worldTransform;
|
|
109
|
+
scaleX /= scaleX / l.scaleX;
|
|
110
|
+
scaleY /= scaleY / l.scaleY;
|
|
111
|
+
}
|
|
112
|
+
renderBounds = leaf.getBounds('render', relative);
|
|
113
|
+
}
|
|
114
|
+
const scaleData = { scaleX: 1, scaleY: 1 };
|
|
115
|
+
draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
|
|
116
|
+
let pixelRatio = options.pixelRatio || 1;
|
|
117
|
+
let { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
|
|
118
|
+
if (clip)
|
|
119
|
+
x += clip.x, y += clip.y, width = clip.width, height = clip.height;
|
|
120
|
+
const renderOptions = { exporting: true, matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
|
|
121
|
+
let canvas = draw.Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
|
|
122
|
+
let sliceLeaf;
|
|
123
|
+
if (slice) {
|
|
124
|
+
sliceLeaf = leaf;
|
|
125
|
+
sliceLeaf.__worldOpacity = 0;
|
|
126
|
+
leaf = leafer || leaf;
|
|
127
|
+
renderOptions.bounds = canvas.bounds;
|
|
128
|
+
}
|
|
129
|
+
canvas.save();
|
|
130
|
+
if (isFrame && fill !== undefined) {
|
|
131
|
+
const oldFill = leaf.get('fill');
|
|
132
|
+
leaf.fill = '';
|
|
133
|
+
leaf.__render(canvas, renderOptions);
|
|
134
|
+
leaf.fill = oldFill;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
leaf.__render(canvas, renderOptions);
|
|
138
|
+
}
|
|
139
|
+
canvas.restore();
|
|
140
|
+
if (sliceLeaf)
|
|
141
|
+
sliceLeaf.__updateWorldOpacity();
|
|
142
|
+
if (trim) {
|
|
143
|
+
trimBounds = getTrimBounds(canvas);
|
|
144
|
+
const old = canvas, { width, height } = trimBounds;
|
|
145
|
+
const config = { x: 0, y: 0, width, height, pixelRatio };
|
|
146
|
+
canvas = draw.Creator.canvas(config);
|
|
147
|
+
canvas.copyWorld(old, trimBounds, config);
|
|
148
|
+
}
|
|
149
|
+
if (padding) {
|
|
150
|
+
const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
|
|
151
|
+
const old = canvas, { width, height } = old;
|
|
152
|
+
canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
|
|
153
|
+
canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
|
|
154
|
+
}
|
|
155
|
+
if (needFill)
|
|
156
|
+
canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
|
|
157
|
+
if (onCanvas)
|
|
158
|
+
onCanvas(canvas);
|
|
159
|
+
const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
|
|
160
|
+
result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
|
|
150
161
|
}
|
|
151
|
-
if (needFill)
|
|
152
|
-
canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
|
|
153
|
-
if (onCanvas)
|
|
154
|
-
onCanvas(canvas);
|
|
155
|
-
const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
|
|
156
|
-
result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
|
|
157
162
|
}
|
|
158
|
-
|
|
163
|
+
catch (error) {
|
|
164
|
+
result = { data: '', error };
|
|
165
|
+
}
|
|
166
|
+
draw.Export.running = false;
|
|
159
167
|
return result;
|
|
160
168
|
},
|
|
161
169
|
export(leaf, filename, options) {
|
|
162
|
-
|
|
170
|
+
draw.Export.running = true;
|
|
163
171
|
return addTask((success) => new Promise((resolve) => {
|
|
164
172
|
const getResult = () => __awaiter(this, void 0, void 0, function* () {
|
|
165
173
|
if (!draw.Resource.isComplete)
|
|
166
174
|
return draw.Platform.requestRender(getResult);
|
|
167
|
-
const result =
|
|
175
|
+
const result = draw.Export.syncExport(leaf, filename, options);
|
|
168
176
|
if (result.data instanceof Promise)
|
|
169
177
|
result.data = yield result.data;
|
|
170
178
|
success(result);
|
package/dist/export.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TwoPointBoundsHelper, Bounds, FileHelper, Platform, Matrix, MathHelper, Creator, TaskProcessor, Resource, LeaferCanvasBase, Debug, Plugin,
|
|
1
|
+
import { TwoPointBoundsHelper, Bounds, Export, FileHelper, Platform, Matrix, MathHelper, Creator, TaskProcessor, Resource, LeaferCanvasBase, Debug, Plugin, UI } from '@leafer-ui/draw';
|
|
2
2
|
|
|
3
3
|
/******************************************************************************
|
|
4
4
|
Copyright (c) Microsoft Corporation.
|
|
@@ -46,123 +46,131 @@ function getTrimBounds(canvas) {
|
|
|
46
46
|
index++;
|
|
47
47
|
}
|
|
48
48
|
const bounds = new Bounds();
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
if (pointBounds) {
|
|
50
|
+
toBounds(pointBounds, bounds);
|
|
51
|
+
bounds.scale(1 / canvas.pixelRatio).ceil();
|
|
52
|
+
}
|
|
53
|
+
return bounds;
|
|
51
54
|
}
|
|
52
55
|
|
|
53
56
|
const ExportModule = {
|
|
54
57
|
syncExport(leaf, filename, options) {
|
|
55
|
-
|
|
58
|
+
Export.running = true;
|
|
56
59
|
let result;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
else if (fileType === 'svg') {
|
|
67
|
-
isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
|
|
68
|
-
result = { data: isDownload ? true : leaf.toSVG() };
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
|
|
72
|
-
const { worldTransform, isLeafer, leafer, isFrame } = leaf;
|
|
73
|
-
const { slice, clip, trim, screenshot, padding, onCanvas } = options;
|
|
74
|
-
const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
|
|
75
|
-
const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
|
|
76
|
-
const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
|
|
77
|
-
const needFill = FileHelper.isOpaqueImage(filename) || fill, matrix = new Matrix();
|
|
78
|
-
if (screenshot) {
|
|
79
|
-
renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
|
|
60
|
+
try {
|
|
61
|
+
const fileType = FileHelper.fileType(filename);
|
|
62
|
+
const isDownload = filename.includes('.');
|
|
63
|
+
options = FileHelper.getExportOptions(options);
|
|
64
|
+
const { toURL } = Platform;
|
|
65
|
+
const { download } = Platform.origin;
|
|
66
|
+
if (fileType === 'json') {
|
|
67
|
+
isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
|
|
68
|
+
result = { data: isDownload ? true : leaf.toJSON(options.json) };
|
|
80
69
|
}
|
|
81
|
-
else {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
scaleY = worldTransform.scaleY;
|
|
85
|
-
switch (relative) {
|
|
86
|
-
case 'inner':
|
|
87
|
-
matrix.set(worldTransform);
|
|
88
|
-
break;
|
|
89
|
-
case 'local':
|
|
90
|
-
matrix.set(worldTransform).divide(leaf.localTransform);
|
|
91
|
-
scaleX /= leaf.scaleX;
|
|
92
|
-
scaleY /= leaf.scaleY;
|
|
93
|
-
break;
|
|
94
|
-
case 'world':
|
|
95
|
-
scaleX = 1;
|
|
96
|
-
scaleY = 1;
|
|
97
|
-
break;
|
|
98
|
-
case 'page':
|
|
99
|
-
relative = leafer || leaf;
|
|
100
|
-
default:
|
|
101
|
-
matrix.set(worldTransform).divide(leaf.getTransform(relative));
|
|
102
|
-
const l = relative.worldTransform;
|
|
103
|
-
scaleX /= scaleX / l.scaleX;
|
|
104
|
-
scaleY /= scaleY / l.scaleY;
|
|
105
|
-
}
|
|
106
|
-
renderBounds = leaf.getBounds('render', relative);
|
|
107
|
-
}
|
|
108
|
-
const scaleData = { scaleX: 1, scaleY: 1 };
|
|
109
|
-
MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
|
|
110
|
-
let pixelRatio = options.pixelRatio || 1;
|
|
111
|
-
let { x, y, width, height } = new Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
|
|
112
|
-
if (clip)
|
|
113
|
-
x += clip.x, y += clip.y, width = clip.width, height = clip.height;
|
|
114
|
-
const renderOptions = { exporting: true, matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
|
|
115
|
-
let canvas = Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
|
|
116
|
-
let sliceLeaf;
|
|
117
|
-
if (slice) {
|
|
118
|
-
sliceLeaf = leaf;
|
|
119
|
-
sliceLeaf.__worldOpacity = 0;
|
|
120
|
-
leaf = leafer || leaf;
|
|
121
|
-
renderOptions.bounds = canvas.bounds;
|
|
122
|
-
}
|
|
123
|
-
canvas.save();
|
|
124
|
-
if (isFrame && fill !== undefined) {
|
|
125
|
-
const oldFill = leaf.get('fill');
|
|
126
|
-
leaf.fill = '';
|
|
127
|
-
leaf.__render(canvas, renderOptions);
|
|
128
|
-
leaf.fill = oldFill;
|
|
70
|
+
else if (fileType === 'svg') {
|
|
71
|
+
isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
|
|
72
|
+
result = { data: isDownload ? true : leaf.toSVG() };
|
|
129
73
|
}
|
|
130
74
|
else {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
75
|
+
let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
|
|
76
|
+
const { worldTransform, isLeafer, leafer, isFrame } = leaf;
|
|
77
|
+
const { slice, clip, trim, screenshot, padding, onCanvas } = options;
|
|
78
|
+
const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
|
|
79
|
+
const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
|
|
80
|
+
const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
|
|
81
|
+
const needFill = FileHelper.isOpaqueImage(filename) || fill, matrix = new Matrix();
|
|
82
|
+
if (screenshot) {
|
|
83
|
+
renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
let relative = options.relative || (isLeafer ? 'inner' : 'local');
|
|
87
|
+
scaleX = worldTransform.scaleX;
|
|
88
|
+
scaleY = worldTransform.scaleY;
|
|
89
|
+
switch (relative) {
|
|
90
|
+
case 'inner':
|
|
91
|
+
matrix.set(worldTransform);
|
|
92
|
+
break;
|
|
93
|
+
case 'local':
|
|
94
|
+
matrix.set(worldTransform).divide(leaf.localTransform);
|
|
95
|
+
scaleX /= leaf.scaleX;
|
|
96
|
+
scaleY /= leaf.scaleY;
|
|
97
|
+
break;
|
|
98
|
+
case 'world':
|
|
99
|
+
scaleX = 1;
|
|
100
|
+
scaleY = 1;
|
|
101
|
+
break;
|
|
102
|
+
case 'page':
|
|
103
|
+
relative = leafer || leaf;
|
|
104
|
+
default:
|
|
105
|
+
matrix.set(worldTransform).divide(leaf.getTransform(relative));
|
|
106
|
+
const l = relative.worldTransform;
|
|
107
|
+
scaleX /= scaleX / l.scaleX;
|
|
108
|
+
scaleY /= scaleY / l.scaleY;
|
|
109
|
+
}
|
|
110
|
+
renderBounds = leaf.getBounds('render', relative);
|
|
111
|
+
}
|
|
112
|
+
const scaleData = { scaleX: 1, scaleY: 1 };
|
|
113
|
+
MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
|
|
114
|
+
let pixelRatio = options.pixelRatio || 1;
|
|
115
|
+
let { x, y, width, height } = new Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
|
|
116
|
+
if (clip)
|
|
117
|
+
x += clip.x, y += clip.y, width = clip.width, height = clip.height;
|
|
118
|
+
const renderOptions = { exporting: true, matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
|
|
119
|
+
let canvas = Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
|
|
120
|
+
let sliceLeaf;
|
|
121
|
+
if (slice) {
|
|
122
|
+
sliceLeaf = leaf;
|
|
123
|
+
sliceLeaf.__worldOpacity = 0;
|
|
124
|
+
leaf = leafer || leaf;
|
|
125
|
+
renderOptions.bounds = canvas.bounds;
|
|
126
|
+
}
|
|
127
|
+
canvas.save();
|
|
128
|
+
if (isFrame && fill !== undefined) {
|
|
129
|
+
const oldFill = leaf.get('fill');
|
|
130
|
+
leaf.fill = '';
|
|
131
|
+
leaf.__render(canvas, renderOptions);
|
|
132
|
+
leaf.fill = oldFill;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
leaf.__render(canvas, renderOptions);
|
|
136
|
+
}
|
|
137
|
+
canvas.restore();
|
|
138
|
+
if (sliceLeaf)
|
|
139
|
+
sliceLeaf.__updateWorldOpacity();
|
|
140
|
+
if (trim) {
|
|
141
|
+
trimBounds = getTrimBounds(canvas);
|
|
142
|
+
const old = canvas, { width, height } = trimBounds;
|
|
143
|
+
const config = { x: 0, y: 0, width, height, pixelRatio };
|
|
144
|
+
canvas = Creator.canvas(config);
|
|
145
|
+
canvas.copyWorld(old, trimBounds, config);
|
|
146
|
+
}
|
|
147
|
+
if (padding) {
|
|
148
|
+
const [top, right, bottom, left] = MathHelper.fourNumber(padding);
|
|
149
|
+
const old = canvas, { width, height } = old;
|
|
150
|
+
canvas = Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
|
|
151
|
+
canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
|
|
152
|
+
}
|
|
153
|
+
if (needFill)
|
|
154
|
+
canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
|
|
155
|
+
if (onCanvas)
|
|
156
|
+
onCanvas(canvas);
|
|
157
|
+
const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
|
|
158
|
+
result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
|
|
148
159
|
}
|
|
149
|
-
if (needFill)
|
|
150
|
-
canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
|
|
151
|
-
if (onCanvas)
|
|
152
|
-
onCanvas(canvas);
|
|
153
|
-
const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
|
|
154
|
-
result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
|
|
155
160
|
}
|
|
156
|
-
|
|
161
|
+
catch (error) {
|
|
162
|
+
result = { data: '', error };
|
|
163
|
+
}
|
|
164
|
+
Export.running = false;
|
|
157
165
|
return result;
|
|
158
166
|
},
|
|
159
167
|
export(leaf, filename, options) {
|
|
160
|
-
|
|
168
|
+
Export.running = true;
|
|
161
169
|
return addTask((success) => new Promise((resolve) => {
|
|
162
170
|
const getResult = () => __awaiter(this, void 0, void 0, function* () {
|
|
163
171
|
if (!Resource.isComplete)
|
|
164
172
|
return Platform.requestRender(getResult);
|
|
165
|
-
const result =
|
|
173
|
+
const result = Export.syncExport(leaf, filename, options);
|
|
166
174
|
if (result.data instanceof Promise)
|
|
167
175
|
result.data = yield result.data;
|
|
168
176
|
success(result);
|
package/dist/export.esm.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{TwoPointBoundsHelper as t,Bounds as e,FileHelper as n,Platform as
|
|
1
|
+
import{TwoPointBoundsHelper as t,Bounds as e,Export as o,FileHelper as n,Platform as i,Matrix as r,MathHelper as a,Creator as s,TaskProcessor as c,Resource as l,LeaferCanvasBase as d,Debug as h,Plugin as u,UI as f}from"@leafer-ui/draw";function p(t,e,o,n){return new(o||(o=Promise))((function(i,r){function a(t){try{c(n.next(t))}catch(t){r(t)}}function s(t){try{c(n.throw(t))}catch(t){r(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(a,s)}c((n=n.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const{setPoint:g,addPoint:w,toBounds:v}=t;const x={syncExport(t,c,l){let d;o.running=!0;try{const o=n.fileType(c),h=c.includes(".");l=n.getExportOptions(l);const{toURL:u}=i,{download:f}=i.origin;if("json"===o)h&&f(u(JSON.stringify(t.toJSON(l.json)),"text"),c),d={data:!!h||t.toJSON(l.json)};else if("svg"===o)h&&f(u(t.toSVG(),"svg"),c),d={data:!!h||t.toSVG()};else{let o,i,h=1,u=1;const{worldTransform:f,isLeafer:p,leafer:x,isFrame:m}=t,{slice:y,clip:_,trim:b,screenshot:S,padding:R,onCanvas:P}=l,B=void 0===l.smooth?!x||x.config.smooth:l.smooth,E=l.contextSettings||(x?x.config.contextSettings:void 0),O=p&&S&&void 0===l.fill?t.fill:l.fill,F=n.isOpaqueImage(c)||O,T=new r;if(S)o=!0===S?p?x.canvas.bounds:t.worldRenderBounds:S;else{let e=l.relative||(p?"inner":"local");switch(h=f.scaleX,u=f.scaleY,e){case"inner":T.set(f);break;case"local":T.set(f).divide(t.localTransform),h/=t.scaleX,u/=t.scaleY;break;case"world":h=1,u=1;break;case"page":e=x||t;default:T.set(f).divide(t.getTransform(e));const o=e.worldTransform;h/=h/o.scaleX,u/=u/o.scaleY}o=t.getBounds("render",e)}const X={scaleX:1,scaleY:1};a.getScaleData(l.scale,l.size,o,X);let Y=l.pixelRatio||1,{x:L,y:D,width:W,height:j}=new e(o).scale(X.scaleX,X.scaleY);_&&(L+=_.x,D+=_.y,W=_.width,j=_.height);const C={exporting:!0,matrix:T.scale(1/X.scaleX,1/X.scaleY).invert().translate(-L,-D).withScale(1/h*X.scaleX,1/u*X.scaleY)};let N,U=s.canvas({width:Math.floor(W),height:Math.floor(j),pixelRatio:Y,smooth:B,contextSettings:E});if(y&&(N=t,N.__worldOpacity=0,t=x||t,C.bounds=U.bounds),U.save(),m&&void 0!==O){const e=t.get("fill");t.fill="",t.__render(U,C),t.fill=e}else t.__render(U,C);if(U.restore(),N&&N.__updateWorldOpacity(),b){i=function(t){const{width:o,height:n}=t.view,{data:i}=t.context.getImageData(0,0,o,n);let r,a,s,c=0;for(let t=0;t<i.length;t+=4)0!==i[t+3]&&(r=c%o,a=(c-r)/o,s?w(s,r,a):g(s={},r,a)),c++;const l=new e;return s&&(v(s,l),l.scale(1/t.pixelRatio).ceil()),l}(U);const t=U,{width:o,height:n}=i,r={x:0,y:0,width:o,height:n,pixelRatio:Y};U=s.canvas(r),U.copyWorld(t,i,r)}if(R){const[t,e,o,n]=a.fourNumber(R),i=U,{width:r,height:c}=i;U=s.canvas({width:r+n+e,height:c+t+o,pixelRatio:Y}),U.copyWorld(i,i.bounds,{x:n,y:t,width:r,height:c})}F&&U.fillWorld(U.bounds,O||"#FFFFFF","destination-over"),P&&P(U);d={data:"canvas"===c?U:U.export(c,l),width:U.pixelWidth,height:U.pixelHeight,renderBounds:o,trimBounds:i}}}catch(t){d={data:"",error:t}}return o.running=!1,d},export(t,e,n){return o.running=!0,function(t){m||(m=new c);return new Promise((e=>{m.add((()=>p(this,void 0,void 0,(function*(){return yield t(e)}))),{parallel:!1})}))}((r=>new Promise((a=>{const s=()=>p(this,void 0,void 0,(function*(){if(!l.isComplete)return i.requestRender(s);const c=o.syncExport(t,e,n);c.data instanceof Promise&&(c.data=yield c.data),r(c),a()}));t.updateLayout(),y(t);const{leafer:c}=t;c?c.waitViewCompleted(s):s()}))))}};let m;function y(t){t.__.__needComputePaint&&t.__.__computePaint(),t.isBranch&&t.children.forEach((t=>y(t)))}const _=d.prototype,b=h.get("@leafer-in/export");_.export=function(t,e){const{quality:o,blob:i}=n.getExportOptions(e);return t.includes(".")?this.saveAs(t,o):i?this.toBlob(t,o):this.toDataURL(t,o)},_.toBlob=function(t,e){return new Promise((o=>{i.origin.canvasToBolb(this.view,t,e).then((t=>{o(t)})).catch((t=>{b.error(t),o(null)}))}))},_.toDataURL=function(t,e){return i.origin.canvasToDataURL(this.view,t,e)},_.saveAs=function(t,e){return new Promise((o=>{i.origin.canvasSaveAs(this.view,t,e).then((()=>{o(!0)})).catch((t=>{b.error(t),o(!1)}))}))},u.add("export"),Object.assign(o,x),f.prototype.export=function(t,e){return o.export(this,t,e)},f.prototype.syncExport=function(t,e){return o.syncExport(this,t,e)};
|
|
2
2
|
//# sourceMappingURL=export.esm.min.js.map
|