@@ -62,44 +62,60 @@ const LogoCard: React.FC<LogoCardProps> = ({ title, LogoComponent, svgContent, f
6262 } ;
6363
6464 const handleDownload = ( format : 'svg' | 'png' , size ?: number ) => {
65- if ( format === 'svg' ) {
66- const blob = new Blob ( [ svgContent ] , { type : 'image/svg+xml' } ) ;
67- const url = URL . createObjectURL ( blob ) ;
68- const a = document . createElement ( 'a' ) ;
69- a . href = url ;
70- a . download = `${ fileName } .svg` ;
71- document . body . appendChild ( a ) ;
72- a . click ( ) ;
73- document . body . removeChild ( a ) ;
74- URL . revokeObjectURL ( url ) ;
75- } else if ( format === 'png' && size ) {
76- const canvas = canvasRef . current ;
77- const ctx = canvas ?. getContext ( '2d' ) ;
78- if ( ! ctx || ! canvas ) return ;
79- const img = new Image ( ) ;
80- const svgBlob = new Blob ( [ svgContent ] , { type : 'image/svg+xml' } ) ;
81- const url = URL . createObjectURL ( svgBlob ) ;
82- img . onload = ( ) => {
83- const tempViewBox = svgContent . match ( / v i e w B o x = " ( [ ^ " ] + ) " / ) ;
84- const dims = tempViewBox ? tempViewBox [ 1 ] . split ( ' ' ) . map ( Number ) : [ 0 , 0 , 400 , 100 ] ;
85- const originalWidth = dims [ 2 ] ;
86- const originalHeight = dims [ 3 ] ;
87- const aspectRatio = originalWidth / originalHeight ;
88- canvas . width = size ;
89- canvas . height = size / aspectRatio ;
90- ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
91- ctx . drawImage ( img , 0 , 0 , canvas . width , canvas . height ) ;
92- const pngUrl = canvas . toDataURL ( 'image/png' ) ;
65+ let url : string | null = null ;
66+ try {
67+ if ( format === 'svg' ) {
68+ const blob = new Blob ( [ svgContent ] , { type : 'image/svg+xml' } ) ;
69+ url = URL . createObjectURL ( blob ) ;
9370 const a = document . createElement ( 'a' ) ;
94- a . href = pngUrl ;
95- a . download = `${ fileName } _ ${ size } px.png ` ;
71+ a . href = url ;
72+ a . download = `${ fileName } .svg ` ;
9673 document . body . appendChild ( a ) ;
9774 a . click ( ) ;
9875 document . body . removeChild ( a ) ;
76+ } else if ( format === 'png' && size ) {
77+ const canvas = canvasRef . current ;
78+ const ctx = canvas ?. getContext ( '2d' ) ;
79+ if ( ! ctx || ! canvas ) {
80+ console . error ( "Canvas context is not available." ) ;
81+ return ;
82+ }
83+ const img = new Image ( ) ;
84+ const svgBlob = new Blob ( [ svgContent ] , { type : 'image/svg+xml' } ) ;
85+ url = URL . createObjectURL ( svgBlob ) ;
86+
87+ img . onload = ( ) => {
88+ // Validate image dimensions before calculations
89+ if ( img . width > 0 && img . height > 0 ) {
90+ const aspectRatio = img . width / img . height ;
91+ canvas . width = size ;
92+ canvas . height = size / aspectRatio ;
93+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
94+ ctx . drawImage ( img , 0 , 0 , canvas . width , canvas . height ) ;
95+ const pngUrl = canvas . toDataURL ( 'image/png' ) ;
96+ const a = document . createElement ( 'a' ) ;
97+ a . href = pngUrl ;
98+ a . download = `${ fileName } _${ size } px.png` ;
99+ document . body . appendChild ( a ) ;
100+ a . click ( ) ;
101+ document . body . removeChild ( a ) ;
102+ } else {
103+ console . error ( "Failed to process SVG: Invalid image dimensions (width or height is zero)." ) ;
104+ }
105+ } ;
106+
107+ img . onerror = ( ) => {
108+ console . error ( "Failed to load SVG image for canvas conversion." ) ;
109+ } ;
110+
111+ img . src = url ;
112+ }
113+ } catch ( error ) {
114+ console . error ( "An error occurred during the download process:" , error ) ;
115+ } finally {
116+ if ( url ) {
99117 URL . revokeObjectURL ( url ) ;
100- } ;
101- img . onerror = ( ) => { console . error ( "Failed to load SVG image for canvas conversion." ) ; URL . revokeObjectURL ( url ) ; } ;
102- img . src = url ;
118+ }
103119 }
104120 } ;
105121
@@ -144,7 +160,6 @@ const ColorPalette = () => {
144160
145161
146162export default function BrandKitPage ( ) {
147-
148163 const logoAssets : Omit < LogoCardProps , 'svgContent' > [ ] = [
149164 { title : 'Primary Logo' , LogoComponent : LogoCompleto , fileName : 'papi_simulator_logo_primary' } ,
150165 { title : 'Primary Logo (White Version)' , LogoComponent : LogoCompleto , fileName : 'papi_simulator_logo_primary_white' , isDark : true } ,
0 commit comments