Skip to content

Commit

Permalink
fix: resolve type mismatch in Canvas component and improve preview code
Browse files Browse the repository at this point in the history
  • Loading branch information
ishworrsubedii committed Oct 5, 2024
1 parent 2660d20 commit 22a95c6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6,315 deletions.
16 changes: 9 additions & 7 deletions app/components/SVGCanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useRef, useEffect } from 'react'

interface SVGElement {
export type CanvasSVGElement = {
id: string;
type: 'rect' | 'circle' | 'text' | 'path';
x: number;
Expand All @@ -24,9 +24,9 @@ const SVGCanvas: React.FC<{
onPanChange: (pan: { x: number; y: number }) => void;
showGrid: boolean;
svgCode: string;
onElementsChange: (elements: SVGElement[]) => void;
onElementsChange: (elements: CanvasSVGElement[]) => void;
eraserSize: number;
elements: SVGElement[];
elements: CanvasSVGElement[];
}> = ({
activeTool,
fillColor,
Expand Down Expand Up @@ -57,7 +57,7 @@ const SVGCanvas: React.FC<{
const parser = new DOMParser();
const svgDoc = parser.parseFromString(cleanedSvgCode, 'image/svg+xml');
const svgElement = svgDoc.documentElement;
const newElements: SVGElement[] = [];
const newElements: CanvasSVGElement[] = [];

Array.from(svgElement.children).forEach((child, index) => {
if (child instanceof SVGElement) {
Expand All @@ -81,7 +81,8 @@ const SVGCanvas: React.FC<{
}
});


console.log('SVG Code:', cleanedSvgCode);
console.log('Parsed SVG Elements:', newElements);
onElementsChange(newElements);
}
}, [svgCode, fillColor, strokeColor, strokeWidth, onElementsChange]);
Expand All @@ -102,7 +103,7 @@ const SVGCanvas: React.FC<{
const draw = (event: React.MouseEvent<SVGElement, MouseEvent>) => {
if (!isDrawing) return
const currentPoint = getMousePosition(event as unknown as React.MouseEvent<SVGSVGElement, MouseEvent>)
let newElement: SVGElement | null = null
let newElement: CanvasSVGElement | null = null

switch (activeTool) {
case 'Rectangle':
Expand Down Expand Up @@ -307,7 +308,7 @@ const SVGCanvas: React.FC<{
);
};

const renderResizeHandles = (element: SVGElement) => {
const renderResizeHandles = (element: CanvasSVGElement) => {
if ((element.type !== 'rect' && element.type !== 'circle') || selectedElementId !== element.id) return null;

const handleSize = 8;
Expand Down Expand Up @@ -368,6 +369,7 @@ const SVGCanvas: React.FC<{
{renderGrid()}
<g transform={`translate(${400 / zoom}, ${300 / zoom}) scale(${1.4 * zoom})`}> {/* Adjust translation and scale */}
{elements.map((element) => {
console.log('Rendering Element:', element);
switch (element.type) {
case 'rect':
return (
Expand Down
8 changes: 6 additions & 2 deletions app/playground/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import SVGCanvas from '../components/SVGCanvas'
import DownloadMenu from '../components/DownloadMenu'
import ThemeToggle from '../components/ThemeToggle'
import GenerateSVG from '../components/GenerateSVG'
import { CanvasSVGElement } from '../components/SVGCanvas'


type ApiResponse = {
status: string;
Expand All @@ -35,6 +37,8 @@ export default function AdvancedSVGEditor() {
const [isLoading, setIsLoading] = useState(false)
const [prompt, setPrompt] = useState<string>('')



const handleToolClick = (toolName: string) => {
setActiveTool(toolName)
}
Expand Down Expand Up @@ -211,9 +215,9 @@ export default function AdvancedSVGEditor() {
onPanChange={setPan}
showGrid={showGrid}
svgCode={svgCode}
onElementsChange={setElements}
onElementsChange={(newElements: CanvasSVGElement[]) => setElements(newElements as unknown as SVGElement[])}
eraserSize={eraserSize}
elements={elements}
elements={elements as unknown as CanvasSVGElement[]}
/>
</div>
<div className="mt-4 flex justify-center space-x-4">
Expand Down
27 changes: 0 additions & 27 deletions app/routes/about/page.tsx

This file was deleted.

56 changes: 0 additions & 56 deletions app/routes/features/page.tsx

This file was deleted.

Loading

0 comments on commit 22a95c6

Please sign in to comment.