@leafer-draw/worker 1.2.1 → 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/dist/worker.cjs CHANGED
@@ -69,6 +69,7 @@ function useCanvas(_canvasType, _power) {
69
69
  }
70
70
  core.Platform.name = 'web';
71
71
  core.Platform.isWorker = true;
72
+ core.Platform.backgrounder = true;
72
73
  core.Platform.requestRender = function (render) { requestAnimationFrame(render); };
73
74
  core.defineKey(core.Platform, 'devicePixelRatio', { get() { return 1; } });
74
75
  const { userAgent } = navigator;
@@ -275,7 +276,7 @@ class LayoutBlockData {
275
276
  }
276
277
 
277
278
  const { updateAllMatrix, updateAllChange } = core.LeafHelper;
278
- const debug$2 = core.Debug.get('Layouter');
279
+ const debug$1 = core.Debug.get('Layouter');
279
280
  class Layouter {
280
281
  constructor(target, userConfig) {
281
282
  this.totalTimes = 0;
@@ -310,7 +311,7 @@ class Layouter {
310
311
  target.emitEvent(new core.LayoutEvent(core.LayoutEvent.END, this.layoutedBlocks, this.times));
311
312
  }
312
313
  catch (e) {
313
- debug$2.error(e);
314
+ debug$1.error(e);
314
315
  }
315
316
  this.layoutedBlocks = null;
316
317
  }
@@ -324,9 +325,9 @@ class Layouter {
324
325
  }
325
326
  layoutOnce() {
326
327
  if (this.layouting)
327
- return debug$2.warn('layouting');
328
+ return debug$1.warn('layouting');
328
329
  if (this.times > 3)
329
- return debug$2.warn('layout max times');
330
+ return debug$1.warn('layout max times');
330
331
  this.times++;
331
332
  this.totalTimes++;
332
333
  this.layouting = true;
@@ -430,7 +431,7 @@ class Layouter {
430
431
  }
431
432
  }
432
433
 
433
- const debug$1 = core.Debug.get('Renderer');
434
+ const debug = core.Debug.get('Renderer');
434
435
  class Renderer {
435
436
  get needFill() { return !!(!this.canvas.allowBackgroundColor && this.config.fill); }
436
437
  constructor(target, canvas, userConfig) {
@@ -468,7 +469,7 @@ class Renderer {
468
469
  const { target } = this;
469
470
  this.times = 0;
470
471
  this.totalBounds = new core.Bounds();
471
- debug$1.log(target.innerName, '--->');
472
+ debug.log(target.innerName, '--->');
472
473
  try {
473
474
  if (!target.isApp)
474
475
  target.app.emit(core.RenderEvent.CHILD_START, target);
@@ -479,9 +480,9 @@ class Renderer {
479
480
  }
480
481
  catch (e) {
481
482
  this.rendering = false;
482
- debug$1.error(e);
483
+ debug.error(e);
483
484
  }
484
- debug$1.log('-------------|');
485
+ debug.log('-------------|');
485
486
  }
486
487
  renderAgain() {
487
488
  if (this.rendering) {
@@ -493,9 +494,9 @@ class Renderer {
493
494
  }
494
495
  renderOnce(callback) {
495
496
  if (this.rendering)
496
- return debug$1.warn('rendering');
497
+ return debug.warn('rendering');
497
498
  if (this.times > 3)
498
- return debug$1.warn('render max times');
499
+ return debug.warn('render max times');
499
500
  this.times++;
500
501
  this.totalTimes++;
501
502
  this.rendering = true;
@@ -532,7 +533,7 @@ class Renderer {
532
533
  partRender() {
533
534
  const { canvas, updateBlocks: list } = this;
534
535
  if (!list)
535
- return debug$1.warn('PartRender: need update attr');
536
+ return debug.warn('PartRender: need update attr');
536
537
  this.mergeBlocks();
537
538
  list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
538
539
  this.clipRender(block); });
@@ -635,7 +636,7 @@ class Renderer {
635
636
  empty = (!leaf.__world.width || !leaf.__world.height);
636
637
  if (empty) {
637
638
  if (!leaf.isLeafer)
638
- debug$1.tip(leaf.innerName, ': empty');
639
+ debug.tip(leaf.innerName, ': empty');
639
640
  empty = (!leaf.isBranch || leaf.isBranchLeaf);
640
641
  }
641
642
  return empty;
@@ -2187,197 +2188,12 @@ const ColorConvertModule = {
2187
2188
  string
2188
2189
  };
2189
2190
 
2190
- const { setPoint, addPoint, toBounds } = core.TwoPointBoundsHelper;
2191
- function getTrimBounds(canvas) {
2192
- const { width, height } = canvas.view;
2193
- const { data } = canvas.context.getImageData(0, 0, width, height);
2194
- let x, y, pointBounds, index = 0;
2195
- for (let i = 0; i < data.length; i += 4) {
2196
- if (data[i + 3] !== 0) {
2197
- x = index % width;
2198
- y = (index - x) / width;
2199
- pointBounds ? addPoint(pointBounds, x, y) : setPoint(pointBounds = {}, x, y);
2200
- }
2201
- index++;
2202
- }
2203
- const bounds = new core.Bounds();
2204
- toBounds(pointBounds, bounds);
2205
- return bounds.scale(1 / canvas.pixelRatio).ceil();
2206
- }
2207
-
2208
- const ExportModule = {
2209
- export(leaf, filename, options) {
2210
- this.running = true;
2211
- const fileType = core.FileHelper.fileType(filename);
2212
- const isDownload = filename.includes('.');
2213
- options = core.FileHelper.getExportOptions(options);
2214
- return addTask((success) => new Promise((resolve) => {
2215
- const over = (result) => {
2216
- success(result);
2217
- resolve();
2218
- this.running = false;
2219
- };
2220
- const { toURL } = core.Platform;
2221
- const { download } = core.Platform.origin;
2222
- if (fileType === 'json') {
2223
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2224
- return over({ data: isDownload ? true : leaf.toJSON(options.json) });
2225
- }
2226
- if (fileType === 'svg') {
2227
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2228
- return over({ data: isDownload ? true : leaf.toSVG() });
2229
- }
2230
- const { leafer } = leaf;
2231
- if (leafer) {
2232
- checkLazy(leaf);
2233
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2234
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2235
- const { worldTransform, isLeafer, isFrame } = leaf;
2236
- const { slice, trim, onCanvas } = options;
2237
- const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
2238
- const contextSettings = options.contextSettings || leafer.config.contextSettings;
2239
- const screenshot = options.screenshot || leaf.isApp;
2240
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2241
- const needFill = core.FileHelper.isOpaqueImage(filename) || fill, matrix = new core.Matrix();
2242
- if (screenshot) {
2243
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2244
- }
2245
- else {
2246
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2247
- scaleX = worldTransform.scaleX;
2248
- scaleY = worldTransform.scaleY;
2249
- switch (relative) {
2250
- case 'inner':
2251
- matrix.set(worldTransform);
2252
- break;
2253
- case 'local':
2254
- matrix.set(worldTransform).divide(leaf.localTransform);
2255
- scaleX /= leaf.scaleX;
2256
- scaleY /= leaf.scaleY;
2257
- break;
2258
- case 'world':
2259
- scaleX = 1;
2260
- scaleY = 1;
2261
- break;
2262
- case 'page':
2263
- relative = leaf.leafer;
2264
- default:
2265
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2266
- const l = relative.worldTransform;
2267
- scaleX /= scaleX / l.scaleX;
2268
- scaleY /= scaleY / l.scaleY;
2269
- }
2270
- renderBounds = leaf.getBounds('render', relative);
2271
- }
2272
- const scaleData = { scaleX: 1, scaleY: 1 };
2273
- core.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2274
- let pixelRatio = options.pixelRatio || 1;
2275
- if (leaf.isApp) {
2276
- scaleData.scaleX *= pixelRatio;
2277
- scaleData.scaleY *= pixelRatio;
2278
- pixelRatio = leaf.app.pixelRatio;
2279
- }
2280
- const { x, y, width, height } = new core.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2281
- const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2282
- let canvas = core.Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
2283
- let sliceLeaf;
2284
- if (slice) {
2285
- sliceLeaf = leaf;
2286
- sliceLeaf.__worldOpacity = 0;
2287
- leaf = leafer;
2288
- renderOptions.bounds = canvas.bounds;
2289
- }
2290
- canvas.save();
2291
- if (isFrame && fill !== undefined) {
2292
- const oldFill = leaf.get('fill');
2293
- leaf.fill = '';
2294
- leaf.__render(canvas, renderOptions);
2295
- leaf.fill = oldFill;
2296
- }
2297
- else {
2298
- leaf.__render(canvas, renderOptions);
2299
- }
2300
- canvas.restore();
2301
- if (sliceLeaf)
2302
- sliceLeaf.__updateWorldOpacity();
2303
- if (trim) {
2304
- trimBounds = getTrimBounds(canvas);
2305
- const old = canvas, { width, height } = trimBounds;
2306
- const config = { x: 0, y: 0, width, height, pixelRatio };
2307
- canvas = core.Creator.canvas(config);
2308
- canvas.copyWorld(old, trimBounds, config);
2309
- }
2310
- if (needFill)
2311
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2312
- if (onCanvas)
2313
- onCanvas(canvas);
2314
- const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
2315
- over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
2316
- }));
2317
- }
2318
- else {
2319
- over({ data: false });
2320
- }
2321
- }));
2322
- }
2323
- };
2324
- let tasker;
2325
- function addTask(task) {
2326
- if (!tasker)
2327
- tasker = new core.TaskProcessor();
2328
- return new Promise((resolve) => {
2329
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
2330
- });
2331
- }
2332
- function checkLazy(leaf) {
2333
- if (leaf.__.__needComputePaint)
2334
- leaf.__.__computePaint();
2335
- if (leaf.isBranch)
2336
- leaf.children.forEach(child => checkLazy(child));
2337
- }
2338
-
2339
- const canvas = core.LeaferCanvasBase.prototype;
2340
- const debug = core.Debug.get('@leafer-ui/export');
2341
- canvas.export = function (filename, options) {
2342
- const { quality, blob } = core.FileHelper.getExportOptions(options);
2343
- if (filename.includes('.'))
2344
- return this.saveAs(filename, quality);
2345
- else if (blob)
2346
- return this.toBlob(filename, quality);
2347
- else
2348
- return this.toDataURL(filename, quality);
2349
- };
2350
- canvas.toBlob = function (type, quality) {
2351
- return new Promise((resolve) => {
2352
- core.Platform.origin.canvasToBolb(this.view, type, quality).then((blob) => {
2353
- resolve(blob);
2354
- }).catch((e) => {
2355
- debug.error(e);
2356
- resolve(null);
2357
- });
2358
- });
2359
- };
2360
- canvas.toDataURL = function (type, quality) {
2361
- return core.Platform.origin.canvasToDataURL(this.view, type, quality);
2362
- };
2363
- canvas.saveAs = function (filename, quality) {
2364
- return new Promise((resolve) => {
2365
- core.Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
2366
- resolve(true);
2367
- }).catch((e) => {
2368
- debug.error(e);
2369
- resolve(false);
2370
- });
2371
- });
2372
- };
2373
-
2374
2191
  Object.assign(draw.TextConvert, TextConvertModule);
2375
2192
  Object.assign(draw.ColorConvert, ColorConvertModule);
2376
2193
  Object.assign(draw.Paint, PaintModule);
2377
2194
  Object.assign(draw.PaintImage, PaintImageModule);
2378
2195
  Object.assign(draw.PaintGradient, PaintGradientModule);
2379
2196
  Object.assign(draw.Effect, EffectModule);
2380
- Object.assign(draw.Export, ExportModule);
2381
2197
 
2382
2198
  useCanvas();
2383
2199
 
@@ -1,4 +1,4 @@
1
- import { LeaferCanvasBase, Platform, canvasPatch, Creator, LeaferImage, defineKey, FileHelper, LeafList, DataHelper, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, ResizeEvent, BoundsHelper, MatrixHelper, MathHelper, AlignHelper, ImageEvent, AroundHelper, PointHelper, Direction4, TwoPointBoundsHelper, TaskProcessor, Matrix } from '@leafer/core';
1
+ import { LeaferCanvasBase, Platform, canvasPatch, Creator, LeaferImage, defineKey, FileHelper, LeafList, DataHelper, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, ResizeEvent, BoundsHelper, MatrixHelper, MathHelper, AlignHelper, ImageEvent, AroundHelper, PointHelper, Direction4 } from '@leafer/core';
2
2
  export * from '@leafer/core';
3
3
  export { LeaferImage } from '@leafer/core';
4
4
  import { PaintImage, ColorConvert, PaintGradient, Export, Group, TextConvert, Paint, Effect } from '@leafer-ui/draw';
@@ -70,6 +70,7 @@ function useCanvas(_canvasType, _power) {
70
70
  }
71
71
  Platform.name = 'web';
72
72
  Platform.isWorker = true;
73
+ Platform.backgrounder = true;
73
74
  Platform.requestRender = function (render) { requestAnimationFrame(render); };
74
75
  defineKey(Platform, 'devicePixelRatio', { get() { return 1; } });
75
76
  const { userAgent } = navigator;
@@ -276,7 +277,7 @@ class LayoutBlockData {
276
277
  }
277
278
 
278
279
  const { updateAllMatrix, updateAllChange } = LeafHelper;
279
- const debug$2 = Debug.get('Layouter');
280
+ const debug$1 = Debug.get('Layouter');
280
281
  class Layouter {
281
282
  constructor(target, userConfig) {
282
283
  this.totalTimes = 0;
@@ -311,7 +312,7 @@ class Layouter {
311
312
  target.emitEvent(new LayoutEvent(LayoutEvent.END, this.layoutedBlocks, this.times));
312
313
  }
313
314
  catch (e) {
314
- debug$2.error(e);
315
+ debug$1.error(e);
315
316
  }
316
317
  this.layoutedBlocks = null;
317
318
  }
@@ -325,9 +326,9 @@ class Layouter {
325
326
  }
326
327
  layoutOnce() {
327
328
  if (this.layouting)
328
- return debug$2.warn('layouting');
329
+ return debug$1.warn('layouting');
329
330
  if (this.times > 3)
330
- return debug$2.warn('layout max times');
331
+ return debug$1.warn('layout max times');
331
332
  this.times++;
332
333
  this.totalTimes++;
333
334
  this.layouting = true;
@@ -431,7 +432,7 @@ class Layouter {
431
432
  }
432
433
  }
433
434
 
434
- const debug$1 = Debug.get('Renderer');
435
+ const debug = Debug.get('Renderer');
435
436
  class Renderer {
436
437
  get needFill() { return !!(!this.canvas.allowBackgroundColor && this.config.fill); }
437
438
  constructor(target, canvas, userConfig) {
@@ -469,7 +470,7 @@ class Renderer {
469
470
  const { target } = this;
470
471
  this.times = 0;
471
472
  this.totalBounds = new Bounds();
472
- debug$1.log(target.innerName, '--->');
473
+ debug.log(target.innerName, '--->');
473
474
  try {
474
475
  if (!target.isApp)
475
476
  target.app.emit(RenderEvent.CHILD_START, target);
@@ -480,9 +481,9 @@ class Renderer {
480
481
  }
481
482
  catch (e) {
482
483
  this.rendering = false;
483
- debug$1.error(e);
484
+ debug.error(e);
484
485
  }
485
- debug$1.log('-------------|');
486
+ debug.log('-------------|');
486
487
  }
487
488
  renderAgain() {
488
489
  if (this.rendering) {
@@ -494,9 +495,9 @@ class Renderer {
494
495
  }
495
496
  renderOnce(callback) {
496
497
  if (this.rendering)
497
- return debug$1.warn('rendering');
498
+ return debug.warn('rendering');
498
499
  if (this.times > 3)
499
- return debug$1.warn('render max times');
500
+ return debug.warn('render max times');
500
501
  this.times++;
501
502
  this.totalTimes++;
502
503
  this.rendering = true;
@@ -533,7 +534,7 @@ class Renderer {
533
534
  partRender() {
534
535
  const { canvas, updateBlocks: list } = this;
535
536
  if (!list)
536
- return debug$1.warn('PartRender: need update attr');
537
+ return debug.warn('PartRender: need update attr');
537
538
  this.mergeBlocks();
538
539
  list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
539
540
  this.clipRender(block); });
@@ -636,7 +637,7 @@ class Renderer {
636
637
  empty = (!leaf.__world.width || !leaf.__world.height);
637
638
  if (empty) {
638
639
  if (!leaf.isLeafer)
639
- debug$1.tip(leaf.innerName, ': empty');
640
+ debug.tip(leaf.innerName, ': empty');
640
641
  empty = (!leaf.isBranch || leaf.isBranchLeaf);
641
642
  }
642
643
  return empty;
@@ -2188,197 +2189,12 @@ const ColorConvertModule = {
2188
2189
  string
2189
2190
  };
2190
2191
 
2191
- const { setPoint, addPoint, toBounds } = TwoPointBoundsHelper;
2192
- function getTrimBounds(canvas) {
2193
- const { width, height } = canvas.view;
2194
- const { data } = canvas.context.getImageData(0, 0, width, height);
2195
- let x, y, pointBounds, index = 0;
2196
- for (let i = 0; i < data.length; i += 4) {
2197
- if (data[i + 3] !== 0) {
2198
- x = index % width;
2199
- y = (index - x) / width;
2200
- pointBounds ? addPoint(pointBounds, x, y) : setPoint(pointBounds = {}, x, y);
2201
- }
2202
- index++;
2203
- }
2204
- const bounds = new Bounds();
2205
- toBounds(pointBounds, bounds);
2206
- return bounds.scale(1 / canvas.pixelRatio).ceil();
2207
- }
2208
-
2209
- const ExportModule = {
2210
- export(leaf, filename, options) {
2211
- this.running = true;
2212
- const fileType = FileHelper.fileType(filename);
2213
- const isDownload = filename.includes('.');
2214
- options = FileHelper.getExportOptions(options);
2215
- return addTask((success) => new Promise((resolve) => {
2216
- const over = (result) => {
2217
- success(result);
2218
- resolve();
2219
- this.running = false;
2220
- };
2221
- const { toURL } = Platform;
2222
- const { download } = Platform.origin;
2223
- if (fileType === 'json') {
2224
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2225
- return over({ data: isDownload ? true : leaf.toJSON(options.json) });
2226
- }
2227
- if (fileType === 'svg') {
2228
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2229
- return over({ data: isDownload ? true : leaf.toSVG() });
2230
- }
2231
- const { leafer } = leaf;
2232
- if (leafer) {
2233
- checkLazy(leaf);
2234
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2235
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2236
- const { worldTransform, isLeafer, isFrame } = leaf;
2237
- const { slice, trim, onCanvas } = options;
2238
- const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
2239
- const contextSettings = options.contextSettings || leafer.config.contextSettings;
2240
- const screenshot = options.screenshot || leaf.isApp;
2241
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2242
- const needFill = FileHelper.isOpaqueImage(filename) || fill, matrix = new Matrix();
2243
- if (screenshot) {
2244
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2245
- }
2246
- else {
2247
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2248
- scaleX = worldTransform.scaleX;
2249
- scaleY = worldTransform.scaleY;
2250
- switch (relative) {
2251
- case 'inner':
2252
- matrix.set(worldTransform);
2253
- break;
2254
- case 'local':
2255
- matrix.set(worldTransform).divide(leaf.localTransform);
2256
- scaleX /= leaf.scaleX;
2257
- scaleY /= leaf.scaleY;
2258
- break;
2259
- case 'world':
2260
- scaleX = 1;
2261
- scaleY = 1;
2262
- break;
2263
- case 'page':
2264
- relative = leaf.leafer;
2265
- default:
2266
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2267
- const l = relative.worldTransform;
2268
- scaleX /= scaleX / l.scaleX;
2269
- scaleY /= scaleY / l.scaleY;
2270
- }
2271
- renderBounds = leaf.getBounds('render', relative);
2272
- }
2273
- const scaleData = { scaleX: 1, scaleY: 1 };
2274
- MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2275
- let pixelRatio = options.pixelRatio || 1;
2276
- if (leaf.isApp) {
2277
- scaleData.scaleX *= pixelRatio;
2278
- scaleData.scaleY *= pixelRatio;
2279
- pixelRatio = leaf.app.pixelRatio;
2280
- }
2281
- const { x, y, width, height } = new Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2282
- const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2283
- let canvas = Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
2284
- let sliceLeaf;
2285
- if (slice) {
2286
- sliceLeaf = leaf;
2287
- sliceLeaf.__worldOpacity = 0;
2288
- leaf = leafer;
2289
- renderOptions.bounds = canvas.bounds;
2290
- }
2291
- canvas.save();
2292
- if (isFrame && fill !== undefined) {
2293
- const oldFill = leaf.get('fill');
2294
- leaf.fill = '';
2295
- leaf.__render(canvas, renderOptions);
2296
- leaf.fill = oldFill;
2297
- }
2298
- else {
2299
- leaf.__render(canvas, renderOptions);
2300
- }
2301
- canvas.restore();
2302
- if (sliceLeaf)
2303
- sliceLeaf.__updateWorldOpacity();
2304
- if (trim) {
2305
- trimBounds = getTrimBounds(canvas);
2306
- const old = canvas, { width, height } = trimBounds;
2307
- const config = { x: 0, y: 0, width, height, pixelRatio };
2308
- canvas = Creator.canvas(config);
2309
- canvas.copyWorld(old, trimBounds, config);
2310
- }
2311
- if (needFill)
2312
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2313
- if (onCanvas)
2314
- onCanvas(canvas);
2315
- const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
2316
- over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
2317
- }));
2318
- }
2319
- else {
2320
- over({ data: false });
2321
- }
2322
- }));
2323
- }
2324
- };
2325
- let tasker;
2326
- function addTask(task) {
2327
- if (!tasker)
2328
- tasker = new TaskProcessor();
2329
- return new Promise((resolve) => {
2330
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
2331
- });
2332
- }
2333
- function checkLazy(leaf) {
2334
- if (leaf.__.__needComputePaint)
2335
- leaf.__.__computePaint();
2336
- if (leaf.isBranch)
2337
- leaf.children.forEach(child => checkLazy(child));
2338
- }
2339
-
2340
- const canvas = LeaferCanvasBase.prototype;
2341
- const debug = Debug.get('@leafer-ui/export');
2342
- canvas.export = function (filename, options) {
2343
- const { quality, blob } = FileHelper.getExportOptions(options);
2344
- if (filename.includes('.'))
2345
- return this.saveAs(filename, quality);
2346
- else if (blob)
2347
- return this.toBlob(filename, quality);
2348
- else
2349
- return this.toDataURL(filename, quality);
2350
- };
2351
- canvas.toBlob = function (type, quality) {
2352
- return new Promise((resolve) => {
2353
- Platform.origin.canvasToBolb(this.view, type, quality).then((blob) => {
2354
- resolve(blob);
2355
- }).catch((e) => {
2356
- debug.error(e);
2357
- resolve(null);
2358
- });
2359
- });
2360
- };
2361
- canvas.toDataURL = function (type, quality) {
2362
- return Platform.origin.canvasToDataURL(this.view, type, quality);
2363
- };
2364
- canvas.saveAs = function (filename, quality) {
2365
- return new Promise((resolve) => {
2366
- Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
2367
- resolve(true);
2368
- }).catch((e) => {
2369
- debug.error(e);
2370
- resolve(false);
2371
- });
2372
- });
2373
- };
2374
-
2375
2192
  Object.assign(TextConvert, TextConvertModule);
2376
2193
  Object.assign(ColorConvert, ColorConvertModule);
2377
2194
  Object.assign(Paint, PaintModule);
2378
2195
  Object.assign(PaintImage, PaintImageModule);
2379
2196
  Object.assign(PaintGradient, PaintGradientModule);
2380
2197
  Object.assign(Effect, EffectModule);
2381
- Object.assign(Export, ExportModule);
2382
2198
 
2383
2199
  useCanvas();
2384
2200