diff options
| author | Maze Winther <mazewinther@gmail.com> | 2026-04-25 02:46:31 +0200 |
|---|---|---|
| committer | Maze Winther <mazewinther@gmail.com> | 2026-04-25 02:46:31 +0200 |
| commit | 0dffefdd59d8c70fece63436e8556f8ec1e43a49 (patch) | |
| tree | 2fad7166bbc7e5e7645bbd0456c8f4eef44c19dc /apps/web/src/media/thumbnail.ts | |
| parent | cceb835a84c125990ad8e49784e01a7c52636bb3 (diff) | |
| download | opencut-0dffefdd59d8c70fece63436e8556f8ec1e43a49.tar.gz | |
fix: surface unsupported video codec on import, fix Input leaks
Diffstat (limited to 'apps/web/src/media/thumbnail.ts')
| -rw-r--r-- | apps/web/src/media/thumbnail.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/apps/web/src/media/thumbnail.ts b/apps/web/src/media/thumbnail.ts new file mode 100644 index 00000000..e71077d9 --- /dev/null +++ b/apps/web/src/media/thumbnail.ts @@ -0,0 +1,56 @@ +const THUMBNAIL_MAX_WIDTH = 1280; +const THUMBNAIL_MAX_HEIGHT = 720; + +export function thumbnailSize({ + width, + height, +}: { + width: number; + height: number; +}): { width: number; height: number } { + const aspectRatio = width / height; + let targetWidth = width; + let targetHeight = height; + + if (targetWidth > THUMBNAIL_MAX_WIDTH) { + targetWidth = THUMBNAIL_MAX_WIDTH; + targetHeight = Math.round(targetWidth / aspectRatio); + } + if (targetHeight > THUMBNAIL_MAX_HEIGHT) { + targetHeight = THUMBNAIL_MAX_HEIGHT; + targetWidth = Math.round(targetHeight * aspectRatio); + } + + return { width: targetWidth, height: targetHeight }; +} + +export function renderThumbnailDataUrl({ + width, + height, + draw, +}: { + width: number; + height: number; + draw: ({ + context, + width, + height, + }: { + context: CanvasRenderingContext2D; + width: number; + height: number; + }) => void; +}): string { + const size = thumbnailSize({ width, height }); + const canvas = document.createElement("canvas"); + canvas.width = size.width; + canvas.height = size.height; + const context = canvas.getContext("2d"); + + if (!context) { + throw new Error("Could not get canvas context"); + } + + draw({ context, width: size.width, height: size.height }); + return canvas.toDataURL("image/jpeg", 0.8); +} |
