@@ -139,6 +139,30 @@ export const ExampleSignature = () => {
139
139
copyTextToClipboard ( svgelm . outerHTML ) ;
140
140
} ;
141
141
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
+
142
166
return (
143
167
< Wrapper >
144
168
< Signature
@@ -147,11 +171,12 @@ export const ExampleSignature = () => {
147
171
options = { options }
148
172
defaultPoints = { points }
149
173
/>
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' } } >
151
175
< button onClick = { handle } > Clear</ button >
152
176
< button onClick = { resetOption } > Reset Options</ button >
153
177
< button onClick = { handleCopy } > Copy Options</ button >
154
178
< button onClick = { handleSVGCopy } > Copy to SVG</ button >
179
+ < button onClick = { downloadImage } > Download Image</ button >
155
180
</ div >
156
181
< div style = { { display : 'flex' , flexWrap : 'wrap' , gap : '1rem' , paddingTop : '1rem' } } >
157
182
< label >
0 commit comments