8
8
getRandomDirection ,
9
9
} from "../utils/color" ;
10
10
import Modal from "./Modal" ;
11
- import html2canvas from "html2canvas " ;
11
+ import { toPng } from "html-to-image " ;
12
12
import saveAs from "file-saver" ;
13
13
import { formattedDate } from "../utils/date" ;
14
14
import { useInput } from "../hooks/useInput" ;
@@ -31,20 +31,16 @@ export default function ImageGenerator() {
31
31
32
32
const divImageRef = useRef < HTMLDivElement > ( null ) ;
33
33
34
- const generateImage = async ( ) => {
35
- if ( ! divImageRef . current ) return ;
36
-
37
- try {
38
- const imageRef = divImageRef . current ;
39
- const canvas = await html2canvas ( imageRef , { scale : 2 } ) ;
40
- canvas . toBlob ( ( blob ) => {
41
- if ( blob !== null ) {
42
- saveAs ( blob , `thumbnail-${ formattedDate } .png` ) ;
43
- }
34
+ const htmlToImageConvert = ( ) => {
35
+ const imageRef = divImageRef . current ;
36
+ if ( ! imageRef ) return ;
37
+ toPng ( imageRef , { cacheBust : false } )
38
+ . then ( ( dataUrl ) => {
39
+ saveAs ( dataUrl , `thumbnail-${ formattedDate } .png` )
40
+ } )
41
+ . catch ( ( err ) => {
42
+ console . error ( err ) ;
44
43
} ) ;
45
- } catch ( error ) {
46
- console . error ( "Error converting div to Image" , error ) ;
47
- }
48
44
} ;
49
45
50
46
const generateRandomColorBackground = ( ) => {
@@ -73,10 +69,13 @@ export default function ImageGenerator() {
73
69
} ;
74
70
75
71
const generateImageBackground = ( ) => {
72
+
76
73
setBackgroundStyle ( {
77
74
...backgroundStyle ,
78
75
backgroundImage : `url(${ imageUrl } )` ,
79
76
backgroundSize : "cover" ,
77
+ backgroundRepeat : "no-repeat" ,
78
+ border : "none" ,
80
79
} ) ;
81
80
82
81
setShowModal ( false ) ;
@@ -194,7 +193,7 @@ export default function ImageGenerator() {
194
193
195
194
< button
196
195
className = { `${ buttonStyle } p-4 mt-5` }
197
- onClick = { generateImage }
196
+ onClick = { htmlToImageConvert }
198
197
>
199
198
저장하기
200
199
</ button >
0 commit comments