import { useContext, useEffect } from 'react' import { Label } from './ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select'; import { Input } from './ui/input'; import ColorContext from './Context/colorContext/ColorContext'; import CanvasContext from './Context/canvasContext/CanvasContext'; import { fabric } from 'fabric'; const ColorComponent = () => { const { canvas } = useContext(CanvasContext); const { backgroundType, setBackgroundType, solidColor, gradientColors, setSolidColor, setGradientColors, gradientDirection, setGradientDirection, setDirectionCoords, directionCoords, } = useContext(ColorContext); const applySolidColor = (color) => { setSolidColor(color); }; const applyGradient = () => { const width = canvas?.width || 0; const height = canvas?.height || 0; // Define coordinates for linear gradient const linearCoords = { "top-to-bottom": { x1: 0, y1: 0, x2: 0, y2: height }, "bottom-to-top": { x1: 0, y1: height, x2: 0, y2: 0 }, "left-to-right": { x1: 0, y1: 0, x2: width, y2: 0 }, "right-to-left": { x1: width, y1: 0, x2: 0, y2: 0 }, }; const directionCoords = linearCoords[gradientDirection]; if (directionCoords) { setDirectionCoords(directionCoords); } else { console.error(`Invalid gradient direction: ${gradientDirection}`); } }; useEffect(() => { if (gradientDirection && backgroundType === "gradient") { applyGradient(); } }, [gradientDirection, backgroundType]); useEffect(() => { const object = canvas?.getActiveObject(); if (!object) { if (canvas && solidColor && backgroundType === "color") { canvas.backgroundColor = solidColor; canvas.renderAll(); } if (canvas && directionCoords && gradientColors && backgroundType === "gradient") { const gradient = new fabric.Gradient({ type: "linear", gradientUnits: "pixels", coords: directionCoords, colorStops: [ { offset: 0, color: gradientColors.color1 }, { offset: 1, color: gradientColors.color2 }, ], }); canvas.backgroundColor = gradient; canvas.renderAll(); } if (canvas && gradientColors && backgroundType === "radial") { const gradient = new fabric.Gradient({ type: "radial", gradientUnits: "pixels", coords: { x1: canvas.width / 2, y1: canvas.height / 2, r1: 0, x2: canvas.width / 2, y2: canvas.height / 2, r2: Math.min(canvas.width, canvas.height) / 2, }, colorStops: [ { offset: 0, color: gradientColors.color1 }, { offset: 1, color: gradientColors.color2 }, ], }); canvas.backgroundColor = gradient; canvas.renderAll(); } } }, [gradientColors, directionCoords, solidColor, canvas, backgroundType]); return (