@remotion/web-renderer 4.0.394 → 4.0.396

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.
Files changed (72) hide show
  1. package/dist/border-radius.d.ts +31 -0
  2. package/dist/border-radius.js +152 -0
  3. package/dist/calculate-transforms.d.ts +2 -0
  4. package/dist/calculate-transforms.js +17 -0
  5. package/dist/composable.d.ts +2 -8
  6. package/dist/compose-canvas.js +28 -4
  7. package/dist/compose.d.ts +6 -3
  8. package/dist/compose.js +41 -12
  9. package/dist/drawing/border-radius.d.ts +3 -5
  10. package/dist/drawing/border-radius.js +12 -11
  11. package/dist/drawing/calculate-transforms.d.ts +6 -2
  12. package/dist/drawing/calculate-transforms.js +19 -22
  13. package/dist/drawing/canvas-offset-from-rect.d.ts +8 -0
  14. package/dist/drawing/canvas-offset-from-rect.js +12 -0
  15. package/dist/drawing/clamp-rect-to-parent-bounds.d.ts +4 -0
  16. package/dist/drawing/clamp-rect-to-parent-bounds.js +7 -0
  17. package/dist/drawing/compose-canvas.d.ts +1 -0
  18. package/dist/drawing/compose-canvas.js +36 -0
  19. package/dist/drawing/compose-svg.d.ts +1 -0
  20. package/dist/drawing/compose-svg.js +34 -0
  21. package/dist/drawing/compose.d.ts +5 -0
  22. package/dist/drawing/compose.js +6 -0
  23. package/dist/drawing/draw-border.d.ts +2 -5
  24. package/dist/drawing/draw-border.js +307 -55
  25. package/dist/drawing/draw-element-to-canvas.d.ts +4 -1
  26. package/dist/drawing/draw-element-to-canvas.js +9 -26
  27. package/dist/drawing/draw-element.d.ts +5 -3
  28. package/dist/drawing/draw-element.js +29 -14
  29. package/dist/drawing/draw-outline.d.ts +9 -0
  30. package/dist/drawing/draw-outline.js +116 -0
  31. package/dist/drawing/get-bounding-box-including-shadow.d.ts +1 -0
  32. package/dist/drawing/get-bounding-box-including-shadow.js +6 -0
  33. package/dist/drawing/get-computed-style-cache.d.ts +0 -0
  34. package/dist/drawing/get-computed-style-cache.js +1 -0
  35. package/dist/drawing/get-pretransform-rect.d.ts +1 -0
  36. package/dist/drawing/get-pretransform-rect.js +31 -0
  37. package/dist/drawing/handle-3d-transform.d.ts +10 -0
  38. package/dist/drawing/handle-3d-transform.js +39 -0
  39. package/dist/drawing/has-transform.d.ts +4 -0
  40. package/dist/drawing/has-transform.js +14 -0
  41. package/dist/drawing/overflow.d.ts +7 -0
  42. package/dist/drawing/overflow.js +12 -0
  43. package/dist/drawing/process-node.d.ts +17 -0
  44. package/dist/drawing/process-node.js +41 -0
  45. package/dist/drawing/text/draw-text.js +6 -8
  46. package/dist/drawing/text/handle-text-node.d.ts +8 -5
  47. package/dist/drawing/text/handle-text-node.js +6 -5
  48. package/dist/drawing/transform-in-3d.d.ts +7 -7
  49. package/dist/drawing/transform-in-3d.js +27 -13
  50. package/dist/drawing/transform-rect-with-matrix.d.ts +4 -0
  51. package/dist/drawing/transform-rect-with-matrix.js +19 -0
  52. package/dist/drawing/turn-svg-into-drawable.js +7 -0
  53. package/dist/esm/index.mjs +897 -205
  54. package/dist/find-canvas-elements.d.ts +1 -0
  55. package/dist/find-canvas-elements.js +13 -0
  56. package/dist/find-capturable-elements.d.ts +1 -1
  57. package/dist/find-capturable-elements.js +20 -22
  58. package/dist/get-biggest-bounding-client-rect.js +18 -5
  59. package/dist/internal-state.d.ts +9 -0
  60. package/dist/internal-state.js +12 -0
  61. package/dist/opacity.d.ts +4 -0
  62. package/dist/opacity.js +7 -0
  63. package/dist/render-media-on-web.d.ts +3 -0
  64. package/dist/render-media-on-web.js +38 -15
  65. package/dist/render-still-on-web.d.ts +6 -1
  66. package/dist/render-still-on-web.js +29 -8
  67. package/dist/send-telemetry-event.js +1 -1
  68. package/dist/take-screenshot.d.ts +8 -2
  69. package/dist/take-screenshot.js +16 -4
  70. package/dist/transform.d.ts +4 -0
  71. package/dist/transform.js +6 -0
  72. package/package.json +7 -6
