@remotion/web-renderer 4.0.392 → 4.0.393

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 (64) hide show
  1. package/dist/can-use-webfs-target.d.ts +1 -0
  2. package/dist/can-use-webfs-target.js +19 -0
  3. package/dist/compose.d.ts +6 -1
  4. package/dist/compose.js +31 -23
  5. package/dist/drawing/calculate-transforms.js +2 -2
  6. package/dist/drawing/draw-dom-element.d.ts +2 -0
  7. package/dist/drawing/draw-dom-element.js +17 -0
  8. package/dist/drawing/draw-element-to-canvas.d.ts +7 -3
  9. package/dist/drawing/draw-element-to-canvas.js +23 -24
  10. package/dist/drawing/draw-element.d.ts +2 -1
  11. package/dist/drawing/draw-element.js +1 -1
  12. package/dist/drawing/drawn-fn.d.ts +5 -0
  13. package/dist/drawing/text/apply-text-transform.d.ts +1 -0
  14. package/dist/drawing/text/apply-text-transform.js +12 -0
  15. package/dist/drawing/text/draw-text.d.ts +2 -1
  16. package/dist/drawing/text/draw-text.js +41 -54
  17. package/dist/drawing/text/handle-text-node.d.ts +7 -1
  18. package/dist/drawing/text/handle-text-node.js +7 -54
  19. package/dist/drawing/transform-in-3d.js +28 -11
  20. package/dist/esm/index.mjs +365 -8420
  21. package/dist/index.d.ts +2 -1
  22. package/dist/mediabunny-mappings.d.ts +1 -0
  23. package/dist/mediabunny-mappings.js +10 -0
  24. package/dist/output-target.d.ts +1 -0
  25. package/dist/output-target.js +1 -0
  26. package/dist/render-media-on-web.d.ts +6 -1
  27. package/dist/render-media-on-web.js +64 -24
  28. package/dist/render-operations-queue.d.ts +3 -0
  29. package/dist/render-operations-queue.js +3 -0
  30. package/dist/render-still-on-web.js +15 -9
  31. package/dist/take-screenshot.js +1 -1
  32. package/dist/walk-tree.d.ts +1 -0
  33. package/dist/walk-tree.js +14 -0
  34. package/dist/web-fs-target.d.ts +7 -0
  35. package/dist/web-fs-target.js +41 -0
  36. package/package.json +6 -6
  37. package/dist/border-radius.d.ts +0 -31
  38. package/dist/border-radius.js +0 -152
  39. package/dist/calculate-transforms.d.ts +0 -11
  40. package/dist/calculate-transforms.js +0 -91
  41. package/dist/composable.d.ts +0 -4
  42. package/dist/compose-canvas.d.ts +0 -1
  43. package/dist/compose-canvas.js +0 -36
  44. package/dist/compose-svg.d.ts +0 -1
  45. package/dist/compose-svg.js +0 -34
  46. package/dist/drawing/compose-canvas.d.ts +0 -1
  47. package/dist/drawing/compose-canvas.js +0 -36
  48. package/dist/drawing/compose-svg.d.ts +0 -1
  49. package/dist/drawing/compose-svg.js +0 -34
  50. package/dist/drawing/compose.d.ts +0 -5
  51. package/dist/drawing/compose.js +0 -6
  52. package/dist/drawing/get-computed-style-cache.d.ts +0 -0
  53. package/dist/drawing/get-computed-style-cache.js +0 -1
  54. package/dist/find-canvas-elements.d.ts +0 -1
  55. package/dist/find-canvas-elements.js +0 -13
  56. package/dist/find-capturable-elements.d.ts +0 -2
  57. package/dist/find-capturable-elements.js +0 -26
  58. package/dist/opacity.d.ts +0 -4
  59. package/dist/opacity.js +0 -7
  60. package/dist/parse-transform-origin.d.ts +0 -4
  61. package/dist/parse-transform-origin.js +0 -7
  62. package/dist/transform.d.ts +0 -4
  63. package/dist/transform.js +0 -6
  64. /package/dist/{composable.js → drawing/drawn-fn.js} +0 -0
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export type { WebRendererCodec, WebRendererContainer, WebRendererQuality, } from './mediabunny-mappings';
2
+ export type { WebRendererOutputTarget } from './output-target';
2
3
  export { renderMediaOnWeb } from './render-media-on-web';
