import ActiveObjectContext from '@/components/Context/activeObject/ObjectContext'; import CanvasContext from '@/components/Context/canvasContext/CanvasContext'; import { useContext } from 'react' import { shapes } from './shapes'; import { fabric } from 'fabric'; const CustomShape = () => { const { canvas } = useContext(CanvasContext); const { setActiveObject } = useContext(ActiveObjectContext); const addShape = (each) => { // Load the SVG from the imported file fabric.loadSVGFromURL(each, (objects, options) => { const svgGroup = fabric.util.groupSVGElements(objects, options); // Calculate canvas center const centerX = canvas.getWidth() / 2; const centerY = canvas.getHeight() / 2; // Set properties for centering the SVG svgGroup.set({ left: centerX, // Center horizontally top: centerY, // Center vertically originX: 'center', // Set the origin to the center originY: 'center', fill: "#f09b0a", scaleX: 1, scaleY: 1, }); // Add SVG to the canvas canvas.add(svgGroup); canvas.setActiveObject(svgGroup); // Update the active object state setActiveObject(svgGroup); // Render the canvas canvas.renderAll(); }); }; return (
addShape()} > {shapes.map((each) => (
{ e.stopPropagation() addShape(each.source) }} > {`Shape
))}
) } export default CustomShape