diff --git a/src/components/AspectCanvas.jsx b/src/components/AspectCanvas.jsx index f3f2553..3bc38af 100644 --- a/src/components/AspectCanvas.jsx +++ b/src/components/AspectCanvas.jsx @@ -48,12 +48,38 @@ export function AspectCanvas() { useEffect(() => { const updateCanvasSize = () => { if (canvasRef.current && canvas) { - canvas.setWidth(canvasRef?.current?.offsetWidth); - canvas.setHeight(canvasRef?.current?.offsetHeight); - canvas.backgroundImage = null; - canvas.overlayImage = null; - canvas.renderAll(); + // Update canvas dimensions + const newWidth = canvasRef?.current?.offsetWidth; + const newHeight = canvasRef?.current?.offsetHeight; + + canvas.setWidth(newWidth); + canvas.setHeight(newHeight); + + // Adjust the background image to fit the updated canvas size + const bgImage = canvas.backgroundImage; + if (bgImage) { + // Calculate scaling factors for width and height + const scaleX = newWidth / bgImage.width; + const scaleY = newHeight / bgImage.height; + + // Use the larger scale to cover the entire canvas + const scale = Math.max(scaleX, scaleY); + + // Apply scale and position the image + bgImage.scaleX = scale; + bgImage.scaleY = scale; + bgImage.left = 0; // Align left + bgImage.top = 0; // Align top + + // Update the background image + canvas.setBackgroundImage(bgImage, canvas.renderAll.bind(canvas)); + } else { + // Render the canvas if no background image + canvas.renderAll(); + } } + + // Handle responsive behavior for panels if (document.getElementById("root").offsetWidth <= 640) { setLeftPanelOpen(false); setRightPanelOpen(false); @@ -62,7 +88,6 @@ export function AspectCanvas() { } } if (document.getElementById("root").offsetWidth > 640) { - // setLeftPanelOpen(true); if (openObjectPanel === true) { setRightPanelOpen(true); } @@ -70,13 +95,26 @@ export function AspectCanvas() { if (document.getElementById("root").offsetWidth > 640) { setOpenObjectPanel(false); } - } + }; - updateCanvasSize() - window.addEventListener('resize', updateCanvasSize) + // Initial setup + updateCanvasSize(); - return () => window.removeEventListener('resize', updateCanvasSize) - }, [selectedRatio, canvasRef, canvas, openObjectPanel, setLeftPanelOpen, setOpenObjectPanel, setRightPanelOpen, rightPanelOpen]) + // Listen for window resize + window.addEventListener('resize', updateCanvasSize); + + // Cleanup listener on unmount + return () => window.removeEventListener('resize', updateCanvasSize); + }, [ + selectedRatio, + canvasRef, + canvas, + openObjectPanel, + setLeftPanelOpen, + setOpenObjectPanel, + setRightPanelOpen, + rightPanelOpen + ]); useEffect(() => { if (window.fabric) { @@ -182,7 +220,7 @@ export function AspectCanvas() { -

After changing the resolution, the canvas background image will reset.

+

Changing the canvas size.

diff --git a/src/components/CanvasSetting.jsx b/src/components/CanvasSetting.jsx index 4d4211a..6f0cefe 100644 --- a/src/components/CanvasSetting.jsx +++ b/src/components/CanvasSetting.jsx @@ -29,13 +29,37 @@ const CanvasSetting = () => { [key]: value, })); - // Use the value directly for canvas updates + // Update canvas dimensions if (key === 'width') { - canvas.setWidth(value); + canvas.setWidth(value); // Update canvas width } else if (key === 'height') { - canvas.setHeight(value); + canvas.setHeight(value); // Update canvas height + } + + // Adjust the background image to fit the updated canvas + const bgImage = canvas.backgroundImage; + if (bgImage) { + const canvasWidth = canvas.width; + const canvasHeight = canvas.height; + + // Calculate scaling factors for width and height + const scaleX = canvasWidth / bgImage.width; + const scaleY = canvasHeight / bgImage.height; + + // Choose the larger scale to cover the entire canvas + const scale = Math.max(scaleX, scaleY); + + // Apply the scale and center the image + bgImage.scaleX = scale; + bgImage.scaleY = scale; + bgImage.left = 0; // Align left + bgImage.top = 0; // Align top + + // Mark the background image as needing an update + canvas.setBackgroundImage(bgImage, canvas.renderAll.bind(canvas)); + } else { + canvas.renderAll(); // Render if no background image } - canvas.renderAll(); }; const setBackgroundImage = (e) => { diff --git a/src/components/EachComponent/Icons/AllIcons.jsx b/src/components/EachComponent/Icons/AllIcons.jsx index aaaad89..ae7d45f 100644 --- a/src/components/EachComponent/Icons/AllIcons.jsx +++ b/src/components/EachComponent/Icons/AllIcons.jsx @@ -1,6 +1,6 @@ import { useContext, useState } from "react"; import { FixedSizeGrid as Grid } from "react-window"; -import { Card, CardTitle } from "@/components/ui/card"; +import { Card, CardDescription, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import * as lucideIcons from "lucide-react"; import CanvasContext from "@/components/Context/canvasContext/CanvasContext"; @@ -107,6 +107,7 @@ const AllIconsPage = () => { return ( All Icons + All copyright (c) for Lucide are held by Lucide Contributors 2022. {
{ - open ? :

No active object found

+ open ? :

No active object found

}