3
- export type { RenderMediaOnWebOptions, RenderMediaOnWebProgress, RenderMediaOnWebProgressCallback, } from './render-media-on-web';
4
+ export type { RenderMediaOnWebOptions, RenderMediaOnWebProgress, RenderMediaOnWebProgressCallback, RenderMediaOnWebResult, } from './render-media-on-web';
4
5
  export { renderStillOnWeb } from './render-still-on-web';
5
6
  export type { RenderStillOnWebImageFormat as RenderStillImageFormat, RenderStillOnWebOptions, } from './render-still-on-web';
6
7
  export type { OnFrameCallback } from './validate-video-frame';
@@ -7,3 +7,4 @@ export declare const codecToMediabunnyCodec: (codec: WebRendererCodec) => VideoC
7
7
  export declare const containerToMediabunnyContainer: (container: WebRendererContainer) => OutputFormat;
8
8
  export declare const getDefaultVideoCodecForContainer: (container: WebRendererContainer) => WebRendererCodec;
9
9
  export declare const getQualityForWebRendererQuality: (quality: WebRendererQuality) => Quality;
10
+ export declare const getMimeType: (container: WebRendererContainer) => string;
@@ -51,3 +51,13 @@ export const getQualityForWebRendererQuality = (quality) => {
51
51
  throw new Error(`Unsupported quality: ${quality}`);
52
52
  }
53
53
  };
54
+ export const getMimeType = (container) => {
55
+ switch (container) {
56
+ case 'mp4':
57
+ return 'video/mp4';
58
+ case 'webm':
59
+ return 'video/webm';
60
+ default:
61
+ throw new Error(`Unsupported container: ${container}`);
62
+ }
63
+ };
@@ -0,0 +1 @@
1
+ export type WebRendererOutputTarget = 'arraybuffer' | 'web-fs';
@@ -0,0 +1 @@
1
+ export {};
@@ -4,6 +4,7 @@ import { type OnArtifact } from './artifact';
4
4
  import { type FrameRange } from './frame-range';
5
5
  import type { WebRendererContainer, WebRendererQuality } from './mediabunny-mappings';
6
6
  import { type WebRendererCodec } from './mediabunny-mappings';
7
+ import type { WebRendererOutputTarget } from './output-target';
7
8
  import type { CompositionCalculateMetadataOrExplicit } from './props-if-has-props';
8
9
  import { type OnFrameCallback } from './validate-video-frame';
