leafer-draw 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/web.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
  const debug$2 = core.Debug.get("LeaferCanvas");
17
50
 
18
51
  class LeaferCanvas extends core.LeaferCanvasBase {
@@ -216,8 +249,6 @@ core.canvasPatch(CanvasRenderingContext2D.prototype);
216
249
 
217
250
  core.canvasPatch(Path2D.prototype);
218
251
 
219
- const {mineType: mineType, fileType: fileType} = core.FileHelper;
220
-
221
252
  Object.assign(core.Creator, {
222
253
  canvas: (options, manager) => new LeaferCanvas(options, manager),
223
254
  image: options => new core.LeaferImage(options)
@@ -232,29 +263,27 @@ function useCanvas(_canvasType, _power) {
232
263
  return canvas;
233
264
  },
234
265
  canvasToDataURL: (canvas, type, quality) => {
235
- const imageType = mineType(type), url = canvas.toDataURL(imageType, quality);
266
+ const imageType = core.FileHelper.mimeType(type), url = canvas.toDataURL(imageType, quality);
236
267
  return imageType === "image/bmp" ? url.replace("image/png;", "image/bmp;") : url;
237
268
  },
238
- canvasToBolb: (canvas, type, quality) => new Promise(resolve => canvas.toBlob(resolve, mineType(type), quality)),
269
+ canvasToBolb: (canvas, type, quality) => new Promise(resolve => canvas.toBlob(resolve, core.FileHelper.mimeType(type), quality)),
239
270
  canvasSaveAs: (canvas, filename, quality) => {
240
- const url = canvas.toDataURL(mineType(fileType(filename)), quality);
271
+ const url = canvas.toDataURL(core.FileHelper.mimeType(core.FileHelper.fileType(filename)), quality);
241
272
  return core.Platform.origin.download(url, filename);
242
273
  },
243
274
  download(url, filename) {
244
- return new Promise(resolve => {
275
+ return __awaiter(this, void 0, void 0, function*() {
245
276
  let el = document.createElement("a");
246
277
  el.href = url;
247
278
  el.download = filename;
248
279
  document.body.appendChild(el);
249
280
  el.click();
250
281
  document.body.removeChild(el);
251
- resolve();
252
282
  });
253
283
  },
254
- loadImage(src) {
284
+ loadImage(src, crossOrigin, _leaferImage) {
255
285
  return new Promise((resolve, reject) => {
256
286
  const img = new core.Platform.origin.Image;
257
- const {crossOrigin: crossOrigin} = core.Platform.image;
258
287
  if (crossOrigin) {
259
288
  img.setAttribute("crossOrigin", crossOrigin);
260
289
  img.crossOrigin = crossOrigin;
@@ -268,6 +297,13 @@ function useCanvas(_canvasType, _power) {
268
297
  img.src = core.Platform.image.getRealURL(src);
269
298
  });
270
299
  },
300
+ loadContent(url_1) {
301
+ return __awaiter(this, arguments, void 0, function*(url, responseType = "text") {
302
+ const response = yield fetch(url);
303
+ if (!response.ok) throw new Error(`${response.status}`);
304
+ return yield response[responseType]();
305
+ });
306
+ },
271
307
  Image: Image,
272
308
  PointerEvent: PointerEvent,
273
309
  DragEvent: DragEvent
@@ -779,7 +815,7 @@ class Renderer {
779
815
  getCellList() {
780
816
  return undefined;
781
817
  }
782
- addBlock(block) {
818
+ addBlock(block, _leafList) {
783
819
  if (!this.updateBlocks) this.updateBlocks = [];
784
820
  this.updateBlocks.push(block);
785
821
  }
@@ -827,7 +863,8 @@ class Renderer {
827
863
  __onLayoutEnd(event) {
828
864
  if (event.data) event.data.map(item => {
829
865
  let empty;
830
- if (item.updatedList) item.updatedList.list.some(leaf => {
866
+ const {updatedList: updatedList} = item;
867
+ if (updatedList) updatedList.list.some(leaf => {
831
868
  empty = !leaf.__world.width || !leaf.__world.height;
832
869
  if (empty) {
833
870
  if (!leaf.isLeafer) debug.tip(leaf.innerName, ": empty");
@@ -835,7 +872,7 @@ class Renderer {
835
872
  }
836
873
  return empty;
837
874
  });
838
- this.addBlock(empty ? this.canvas.bounds : item.updatedBounds);
875
+ this.addBlock(empty ? this.canvas.bounds : item.updatedBounds, updatedList);
839
876
  });
840
877
  }
841
878
  emitRender(type, bounds, options) {
@@ -900,8 +937,8 @@ function fills(fills, ui, canvas, renderOptions) {
900
937
  canvas.save();
901
938
  if (item.transform) canvas.transform(item.transform);
902
939
  if (originPaint.scaleFixed) {
903
- const {scaleX: scaleX, scaleY: scaleY} = ui.getRenderScaleData(true);
904
- if (originPaint.scaleFixed === true || originPaint.scaleFixed === "zoom-in" && scaleX > 1 && scaleY > 1) canvas.scale(1 / scaleX, 1 / scaleY);
940
+ const {scaleX: scaleX, scaleY: scaleY} = ui.getRenderScaleData(true, originPaint.scaleFixed, false);
941
+ if (scaleX !== 1) canvas.scale(scaleX, scaleY);
905
942
  }
906
943
  if (originPaint.blendMode) canvas.blendMode = originPaint.blendMode;
907
944
  fillPathOrText(ui, canvas, renderOptions);
@@ -1175,11 +1212,14 @@ function compute(attrName, ui) {
1175
1212
  function getLeafPaint(attrName, paint, ui) {
1176
1213
  if (!core.isObject(paint) || paint.visible === false || paint.opacity === 0) return undefined;
1177
1214
  let leafPaint;
1178
- const {boxBounds: boxBounds} = ui.__layout;
1179
- switch (paint.type) {
1215
+ const {boxBounds: boxBounds} = ui.__layout, {type: type} = paint;
1216
+ switch (type) {
1180
1217
  case "image":
1218
+ case "film":
1219
+ case "video":
1181
1220
  if (!paint.url) return undefined;
1182
1221
  leafPaint = draw.PaintImage.image(ui, attrName, paint, boxBounds, !recycleMap || !recycleMap[paint.url]);
1222
+ if (type !== "image") draw.PaintImage[type](leafPaint);
1183
1223
  break;
1184
1224
 
1185
1225
  case "linear":
@@ -1195,7 +1235,7 @@ function getLeafPaint(attrName, paint, ui) {
1195
1235
  break;
1196
1236
 
1197
1237
  case "solid":
1198
- const {type: type, color: color, opacity: opacity} = paint;
1238
+ const {color: color, opacity: opacity} = paint;
1199
1239
  leafPaint = {
1200
1240
  type: type,
1201
1241
  style: draw.ColorConvert.string(color, opacity)
@@ -1239,7 +1279,7 @@ const {isSame: isSame} = core.BoundsHelper;
1239
1279
 
1240
1280
  function image(ui, attrName, paint, boxBounds, firstUse) {
1241
1281
  let leafPaint, event;
1242
- const image = core.ImageManager.get(paint);
1282
+ const image = core.ImageManager.get(paint, paint.type);
1243
1283
  if (cache && paint === cache.paint && isSame(boxBounds, cache.boxBounds)) {
1244
1284
  leafPaint = cache.leafPaint;
1245
1285
  } else {
@@ -1300,8 +1340,8 @@ function image(ui, attrName, paint, boxBounds, firstUse) {
1300
1340
  }
1301
1341
 
1302
1342
  function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds) {
1303
- if (attrName === "fill" && !ui.__.__naturalWidth) {
1304
- const data = ui.__;
1343
+ const data = ui.__;
1344
+ if (attrName === "fill" && !data.__naturalWidth) {
1305
1345
  data.__naturalWidth = image.width / data.pixelRatio;
1306
1346
  data.__naturalHeight = image.height / data.pixelRatio;
1307
1347
  if (data.__autoSide) {
@@ -1313,7 +1353,13 @@ function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds
1313
1353
  return false;
1314
1354
  }
1315
1355
  }
1316
- if (!leafPaint.data) draw.PaintImage.createData(leafPaint, image, paint, boxBounds);
1356
+ if (!leafPaint.data) {
1357
+ draw.PaintImage.createData(leafPaint, image, paint, boxBounds);
1358
+ const {transform: transform} = leafPaint.data, {opacity: opacity, blendMode: blendMode} = paint;
1359
+ const clip = transform && !transform.onlyScale || data.path || data.cornerRadius;
1360
+ if (clip || opacity && opacity < 1 || blendMode) leafPaint.complex = clip ? 2 : true;
1361
+ }
1362
+ if (paint.filter) draw.PaintImage.applyFilter(leafPaint, image, paint.filter, ui);
1317
1363
  return true;
1318
1364
  }
1319
1365
 
@@ -1356,7 +1402,7 @@ function getPatternData(paint, box, image) {
1356
1402
  if (paint.padding) box = tempBox.set(box).shrink(paint.padding);
1357
1403
  if (paint.mode === "strench") paint.mode = "stretch";
1358
1404
  const {width: width, height: height} = image;
1359
- 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;
1405
+ const {mode: mode, align: align, offset: offset, scale: scale, size: size, rotation: rotation, skew: skew, clipSize: clipSize, repeat: repeat, gap: gap, interlace: interlace} = paint;
1360
1406
  const sameBox = box.width === width && box.height === height;
1361
1407
  const data = {
1362
1408
  mode: mode
@@ -1419,8 +1465,6 @@ function getPatternData(paint, box, image) {
1419
1465
  data.scaleX = scaleX;
1420
1466
  data.scaleY = scaleY;
1421
1467
  }
1422
- if (opacity && opacity < 1) data.opacity = opacity;
1423
- if (filters) data.filters = filters;
1424
1468
  if (repeat) data.repeat = core.isString(repeat) ? repeat === "x" ? "repeat-x" : "repeat-y" : "repeat";
1425
1469
  if (interlace) data.interlace = core.isNumber(interlace) || interlace.type === "percent" ? {
1426
1470
  type: "x",
@@ -1451,7 +1495,7 @@ const {get: get$2, set: set, rotateOfOuter: rotateOfOuter$1, translate: translat
1451
1495
 
1452
1496
  function stretchMode(data, box, scaleX, scaleY) {
1453
1497
  const transform = get$2(), {x: x, y: y} = box;
1454
- if (x || y) translate(transform, x, y); else transform.onlyScale = true;
1498
+ if (x || y) translate(transform, x, y); else if (scaleX > 0 && scaleY > 0) transform.onlyScale = true;
1455
1499
  scaleHelper(transform, scaleX, scaleY);
1456
1500
  data.transform = transform;
1457
1501
  }
@@ -1523,39 +1567,6 @@ function layout(transform, box, x, y, scaleX, scaleY, rotation, skew) {
1523
1567
  translate(transform, box.x + x, box.y + y);
1524
1568
  }
1525
1569
 
1526
- function __awaiter(thisArg, _arguments, P, generator) {
1527
- function adopt(value) {
1528
- return value instanceof P ? value : new P(function(resolve) {
1529
- resolve(value);
1530
- });
1531
- }
1532
- return new (P || (P = Promise))(function(resolve, reject) {
1533
- function fulfilled(value) {
1534
- try {
1535
- step(generator.next(value));
1536
- } catch (e) {
1537
- reject(e);
1538
- }
1539
- }
1540
- function rejected(value) {
1541
- try {
1542
- step(generator["throw"](value));
1543
- } catch (e) {
1544
- reject(e);
1545
- }
1546
- }
1547
- function step(result) {
1548
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
1549
- }
1550
- step((generator = generator.apply(thisArg, _arguments || [])).next());
1551
- });
1552
- }
1553
-
1554
- typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
1555
- var e = new Error(message);
1556
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1557
- };
1558
-
1559
1570
  const {get: get$1, scale: scale, copy: copy$1} = core.MatrixHelper;
1560
1571
 
1561
1572
  const {getFloorScale: getFloorScale} = core.MathHelper, {abs: abs$1} = Math;
@@ -1573,10 +1584,10 @@ function createPatternTask(paint, ui, canvas, renderOptions) {
1573
1584
  }
1574
1585
 
1575
1586
  function createPattern(paint, ui, canvas, renderOptions) {
1576
- let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = scaleX + "-" + scaleY;
1587
+ let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1577
1588
  if (paint.patternId !== id && !ui.destroyed) {
1578
1589
  if (!(core.Platform.image.isLarge(paint.image, scaleX, scaleY) && !paint.data.repeat)) {
1579
- const {image: image, data: data} = paint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1590
+ const {image: image, data: data} = paint, {opacity: opacity} = paint.originPaint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1580
1591
  let imageMatrix, xGap, yGap, {width: width, height: height} = image;
1581
1592
  if (fixScale) scaleX *= fixScale, scaleY *= fixScale;
1582
1593
  width *= scaleX;
@@ -1592,7 +1603,7 @@ function createPattern(paint, ui, canvas, renderOptions) {
1592
1603
  if (transform) copy$1(imageMatrix, transform);
1593
1604
  scale(imageMatrix, 1 / scaleX, 1 / scaleY);
1594
1605
  }
1595
- const imageCanvas = image.getCanvas(width, height, data.opacity, data.filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1606
+ const imageCanvas = image.getCanvas(width, height, opacity, undefined, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1596
1607
  const pattern = image.getPattern(imageCanvas, data.repeat || (core.Platform.origin.noRepeat || "no-repeat"), imageMatrix, paint);
1597
1608
  paint.style = pattern;
1598
1609
  paint.patternId = id;
@@ -1613,15 +1624,15 @@ function getPatternFixScale(paint, imageScaleX, imageScaleY) {
1613
1624
  }
1614
1625
 
1615
1626
  function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1616
- const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
1627
+ const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1617
1628
  const {image: image, data: data, originPaint: originPaint} = paint, {exporting: exporting, snapshot: snapshot} = renderOptions;
1618
- if (!data || paint.patternId === scaleX + "-" + scaleY && !exporting || snapshot) {
1629
+ if (!data || paint.patternId === id && !exporting || snapshot) {
1619
1630
  return false;
1620
1631
  } else {
1621
1632
  if (drawImage) {
1622
1633
  if (data.repeat) {
1623
1634
  drawImage = false;
1624
- } else if (!(originPaint.changeful || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1635
+ } else if (!(originPaint.changeful || paint.film || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1625
1636
  drawImage = core.Platform.image.isLarge(image, scaleX, scaleY) || image.width * scaleX > 8096 || image.height * scaleY > 8096;
1626
1637
  }
1627
1638
  }
@@ -1639,20 +1650,21 @@ function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1639
1650
  }
1640
1651
  }
1641
1652
 
1642
- function drawImage(paint, _imageScaleX, _imageScaleY, ui, canvas, _renderOptions) {
1643
- const {data: data, image: image} = paint, {blendMode: blendMode} = paint.originPaint, {opacity: opacity, transform: transform} = data, view = image.getFull(data.filters), u = ui.__;
1644
- let {width: width, height: height} = image, clipUI;
1645
- if ((clipUI = transform && !transform.onlyScale || u.path || u.cornerRadius) || opacity || blendMode) {
1653
+ function drawImage(paint, imageScaleX, imageScaleY, ui, canvas, _renderOptions) {
1654
+ const {data: data, image: image, complex: complex} = paint;
1655
+ let {width: width, height: height} = image;
1656
+ if (complex) {
1657
+ const {blendMode: blendMode, opacity: opacity} = paint.originPaint, {transform: transform} = data;
1646
1658
  canvas.save();
1647
- clipUI && canvas.clipUI(ui);
1659
+ complex === 2 && canvas.clipUI(ui);
1648
1660
  blendMode && (canvas.blendMode = blendMode);
1649
1661
  opacity && (canvas.opacity *= opacity);
1650
1662
  transform && canvas.transform(transform);
1651
- canvas.drawImage(view, 0, 0, width, height);
1663
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1652
1664
  canvas.restore();
1653
1665
  } else {
1654
1666
  if (data.scaleX) width *= data.scaleX, height *= data.scaleY;
1655
- canvas.drawImage(view, 0, 0, width, height);
1667
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1656
1668
  }
1657
1669
  }
1658
1670
 
@@ -1682,6 +1694,7 @@ function recycleImage(attrName, data) {
1682
1694
  if (!recycleMap) recycleMap = {};
1683
1695
  recycleMap[url] = true;
1684
1696
  core.ImageManager.recyclePaint(paint);
1697
+ if (data.__willDestroy && image.parent) draw.PaintImage.recycleFilter(image, data.__leaf);
1685
1698
  if (image.loading) {
1686
1699
  if (!input) {
1687
1700
  input = data.__input && data.__input[attrName] || [];
@@ -2591,6 +2604,13 @@ Object.assign(draw.Effect, EffectModule);
2591
2604
 
2592
2605
  useCanvas();
2593
2606
 
2607
+ Object.defineProperty(exports, "LeaferFilm", {
2608
+ enumerable: true,
2609
+ get: function() {
2610
+ return core.LeaferFilm;
2611
+ }
2612
+ });
2613
+
2594
2614
  Object.defineProperty(exports, "LeaferImage", {
2595
2615
  enumerable: true,
2596
2616
  get: function() {
@@ -2598,6 +2618,13 @@ Object.defineProperty(exports, "LeaferImage", {
2598
2618
  }
2599
2619
  });
2600
2620
 
2621
+ Object.defineProperty(exports, "LeaferVideo", {
2622
+ enumerable: true,
2623
+ get: function() {
2624
+ return core.LeaferVideo;
2625
+ }
2626
+ });
2627
+
2601
2628
  exports.Layouter = Layouter;
2602
2629
 
2603
2630
  exports.LeaferCanvas = LeaferCanvas;