leafer-draw 2.0.0 → 2.0.2

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) {
@@ -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,12 @@ 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
+ }
1317
1362
  return true;
1318
1363
  }
1319
1364
 
@@ -1356,7 +1401,7 @@ function getPatternData(paint, box, image) {
1356
1401
  if (paint.padding) box = tempBox.set(box).shrink(paint.padding);
1357
1402
  if (paint.mode === "strench") paint.mode = "stretch";
1358
1403
  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;
1404
+ 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
1405
  const sameBox = box.width === width && box.height === height;
1361
1406
  const data = {
1362
1407
  mode: mode
@@ -1419,8 +1464,6 @@ function getPatternData(paint, box, image) {
1419
1464
  data.scaleX = scaleX;
1420
1465
  data.scaleY = scaleY;
1421
1466
  }
1422
- if (opacity && opacity < 1) data.opacity = opacity;
1423
- if (filters) data.filters = filters;
1424
1467
  if (repeat) data.repeat = core.isString(repeat) ? repeat === "x" ? "repeat-x" : "repeat-y" : "repeat";
1425
1468
  if (interlace) data.interlace = core.isNumber(interlace) || interlace.type === "percent" ? {
1426
1469
  type: "x",
@@ -1451,7 +1494,7 @@ const {get: get$2, set: set, rotateOfOuter: rotateOfOuter$1, translate: translat
1451
1494
 
1452
1495
  function stretchMode(data, box, scaleX, scaleY) {
1453
1496
  const transform = get$2(), {x: x, y: y} = box;
1454
- if (x || y) translate(transform, x, y); else transform.onlyScale = true;
1497
+ if (x || y) translate(transform, x, y); else if (scaleX > 0 && scaleY > 0) transform.onlyScale = true;
1455
1498
  scaleHelper(transform, scaleX, scaleY);
1456
1499
  data.transform = transform;
1457
1500
  }
@@ -1523,39 +1566,6 @@ function layout(transform, box, x, y, scaleX, scaleY, rotation, skew) {
1523
1566
  translate(transform, box.x + x, box.y + y);
1524
1567
  }
1525
1568
 
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
1569
  const {get: get$1, scale: scale, copy: copy$1} = core.MatrixHelper;
1560
1570
 
1561
1571
  const {getFloorScale: getFloorScale} = core.MathHelper, {abs: abs$1} = Math;
@@ -1573,10 +1583,10 @@ function createPatternTask(paint, ui, canvas, renderOptions) {
1573
1583
  }
1574
1584
 
1575
1585
  function createPattern(paint, ui, canvas, renderOptions) {
1576
- let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = scaleX + "-" + scaleY;
1586
+ let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1577
1587
  if (paint.patternId !== id && !ui.destroyed) {
1578
1588
  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);
1589
+ const {image: image, data: data} = paint, {opacity: opacity, filters: filters} = paint.originPaint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1580
1590
  let imageMatrix, xGap, yGap, {width: width, height: height} = image;
1581
1591
  if (fixScale) scaleX *= fixScale, scaleY *= fixScale;
1582
1592
  width *= scaleX;
@@ -1592,7 +1602,7 @@ function createPattern(paint, ui, canvas, renderOptions) {
1592
1602
  if (transform) copy$1(imageMatrix, transform);
1593
1603
  scale(imageMatrix, 1 / scaleX, 1 / scaleY);
1594
1604
  }
1595
- const imageCanvas = image.getCanvas(width, height, data.opacity, data.filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1605
+ const imageCanvas = image.getCanvas(width, height, opacity, filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1596
1606
  const pattern = image.getPattern(imageCanvas, data.repeat || (core.Platform.origin.noRepeat || "no-repeat"), imageMatrix, paint);
1597
1607
  paint.style = pattern;
1598
1608
  paint.patternId = id;
@@ -1613,15 +1623,15 @@ function getPatternFixScale(paint, imageScaleX, imageScaleY) {
1613
1623
  }
1614
1624
 
1615
1625
  function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1616
- const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
1626
+ const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1617
1627
  const {image: image, data: data, originPaint: originPaint} = paint, {exporting: exporting, snapshot: snapshot} = renderOptions;
1618
- if (!data || paint.patternId === scaleX + "-" + scaleY && !exporting || snapshot) {
1628
+ if (!data || paint.patternId === id && !exporting || snapshot) {
1619
1629
  return false;
1620
1630
  } else {
1621
1631
  if (drawImage) {
1622
1632
  if (data.repeat) {
1623
1633
  drawImage = false;
1624
- } else if (!(originPaint.changeful || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1634
+ } else if (!(originPaint.changeful || paint.film || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1625
1635
  drawImage = core.Platform.image.isLarge(image, scaleX, scaleY) || image.width * scaleX > 8096 || image.height * scaleY > 8096;
1626
1636
  }
1627
1637
  }
@@ -1639,20 +1649,21 @@ function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1639
1649
  }
1640
1650
  }
1641
1651
 
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) {
1652
+ function drawImage(paint, imageScaleX, imageScaleY, ui, canvas, _renderOptions) {
1653
+ const {data: data, image: image, complex: complex} = paint;
1654
+ let {width: width, height: height} = image;
1655
+ if (complex) {
1656
+ const {blendMode: blendMode, opacity: opacity} = paint.originPaint, {transform: transform} = data;
1646
1657
  canvas.save();
1647
- clipUI && canvas.clipUI(ui);
1658
+ complex === 2 && canvas.clipUI(ui);
1648
1659
  blendMode && (canvas.blendMode = blendMode);
1649
1660
  opacity && (canvas.opacity *= opacity);
1650
1661
  transform && canvas.transform(transform);
1651
- canvas.drawImage(view, 0, 0, width, height);
1662
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1652
1663
  canvas.restore();
1653
1664
  } else {
1654
1665
  if (data.scaleX) width *= data.scaleX, height *= data.scaleY;
1655
- canvas.drawImage(view, 0, 0, width, height);
1666
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1656
1667
  }
1657
1668
  }
1658
1669
 
@@ -2372,7 +2383,7 @@ function layoutText(drawData, style) {
2372
2383
  let {x: x, y: y, width: width, height: height} = bounds, realHeight = __lineHeight * countRows + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
2373
2384
  let starY = __baseLine;
2374
2385
  if (__clipText && realHeight > height) {
2375
- realHeight = Math.max(height, __lineHeight);
2386
+ realHeight = Math.max(style.__autoHeight ? realHeight : height, __lineHeight);
2376
2387
  if (countRows > 1) drawData.overflow = countRows;
2377
2388
  } else if (height || autoSizeAlign) {
2378
2389
  switch (verticalAlign) {
@@ -2429,10 +2440,10 @@ function layoutText(drawData, style) {
2429
2440
  }
2430
2441
 
2431
2442
  function clipText(drawData, style, x, width) {
2432
- if (!width) return;
2433
2443
  const {rows: rows, overflow: overflow} = drawData;
2434
2444
  let {textOverflow: textOverflow} = style;
2435
- rows.splice(overflow);
2445
+ if (overflow) rows.splice(overflow);
2446
+ if (!width) return;
2436
2447
  if (textOverflow && textOverflow !== "show") {
2437
2448
  if (textOverflow === "hide") textOverflow = ""; else if (textOverflow === "ellipsis") textOverflow = "...";
2438
2449
  let char, charRight;
@@ -2591,6 +2602,13 @@ Object.assign(draw.Effect, EffectModule);
2591
2602
 
2592
2603
  useCanvas();
2593
2604
 
2605
+ Object.defineProperty(exports, "LeaferFilm", {
2606
+ enumerable: true,
2607
+ get: function() {
2608
+ return core.LeaferFilm;
2609
+ }
2610
+ });
2611
+
2594
2612
  Object.defineProperty(exports, "LeaferImage", {
2595
2613
  enumerable: true,
2596
2614
  get: function() {
@@ -2598,6 +2616,13 @@ Object.defineProperty(exports, "LeaferImage", {
2598
2616
  }
2599
2617
  });
2600
2618
 
2619
+ Object.defineProperty(exports, "LeaferVideo", {
2620
+ enumerable: true,
2621
+ get: function() {
2622
+ return core.LeaferVideo;
2623
+ }
2624
+ });
2625
+
2601
2626
  exports.Layouter = Layouter;
2602
2627
 
2603
2628
  exports.LeaferCanvas = LeaferCanvas;