9
10
  export type InputPropsIfHasProps<Schema extends AnyZodObject, Props> = AnyZodObject extends Schema ? {} extends Props ? {
@@ -22,6 +23,9 @@ export type RenderMediaOnWebProgress = {
22
23
  renderedFrames: number;
23
24
  encodedFrames: number;
24
25
  };
26
+ export type RenderMediaOnWebResult = {
27
+ getBlob: () => Promise<Blob>;
28
+ };
25
29
  export type RenderMediaOnWebProgressCallback = (progress: RenderMediaOnWebProgress) => void;
26
30
  type OptionalRenderMediaOnWebOptions<Schema extends AnyZodObject> = {
27
31
  delayRenderTimeoutInMilliseconds: number;
@@ -39,7 +43,8 @@ type OptionalRenderMediaOnWebOptions<Schema extends AnyZodObject> = {
39
43
  transparent: boolean;
40
44
  onArtifact: OnArtifact | null;
41
45
  onFrame: OnFrameCallback | null;
46
+ outputTarget: WebRendererOutputTarget | null;
42
47
  };
43
48
  export type RenderMediaOnWebOptions<Schema extends AnyZodObject, Props extends Record<string, unknown>> = MandatoryRenderMediaOnWebOptions<Schema, Props> & Partial<OptionalRenderMediaOnWebOptions<Schema>> & InputPropsIfHasProps<Schema, Props>;
44
- export declare const renderMediaOnWeb: <Schema extends AnyZodObject, Props extends Record<string, unknown>>(options: RenderMediaOnWebOptions<Schema, Props>) => Promise<ArrayBuffer>;
49
+ export declare const renderMediaOnWeb: <Schema extends AnyZodObject, Props extends Record<string, unknown>>(options: RenderMediaOnWebOptions<Schema, Props>) => Promise<RenderMediaOnWebResult>;
45
50
  export {};
@@ -1,24 +1,34 @@
1
- import { AudioSampleSource, BufferTarget, Output, VideoSampleSource, } from 'mediabunny';
1
+ import { AudioSampleSource, BufferTarget, Output, StreamTarget, VideoSampleSource, } from 'mediabunny';
2
2
  import { Internals } from 'remotion';
3
3
  import { addAudioSample, addVideoSampleAndCloseFrame } from './add-sample';
4
4
  import { handleArtifacts } from './artifact';
5
5
  import { onlyInlineAudio } from './audio';
6
+ import { canUseWebFsWriter } from './can-use-webfs-target';
6
7
  import { createScaffold } from './create-scaffold';
7
8
  import { getRealFrameRange } from './frame-range';
8
9
  import { getDefaultAudioEncodingConfig } from './get-audio-encoding-config';
9
- import { codecToMediabunnyCodec, containerToMediabunnyContainer, getDefaultVideoCodecForContainer, getQualityForWebRendererQuality, } from './mediabunny-mappings';
10
+ import { codecToMediabunnyCodec, containerToMediabunnyContainer, getDefaultVideoCodecForContainer, getMimeType, getQualityForWebRendererQuality, } from './mediabunny-mappings';
11
+ import { onlyOneRenderAtATimeQueue } from './render-operations-queue';
10
12
  import { createFrame } from './take-screenshot';
11
13
  import { createThrottledProgressCallback } from './throttle-progress';
12
14
  import { validateVideoFrame } from './validate-video-frame';
13
15
  import { waitForReady } from './wait-for-ready';
16
+ import { cleanupStaleOpfsFiles, createWebFsTarget } from './web-fs-target';
14
17
  // TODO: More containers
15
18
  // TODO: Audio
16
19
  // TODO: Metadata
17
20
  // TODO: Validating inputs
18
- // TODO: Web file system API
19
21
  // TODO: Apply defaultCodec
20
- const internalRenderMediaOnWeb = async ({ composition, inputProps, delayRenderTimeoutInMilliseconds, logLevel, mediaCacheSizeInBytes, schema, codec, container, signal, onProgress, hardwareAcceleration, keyframeIntervalInSeconds, videoBitrate, frameRange, transparent, onArtifact, onFrame, }) => {
22
+ const internalRenderMediaOnWeb = async ({ composition, inputProps, delayRenderTimeoutInMilliseconds, logLevel, mediaCacheSizeInBytes, schema, codec, container, signal, onProgress, hardwareAcceleration, keyframeIntervalInSeconds, videoBitrate, frameRange, transparent, onArtifact, onFrame, outputTarget: userDesiredOutputTarget, }) => {
21
23
  var _a, _b, _c, _d, _e, _f, _g;
24
+ const outputTarget = userDesiredOutputTarget === null
25
+ ? (await canUseWebFsWriter())
26
+ ? 'web-fs'
27
+ : 'arraybuffer'
28
+ : userDesiredOutputTarget;
29
+ if (outputTarget === 'web-fs') {
30
+ await cleanupStaleOpfsFiles();
31
+ }
22
32
  const cleanupFns = [];
23
33
  const format = containerToMediabunnyContainer(container);
24
34
  if (codec &&
@@ -62,9 +72,13 @@ const internalRenderMediaOnWeb = async ({ composition, inputProps, delayRenderTi
62
72
  cleanupFns.push(() => {
63
73
  cleanupScaffold();
64
74
  });
75
+ const webFsTarget = outputTarget === 'web-fs' ? await createWebFsTarget() : null;
76
+ const target = webFsTarget
77
+ ? new StreamTarget(webFsTarget.stream)
78
+ : new BufferTarget();
65
79
  const output = new Output({
66
80
  format,
67
- target: new BufferTarget(),
81
+ target,
68
82
  });
69
83
  try {
70
84
  if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
@@ -190,32 +204,58 @@ const internalRenderMediaOnWeb = async ({ composition, inputProps, delayRenderTi
190
204
  videoSampleSource.close();
191
205
  audioSampleSource.close();
192
206
  await output.finalize();
193
- return output.target.buffer;
207
+ const mimeType = getMimeType(container);
208
+ if (webFsTarget) {
209
+ await webFsTarget.close();
210
+ return {
211
+ getBlob: () => {
212
+ return webFsTarget.getBlob();
213
+ },
214
+ };
215
+ }
216
+ if (!(target instanceof BufferTarget)) {
217
+ throw new Error('Expected target to be a BufferTarget');
218
+ }
219
+ return {
220
+ getBlob: () => {
221
+ if (!target.buffer) {
222
+ throw new Error('The resulting buffer is empty');
223
+ }
224
+ return Promise.resolve(new Blob([target.buffer], { type: mimeType }));
225
+ },
226
+ };
194
227
  }
195
228
  finally {
196
229
  cleanupFns.forEach((fn) => fn());
197
230
  }
198
231
  };
199
232
  export const renderMediaOnWeb = (options) => {
200
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
233
+ var _a, _b;
201
234
  const container = (_a = options.container) !== null && _a !== void 0 ? _a : 'mp4';
202
235
  const codec = (_b = options.codec) !== null && _b !== void 0 ? _b : getDefaultVideoCodecForContainer(container);
203
- return internalRenderMediaOnWeb({
204
- ...options,
205
- delayRenderTimeoutInMilliseconds: (_c = options.delayRenderTimeoutInMilliseconds) !== null && _c !== void 0 ? _c : 30000,
206
- logLevel: (_d = options.logLevel) !== null && _d !== void 0 ? _d : 'info',
207
- schema: (_e = options.schema) !== null && _e !== void 0 ? _e : undefined,
208
- mediaCacheSizeInBytes: (_f = options.mediaCacheSizeInBytes) !== null && _f !== void 0 ? _f : null,
209
- codec,
210
- container,
211
- signal: (_g = options.signal) !== null && _g !== void 0 ? _g : null,
212
- onProgress: (_h = options.onProgress) !== null && _h !== void 0 ? _h : null,
213
- hardwareAcceleration: (_j = options.hardwareAcceleration) !== null && _j !== void 0 ? _j : 'no-preference',
214
- keyframeIntervalInSeconds: (_k = options.keyframeIntervalInSeconds) !== null && _k !== void 0 ? _k : 5,
215
- videoBitrate: (_l = options.videoBitrate) !== null && _l !== void 0 ? _l : 'medium',
216
- frameRange: (_m = options.frameRange) !== null && _m !== void 0 ? _m : null,
217
- transparent: (_o = options.transparent) !== null && _o !== void 0 ? _o : false,
218
- onArtifact: (_p = options.onArtifact) !== null && _p !== void 0 ? _p : null,
219
- onFrame: (_q = options.onFrame) !== null && _q !== void 0 ? _q : null,
236
+ onlyOneRenderAtATimeQueue.ref = onlyOneRenderAtATimeQueue.ref
237
+ .catch(() => Promise.resolve())
238
+ .then(() => {
239
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
240
+ return internalRenderMediaOnWeb({
241
+ ...options,
242
+ delayRenderTimeoutInMilliseconds: (_a = options.delayRenderTimeoutInMilliseconds) !== null && _a !== void 0 ? _a : 30000,
243
+ logLevel: (_b = options.logLevel) !== null && _b !== void 0 ? _b : 'info',
244
+ schema: (_c = options.schema) !== null && _c !== void 0 ? _c : undefined,
245
+ mediaCacheSizeInBytes: (_d = options.mediaCacheSizeInBytes) !== null && _d !== void 0 ? _d : null,
246
+ codec,
247
+ container,
248
+ signal: (_e = options.signal) !== null && _e !== void 0 ? _e : null,
249
+ onProgress: (_f = options.onProgress) !== null && _f !== void 0 ? _f : null,
250
+ hardwareAcceleration: (_g = options.hardwareAcceleration) !== null && _g !== void 0 ? _g : 'no-preference',
251
+ keyframeIntervalInSeconds: (_h = options.keyframeIntervalInSeconds) !== null && _h !== void 0 ? _h : 5,
252
+ videoBitrate: (_j = options.videoBitrate) !== null && _j !== void 0 ? _j : 'medium',
253
+ frameRange: (_k = options.frameRange) !== null && _k !== void 0 ? _k : null,
254
+ transparent: (_l = options.transparent) !== null && _l !== void 0 ? _l : false,
255
+ onArtifact: (_m = options.onArtifact) !== null && _m !== void 0 ? _m : null,
256
+ onFrame: (_o = options.onFrame) !== null && _o !== void 0 ? _o : null,
257
+ outputTarget: (_p = options.outputTarget) !== null && _p !== void 0 ? _p : null,
258
+ });
220
259
  });
260
+ return onlyOneRenderAtATimeQueue.ref;
221
261
  };
@@ -0,0 +1,3 @@
1
+ export declare const onlyOneRenderAtATimeQueue: {
2
+ ref: Promise<unknown>;
3
+ };
@@ -0,0 +1,3 @@
1
+ export const onlyOneRenderAtATimeQueue = {
2
+ ref: Promise.resolve(),
3
+ };
@@ -1,6 +1,7 @@
1
1
  import { Internals, } from 'remotion';
2
2
  import { handleArtifacts } from './artifact';
3
3
  import { createScaffold } from './create-scaffold';
4
+ import { onlyOneRenderAtATimeQueue } from './render-operations-queue';
4
5
  import { takeScreenshot } from './take-screenshot';
5
6
  import { waitForReady } from './wait-for-ready';
6
7
  async function internalRenderStillOnWeb({ frame, delayRenderTimeoutInMilliseconds, logLevel, inputProps, schema, imageFormat, mediaCacheSizeInBytes, composition, signal, onArtifact, }) {
@@ -68,14 +69,19 @@ async function internalRenderStillOnWeb({ frame, delayRenderTimeoutInMillisecond
68
69
  }
69
70
  }
70
71
  export const renderStillOnWeb = (options) => {
71
- var _a, _b, _c, _d, _e, _f;
72
- return internalRenderStillOnWeb({
73
- ...options,
74
- delayRenderTimeoutInMilliseconds: (_a = options.delayRenderTimeoutInMilliseconds) !== null && _a !== void 0 ? _a : 30000,
75
- logLevel: (_b = options.logLevel) !== null && _b !== void 0 ? _b : 'info',
76
- schema: (_c = options.schema) !== null && _c !== void 0 ? _c : undefined,
77
- mediaCacheSizeInBytes: (_d = options.mediaCacheSizeInBytes) !== null && _d !== void 0 ? _d : null,
78
- signal: (_e = options.signal) !== null && _e !== void 0 ? _e : null,
79
- onArtifact: (_f = options.onArtifact) !== null && _f !== void 0 ? _f : null,
72
+ onlyOneRenderAtATimeQueue.ref = onlyOneRenderAtATimeQueue.ref
73
+ .catch(() => Promise.resolve())
74
+ .then(() => {
75
+ var _a, _b, _c, _d, _e, _f;
76
+ return internalRenderStillOnWeb({
77
+ ...options,
78
+ delayRenderTimeoutInMilliseconds: (_a = options.delayRenderTimeoutInMilliseconds) !== null && _a !== void 0 ? _a : 30000,
79
+ logLevel: (_b = options.logLevel) !== null && _b !== void 0 ? _b : 'info',
80
+ schema: (_c = options.schema) !== null && _c !== void 0 ? _c : undefined,
81
+ mediaCacheSizeInBytes: (_d = options.mediaCacheSizeInBytes) !== null && _d !== void 0 ? _d : null,
82
+ signal: (_e = options.signal) !== null && _e !== void 0 ? _e : null,
83
+ onArtifact: (_f = options.onArtifact) !== null && _f !== void 0 ? _f : null,
84
+ });
80
85
  });
86
+ return onlyOneRenderAtATimeQueue.ref;
81
87
  };
@@ -5,7 +5,7 @@ export const createFrame = async ({ div, width, height, }) => {
5
5
  if (!context) {
6
6
  throw new Error('Could not get context');
7
7
  }
8
- await compose(div, context);
8
+ await compose({ element: div, context, offsetLeft: 0, offsetTop: 0 });
9
9
  return canvas;
10
10
  };
11
11
  export const takeScreenshot = async ({ div, width, height, imageFormat, }) => {
@@ -0,0 +1 @@
1
+ export declare function skipToNextNonDescendant(treeWalker: TreeWalker): boolean;
@@ -0,0 +1,14 @@
1
+ export function skipToNextNonDescendant(treeWalker) {
2
+ // Try to go to next sibling
3
+ if (treeWalker.nextSibling()) {
4
+ return true;
5
+ }
6
+ // No sibling, go up to parent and try to find ancestor's sibling
7
+ while (treeWalker.parentNode()) {
8
+ if (treeWalker.nextSibling()) {
9
+ return true;
10
+ }
11
+ }
12
+ // No more nodes
13
+ return false;
14
+ }
@@ -0,0 +1,7 @@
1
+ import type { StreamTargetChunk } from 'mediabunny';
2
+ export declare const cleanupStaleOpfsFiles: () => Promise<void>;
3
+ export declare const createWebFsTarget: () => Promise<{
4
+ stream: WritableStream<StreamTargetChunk>;
5
+ getBlob: () => Promise<File>;
6
+ close: () => Promise<void>;
7
+ }>;
@@ -0,0 +1,41 @@
1
+ let sessionId = null;
2
+ const getPrefix = () => {
3
+ if (!sessionId) {
4
+ sessionId = crypto.randomUUID();
5
+ }
6
+ return `__remotion_render:${sessionId}:`;
7
+ };
8
+ export const cleanupStaleOpfsFiles = async () => {
9
+ try {
10
+ const root = await navigator.storage.getDirectory();
11
+ for await (const [name] of root.entries()) {
12
+ if (name.startsWith('__remotion_render:') &&
13
+ !name.startsWith(getPrefix())) {
14
+ await root.removeEntry(name);
15
+ }
16
+ }
17
+ }
18
+ catch (_a) {
19
+ // Ignore, could already be closed
20
+ }
21
+ };
22
+ export const createWebFsTarget = async () => {
23
+ const directoryHandle = await navigator.storage.getDirectory();
24
+ const filename = `${getPrefix()}${crypto.randomUUID()}`;
25
+ const fileHandle = await directoryHandle.getFileHandle(filename, {
26
+ create: true,
27
+ });
28
+ const writable = await fileHandle.createWritable();
29
+ const stream = new WritableStream({
30
+ async write(chunk) {
31
+ await writable.seek(chunk.position);
32
+ await writable.write(chunk);
33
+ },
34
+ });
35
+ const getBlob = async () => {
36
+ const handle = await directoryHandle.getFileHandle(filename);
37
+ return handle.getFile();
38
+ };
39
+ const close = () => writable.close();
40
+ return { stream, getBlob, close };
41
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "url": "https://github.com/remotion-dev/remotion/tree/main/packages/web-renderer"
4
4
  },
5
5
  "name": "@remotion/web-renderer",
6
- "version": "4.0.392",
6
+ "version": "4.0.393",
7
7
  "main": "dist/index.js",
8
8
  "sideEffects": false,
9
9
  "scripts": {
@@ -16,13 +16,13 @@
16
16
  "author": "Remotion <jonny@remotion.dev>",
17
17
  "license": "UNLICENSED",
18
18
  "dependencies": {
19
- "remotion": "4.0.392",
20
- "mediabunny": "1.25.8"
19
+ "remotion": "4.0.393",
20
+ "mediabunny": "1.27.0"
21
21
  },
22
22
  "devDependencies": {
23
- "@remotion/eslint-config-internal": "4.0.392",
24
- "@remotion/player": "4.0.392",
25
- "@remotion/media": "4.0.392",
23
+ "@remotion/eslint-config-internal": "4.0.393",
24
+ "@remotion/player": "4.0.393",
25
+ "@remotion/media": "4.0.393",
26
26
  "@vitejs/plugin-react": "4.1.0",
27
27
  "@vitest/browser-playwright": "4.0.9",
28
28
  "playwright": "1.55.1",
@@ -1,31 +0,0 @@
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;
@@ -1,152 +0,0 @@
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,11 +0,0 @@
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
- };
@@ -1,91 +0,0 @@
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
- };
@@ -1,4 +0,0 @@
1
- export type Composable = {
2
- type: 'element';
3
- element: HTMLElement | SVGElement;
4
- };
@@ -1 +0,0 @@
1
- export declare const composeCanvas: (canvas: HTMLCanvasElement | HTMLImageElement | SVGSVGElement, context: OffscreenCanvasRenderingContext2D) => Promise<void>;