@effing/canvas 0.22.1 → 0.22.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/index.d.ts CHANGED
@@ -93,7 +93,7 @@ type RenderReactElementOptions = {
93
93
  *
94
94
  * await renderReactElement(ctx, <MyComponent />, { fonts: [myFont] });
95
95
  *
96
- * const png = canvas.encodeSync("png");
96
+ * const png = await canvas.encode("png");
97
97
  * ```
98
98
  *
99
99
  * @example HiDPI rendering (2x)
package/dist/index.js CHANGED
@@ -764,8 +764,7 @@ function acquireOffscreen(w, h) {
764
764
  const canvas2 = ref.deref();
765
765
  if (canvas2) {
766
766
  const ctx = canvas2.getContext("2d");
767
- ctx.setTransform(1, 0, 0, 1, 0, 0);
768
- ctx.clearRect(0, 0, w, h);
767
+ ctx.reset();
769
768
  return [canvas2, ctx];
770
769
  }
771
770
  }
@@ -1618,9 +1617,11 @@ function drawSvgChild(ctx, child, inherited, color, defs = EMPTY_DEFS, vbW = 0,
1618
1617
  if (type === "defs" || type === "clipPath" || type === "mask" || type === "filter")
1619
1618
  return;
1620
1619
  const transform = type !== "g" ? props.transform : void 0;
1621
- if (transform) {
1620
+ const opacity = type !== "g" ? Number(props.opacity ?? 1) : 1;
1621
+ if (transform || opacity < 1) {
1622
1622
  ctx.save();
1623
- applySvgTransform(ctx, transform);
1623
+ if (transform) applySvgTransform(ctx, transform);
1624
+ if (opacity < 1) ctx.globalAlpha *= opacity;
1624
1625
  }
1625
1626
  const clipRef = parseUrlRef(props.clipPath ?? props["clip-path"]);
1626
1627
  const clipShapes = clipRef ? defs.clips.get(clipRef) : void 0;
@@ -1701,7 +1702,7 @@ function drawSvgChild(ctx, child, inherited, color, defs = EMPTY_DEFS, vbW = 0,
1701
1702
  releaseOffscreen(elemCanvas);
1702
1703
  }
1703
1704
  if (clipPath) ctx.restore();
1704
- if (transform) ctx.restore();
1705
+ if (transform || opacity < 1) ctx.restore();
1705
1706
  }
1706
1707
  function drawPath(ctx, props, inherited, color, defs) {
1707
1708
  const d = props.d;
@@ -1869,17 +1870,19 @@ function drawGroup(ctx, node, inherited, color, defs = EMPTY_DEFS, vbW = 0, vbH
1869
1870
  strokeLinejoin: merged.strokeLinejoin ?? merged["stroke-linejoin"] ?? inherited.strokeLinejoin,
1870
1871
  strokeOpacity: merged.strokeOpacity ?? merged["stroke-opacity"] ?? inherited.strokeOpacity
1871
1872
  };
1873
+ const opacity = Number(merged.opacity ?? 1);
1872
1874
  const transform = merged.transform;
1873
- if (transform) {
1875
+ if (transform || opacity < 1) {
1874
1876
  ctx.save();
1875
- applySvgTransform(ctx, transform);
1877
+ if (transform) applySvgTransform(ctx, transform);
1878
+ if (opacity < 1) ctx.globalAlpha *= opacity;
1876
1879
  }
1877
1880
  for (const child of children) {
1878
1881
  if (child != null && typeof child === "object") {
1879
1882
  drawSvgChild(ctx, child, groupInherited, color, defs, vbW, vbH);
1880
1883
  }
1881
1884
  }
1882
- if (transform) {
1885
+ if (transform || opacity < 1) {
1883
1886
  ctx.restore();
1884
1887
  }
1885
1888
  }
@@ -3219,11 +3222,21 @@ async function buildNode(element, parentStyle, yogaNode, viewportWidth, viewport
3219
3222
  if (tagName === "svg") {
3220
3223
  if (props.width != null && style.width === void 0) {
3221
3224
  const v = props.width;
3222
- style.width = typeof v === "string" && v.endsWith("%") ? v : Number(v);
3225
+ if (typeof v === "string") {
3226
+ const resolved = resolveUnit(v, viewportWidth, viewportHeight, 16, 16);
3227
+ style.width = typeof resolved === "string" && resolved.endsWith("%") ? resolved : Number(resolved);
3228
+ } else {
3229
+ style.width = Number(v);
3230
+ }
3223
3231
  }
3224
3232
  if (props.height != null && style.height === void 0) {
3225
3233
  const v = props.height;
3226
- style.height = typeof v === "string" && v.endsWith("%") ? v : Number(v);
3234
+ if (typeof v === "string") {
3235
+ const resolved = resolveUnit(v, viewportWidth, viewportHeight, 16, 16);
3236
+ style.height = typeof resolved === "string" && resolved.endsWith("%") ? resolved : Number(resolved);
3237
+ } else {
3238
+ style.height = Number(v);
3239
+ }
3227
3240
  }
3228
3241
  const viewBox = props.viewBox;
3229
3242
  if (viewBox) {