@@ -0,0 +1,31 @@
1
+ export type BorderRadiusCorners = {
2
+ topLeft: {
3
+ horizontal: number;
4
+ vertical: number;
5
+ };
6
+ topRight: {
7
+ horizontal: number;
8
+ vertical: number;
9
+ };
10
+ bottomRight: {
11
+ horizontal: number;
12
+ vertical: number;
13
+ };
14
+ bottomLeft: {
15
+ horizontal: number;
16
+ vertical: number;
17
+ };
18
+ };
19
+ export declare function parseBorderRadius({ borderRadius, width, height, }: {
20
+ borderRadius: string;
21
+ width: number;
22
+ height: number;
23
+ }): BorderRadiusCorners;
24
+ export declare function setBorderRadius({ ctx, x, y, width, height, borderRadius, }: {
25
+ ctx: OffscreenCanvasRenderingContext2D;
26
+ x: number;
27
+ y: number;
28
+ width: number;
29
+ height: number;
30
+ borderRadius: BorderRadiusCorners;
31
+ }): () => void;
@@ -0,0 +1,152 @@
1
+ function parseValue({ value, reference, }) {
2
+ value = value.trim();
3
+ if (value.endsWith('%')) {
4
+ const percentage = parseFloat(value);
5
+ return (percentage / 100) * reference;
6
+ }
7
+ if (value.endsWith('px')) {
8
+ return parseFloat(value);
9
+ }
10
+ // If no unit, assume pixels
11
+ return parseFloat(value);
12
+ }
13
+ function expandShorthand(values) {
14
+ if (values.length === 1) {
15
+ // All corners the same
16
+ return [values[0], values[0], values[0], values[0]];
17
+ }
18
+ if (values.length === 2) {
19
+ // [0] = top-left & bottom-right, [1] = top-right & bottom-left
20
+ return [values[0], values[1], values[0], values[1]];
21
+ }
22
+ if (values.length === 3) {
23
+ // [0] = top-left, [1] = top-right & bottom-left, [2] = bottom-right
24
+ return [values[0], values[1], values[2], values[1]];
25
+ }
26
+ // 4 values: top-left, top-right, bottom-right, bottom-left
27
+ return [values[0], values[1], values[2], values[3]];
28
+ }
29
+ function clampBorderRadius({ borderRadius, width, height, }) {
30
+ // According to CSS spec, if the sum of border radii on adjacent corners
31
+ // exceeds the length of the edge, they should be proportionally reduced
32
+ const clamped = {
33
+ topLeft: { ...borderRadius.topLeft },
34
+ topRight: { ...borderRadius.topRight },
35
+ bottomRight: { ...borderRadius.bottomRight },
36
+ bottomLeft: { ...borderRadius.bottomLeft },
37
+ };
38
+ // Check top edge
39
+ const topSum = clamped.topLeft.horizontal + clamped.topRight.horizontal;
40
+ if (topSum > width) {
41
+ const factor = width / topSum;
42
+ clamped.topLeft.horizontal *= factor;
43
+ clamped.topRight.horizontal *= factor;
44
+ }
45
+ // Check right edge
46
+ const rightSum = clamped.topRight.vertical + clamped.bottomRight.vertical;
47
+ if (rightSum > height) {
48
+ const factor = height / rightSum;
49
+ clamped.topRight.vertical *= factor;
50
+ clamped.bottomRight.vertical *= factor;
51
+ }
52
+ // Check bottom edge
53
+ const bottomSum = clamped.bottomRight.horizontal + clamped.bottomLeft.horizontal;
54
+ if (bottomSum > width) {
55
+ const factor = width / bottomSum;
56
+ clamped.bottomRight.horizontal *= factor;
57
+ clamped.bottomLeft.horizontal *= factor;
58
+ }
59
+ // Check left edge
60
+ const leftSum = clamped.bottomLeft.vertical + clamped.topLeft.vertical;
61
+ if (leftSum > height) {
62
+ const factor = height / leftSum;
63
+ clamped.bottomLeft.vertical *= factor;
64
+ clamped.topLeft.vertical *= factor;
65
+ }
66
+ return clamped;
67
+ }
68
+ export function parseBorderRadius({ borderRadius, width, height, }) {
69
+ // Split by '/' to separate horizontal and vertical radii
70
+ const parts = borderRadius.split('/').map((part) => part.trim());
71
+ const horizontalPart = parts[0];
72
+ const verticalPart = parts[1];
73
+ // Split each part into individual values
74
+ const horizontalValues = horizontalPart.split(/\s+/).filter((v) => v);
75
+ const verticalValues = verticalPart
76
+ ? verticalPart.split(/\s+/).filter((v) => v)
77
+ : horizontalValues; // If no '/', use horizontal values for vertical
78
+ // Expand shorthand to 4 values
79
+ const [hTopLeft, hTopRight, hBottomRight, hBottomLeft] = expandShorthand(horizontalValues);
80
+ const [vTopLeft, vTopRight, vBottomRight, vBottomLeft] = expandShorthand(verticalValues);
81
+ return clampBorderRadius({
82
+ borderRadius: {
83
+ topLeft: {
84
+ horizontal: parseValue({ value: hTopLeft, reference: width }),
85
+ vertical: parseValue({ value: vTopLeft, reference: height }),
86
+ },
87
+ topRight: {
88
+ horizontal: parseValue({ value: hTopRight, reference: width }),
89
+ vertical: parseValue({ value: vTopRight, reference: height }),
90
+ },
91
+ bottomRight: {
92
+ horizontal: parseValue({ value: hBottomRight, reference: width }),
93
+ vertical: parseValue({ value: vBottomRight, reference: height }),
94
+ },
95
+ bottomLeft: {
96
+ horizontal: parseValue({ value: hBottomLeft, reference: width }),
97
+ vertical: parseValue({ value: vBottomLeft, reference: height }),
98
+ },
99
+ },
100
+ width,
101
+ height,
102
+ });
103
+ }
104
+ export function setBorderRadius({ ctx, x, y, width, height, borderRadius, }) {
105
+ if (borderRadius.topLeft.horizontal === 0 &&
106
+ borderRadius.topLeft.vertical === 0 &&
107
+ borderRadius.topRight.horizontal === 0 &&
108
+ borderRadius.topRight.vertical === 0 &&
109
+ borderRadius.bottomRight.horizontal === 0 &&
110
+ borderRadius.bottomRight.vertical === 0 &&
111
+ borderRadius.bottomLeft.horizontal === 0 &&
112
+ borderRadius.bottomLeft.vertical === 0) {
113
+ return () => { };
114
+ }
115
+ ctx.save();
116
+ ctx.beginPath();
117
+ // Start at top-left corner, after the horizontal radius
118
+ ctx.moveTo(x + borderRadius.topLeft.horizontal, y);
119
+ // Top edge to top-right corner
120
+ ctx.lineTo(x + width - borderRadius.topRight.horizontal, y);
121
+ // Top-right corner (elliptical arc)
122
+ if (borderRadius.topRight.horizontal > 0 ||
123
+ borderRadius.topRight.vertical > 0) {
124
+ ctx.ellipse(x + width - borderRadius.topRight.horizontal, y + borderRadius.topRight.vertical, borderRadius.topRight.horizontal, borderRadius.topRight.vertical, 0, -Math.PI / 2, 0);
125
+ }
126
+ // Right edge to bottom-right corner
127
+ ctx.lineTo(x + width, y + height - borderRadius.bottomRight.vertical);
128
+ // Bottom-right corner (elliptical arc)
129
+ if (borderRadius.bottomRight.horizontal > 0 ||
130
+ borderRadius.bottomRight.vertical > 0) {
131
+ ctx.ellipse(x + width - borderRadius.bottomRight.horizontal, y + height - borderRadius.bottomRight.vertical, borderRadius.bottomRight.horizontal, borderRadius.bottomRight.vertical, 0, 0, Math.PI / 2);
132
+ }
133
+ // Bottom edge to bottom-left corner
134
+ ctx.lineTo(x + borderRadius.bottomLeft.horizontal, y + height);
135
+ // Bottom-left corner (elliptical arc)
136
+ if (borderRadius.bottomLeft.horizontal > 0 ||
137
+ borderRadius.bottomLeft.vertical > 0) {
138
+ ctx.ellipse(x + borderRadius.bottomLeft.horizontal, y + height - borderRadius.bottomLeft.vertical, borderRadius.bottomLeft.horizontal, borderRadius.bottomLeft.vertical, 0, Math.PI / 2, Math.PI);
139
+ }
140
+ // Left edge to top-left corner
141
+ ctx.lineTo(x, y + borderRadius.topLeft.vertical);
142
+ // Top-left corner (elliptical arc)
143
+ if (borderRadius.topLeft.horizontal > 0 ||
144
+ borderRadius.topLeft.vertical > 0) {
145
+ ctx.ellipse(x + borderRadius.topLeft.horizontal, y + borderRadius.topLeft.vertical, borderRadius.topLeft.horizontal, borderRadius.topLeft.vertical, 0, Math.PI, (Math.PI * 3) / 2);
146
+ }
147
+ ctx.closePath();
148
+ ctx.clip();
149
+ return () => {
150
+ ctx.restore();
151
+ };
152
+ }
@@ -6,4 +6,6 @@ export declare const calculateTransforms: (element: HTMLElement | SVGSVGElement)
6
6
  x: number;
