import { useContext, useState } from 'react'; import CanvasContext from './Context/canvasContext/CanvasContext'; import OpenContext from './Context/openContext/OpenContext'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Label } from '@/components/ui/label'; import { Button } from '@/components/ui/button'; import { Card, CardHeader, CardTitle } from './ui/card'; import { Rnd } from 'react-rnd'; import { X } from 'lucide-react'; import { Separator } from './ui/separator'; const resolutions = [ { value: '720p', label: 'HD (1280x720)', width: 1280, height: 720 }, { value: '1080p', label: 'Full HD (1920x1080)', width: 1920, height: 1080 }, { value: '1440p', label: 'QHD (2560x1440)', width: 2560, height: 1440 }, { value: '4k', label: '4K (3840x2160)', width: 3840, height: 2160 }, { value: 'story', label: 'Story (1080x1920)', width: 1080, height: 1920 }, // Added for Stories { value: '5k', label: '5K (5120x2880)', width: 5120, height: 2880 }, { value: 'ultrawide', label: 'Ultrawide (3440x1440)', width: 3440, height: 1440 }, { value: 'cinematic', label: 'Cinematic (2560x1080)', width: 2560, height: 1080 }, ]; const imageTypes = [ { value: 'png', label: 'PNG' }, { value: 'jpeg', label: 'JPEG' }, { value: 'webp', label: 'WebP' }, ]; const CanvasCapture = () => { const [resolution, setResolution] = useState(resolutions[0].value); const [imageType, setImageType] = useState(imageTypes[0].value); const { canvas } = useContext(CanvasContext); const { captureOpen, setCaptureOpen } = useContext(OpenContext); const captureImage = async () => { if (!canvas) return; const selectedResolution = resolutions.find(res => res.value === resolution); const { width, height } = selectedResolution; // Create a temporary canvas for resizing const tempCanvas = document.createElement('canvas'); const tempCtx = tempCanvas.getContext('2d'); // Set desired resolution tempCanvas.width = width; tempCanvas.height = height; // Render the fabric.js canvas onto the temporary canvas const dataUrl = canvas.toDataURL({ format: imageType, quality: 1, multiplier: width / canvas.width, // Adjust the scale based on width }); const img = new Image(); img.src = dataUrl; img.onload = () => { tempCtx.drawImage(img, 0, 0, width, height); // Generate resized image data URL const resizedDataUrl = tempCanvas.toDataURL(`image/${imageType}`, 1); // Download the resized image const link = document.createElement('a'); link.href = resizedDataUrl; link.download = `PlanPostAi-capture-${resolution}.${imageType}`; document.body.appendChild(link); link.click(); document.body.removeChild(link); }; }; return ( <> {captureOpen && (
Capture Panel
)} ); }; export default CanvasCapture;