import { CameraIcon, CopyIcon, DownloadIcon } from "@radix-ui/react-icons"; import { Button, Flex, IconButton, Popover, type ButtonProps, } from "@radix-ui/themes"; import { Jimp } from "jimp"; import { useCallback, type RefObject } from "react"; import { useLocale } from "../hooks/useLocale"; import { screenshotReadyEvent } from "./SceneProps"; interface Props extends ButtonProps { canvas: RefObject; name: string; } export function ScreenshotButton({ canvas, name, children, ...props }: Props) { const { strings } = useLocale(); const screenshot = useCallback(async () => { if (!canvas.current) { throw new Error("Canvas not found"); } screenshotReadyEvent.dispatch(true); const blob = await new Promise((resolve, reject) => { requestAnimationFrame(() => { canvas.current.toBlob((blob) => { screenshotReadyEvent.dispatch(false); if (!blob) { reject(new Error("Failed to capture screenshot")); } else { resolve(blob); } }); }); }); const buffer = await blob.arrayBuffer(); const image = await Jimp.fromBuffer(buffer); image.autocrop(); return image; }, [canvas]); return ( {children ? ( ) : ( )} ); }