7
7
  y: number;
8
8
  };
9
+ borderRadius: import("./drawing/border-radius").BorderRadiusCorners;
10
+ opacity: number;
9
11
  };
@@ -1,3 +1,4 @@
1
+ import { parseBorderRadius } from './drawing/border-radius';
1
2
  import { parseTransformOrigin } from './parse-transform-origin';
2
3
  const getInternalTransformOrigin = (transform) => {
3
4
  var _a;
@@ -21,8 +22,18 @@ export const calculateTransforms = (element) => {
21
22
  let parent = element;
22
23
  const transforms = [];
23
24
  const toReset = [];
25
+ let borderRadius = '';
26
+ let opacity = 1;
24
27
  while (parent) {
25
28
  const computedStyle = getComputedStyle(parent);
29
+ if (parent === element) {
30
+ borderRadius = computedStyle.borderRadius;
31
+ }
32
+ // Multiply opacity values from element and all parents
33
+ const parentOpacity = computedStyle.opacity;
34
+ if (parentOpacity && parentOpacity !== '') {
35
+ opacity *= parseFloat(parentOpacity);
36
+ }
26
37
  if ((computedStyle.transform && computedStyle.transform !== 'none') ||
27
38
  parent === element) {
28
39
  const toParse = computedStyle.transform === 'none' || computedStyle.transform === ''
@@ -70,5 +81,11 @@ export const calculateTransforms = (element) => {
70
81
  }
71
82
  },
72
83
  nativeTransformOrigin,
84
+ borderRadius: parseBorderRadius({
85
+ borderRadius,
86
+ width: dimensions.width,
87
+ height: dimensions.height,
88
+ }),
89
+ opacity,
73
90
  };
74
91
  };
@@ -1,10 +1,4 @@
1
1
  export type Composable = {
2
- type: 'canvas';
3
- element: HTMLCanvasElement;
4
- } | {
5
- type: 'svg';
6
- element: SVGSVGElement;
7
- } | {
8
- type: 'img';
9
- element: HTMLImageElement;
2
+ type: 'element';
3
+ element: HTMLElement | SVGElement;
10
4
  };
@@ -1,12 +1,36 @@
1
+ import { setBorderRadius } from './border-radius';
1
2
  import { calculateTransforms } from './calculate-transforms';
2
- import { turnSvgIntoDrawable } from './compose-svg';
3
+ import { turnSvgIntoDrawable } from './drawing/compose-svg';
4
+ import { setOpacity } from './drawing/opacity';
5
+ import { setTransform } from './drawing/transform';
3
6
  export const composeCanvas = async (canvas, context) => {
4
- const { totalMatrix, reset, dimensions } = calculateTransforms(canvas);
5
- context.setTransform(totalMatrix);
7
+ const { totalMatrix, reset, dimensions, borderRadius, opacity } = calculateTransforms(canvas);
8
+ if (opacity === 0) {
9
+ reset();
10
+ return;
11
+ }
6
12
  const drawable = canvas instanceof SVGSVGElement
7
13
  ? await turnSvgIntoDrawable(canvas)
8
14
  : canvas;
15
+ const finishTransform = setTransform({
16
+ ctx: context,
17
+ transform: totalMatrix,
18
+ });
19
+ const finishBorderRadius = setBorderRadius({
20
+ ctx: context,
21
+ x: dimensions.left,
22
+ y: dimensions.top,
23
+ width: dimensions.width,
24
+ height: dimensions.height,
25
+ borderRadius,
26
+ });
27
+ const finishOpacity = setOpacity({
28
+ ctx: context,
29
+ opacity,
30
+ });
9
31
  context.drawImage(drawable, dimensions.left, dimensions.top, dimensions.width, dimensions.height);
10
- context.setTransform(new DOMMatrix());
32
+ finishOpacity();
33
+ finishBorderRadius();
34
+ finishTransform();
11
35
  reset();
12
36
  };
package/dist/compose.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- export declare const compose: ({ element, context, offsetLeft, offsetTop, }: {
1
+ import type { LogLevel } from 'remotion';
2
+ import type { InternalState } from './internal-state';
3
+ export declare const compose: ({ element, context, logLevel, parentRect, internalState, }: {
2
4
  element: HTMLElement | SVGElement;
3
5
  context: OffscreenCanvasRenderingContext2D;
4
- offsetLeft: number;
5
- offsetTop: number;
6
+ logLevel: LogLevel;
7
+ parentRect: DOMRect;
8
+ internalState: InternalState;
6
9
  }) => Promise<void>;
package/dist/compose.js CHANGED
@@ -1,23 +1,31 @@
1
1
  import { drawDomElement } from './drawing/draw-dom-element';
2
- import { drawElementToCanvas } from './drawing/draw-element-to-canvas';
2
+ import { processNode } from './drawing/process-node';
3
3
  import { handleTextNode } from './drawing/text/handle-text-node';
4
4
  import { skipToNextNonDescendant } from './walk-tree';
5
- const walkOverNode = ({ node, context, offsetLeft, offsetTop, }) => {
5
+ const walkOverNode = ({ node, context, logLevel, parentRect, internalState, }) => {
6
6
  if (node instanceof HTMLElement || node instanceof SVGElement) {
7
- return drawElementToCanvas({
7
+ return processNode({
8
8
  element: node,
9
9
  context,
10
10
  draw: drawDomElement(node),
11
- offsetLeft,
12
- offsetTop,
11
+ logLevel,
12
+ parentRect,
13
+ internalState,
13
14
  });
14
15
  }
15
16
  if (node instanceof Text) {
16
- return handleTextNode({ node, context, offsetLeft, offsetTop });
17
+ return handleTextNode({
18
+ node,
19
+ context,
20
+ logLevel,
21
+ parentRect,
22
+ internalState,
23
+ });
17
24
  }
18
25
  throw new Error('Unknown node type');
19
26
  };
20
- export const compose = async ({ element, context, offsetLeft, offsetTop, }) => {
27
+ export const compose = async ({ element, context, logLevel, parentRect, internalState, }) => {
28
+ const cleanupAfterChildren = [];
21
29
  const treeWalker = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, (node) => {
22
30
  if (node instanceof Element) {
23
31
  // SVG does have children, but we process SVG elements in its
@@ -33,19 +41,40 @@ export const compose = async ({ element, context, offsetLeft, offsetTop, }) => {
33
41
  return NodeFilter.FILTER_ACCEPT;
34
42
  });
35
43
  while (true) {
44
+ for (let i = 0; i < cleanupAfterChildren.length;) {
45
+ const cleanup = cleanupAfterChildren[i];
46
+ if (!(cleanup.element === treeWalker.currentNode ||
47
+ cleanup.element.contains(treeWalker.currentNode))) {
48
+ cleanup.cleanupFn();
49
+ cleanupAfterChildren.splice(i, 1);
50
+ }
51
+ else {
52
+ i++;
53
+ }
54
+ }
36
55
  const val = await walkOverNode({
37
56
  node: treeWalker.currentNode,
38
57
  context,
39
- offsetLeft,
40
- offsetTop,
58
+ logLevel,
59
+ parentRect,
60
+ internalState,
41
61
  });
42
- if (val === 'skip-children') {
62
+ if (val.type === 'skip-children') {
43
63
  if (!skipToNextNonDescendant(treeWalker)) {
44
64
  break;
45
65
  }
46
66
  }
47
- else if (!treeWalker.nextNode()) {
48
- break;
67
+ else {
68
+ cleanupAfterChildren.push({
69
+ element: treeWalker.currentNode,
70
+ cleanupFn: val.cleanupAfterChildren,
71
+ });
72
+ if (!treeWalker.nextNode()) {
73
+ break;
74
+ }
49
75
  }
50
76
  }
77
+ for (const cleanup of cleanupAfterChildren) {
78
+ cleanup.cleanupFn();
79
+ }
51
80
  };
@@ -21,11 +21,9 @@ export declare function parseBorderRadius({ borderRadius, width, height, }: {
21
21
  width: number;
22
22
  height: number;
23
23
  }): BorderRadiusCorners;
24
- export declare function setBorderRadius({ ctx, x, y, width, height, borderRadius, }: {
24
+ export declare function setBorderRadius({ ctx, rect, borderRadius, forceClipEvenWhenZero, }: {
25
25
  ctx: OffscreenCanvasRenderingContext2D;
26
- x: number;
27
- y: number;
28
- width: number;
29
- height: number;
26
+ rect: DOMRect;
30
27
  borderRadius: BorderRadiusCorners;
28
+ forceClipEvenWhenZero: boolean;
31
29
  }): () => void;
@@ -101,7 +101,7 @@ export function parseBorderRadius({ borderRadius, width, height, }) {
101
101
  height,
102
102
  });
103
103
  }
104
- export function setBorderRadius({ ctx, x, y, width, height, borderRadius, }) {
104
+ export function setBorderRadius({ ctx, rect, borderRadius, forceClipEvenWhenZero = false, }) {
105
105
  if (borderRadius.topLeft.horizontal === 0 &&
106
106
  borderRadius.topLeft.vertical === 0 &&
107
107
  borderRadius.topRight.horizontal === 0 &&
@@ -109,40 +109,41 @@ export function setBorderRadius({ ctx, x, y, width, height, borderRadius, }) {
109
109
  borderRadius.bottomRight.horizontal === 0 &&
110
110
  borderRadius.bottomRight.vertical === 0 &&
111
111
  borderRadius.bottomLeft.horizontal === 0 &&
112
- borderRadius.bottomLeft.vertical === 0) {
112
+ borderRadius.bottomLeft.vertical === 0 &&
113
+ !forceClipEvenWhenZero) {
113
114
  return () => { };
114
115
  }
115
116
  ctx.save();
116
117
  ctx.beginPath();
117
118
  // Start at top-left corner, after the horizontal radius
118
- ctx.moveTo(x + borderRadius.topLeft.horizontal, y);
119
+ ctx.moveTo(rect.left + borderRadius.topLeft.horizontal, rect.top);
119
120
  // Top edge to top-right corner
120
- ctx.lineTo(x + width - borderRadius.topRight.horizontal, y);
121
+ ctx.lineTo(rect.left + rect.width - borderRadius.topRight.horizontal, rect.top);
121
122
  // Top-right corner (elliptical arc)
122
123
  if (borderRadius.topRight.horizontal > 0 ||
123
124
  borderRadius.topRight.vertical > 0) {
124
- ctx.ellipse(x + width - borderRadius.topRight.horizontal, y + borderRadius.topRight.vertical, borderRadius.topRight.horizontal, borderRadius.topRight.vertical, 0, -Math.PI / 2, 0);
125
+ ctx.ellipse(rect.left + rect.width - borderRadius.topRight.horizontal, rect.top + borderRadius.topRight.vertical, borderRadius.topRight.horizontal, borderRadius.topRight.vertical, 0, -Math.PI / 2, 0);
125
126
  }
126
127
  // Right edge to bottom-right corner
127
- ctx.lineTo(x + width, y + height - borderRadius.bottomRight.vertical);
128
+ ctx.lineTo(rect.left + rect.width, rect.top + rect.height - borderRadius.bottomRight.vertical);
128
129
  // Bottom-right corner (elliptical arc)
129
130
  if (borderRadius.bottomRight.horizontal > 0 ||
130
131
  borderRadius.bottomRight.vertical > 0) {
131
- ctx.ellipse(x + width - borderRadius.bottomRight.horizontal, y + height - borderRadius.bottomRight.vertical, borderRadius.bottomRight.horizontal, borderRadius.bottomRight.vertical, 0, 0, Math.PI / 2);
132
+ ctx.ellipse(rect.left + rect.width - borderRadius.bottomRight.horizontal, rect.top + rect.height - borderRadius.bottomRight.vertical, borderRadius.bottomRight.horizontal, borderRadius.bottomRight.vertical, 0, 0, Math.PI / 2);
132
133
  }
133
134
  // Bottom edge to bottom-left corner
134
- ctx.lineTo(x + borderRadius.bottomLeft.horizontal, y + height);
135
+ ctx.lineTo(rect.left + borderRadius.bottomLeft.horizontal, rect.top + rect.height);
135
136
  // Bottom-left corner (elliptical arc)
136
137
  if (borderRadius.bottomLeft.horizontal > 0 ||
137
138
  borderRadius.bottomLeft.vertical > 0) {
138
- ctx.ellipse(x + borderRadius.bottomLeft.horizontal, y + height - borderRadius.bottomLeft.vertical, borderRadius.bottomLeft.horizontal, borderRadius.bottomLeft.vertical, 0, Math.PI / 2, Math.PI);
139
+ ctx.ellipse(rect.left + borderRadius.bottomLeft.horizontal, rect.top + rect.height - borderRadius.bottomLeft.vertical, borderRadius.bottomLeft.horizontal, borderRadius.bottomLeft.vertical, 0, Math.PI / 2, Math.PI);
139
140
  }
140
141
  // Left edge to top-left corner
141
- ctx.lineTo(x, y + borderRadius.topLeft.vertical);
142
+ ctx.lineTo(rect.left, rect.top + borderRadius.topLeft.vertical);
142
143
  // Top-left corner (elliptical arc)
143
144
  if (borderRadius.topLeft.horizontal > 0 ||
144
145
  borderRadius.topLeft.vertical > 0) {
145
- ctx.ellipse(x + borderRadius.topLeft.horizontal, y + borderRadius.topLeft.vertical, borderRadius.topLeft.horizontal, borderRadius.topLeft.vertical, 0, Math.PI, (Math.PI * 3) / 2);
146
+ ctx.ellipse(rect.left + borderRadius.topLeft.horizontal, rect.top + borderRadius.topLeft.vertical, borderRadius.topLeft.horizontal, borderRadius.topLeft.vertical, 0, Math.PI, (Math.PI * 3) / 2);
146
147
  }
147
148
  ctx.closePath();
148
149
  ctx.clip();
@@ -1,4 +1,8 @@
1
- export declare const calculateTransforms: (element: HTMLElement | SVGElement) => {
1
+ export declare const calculateTransforms: ({ element, offsetLeft, offsetTop, }: {
2
+ element: HTMLElement | SVGElement;
3
+ offsetLeft: number;
4
+ offsetTop: number;
5
+ }) => {
2
6
  dimensions: DOMRect;
3
7
  totalMatrix: DOMMatrix;
4
8
  reset: () => void;
@@ -6,6 +10,6 @@ export declare const calculateTransforms: (element: HTMLElement | SVGElement) =>
6
10
  x: number;
7
11
  y: number;
8
12
  };
9
- opacity: number;
10
13
  computedStyle: CSSStyleDeclaration;
14
+ opacity: number;
11
15
  };
@@ -1,3 +1,4 @@
1
+ import { hasAnyTransformCssValue, hasTransformCssValue } from './has-transform';
1
2
  import { parseTransformOrigin } from './parse-transform-origin';
2
3
  const getInternalTransformOrigin = (transform) => {
3
4
  var _a;
@@ -9,35 +10,28 @@ const getInternalTransformOrigin = (transform) => {
9
10
  };
10
11
  return origin;
11
12
  };
12
- const getGlobalTransformOrigin = (transform) => {
13
+ const getGlobalTransformOrigin = ({ transform, offsetLeft, offsetTop, }) => {
13
14
  const { x: originX, y: originY } = getInternalTransformOrigin(transform);
14
15
  return {
15
- x: originX + transform.boundingClientRect.left,
16
- y: originY + transform.boundingClientRect.top,
16
+ x: originX + transform.boundingClientRect.left - offsetLeft,
17
+ y: originY + transform.boundingClientRect.top - offsetTop,
17
18
  };
18
19
  };
19
- export const calculateTransforms = (element) => {
20
+ export const calculateTransforms = ({ element, offsetLeft, offsetTop, }) => {
20
21
  // Compute the cumulative transform by traversing parent nodes
21
22
  let parent = element;
22
23
  const transforms = [];
23
24
  const toReset = [];
24
- let opacity = 1;
25
25
  let elementComputedStyle = null;
26
26
  while (parent) {
27
27
  const computedStyle = getComputedStyle(parent);
28
- // Multiply opacity values from element and all parents
29
- const parentOpacity = computedStyle.opacity;
30
- if (parentOpacity && parentOpacity !== '') {
31
- opacity *= parseFloat(parentOpacity);
32
- }
33
28
  if (parent === element) {
34
29
  elementComputedStyle = computedStyle;
35
30
  }
36
- if ((computedStyle.transform && computedStyle.transform !== 'none') ||
37
- parent === element) {
38
- const toParse = computedStyle.transform === 'none' || computedStyle.transform === ''
39
- ? undefined
40
- : computedStyle.transform;
31
+ if (hasAnyTransformCssValue(computedStyle) || parent === element) {
32
+ const toParse = hasTransformCssValue(computedStyle)
33
+ ? computedStyle.transform
34
+ : undefined;
41
35
  const matrix = new DOMMatrix(toParse);
42
36
  const { transform, scale, rotate } = parent.style;
43
37
  const additionalMatrices = [];
@@ -57,7 +51,7 @@ export const calculateTransforms = (element) => {
57
51
  parent.style.scale = 'none';
58
52
  parent.style.rotate = 'none';
59
53
  transforms.push({
60
- rect: parent,
54
+ element: parent,
61
55
  transformOrigin: computedStyle.transformOrigin,
62
56
  boundingClientRect: null,
63
57
  matrices: additionalMatrices,
@@ -72,17 +66,18 @@ export const calculateTransforms = (element) => {
72
66
  parent = parent.parentElement;
73
67
  }
74
68
  for (const transform of transforms) {
75
- transform.boundingClientRect = transform.rect.getBoundingClientRect();
69
+ transform.boundingClientRect = transform.element.getBoundingClientRect();
76
70
  }
77
71
  const dimensions = transforms[0].boundingClientRect;
78
72
  const nativeTransformOrigin = getInternalTransformOrigin(transforms[0]);
79
73
  const totalMatrix = new DOMMatrix();
80
74
  for (const transform of transforms.slice().reverse()) {
81
- if (!transform.boundingClientRect) {
82
- throw new Error('Bounding client rect not found');
83
- }
84
75
  for (const matrix of transform.matrices) {
85
- const globalTransformOrigin = getGlobalTransformOrigin(transform);
76
+ const globalTransformOrigin = getGlobalTransformOrigin({
77
+ transform,
78
+ offsetLeft,
79
+ offsetTop,
80
+ });
86
81
  const transformMatrix = new DOMMatrix()
87
82
  .translate(globalTransformOrigin.x, globalTransformOrigin.y)
88
83
  .multiply(matrix)
@@ -102,7 +97,9 @@ export const calculateTransforms = (element) => {
102
97
  }
103
98
  },
104
99
  nativeTransformOrigin,
105
- opacity,
106
100
  computedStyle: elementComputedStyle,
101
+ opacity: elementComputedStyle.opacity && elementComputedStyle.opacity !== ''
102
+ ? parseFloat(elementComputedStyle.opacity)
103
+ : 1,
107
104
  };
108
105
  };
@@ -0,0 +1,8 @@
1
+ export declare const canvasOffsetFromRect: ({ rect }: {
2
+ rect: DOMRect;
3
+ }) => {
4
+ offsetLeft: number;
5
+ offsetTop: number;
6
+ canvasWidth: number;
7
+ canvasHeight: number;
8
+ };
@@ -0,0 +1,12 @@
1
+ export const canvasOffsetFromRect = ({ rect }) => {
2
+ const canvasOffsetLeft = Math.min(rect.left, 0);
3
+ const canvasOffsetTop = Math.min(rect.top, 0);
4
+ const canvasWidth = Math.max(rect.width, rect.right);
5
+ const canvasHeight = Math.max(rect.height, rect.bottom);
6
+ return {
7
+ offsetLeft: canvasOffsetLeft,
8
+ offsetTop: canvasOffsetTop,
9
+ canvasWidth,
10
+ canvasHeight,
11
+ };
12
+ };
@@ -0,0 +1,4 @@
1
+ export declare const getNarrowerRect: ({ firstRect, secondRect, }: {
2
+ firstRect: DOMRect;
3
+ secondRect: DOMRect;
4
+ }) => DOMRect;
@@ -0,0 +1,7 @@
1
+ export const getNarrowerRect = ({ firstRect, secondRect, }) => {
2
+ const left = Math.max(firstRect.left, secondRect.left);
3
+ const top = Math.max(firstRect.top, secondRect.top);
4
+ const bottom = Math.min(firstRect.bottom, secondRect.bottom);
5
+ const right = Math.min(firstRect.right, secondRect.right);
6
+ return new DOMRect(left, top, right - left, bottom - top);
7
+ };
@@ -0,0 +1 @@
1
+ export declare const drawElementToCanvas: (canvas: HTMLCanvasElement | HTMLImageElement | SVGSVGElement, context: OffscreenCanvasRenderingContext2D) => Promise<void>;
@@ -0,0 +1,36 @@
1
+ import { setBorderRadius } from './border-radius';
2
+ import { calculateTransforms } from './calculate-transforms';
3
+ import { turnSvgIntoDrawable } from './compose-svg';
4
+ import { setOpacity } from './opacity';
5
+ import { setTransform } from './transform';
6
+ export const drawElementToCanvas = async (canvas, context) => {
7
+ const { totalMatrix, reset, dimensions, borderRadius, opacity } = calculateTransforms(canvas);
8
+ if (opacity === 0) {
9
+ reset();
10
+ return;
11
+ }
12
+ const drawable = canvas instanceof SVGSVGElement
13
+ ? await turnSvgIntoDrawable(canvas)
14
+ : canvas;
15
+ const finishTransform = setTransform({
16
+ ctx: context,
17
+ transform: totalMatrix,
18
+ });
19
+ const finishBorderRadius = setBorderRadius({
20
+ ctx: context,
21
+ x: dimensions.left,
22
+ y: dimensions.top,
23
+ width: dimensions.width,
24
+ height: dimensions.height,
25
+ borderRadius,
26
+ });
27
+ const finishOpacity = setOpacity({
28
+ ctx: context,
29
+ opacity,
30
+ });
31
+ context.drawImage(drawable, dimensions.left, dimensions.top, dimensions.width, dimensions.height);
32
+ finishOpacity();
33
+ finishBorderRadius();
34
+ finishTransform();
35
+ reset();
36
+ };
@@ -0,0 +1 @@
1
+ export declare const turnSvgIntoDrawable: (svg: SVGSVGElement) => Promise<HTMLImageElement>;