@@ -139,6 +139,30 @@ export const ExampleSignature = () => {
139139 copyTextToClipboard ( svgelm . outerHTML ) ;
140140 } ;
141141
142+ const downloadImage = ( ) => {
143+ const svgelm = $svg . current ?. svg ?. cloneNode ( true ) as SVGSVGElement ;
144+ const clientWidth = $svg . current ?. svg ?. clientWidth ;
145+ const clientHeight = $svg . current ?. svg ?. clientHeight ;
146+ svgelm . removeAttribute ( 'style' ) ;
147+ svgelm . setAttribute ( 'width' , `${ clientWidth } px` ) ;
148+ svgelm . setAttribute ( 'height' , `${ clientHeight } px` ) ;
149+ svgelm . setAttribute ( 'viewbox' , `${ clientWidth } ${ clientHeight } ` ) ;
150+ const data = new XMLSerializer ( ) . serializeToString ( svgelm ) ;
151+ const canvas = document . createElement ( 'canvas' ) ;
152+ const ctx = canvas . getContext ( '2d' ) ;
153+ const img = new Image ( ) ;
154+ img . onload = ( ) => {
155+ canvas . width = clientWidth || 0 ;
156+ canvas . height = clientHeight || 0 ;
157+ ctx ?. drawImage ( img , 0 , 0 ) ;
158+ const a = document . createElement ( 'a' ) ;
159+ a . download = 'signature.png' ;
160+ a . href = canvas . toDataURL ( 'image/png' ) ;
161+ a . click ( ) ;
162+ } ;
163+ img . src = `data:image/svg+xml;base64,${ window . btoa ( unescape ( encodeURIComponent ( data ) ) ) } ` ;
164+ } ;
165+
142166 return (
143167 < Wrapper >
144168 < Signature
@@ -147,11 +171,12 @@ export const ExampleSignature = () => {
147171 options = { options }
148172 defaultPoints = { points }
149173 />
150- < div style = { { display : 'flex' , flexWrap : 'wrap' , gap : '1rem ' , paddingTop : '0.46rem' } } >
174+ < div style = { { display : 'flex' , flexWrap : 'wrap' , gap : '0.51rem ' , paddingTop : '0.46rem' } } >
151175 < button onClick = { handle } > Clear</ button >
152176 < button onClick = { resetOption } > Reset Options</ button >
153177 < button onClick = { handleCopy } > Copy Options</ button >
154178 < button onClick = { handleSVGCopy } > Copy to SVG</ button >
179+ < button onClick = { downloadImage } > Download Image</ button >
155180 </ div >
156181 < div style = { { display : 'flex' , flexWrap : 'wrap' , gap : '1rem' , paddingTop : '1rem' } } >
157182 < label >
0 commit comments