@remotion/web-renderer 4.0.384 → 4.0.385
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/audio.d.ts +5 -1
- package/dist/audio.js +12 -19
- package/dist/border-radius.d.ts +31 -0
- package/dist/border-radius.js +152 -0
- package/dist/calculate-transforms.d.ts +2 -0
- package/dist/calculate-transforms.js +17 -0
- package/dist/composable.d.ts +2 -8
- package/dist/compose-canvas.js +28 -4
- package/dist/compose.d.ts +1 -6
- package/dist/compose.js +16 -11
- package/dist/drawing/border-radius.d.ts +31 -0
- package/dist/drawing/border-radius.js +152 -0
- package/dist/drawing/calculate-transforms.d.ts +10 -0
- package/dist/drawing/calculate-transforms.js +81 -0
- package/dist/drawing/compose-canvas.d.ts +1 -0
- package/dist/drawing/compose-canvas.js +36 -0
- package/dist/drawing/compose-svg.d.ts +1 -0
- package/dist/drawing/compose-svg.js +34 -0
- package/dist/drawing/compose.d.ts +5 -0
- package/dist/drawing/compose.js +6 -0
- package/dist/drawing/draw-border.d.ts +10 -0
- package/dist/drawing/draw-border.js +101 -0
- package/dist/drawing/draw-element-to-canvas.d.ts +4 -0
- package/dist/drawing/draw-element-to-canvas.js +72 -0
- package/dist/drawing/get-computed-style-cache.d.ts +0 -0
- package/dist/drawing/get-computed-style-cache.js +1 -0
- package/dist/drawing/opacity.d.ts +4 -0
- package/dist/drawing/opacity.js +7 -0
- package/dist/drawing/parse-transform-origin.d.ts +4 -0
- package/dist/drawing/parse-transform-origin.js +7 -0
- package/dist/drawing/transform.d.ts +4 -0
- package/dist/drawing/transform.js +6 -0
- package/dist/drawing/turn-svg-into-drawable.d.ts +1 -0
- package/dist/drawing/turn-svg-into-drawable.js +34 -0
- package/dist/esm/index.mjs +363 -79
- package/dist/find-capturable-elements.d.ts +1 -1
- package/dist/find-capturable-elements.js +20 -22
- package/dist/opacity.d.ts +4 -0
- package/dist/opacity.js +7 -0
- package/dist/render-media-on-web.js +14 -5
- package/dist/take-screenshot.js +7 -8
- package/dist/transform.d.ts +4 -0
- package/dist/transform.js +6 -0
- package/package.json +5 -5
package/dist/audio.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import type { TRenderAsset } from 'remotion';
|
|
2
|
-
export declare const onlyInlineAudio: (assets
|
|
2
|
+
export declare const onlyInlineAudio: ({ assets, fps, frame, }: {
|
|
3
|
+
assets: TRenderAsset[];
|
|
4
|
+
fps: number;
|
|
5
|
+
frame: number;
|
|
6
|
+
}) => AudioData | null;
|
package/dist/audio.js
CHANGED
|
@@ -6,40 +6,33 @@ function mixAudio(waves, length) {
|
|
|
6
6
|
}
|
|
7
7
|
const mixed = new Int16Array(length);
|
|
8
8
|
for (let i = 0; i < length; i++) {
|
|
9
|
-
const sum = waves.reduce((acc, wave) =>
|
|
9
|
+
const sum = waves.reduce((acc, wave) => {
|
|
10
|
+
var _a;
|
|
11
|
+
return acc + ((_a = wave[i]) !== null && _a !== void 0 ? _a : 0);
|
|
12
|
+
}, 0);
|
|
10
13
|
// Clamp to Int16 range
|
|
11
14
|
mixed[i] = Math.max(-32768, Math.min(32767, sum));
|
|
12
15
|
}
|
|
13
16
|
return mixed;
|
|
14
17
|
}
|
|
15
|
-
export const onlyInlineAudio = (assets) => {
|
|
18
|
+
export const onlyInlineAudio = ({ assets, fps, frame, }) => {
|
|
16
19
|
const inlineAudio = assets.filter((asset) => asset.type === 'inline-audio');
|
|
17
|
-
|
|
20
|
+
if (inlineAudio.length === 0) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const expectedLength = Math.round((TARGET_NUMBER_OF_CHANNELS * TARGET_SAMPLE_RATE) / fps);
|
|
18
24
|
for (const asset of inlineAudio) {
|
|
19
25
|
if (asset.toneFrequency !== 1) {
|
|
20
26
|
throw new Error('Setting the toneFrequency is not supported yet in web rendering.');
|
|
21
27
|
}
|
|
22
|
-
if (length === null) {
|
|
23
|
-
length = asset.audio.length;
|
|
24
|
-
// 1 frame offset may happen due to rounding, it is safe to truncate the last sample
|
|
25
|
-
}
|
|
26
|
-
else if (Math.abs(length - asset.audio.length) > TARGET_NUMBER_OF_CHANNELS) {
|
|
27
|
-
throw new Error('All inline audio must have the same length');
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
length = Math.min(length, asset.audio.length);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if (length === null) {
|
|
34
|
-
return null;
|
|
35
28
|
}
|
|
36
|
-
const mixedAudio = mixAudio(inlineAudio.map((asset) => asset.audio),
|
|
29
|
+
const mixedAudio = mixAudio(inlineAudio.map((asset) => asset.audio), expectedLength);
|
|
37
30
|
return new AudioData({
|
|
38
31
|
data: mixedAudio,
|
|
39
32
|
format: 's16',
|
|
40
33
|
numberOfChannels: TARGET_NUMBER_OF_CHANNELS,
|
|
41
|
-
numberOfFrames:
|
|
34
|
+
numberOfFrames: expectedLength / TARGET_NUMBER_OF_CHANNELS,
|
|
42
35
|
sampleRate: TARGET_SAMPLE_RATE,
|
|
43
|
-
timestamp:
|
|
36
|
+
timestamp: (frame / fps) * 1000000,
|
|
44
37
|
});
|
|
45
38
|
};
|
|
@@ -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
|
+
}
|
|
@@ -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
|
};
|
package/dist/composable.d.ts
CHANGED
package/dist/compose-canvas.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
32
|
+
finishOpacity();
|
|
33
|
+
finishBorderRadius();
|
|
34
|
+
finishTransform();
|
|
11
35
|
reset();
|
|
12
36
|
};
|
package/dist/compose.d.ts
CHANGED
|
@@ -1,6 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const compose: ({ composables, width, height, }: {
|
|
3
|
-
composables: Composable[];
|
|
4
|
-
width: number;
|
|
5
|
-
height: number;
|
|
6
|
-
}) => Promise<OffscreenCanvas>;
|
|
1
|
+
export declare const compose: (element: HTMLDivElement, context: OffscreenCanvasRenderingContext2D) => Promise<void>;
|
package/dist/compose.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export const compose = async (
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { drawElementToCanvas } from './drawing/draw-element-to-canvas';
|
|
2
|
+
export const compose = async (element, context) => {
|
|
3
|
+
const treeWalker = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, (node) => {
|
|
4
|
+
if (node instanceof Element) {
|
|
5
|
+
const computedStyle = getComputedStyle(node);
|
|
6
|
+
return computedStyle.display === 'none'
|
|
7
|
+
? NodeFilter.FILTER_REJECT
|
|
8
|
+
: NodeFilter.FILTER_ACCEPT;
|
|
9
|
+
}
|
|
10
|
+
return NodeFilter.FILTER_ACCEPT;
|
|
11
|
+
});
|
|
12
|
+
while (treeWalker.nextNode()) {
|
|
13
|
+
const node = treeWalker.currentNode;
|
|
14
|
+
if (node instanceof HTMLElement || node instanceof SVGElement) {
|
|
15
|
+
await drawElementToCanvas({ element: node, context });
|
|
16
|
+
}
|
|
7
17
|
}
|
|
8
|
-
// TODO: Consider z-index
|
|
9
|
-
for (const composable of composables) {
|
|
10
|
-
await composeCanvas(composable.element, context);
|
|
11
|
-
}
|
|
12
|
-
return canvas;
|
|
13
18
|
};
|
|
@@ -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,81 @@
|
|
|
1
|
+
import { parseTransformOrigin } from './parse-transform-origin';
|
|
2
|
+
const getInternalTransformOrigin = (transform) => {
|
|
3
|
+
var _a;
|
|
4
|
+
const centerX = transform.boundingClientRect.width / 2;
|
|
5
|
+
const centerY = transform.boundingClientRect.height / 2;
|
|
6
|
+
const origin = (_a = parseTransformOrigin(transform.transformOrigin)) !== null && _a !== void 0 ? _a : {
|
|
7
|
+
x: centerX,
|
|
8
|
+
y: centerY,
|
|
9
|
+
};
|
|
10
|
+
return origin;
|
|
11
|
+
};
|
|
12
|
+
const getGlobalTransformOrigin = (transform) => {
|
|
13
|
+
const { x: originX, y: originY } = getInternalTransformOrigin(transform);
|
|
14
|
+
return {
|
|
15
|
+
x: originX + transform.boundingClientRect.left,
|
|
16
|
+
y: originY + transform.boundingClientRect.top,
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export const calculateTransforms = (element) => {
|
|
20
|
+
// Compute the cumulative transform by traversing parent nodes
|
|
21
|
+
let parent = element;
|
|
22
|
+
const transforms = [];
|
|
23
|
+
const toReset = [];
|
|
24
|
+
let opacity = 1;
|
|
25
|
+
while (parent) {
|
|
26
|
+
const computedStyle = getComputedStyle(parent);
|
|
27
|
+
// Multiply opacity values from element and all parents
|
|
28
|
+
const parentOpacity = computedStyle.opacity;
|
|
29
|
+
if (parentOpacity && parentOpacity !== '') {
|
|
30
|
+
opacity *= parseFloat(parentOpacity);
|
|
31
|
+
}
|
|
32
|
+
if ((computedStyle.transform && computedStyle.transform !== 'none') ||
|
|
33
|
+
parent === element) {
|
|
34
|
+
const toParse = computedStyle.transform === 'none' || computedStyle.transform === ''
|
|
35
|
+
? undefined
|
|
36
|
+
: computedStyle.transform;
|
|
37
|
+
const matrix = new DOMMatrix(toParse);
|
|
38
|
+
const { transform } = parent.style;
|
|
39
|
+
parent.style.transform = 'none';
|
|
40
|
+
transforms.push({
|
|
41
|
+
matrix,
|
|
42
|
+
rect: parent,
|
|
43
|
+
transformOrigin: computedStyle.transformOrigin,
|
|
44
|
+
boundingClientRect: null,
|
|
45
|
+
});
|
|
46
|
+
const parentRef = parent;
|
|
47
|
+
toReset.push(() => {
|
|
48
|
+
parentRef.style.transform = transform;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
parent = parent.parentElement;
|
|
52
|
+
}
|
|
53
|
+
for (const transform of transforms) {
|
|
54
|
+
transform.boundingClientRect = transform.rect.getBoundingClientRect();
|
|
55
|
+
}
|
|
56
|
+
const dimensions = transforms[0].boundingClientRect;
|
|
57
|
+
const nativeTransformOrigin = getInternalTransformOrigin(transforms[0]);
|
|
58
|
+
const totalMatrix = new DOMMatrix();
|
|
59
|
+
for (const transform of transforms.slice().reverse()) {
|
|
60
|
+
if (!transform.boundingClientRect) {
|
|
61
|
+
throw new Error('Bounding client rect not found');
|
|
62
|
+
}
|
|
63
|
+
const globalTransformOrigin = getGlobalTransformOrigin(transform);
|
|
64
|
+
const transformMatrix = new DOMMatrix()
|
|
65
|
+
.translate(globalTransformOrigin.x, globalTransformOrigin.y)
|
|
66
|
+
.multiply(transform.matrix)
|
|
67
|
+
.translate(-globalTransformOrigin.x, -globalTransformOrigin.y);
|
|
68
|
+
totalMatrix.multiplySelf(transformMatrix);
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
dimensions,
|
|
72
|
+
totalMatrix,
|
|
73
|
+
reset: () => {
|
|
74
|
+
for (const reset of toReset) {
|
|
75
|
+
reset();
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
nativeTransformOrigin,
|
|
79
|
+
opacity,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const drawElementToCanvas: (canvas: HTMLCanvasElement | HTMLImageElement | SVGSVGElement, context: OffscreenCanvasRenderingContext2D) => Promise<void>;
|