@remotion/web-renderer 4.0.391 → 4.0.392

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 (39) 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 +11 -0
  4. package/dist/calculate-transforms.js +91 -0
  5. package/dist/composable.d.ts +4 -0
  6. package/dist/composable.js +1 -0
  7. package/dist/compose-canvas.d.ts +1 -0
  8. package/dist/compose-canvas.js +36 -0
  9. package/dist/compose-svg.d.ts +1 -0
  10. package/dist/compose-svg.js +34 -0
  11. package/dist/drawing/calculate-transforms.js +27 -8
  12. package/dist/drawing/compose-canvas.d.ts +1 -0
  13. package/dist/drawing/compose-canvas.js +36 -0
  14. package/dist/drawing/compose-svg.d.ts +1 -0
  15. package/dist/drawing/compose-svg.js +34 -0
  16. package/dist/drawing/compose.d.ts +5 -0
  17. package/dist/drawing/compose.js +6 -0
  18. package/dist/drawing/draw-element-to-canvas.js +40 -47
  19. package/dist/drawing/draw-element.d.ts +8 -0
  20. package/dist/drawing/draw-element.js +50 -0
  21. package/dist/drawing/get-computed-style-cache.d.ts +0 -0
  22. package/dist/drawing/get-computed-style-cache.js +1 -0
  23. package/dist/drawing/text/draw-text.d.ts +1 -0
  24. package/dist/drawing/text/draw-text.js +57 -0
  25. package/dist/drawing/text/handle-text-node.js +0 -12
  26. package/dist/drawing/transform-in-3d.d.ts +8 -0
  27. package/dist/drawing/transform-in-3d.js +125 -0
  28. package/dist/esm/index.mjs +301 -103
  29. package/dist/find-canvas-elements.d.ts +1 -0
  30. package/dist/find-canvas-elements.js +13 -0
  31. package/dist/find-capturable-elements.d.ts +2 -0
  32. package/dist/find-capturable-elements.js +26 -0
  33. package/dist/opacity.d.ts +4 -0
  34. package/dist/opacity.js +7 -0
  35. package/dist/parse-transform-origin.d.ts +4 -0
  36. package/dist/parse-transform-origin.js +7 -0
  37. package/dist/transform.d.ts +4 -0
  38. package/dist/transform.js +6 -0
  39. package/package.json +5 -5
