@remotion/web-renderer 4.0.383 → 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/add-sample.d.ts +3 -0
- package/dist/add-sample.js +20 -0
- package/dist/artifact.d.ts +4 -5
- package/dist/artifact.js +12 -15
- package/dist/audio.d.ts +6 -0
- package/dist/audio.js +38 -0
- 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 +1630 -125
- package/dist/find-capturable-elements.d.ts +1 -1
- package/dist/find-capturable-elements.js +20 -22
- package/dist/get-audio-encoding-config.d.ts +2 -0
- package/dist/get-audio-encoding-config.js +18 -0
- package/dist/opacity.d.ts +4 -0
- package/dist/opacity.js +7 -0
- package/dist/render-media-on-web.js +42 -11
- package/dist/render-still-on-web.js +5 -5
- package/dist/take-screenshot.js +7 -8
- package/dist/transform.d.ts +4 -0
- package/dist/transform.js +6 -0
- package/package.json +6 -6
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { AudioSampleSource, VideoSampleSource } from 'mediabunny';
|
|
2
|
+
export declare const addVideoSampleAndCloseFrame: (frameToEncode: VideoFrame, videoSampleSource: VideoSampleSource) => Promise<void>;
|
|
3
|
+
export declare const addAudioSample: (audio: AudioData, audioSampleSource: AudioSampleSource) => Promise<void>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AudioSample, VideoSample } from 'mediabunny';
|
|
2
|
+
export const addVideoSampleAndCloseFrame = async (frameToEncode, videoSampleSource) => {
|
|
3
|
+
const sample = new VideoSample(frameToEncode);
|
|
4
|
+
try {
|
|
5
|
+
await videoSampleSource.add(sample);
|
|
6
|
+
}
|
|
7
|
+
finally {
|
|
8
|
+
sample.close();
|
|
9
|
+
frameToEncode.close();
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
export const addAudioSample = async (audio, audioSampleSource) => {
|
|
13
|
+
const sample = new AudioSample(audio);
|
|
14
|
+
try {
|
|
15
|
+
await audioSampleSource.add(sample);
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
sample.close();
|
|
19
|
+
}
|
|
20
|
+
};
|
package/dist/artifact.d.ts
CHANGED
|
@@ -13,12 +13,11 @@ export type OnArtifact = (asset: EmittedArtifact) => void;
|
|
|
13
13
|
export type ArtifactsRef = React.RefObject<{
|
|
14
14
|
collectAssets: () => TRenderAsset[];
|
|
15
15
|
} | null>;
|
|
16
|
-
export declare const handleArtifacts: (
|
|
17
|
-
|
|
18
|
-
onArtifact: OnArtifact | null;
|
|
19
|
-
}) => {
|
|
20
|
-
handle: ({ imageData, frame, }: {
|
|
16
|
+
export declare const handleArtifacts: () => {
|
|
17
|
+
handle: ({ imageData, frame, assets: artifactAssets, onArtifact, }: {
|
|
21
18
|
imageData: Blob | OffscreenCanvas | null;
|
|
22
19
|
frame: number;
|
|
20
|
+
assets: TRenderAsset[];
|
|
21
|
+
onArtifact: OnArtifact;
|
|
23
22
|
}) => Promise<void>;
|
|
24
23
|
};
|
package/dist/artifact.js
CHANGED
|
@@ -36,23 +36,20 @@ export const onlyArtifact = async ({ assets, frameBuffer, }) => {
|
|
|
36
36
|
}
|
|
37
37
|
return result.filter(NoReactInternals.truthy);
|
|
38
38
|
};
|
|
39
|
-
export const handleArtifacts = (
|
|
39
|
+
export const handleArtifacts = () => {
|
|
40
40
|
const previousArtifacts = [];
|
|
41
|
-
const handle = async ({ imageData, frame, }) => {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (previousArtifact) {
|
|
51
|
-
throw new Error(`An artifact with output "${artifact.filename}" was already registered at frame ${previousArtifact.frame}, but now registered again at frame ${frame}. Artifacts must have unique names. https://remotion.dev/docs/artifacts`);
|
|
52
|
-
}
|
|
53
|
-
onArtifact(artifact);
|
|
54
|
-
previousArtifacts.push({ frame, filename: artifact.filename });
|
|
41
|
+
const handle = async ({ imageData, frame, assets: artifactAssets, onArtifact, }) => {
|
|
42
|
+
const artifacts = await onlyArtifact({
|
|
43
|
+
assets: artifactAssets,
|
|
44
|
+
frameBuffer: imageData,
|
|
45
|
+
});
|
|
46
|
+
for (const artifact of artifacts) {
|
|
47
|
+
const previousArtifact = previousArtifacts.find((a) => a.filename === artifact.filename);
|
|
48
|
+
if (previousArtifact) {
|
|
49
|
+
throw new Error(`An artifact with output "${artifact.filename}" was already registered at frame ${previousArtifact.frame}, but now registered again at frame ${frame}. Artifacts must have unique names. https://remotion.dev/docs/artifacts`);
|
|
55
50
|
}
|
|
51
|
+
onArtifact(artifact);
|
|
52
|
+
previousArtifacts.push({ frame, filename: artifact.filename });
|
|
56
53
|
}
|
|
57
54
|
};
|
|
58
55
|
return { handle };
|
package/dist/audio.d.ts
ADDED
package/dist/audio.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const TARGET_NUMBER_OF_CHANNELS = 2;
|
|
2
|
+
const TARGET_SAMPLE_RATE = 48000;
|
|
3
|
+
function mixAudio(waves, length) {
|
|
4
|
+
if (waves.length === 1) {
|
|
5
|
+
return waves[0];
|
|
6
|
+
}
|
|
7
|
+
const mixed = new Int16Array(length);
|
|
8
|
+
for (let i = 0; i < length; i++) {
|
|
9
|
+
const sum = waves.reduce((acc, wave) => {
|
|
10
|
+
var _a;
|
|
11
|
+
return acc + ((_a = wave[i]) !== null && _a !== void 0 ? _a : 0);
|
|
12
|
+
}, 0);
|
|
13
|
+
// Clamp to Int16 range
|
|
14
|
+
mixed[i] = Math.max(-32768, Math.min(32767, sum));
|
|
15
|
+
}
|
|
16
|
+
return mixed;
|
|
17
|
+
}
|
|
18
|
+
export const onlyInlineAudio = ({ assets, fps, frame, }) => {
|
|
19
|
+
const inlineAudio = assets.filter((asset) => asset.type === 'inline-audio');
|
|
20
|
+
if (inlineAudio.length === 0) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const expectedLength = Math.round((TARGET_NUMBER_OF_CHANNELS * TARGET_SAMPLE_RATE) / fps);
|
|
24
|
+
for (const asset of inlineAudio) {
|
|
25
|
+
if (asset.toneFrequency !== 1) {
|
|
26
|
+
throw new Error('Setting the toneFrequency is not supported yet in web rendering.');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const mixedAudio = mixAudio(inlineAudio.map((asset) => asset.audio), expectedLength);
|
|
30
|
+
return new AudioData({
|
|
31
|
+
data: mixedAudio,
|
|
32
|
+
format: 's16',
|
|
33
|
+
numberOfChannels: TARGET_NUMBER_OF_CHANNELS,
|
|
34
|
+
numberOfFrames: expectedLength / TARGET_NUMBER_OF_CHANNELS,
|
|
35
|
+
sampleRate: TARGET_SAMPLE_RATE,
|
|
36
|
+
timestamp: (frame / fps) * 1000000,
|
|
37
|
+
});
|
|
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
|
+
}
|