"use client"; import { useState, useRef, useEffect, } from "react"; import { useTranslations } from "next-intl"; import { Eye, Download } from "lucide-react"; import { BackgroundProp } from "./BackgroundSelector"; import { TextProp } from "./TextSetting"; import { Box, Flex } from "@radix-ui/themes"; import { init as threeInit, updateBackground, updateTextProps } from "./ThreeTools"; const Sizes = [ "1920x1080", "1024x768", "800x600", ] function gcd(a: number, b: number): number { return b === 0 ? a : gcd(b, a % b); } const AspectRatio = Sizes.map(o => { const [w, h] = o.split("x").map(Number); const a = gcd(w, h); return `${w / a}/${h / a}`; }) export default function PreviewToolbar({ background, text, }: { background: BackgroundProp; text: TextProp; }) { const t = useTranslations("PreviewBar"); const [aspectRadio, setAspectRadio] = useState(0); const container = useRef(null); const updateSize = () => { const box = container.current!; const split = Sizes[aspectRadio].split("x").map(Number); box.width = split[0]; box.height = split[1]; } useEffect(() => { function init() { if (!container.current) { setTimeout(init, 100); } else { updateSize(); const box = container.current; threeInit(box); console.log("three init"); } } init(); }, []); useEffect(updateSize, [aspectRadio]); useEffect(() => { updateBackground(background); console.log("background init"); }, [background]); useEffect(() => { updateTextProps(text); console.log("text init"); }, [text]); const handleDownload = () => { if (!container.current) return; const canvas = container.current; // 创建下载链接 // const link = document.createElement("a"); // link.download = `background-${downloadSize.width}x${downloadSize.height}.png`; // link.href = canvas.toDataURL("image/png"); // link.click(); }; const handleFullScreen = () => { if (!container.current) return; const canvas = container.current; const width = window.screen.width; const height = window.screen.height; canvas.style.width = "100vw"; canvas.style.height = "100vh"; canvas.width = width; canvas.height = height; if (canvas.requestFullscreen) { canvas.requestFullscreen(); } else if ((canvas as any).webkitRequestFullscreen) { (canvas as any).webkitRequestFullscreen(); } else if ((canvas as any).msRequestFullscreen) { (canvas as any).msRequestFullscreen(); } // 退出全屏时隐藏canvas const onFullscreenChange = () => { if (!document.fullscreenElement) { canvas.style.removeProperty("width"); canvas.style.removeProperty("height"); updateSize(); document.removeEventListener("fullscreenchange", onFullscreenChange); } }; document.addEventListener("fullscreenchange", onFullscreenChange); }; useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "F11") { e.preventDefault(); handleFullScreen(); } }; window.addEventListener("keydown", handleKeyDown); return () => { window.removeEventListener("keydown", handleKeyDown); }; }, [handleFullScreen]); return (
); }