@leafer-draw/worker 2.0.1 → 2.0.3

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
@@ -13,6 +13,39 @@ exports.PathNodeHandleType = void 0;
13
13
  PathNodeHandleType[PathNodeHandleType["mirror"] = 4] = "mirror";
14
14
  })(exports.PathNodeHandleType || (exports.PathNodeHandleType = {}));
15
15
 
16
+ function __awaiter(thisArg, _arguments, P, generator) {
17
+ function adopt(value) {
18
+ return value instanceof P ? value : new P(function(resolve) {
19
+ resolve(value);
20
+ });
21
+ }
22
+ return new (P || (P = Promise))(function(resolve, reject) {
23
+ function fulfilled(value) {
24
+ try {
25
+ step(generator.next(value));
26
+ } catch (e) {
27
+ reject(e);
28
+ }
29
+ }
30
+ function rejected(value) {
31
+ try {
32
+ step(generator["throw"](value));
33
+ } catch (e) {
34
+ reject(e);
35
+ }
36
+ }
37
+ function step(result) {
38
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
39
+ }
40
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
41
+ });
42
+ }
43
+
44
+ typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
45
+ var e = new Error(message);
46
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
47
+ };
48
+
16
49
  class LeaferCanvas extends core.LeaferCanvasBase {
17
50
  get allowBackgroundColor() {
18
51
  return true;
@@ -37,8 +70,6 @@ core.canvasPatch(OffscreenCanvasRenderingContext2D.prototype);
37
70
 
38
71
  core.canvasPatch(Path2D.prototype);
39
72
 
40
- const {mineType: mineType} = core.FileHelper;
41
-
42
73
  Object.assign(core.Creator, {
43
74
  canvas: (options, manager) => new LeaferCanvas(options, manager),
44
75
  image: options => new core.LeaferImage(options)
@@ -49,7 +80,7 @@ function useCanvas(_canvasType, _power) {
49
80
  createCanvas: (width, height) => new OffscreenCanvas(width, height),
50
81
  canvasToDataURL: (canvas, type, quality) => new Promise((resolve, reject) => {
51
82
  canvas.convertToBlob({
52
- type: mineType(type),
83
+ type: core.FileHelper.mimeType(type),
53
84
  quality: quality
54
85
  }).then(blob => {
55
86
  var reader = new FileReader;
@@ -61,14 +92,14 @@ function useCanvas(_canvasType, _power) {
61
92
  });
62
93
  }),
63
94
  canvasToBolb: (canvas, type, quality) => canvas.convertToBlob({
64
- type: mineType(type),
95
+ type: core.FileHelper.mimeType(type),
65
96
  quality: quality
66
97
  }),
67
98
  canvasSaveAs: (_canvas, _filename, _quality) => new Promise(resolve => resolve()),
68
99
  download(_url, _filename) {
69
100
  return undefined;
70
101
  },
71
- loadImage(src) {
102
+ loadImage(src, _crossOrigin, _leaferImage) {
72
103
  return new Promise((resolve, reject) => {
73
104
  let req = new XMLHttpRequest;
74
105
  req.open("GET", core.Platform.image.getRealURL(src), true);
@@ -83,6 +114,13 @@ function useCanvas(_canvasType, _power) {
83
114
  req.onerror = e => reject(e);
84
115
  req.send();
85
116
  });
117
+ },
118
+ loadContent(url_1) {
119
+ return __awaiter(this, arguments, void 0, function*(url, responseType = "text") {
120
+ const response = yield fetch(url);
121
+ if (!response.ok) throw new Error(`${response.status}`);
122
+ return yield response[responseType]();
123
+ });
86
124
  }
87
125
  };
88
126
  core.Platform.canvas = core.Creator.canvas();
@@ -582,7 +620,7 @@ class Renderer {
582
620
  getCellList() {
583
621
  return undefined;
584
622
  }
585
- addBlock(block) {
623
+ addBlock(block, _leafList) {
586
624
  if (!this.updateBlocks) this.updateBlocks = [];
587
625
  this.updateBlocks.push(block);
588
626
  }
@@ -630,7 +668,8 @@ class Renderer {
630
668
  __onLayoutEnd(event) {
631
669
  if (event.data) event.data.map(item => {
632
670
  let empty;
633
- if (item.updatedList) item.updatedList.list.some(leaf => {
671
+ const {updatedList: updatedList} = item;
672
+ if (updatedList) updatedList.list.some(leaf => {
634
673
  empty = !leaf.__world.width || !leaf.__world.height;
635
674
  if (empty) {
636
675
  if (!leaf.isLeafer) debug.tip(leaf.innerName, ": empty");
@@ -638,7 +677,7 @@ class Renderer {
638
677
  }
639
678
  return empty;
640
679
  });
641
- this.addBlock(empty ? this.canvas.bounds : item.updatedBounds);
680
+ this.addBlock(empty ? this.canvas.bounds : item.updatedBounds, updatedList);
642
681
  });
643
682
  }
644
683
  emitRender(type, bounds, options) {
@@ -703,8 +742,8 @@ function fills(fills, ui, canvas, renderOptions) {
703
742
  canvas.save();
704
743
  if (item.transform) canvas.transform(item.transform);
705
744
  if (originPaint.scaleFixed) {
706
- const {scaleX: scaleX, scaleY: scaleY} = ui.getRenderScaleData(true);
707
- if (originPaint.scaleFixed === true || originPaint.scaleFixed === "zoom-in" && scaleX > 1 && scaleY > 1) canvas.scale(1 / scaleX, 1 / scaleY);
745
+ const {scaleX: scaleX, scaleY: scaleY} = ui.getRenderScaleData(true, originPaint.scaleFixed, false);
746
+ if (scaleX !== 1) canvas.scale(scaleX, scaleY);
708
747
  }
709
748
  if (originPaint.blendMode) canvas.blendMode = originPaint.blendMode;
710
749
  fillPathOrText(ui, canvas, renderOptions);
@@ -978,11 +1017,14 @@ function compute(attrName, ui) {
978
1017
  function getLeafPaint(attrName, paint, ui) {
979
1018
  if (!core.isObject(paint) || paint.visible === false || paint.opacity === 0) return undefined;
980
1019
  let leafPaint;
981
- const {boxBounds: boxBounds} = ui.__layout;
982
- switch (paint.type) {
1020
+ const {boxBounds: boxBounds} = ui.__layout, {type: type} = paint;
1021
+ switch (type) {
983
1022
  case "image":
1023
+ case "film":
1024
+ case "video":
984
1025
  if (!paint.url) return undefined;
985
1026
  leafPaint = draw.PaintImage.image(ui, attrName, paint, boxBounds, !recycleMap || !recycleMap[paint.url]);
1027
+ if (type !== "image") draw.PaintImage[type](leafPaint);
986
1028
  break;
987
1029
 
988
1030
  case "linear":
@@ -998,7 +1040,7 @@ function getLeafPaint(attrName, paint, ui) {
998
1040
  break;
999
1041
 
1000
1042
  case "solid":
1001
- const {type: type, color: color, opacity: opacity} = paint;
1043
+ const {color: color, opacity: opacity} = paint;
1002
1044
  leafPaint = {
1003
1045
  type: type,
1004
1046
  style: draw.ColorConvert.string(color, opacity)
@@ -1042,7 +1084,7 @@ const {isSame: isSame} = core.BoundsHelper;
1042
1084
 
1043
1085
  function image(ui, attrName, paint, boxBounds, firstUse) {
1044
1086
  let leafPaint, event;
1045
- const image = core.ImageManager.get(paint);
1087
+ const image = core.ImageManager.get(paint, paint.type);
1046
1088
  if (cache && paint === cache.paint && isSame(boxBounds, cache.boxBounds)) {
1047
1089
  leafPaint = cache.leafPaint;
1048
1090
  } else {
@@ -1103,8 +1145,8 @@ function image(ui, attrName, paint, boxBounds, firstUse) {
1103
1145
  }
1104
1146
 
1105
1147
  function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds) {
1106
- if (attrName === "fill" && !ui.__.__naturalWidth) {
1107
- const data = ui.__;
1148
+ const data = ui.__;
1149
+ if (attrName === "fill" && !data.__naturalWidth) {
1108
1150
  data.__naturalWidth = image.width / data.pixelRatio;
1109
1151
  data.__naturalHeight = image.height / data.pixelRatio;
1110
1152
  if (data.__autoSide) {
@@ -1116,7 +1158,13 @@ function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds
1116
1158
  return false;
1117
1159
  }
1118
1160
  }
1119
- if (!leafPaint.data) draw.PaintImage.createData(leafPaint, image, paint, boxBounds);
1161
+ if (!leafPaint.data) {
1162
+ draw.PaintImage.createData(leafPaint, image, paint, boxBounds);
1163
+ const {transform: transform} = leafPaint.data, {opacity: opacity, blendMode: blendMode} = paint;
1164
+ const clip = transform && !transform.onlyScale || data.path || data.cornerRadius;
1165
+ if (clip || opacity && opacity < 1 || blendMode) leafPaint.complex = clip ? 2 : true;
1166
+ }
1167
+ if (paint.filter) draw.PaintImage.applyFilter(leafPaint, image, paint.filter, ui);
1120
1168
  return true;
1121
1169
  }
1122
1170
 
@@ -1159,7 +1207,7 @@ function getPatternData(paint, box, image) {
1159
1207
  if (paint.padding) box = tempBox.set(box).shrink(paint.padding);
1160
1208
  if (paint.mode === "strench") paint.mode = "stretch";
1161
1209
  const {width: width, height: height} = image;
1162
- const {opacity: opacity, mode: mode, align: align, offset: offset, scale: scale, size: size, rotation: rotation, skew: skew, clipSize: clipSize, repeat: repeat, gap: gap, filters: filters, interlace: interlace} = paint;
1210
+ const {mode: mode, align: align, offset: offset, scale: scale, size: size, rotation: rotation, skew: skew, clipSize: clipSize, repeat: repeat, gap: gap, interlace: interlace} = paint;
1163
1211
  const sameBox = box.width === width && box.height === height;
1164
1212
  const data = {
1165
1213
  mode: mode
@@ -1222,8 +1270,6 @@ function getPatternData(paint, box, image) {
1222
1270
  data.scaleX = scaleX;
1223
1271
  data.scaleY = scaleY;
1224
1272
  }
1225
- if (opacity && opacity < 1) data.opacity = opacity;
1226
- if (filters) data.filters = filters;
1227
1273
  if (repeat) data.repeat = core.isString(repeat) ? repeat === "x" ? "repeat-x" : "repeat-y" : "repeat";
1228
1274
  if (interlace) data.interlace = core.isNumber(interlace) || interlace.type === "percent" ? {
1229
1275
  type: "x",
@@ -1254,7 +1300,7 @@ const {get: get$2, set: set, rotateOfOuter: rotateOfOuter$1, translate: translat
1254
1300
 
1255
1301
  function stretchMode(data, box, scaleX, scaleY) {
1256
1302
  const transform = get$2(), {x: x, y: y} = box;
1257
- if (x || y) translate(transform, x, y); else transform.onlyScale = true;
1303
+ if (x || y) translate(transform, x, y); else if (scaleX > 0 && scaleY > 0) transform.onlyScale = true;
1258
1304
  scaleHelper(transform, scaleX, scaleY);
1259
1305
  data.transform = transform;
1260
1306
  }
@@ -1326,39 +1372,6 @@ function layout(transform, box, x, y, scaleX, scaleY, rotation, skew) {
1326
1372
  translate(transform, box.x + x, box.y + y);
1327
1373
  }
1328
1374
 
1329
- function __awaiter(thisArg, _arguments, P, generator) {
1330
- function adopt(value) {
1331
- return value instanceof P ? value : new P(function(resolve) {
1332
- resolve(value);
1333
- });
1334
- }
1335
- return new (P || (P = Promise))(function(resolve, reject) {
1336
- function fulfilled(value) {
1337
- try {
1338
- step(generator.next(value));
1339
- } catch (e) {
1340
- reject(e);
1341
- }
1342
- }
1343
- function rejected(value) {
1344
- try {
1345
- step(generator["throw"](value));
1346
- } catch (e) {
1347
- reject(e);
1348
- }
1349
- }
1350
- function step(result) {
1351
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
1352
- }
1353
- step((generator = generator.apply(thisArg, _arguments || [])).next());
1354
- });
1355
- }
1356
-
1357
- typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
1358
- var e = new Error(message);
1359
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1360
- };
1361
-
1362
1375
  const {get: get$1, scale: scale, copy: copy$1} = core.MatrixHelper;
1363
1376
 
1364
1377
  const {getFloorScale: getFloorScale} = core.MathHelper, {abs: abs$1} = Math;
@@ -1376,10 +1389,10 @@ function createPatternTask(paint, ui, canvas, renderOptions) {
1376
1389
  }
1377
1390
 
1378
1391
  function createPattern(paint, ui, canvas, renderOptions) {
1379
- let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = scaleX + "-" + scaleY;
1392
+ let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1380
1393
  if (paint.patternId !== id && !ui.destroyed) {
1381
1394
  if (!(core.Platform.image.isLarge(paint.image, scaleX, scaleY) && !paint.data.repeat)) {
1382
- const {image: image, data: data} = paint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1395
+ const {image: image, data: data} = paint, {opacity: opacity} = paint.originPaint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1383
1396
  let imageMatrix, xGap, yGap, {width: width, height: height} = image;
1384
1397
  if (fixScale) scaleX *= fixScale, scaleY *= fixScale;
1385
1398
  width *= scaleX;
@@ -1395,7 +1408,7 @@ function createPattern(paint, ui, canvas, renderOptions) {
1395
1408
  if (transform) copy$1(imageMatrix, transform);
1396
1409
  scale(imageMatrix, 1 / scaleX, 1 / scaleY);
1397
1410
  }
1398
- const imageCanvas = image.getCanvas(width, height, data.opacity, data.filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1411
+ const imageCanvas = image.getCanvas(width, height, opacity, undefined, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1399
1412
  const pattern = image.getPattern(imageCanvas, data.repeat || (core.Platform.origin.noRepeat || "no-repeat"), imageMatrix, paint);
1400
1413
  paint.style = pattern;
1401
1414
  paint.patternId = id;
@@ -1416,15 +1429,15 @@ function getPatternFixScale(paint, imageScaleX, imageScaleY) {
1416
1429
  }
1417
1430
 
1418
1431
  function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1419
- const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
1432
+ const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1420
1433
  const {image: image, data: data, originPaint: originPaint} = paint, {exporting: exporting, snapshot: snapshot} = renderOptions;
1421
- if (!data || paint.patternId === scaleX + "-" + scaleY && !exporting || snapshot) {
1434
+ if (!data || paint.patternId === id && !exporting || snapshot) {
1422
1435
  return false;
1423
1436
  } else {
1424
1437
  if (drawImage) {
1425
1438
  if (data.repeat) {
1426
1439
  drawImage = false;
1427
- } else if (!(originPaint.changeful || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1440
+ } else if (!(originPaint.changeful || paint.film || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1428
1441
  drawImage = core.Platform.image.isLarge(image, scaleX, scaleY) || image.width * scaleX > 8096 || image.height * scaleY > 8096;
1429
1442
  }
1430
1443
  }
@@ -1442,20 +1455,21 @@ function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1442
1455
  }
1443
1456
  }
1444
1457
 
1445
- function drawImage(paint, _imageScaleX, _imageScaleY, ui, canvas, _renderOptions) {
1446
- const {data: data, image: image} = paint, {blendMode: blendMode} = paint.originPaint, {opacity: opacity, transform: transform} = data, view = image.getFull(data.filters), u = ui.__;
1447
- let {width: width, height: height} = image, clipUI;
1448
- if ((clipUI = transform && !transform.onlyScale || u.path || u.cornerRadius) || opacity || blendMode) {
1458
+ function drawImage(paint, imageScaleX, imageScaleY, ui, canvas, _renderOptions) {
1459
+ const {data: data, image: image, complex: complex} = paint;
1460
+ let {width: width, height: height} = image;
1461
+ if (complex) {
1462
+ const {blendMode: blendMode, opacity: opacity} = paint.originPaint, {transform: transform} = data;
1449
1463
  canvas.save();
1450
- clipUI && canvas.clipUI(ui);
1464
+ complex === 2 && canvas.clipUI(ui);
1451
1465
  blendMode && (canvas.blendMode = blendMode);
1452
1466
  opacity && (canvas.opacity *= opacity);
1453
1467
  transform && canvas.transform(transform);
1454
- canvas.drawImage(view, 0, 0, width, height);
1468
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1455
1469
  canvas.restore();
1456
1470
  } else {
1457
1471
  if (data.scaleX) width *= data.scaleX, height *= data.scaleY;
1458
- canvas.drawImage(view, 0, 0, width, height);
1472
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1459
1473
  }
1460
1474
  }
1461
1475
 
@@ -1485,6 +1499,7 @@ function recycleImage(attrName, data) {
1485
1499
  if (!recycleMap) recycleMap = {};
1486
1500
  recycleMap[url] = true;
1487
1501
  core.ImageManager.recyclePaint(paint);
1502
+ if (data.__willDestroy && image.parent) draw.PaintImage.recycleFilter(image, data.__leaf);
1488
1503
  if (image.loading) {
1489
1504
  if (!input) {
1490
1505
  input = data.__input && data.__input[attrName] || [];
@@ -2394,6 +2409,13 @@ Object.assign(draw.Effect, EffectModule);
2394
2409
 
2395
2410
  useCanvas();
2396
2411
 
2412
+ Object.defineProperty(exports, "LeaferFilm", {
2413
+ enumerable: true,
2414
+ get: function() {
2415
+ return core.LeaferFilm;
2416
+ }
2417
+ });
2418
+
2397
2419
  Object.defineProperty(exports, "LeaferImage", {
2398
2420
  enumerable: true,
2399
2421
  get: function() {
@@ -2401,6 +2423,13 @@ Object.defineProperty(exports, "LeaferImage", {
2401
2423
  }
2402
2424
  });
2403
2425
 
2426
+ Object.defineProperty(exports, "LeaferVideo", {
2427
+ enumerable: true,
2428
+ get: function() {
2429
+ return core.LeaferVideo;
2430
+ }
2431
+ });
2432
+
2404
2433
  exports.Layouter = Layouter;
2405
2434
 
2406
2435
  exports.LeaferCanvas = LeaferCanvas;