@@ -2,6 +2,7 @@ import React from 'react';
22import { ImageTypes } from '../types/canvas' ;
33
44export type ValidType = 'imageSize' | 'canvasSize' | 'angle' | 'lineHeight' ;
5+
56export const getValidMessage = ( condition : boolean , type : ValidType ) => {
67 const message = {
78 imageSize : `Please register a picture smaller than "Limit Width"` ,
@@ -14,11 +15,7 @@ export const getValidMessage = (condition: boolean, type: ValidType) => {
1415 return '' ;
1516} ;
1617
17- export const downloadCanvas = (
18- ref : React . RefObject < HTMLCanvasElement > ,
19- imageType : ImageTypes
20- ) => {
21- const url = ref . current ?. toDataURL ( `image/${ imageType } ` ) ;
18+ const download = ( url : string , imageType : string ) => {
2219 const link = document . createElement ( 'a' ) ;
2320
2421 link . href = url as string ;
@@ -27,3 +24,30 @@ export const downloadCanvas = (
2724 link . click ( ) ;
2825 document . body . removeChild ( link ) ;
2926} ;
27+
28+ export const downloadCanvas = (
29+ ref : React . RefObject < HTMLCanvasElement > ,
30+ imageType : ImageTypes
31+ ) => {
32+ if ( ref . current ) {
33+ if ( imageType === 'svg' ) {
34+ const imgWidth = ref . current . width ;
35+ const imgHeight = ref . current . height ;
36+ const base64 = ref . current . toDataURL ( 'image/png' ) ;
37+ const svg = `
38+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${ imgWidth } " height="${ imgHeight } ">
39+ <image xlink:href="${ base64 } " width="${ imgWidth } " height="${ imgHeight } " />
40+ </svg>
41+ ` ;
42+
43+ const svgUrl =
44+ 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent ( svg ) ;
45+
46+ download ( svgUrl , 'svg' ) ;
47+ return ;
48+ }
49+
50+ const url = ref . current . toDataURL ( `image/${ imageType } ` ) ;
51+ download ( url , imageType ) ;
52+ }
53+ } ;
0 commit comments