The best way to control Claude Code, Codex and more from any computer or phone

main
ts 41 lines 1.56 KB
Raw
1 // SVG pictures, which only an image element decodes. Workers have none, so the window rasterizes them for the worker.
2
3 /** The longest side a decoded picture keeps: enough for a full-width page or slide on a high-density screen. */
4 export const maxImageSide = 2560;
5
6 /** An SVG picture the worker cannot decode, which the window rasterizes at `size`, or at its own size when absent. */
7 export type VectorRequest = {
8 readonly asset: string;
9 readonly blob: Blob;
10 readonly size: { readonly width: number; readonly height: number } | undefined;
11 };
12
13 /** A rasterized SVG picture, or none when the window could not decode it either. */
14 export type VectorBitmap = { readonly asset: string; readonly bitmap: ImageBitmap | undefined };
15
16 /** Rasterizes a worker's SVG picture in the window. */
17 export async function rasterize(request: VectorRequest): Promise<VectorBitmap> {
18 const url = URL.createObjectURL(request.blob);
19 const image = new Image();
20 image.src = url;
21 try {
22 await image.decode();
23 const shrink = Math.min(1, maxImageSide / Math.max(image.naturalWidth, image.naturalHeight));
24 const size = request.size ?? {
25 width: Math.max(1, Math.round(image.naturalWidth * shrink)),
26 height: Math.max(1, Math.round(image.naturalHeight * shrink)),
27 };
28 return {
29 asset: request.asset,
30 bitmap: await createImageBitmap(image, {
31 resizeWidth: size.width,
32 resizeHeight: size.height,
33 resizeQuality: "high",
34 }),
35 };
36 } catch {
37 return { asset: request.asset, bitmap: undefined };
38 } finally {
39 URL.revokeObjectURL(url);
40 }
41 }