@@ -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
+ }
@@ -0,0 +1,11 @@
1
+ export declare const calculateTransforms: (element: HTMLElement | SVGSVGElement) => {
2
+ dimensions: DOMRect;
3
+ totalMatrix: DOMMatrix;
4
+ reset: () => void;
5
+ nativeTransformOrigin: {
6
+ x: number;
7
+ y: number;
8
+ };
9
+ borderRadius: import("./drawing/border-radius").BorderRadiusCorners;
10
+ opacity: number;
11
+ };
@@ -0,0 +1,91 @@
1
+ import { parseBorderRadius } from './drawing/border-radius';
2
+ import { parseTransformOrigin } from './parse-transform-origin';
3
+ const getInternalTransformOrigin = (transform) => {
4
+ var _a;
5
+ const centerX = transform.boundingClientRect.width / 2;
6
+ const centerY = transform.boundingClientRect.height / 2;
7
+ const origin = (_a = parseTransformOrigin(transform.transformOrigin)) !== null && _a !== void 0 ? _a : {
8
+ x: centerX,
9
+ y: centerY,
10
+ };
11
+ return origin;
12
+ };
13
+ const getGlobalTransformOrigin = (transform) => {
14
+ const { x: originX, y: originY } = getInternalTransformOrigin(transform);
15
+ return {
16
+ x: originX + transform.boundingClientRect.left,
17
+ y: originY + transform.boundingClientRect.top,
18
+ };
19
+ };
20
+ export const calculateTransforms = (element) => {
21
+ // Compute the cumulative transform by traversing parent nodes
22
+ let parent = element;
23
+ const transforms = [];
24
+ const toReset = [];
25
+ let borderRadius = '';
26
+ let opacity = 1;
27
+ while (parent) {
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
+ }
37
+ if ((computedStyle.transform && computedStyle.transform !== 'none') ||
38
+ parent === element) {
39
+ const toParse = computedStyle.transform === 'none' || computedStyle.transform === ''
40
+ ? undefined
41
+ : computedStyle.transform;
42
+ const matrix = new DOMMatrix(toParse);
43
+ const { transform } = parent.style;
44
+ parent.style.transform = 'none';
45
+ transforms.push({
46
+ matrix,
47
+ rect: parent,
48
+ transformOrigin: computedStyle.transformOrigin,
49
+ boundingClientRect: null,
50
+ });
51
+ const parentRef = parent;
52
+ toReset.push(() => {
53
+ parentRef.style.transform = transform;
54
+ });
55
+ }
56
+ parent = parent.parentElement;
57
+ }
58
+ for (const transform of transforms) {
59
+ transform.boundingClientRect = transform.rect.getBoundingClientRect();
60
+ }
61
+ const dimensions = transforms[0].boundingClientRect;
62
+ const nativeTransformOrigin = getInternalTransformOrigin(transforms[0]);
63
+ const totalMatrix = new DOMMatrix();
64
+ for (const transform of transforms.slice().reverse()) {
65
+ if (!transform.boundingClientRect) {
66
+ throw new Error('Bounding client rect not found');
67
+ }
68
+ const globalTransformOrigin = getGlobalTransformOrigin(transform);
69
+ const transformMatrix = new DOMMatrix()
70
+ .translate(globalTransformOrigin.x, globalTransformOrigin.y)
71
+ .multiply(transform.matrix)
72
+ .translate(-globalTransformOrigin.x, -globalTransformOrigin.y);
73
+ totalMatrix.multiplySelf(transformMatrix);
74
+ }
75
+ return {
76
+ dimensions,
77
+ totalMatrix,
78
+ reset: () => {
79
+ for (const reset of toReset) {
80
+ reset();
81
+ }
82
+ },
83
+ nativeTransformOrigin,
84
+ borderRadius: parseBorderRadius({
85
+ borderRadius,
86
+ width: dimensions.width,
87
+ height: dimensions.height,
88
+ }),
89
+ opacity,
90
+ };
91
+ };
@@ -0,0 +1,4 @@
1
+ export type Composable = {
2
+ type: 'element';
3
+ element: HTMLElement | SVGElement;
4
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const composeCanvas: (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 './drawing/compose-svg';
4
+ import { setOpacity } from './drawing/opacity';
5
+ import { setTransform } from './drawing/transform';
6
+ export const composeCanvas = 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>;
@@ -0,0 +1,34 @@
1
+ export const turnSvgIntoDrawable = (svg) => {
2
+ const originalTransform = svg.style.transform;
3
+ const originalTransformOrigin = svg.style.transformOrigin;
4
+ const originalMarginLeft = svg.style.marginLeft;
5
+ const originalMarginRight = svg.style.marginRight;
6
+ const originalMarginTop = svg.style.marginTop;
7
+ const originalMarginBottom = svg.style.marginBottom;
8
+ svg.style.transform = 'none';
9
+ svg.style.transformOrigin = '';
10
+ // Margins were already included in the positioning calculation,
11
+ // so we need to remove them to avoid double counting.
12
+ svg.style.marginLeft = '0';
13
+ svg.style.marginRight = '0';
14
+ svg.style.marginTop = '0';
15
+ svg.style.marginBottom = '0';
16
+ const svgData = new XMLSerializer().serializeToString(svg);
17
+ svg.style.marginLeft = originalMarginLeft;
18
+ svg.style.marginRight = originalMarginRight;
19
+ svg.style.marginTop = originalMarginTop;
20
+ svg.style.marginBottom = originalMarginBottom;
21
+ svg.style.transform = originalTransform;
22
+ svg.style.transformOrigin = originalTransformOrigin;
23
+ return new Promise((resolve, reject) => {
24
+ const image = new Image();
25
+ const url = `data:image/svg+xml;base64,${btoa(svgData)}`;
26
+ image.onload = function () {
27
+ resolve(image);
28
+ };
29
+ image.onerror = () => {
30
+ reject(new Error('Failed to convert SVG to image'));
31
+ };
32
+ image.src = url;
33
+ });
34
+ };
@@ -39,17 +39,34 @@ export const calculateTransforms = (element) => {
39
39
  ? undefined
40
40
  : computedStyle.transform;
41
41
  const matrix = new DOMMatrix(toParse);
42
- const { transform } = parent.style;
42
+ const { transform, scale, rotate } = parent.style;
43
+ const additionalMatrices = [];
44
+ // The order of transformations is:
45
+ // 1. Translate --> We do not have to consider it since it changes getClientBoundingRect()
46
+ // 2. Rotate
47
+ // 3. Scale
48
+ // 4. CSS "transform"
49
+ if (rotate !== '') {
50
+ additionalMatrices.push(new DOMMatrix(`rotate(${rotate})`));
51
+ }
52
+ if (scale !== '') {
53
+ additionalMatrices.push(new DOMMatrix(`scale(${scale})`));
54
+ }
55
+ additionalMatrices.push(matrix);
43
56
  parent.style.transform = 'none';
57
+ parent.style.scale = 'none';
58
+ parent.style.rotate = 'none';
44
59
  transforms.push({
45
- matrix,
46
60
  rect: parent,
47
61
  transformOrigin: computedStyle.transformOrigin,
48
62
  boundingClientRect: null,
63
+ matrices: additionalMatrices,
49
64
  });
50
65
  const parentRef = parent;
51
66
  toReset.push(() => {
52
67
  parentRef.style.transform = transform;
68
+ parentRef.style.scale = scale;
69
+ parentRef.style.rotate = rotate;
53
70
  });
54
71
  }
55
72
  parent = parent.parentElement;
@@ -64,12 +81,14 @@ export const calculateTransforms = (element) => {
64
81
  if (!transform.boundingClientRect) {
65
82
  throw new Error('Bounding client rect not found');
66
83
  }
67
- const globalTransformOrigin = getGlobalTransformOrigin(transform);
68
- const transformMatrix = new DOMMatrix()
69
- .translate(globalTransformOrigin.x, globalTransformOrigin.y)
70
- .multiply(transform.matrix)
71
- .translate(-globalTransformOrigin.x, -globalTransformOrigin.y);
72
- totalMatrix.multiplySelf(transformMatrix);
84
+ for (const matrix of transform.matrices) {
85
+ const globalTransformOrigin = getGlobalTransformOrigin(transform);
86
+ const transformMatrix = new DOMMatrix()
87
+ .translate(globalTransformOrigin.x, globalTransformOrigin.y)
88
+ .multiply(matrix)
89
+ .translate(-globalTransformOrigin.x, -globalTransformOrigin.y);
90
+ totalMatrix.multiplySelf(transformMatrix);
91
+ }
73
92
  }
74
93
  if (!elementComputedStyle) {
75
94
  throw new Error('Element computed style not found');
@@ -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>;
@@ -0,0 +1,34 @@
1
+ export const turnSvgIntoDrawable = (svg) => {
2
+ const originalTransform = svg.style.transform;
3
+ const originalTransformOrigin = svg.style.transformOrigin;
4
+ const originalMarginLeft = svg.style.marginLeft;
5
+ const originalMarginRight = svg.style.marginRight;
6
+ const originalMarginTop = svg.style.marginTop;
7
+ const originalMarginBottom = svg.style.marginBottom;
8
+ svg.style.transform = 'none';
9
+ svg.style.transformOrigin = '';
10
+ // Margins were already included in the positioning calculation,
11
+ // so we need to remove them to avoid double counting.
12
+ svg.style.marginLeft = '0';
13
+ svg.style.marginRight = '0';
14
+ svg.style.marginTop = '0';
15
+ svg.style.marginBottom = '0';
16
+ const svgData = new XMLSerializer().serializeToString(svg);
17
+ svg.style.marginLeft = originalMarginLeft;
18
+ svg.style.marginRight = originalMarginRight;
19
+ svg.style.marginTop = originalMarginTop;
20
+ svg.style.marginBottom = originalMarginBottom;
21
+ svg.style.transform = originalTransform;
22
+ svg.style.transformOrigin = originalTransformOrigin;
23
+ return new Promise((resolve, reject) => {
24
+ const image = new Image();
25
+ const url = `data:image/svg+xml;base64,${btoa(svgData)}`;
26
+ image.onload = function () {
27
+ resolve(image);
28
+ };
29
+ image.onerror = () => {
30
+ reject(new Error('Failed to convert SVG to image'));
31
+ };
32
+ image.src = url;
33
+ });
34
+ };
@@ -0,0 +1,5 @@
1
+ import type { Composable } from '../composable';
2
+ export declare const compose: ({ composables, context, }: {
3
+ composables: Composable[];
4
+ context: OffscreenCanvasRenderingContext2D;
5
+ }) => Promise<void>;
@@ -0,0 +1,6 @@
1
+ import { drawElementToCanvas } from './draw-element-to-canvas';
2
+ export const compose = async ({ composables, context, }) => {
3
+ for (const composable of composables) {
4
+ await drawElementToCanvas(composable.element, context);
5
+ }
6
+ };
@@ -1,8 +1,6 @@
1
- import { parseBorderRadius, setBorderRadius } from './border-radius';
2
1
  import { calculateTransforms } from './calculate-transforms';
3
- import { drawBorder } from './draw-border';
4
- import { setOpacity } from './opacity';
5
- import { setTransform } from './transform';
2
+ import { drawElement } from './draw-element';
3
+ import { transformIn3d } from './transform-in-3d';
6
4
  export const drawElementToCanvas = async ({ element, context, draw, }) => {
7
5
  const { totalMatrix, reset, dimensions, opacity, computedStyle } = calculateTransforms(element);
8
6
  if (opacity === 0) {
@@ -13,49 +11,44 @@ export const drawElementToCanvas = async ({ element, context, draw, }) => {
13
11
  reset();
14
12
  return;
15
13
  }
16
- const background = computedStyle.backgroundColor;
17
- const borderRadius = parseBorderRadius({
18
- borderRadius: computedStyle.borderRadius,
19
- width: dimensions.width,
20
- height: dimensions.height,
21
- });
22
- const finishTransform = setTransform({
23
- ctx: context,
24
- transform: totalMatrix,
25
- });
26
- const finishBorderRadius = setBorderRadius({
27
- ctx: context,
28
- x: dimensions.left,
29
- y: dimensions.top,
30
- width: dimensions.width,
31
- height: dimensions.height,
32
- borderRadius,
33
- });
34
- const finishOpacity = setOpacity({
35
- ctx: context,
36
- opacity,
37
- });
38
- if (background &&
39
- background !== 'transparent' &&
40
- !(background.startsWith('rgba') &&
41
- (background.endsWith(', 0)') || background.endsWith(',0')))) {
42
- const originalFillStyle = context.fillStyle;
43
- context.fillStyle = background;
44
- context.fillRect(dimensions.left, dimensions.top, dimensions.width, dimensions.height);
45
- context.fillStyle = originalFillStyle;
14
+ if (!totalMatrix.is2D) {
15
+ const offsetLeft = Math.min(dimensions.left, 0);
16
+ const offsetTop = Math.min(dimensions.top, 0);
17
+ const tempCanvasWidth = Math.max(dimensions.width, dimensions.right);
18
+ const tempCanvasHeight = Math.max(dimensions.height, dimensions.bottom);
19
+ const tempCanvas = new OffscreenCanvas(tempCanvasWidth, tempCanvasHeight);
20
+ const context2 = tempCanvas.getContext('2d');
21
+ if (!context2) {
22
+ throw new Error('Could not get context');
23
+ }
24
+ const adjustedDimensions = new DOMRect(dimensions.left - offsetLeft, dimensions.top - offsetTop, dimensions.width, dimensions.height);
25
+ await drawElement({
26
+ dimensions: adjustedDimensions,
27
+ computedStyle,
28
+ context: context2,
29
+ draw,
30
+ opacity,
31
+ totalMatrix: new DOMMatrix(),
32
+ });
33
+ const transformed = transformIn3d({
34
+ canvasWidth: tempCanvasWidth,
35
+ canvasHeight: tempCanvasHeight,
36
+ matrix: totalMatrix,
37
+ sourceCanvas: tempCanvas,
38
+ offsetLeft,
39
+ offsetTop,
40
+ });
41
+ context.drawImage(transformed, 0, 0);
42
+ }
43
+ else {
44
+ await drawElement({
45
+ dimensions,
46
+ computedStyle,
47
+ context,
48
+ draw,
49
+ opacity,
50
+ totalMatrix,
51
+ });
46
52
  }
47
- await draw(dimensions, computedStyle);
48
- drawBorder({
49
- ctx: context,
50
- x: dimensions.left,
51
- y: dimensions.top,
52
- width: dimensions.width,
53
- height: dimensions.height,
54
- borderRadius,
55
- computedStyle,
56
- });
57
- finishOpacity();
58
- finishBorderRadius();
59
- finishTransform();
60
53
  reset();
61
54
  };
@@ -0,0 +1,8 @@
1
+ export declare const drawElement: ({ dimensions, computedStyle, context, draw, opacity, totalMatrix, }: {
2
+ dimensions: DOMRect;
3
+ computedStyle: CSSStyleDeclaration;
4
+ context: OffscreenCanvasRenderingContext2D;
5
+ opacity: number;
6
+ totalMatrix: DOMMatrix;
7
+ draw: (dimensions: DOMRect, computedStyle: CSSStyleDeclaration) => Promise<void> | void;
8
+ }) => Promise<void>;