Skip to content

Commit dc6b9bf

Browse files
committed
fix: url image background
- change package from html2canvas to html-to-image
1 parent 2775501 commit dc6b9bf

File tree

3 files changed

+140
-180
lines changed

3 files changed

+140
-180
lines changed

app/components/ImageGenerator.tsx

+14-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
getRandomDirection,
99
} from "../utils/color";
1010
import Modal from "./Modal";
11-
import html2canvas from "html2canvas";
11+
import { toPng } from "html-to-image";
1212
import saveAs from "file-saver";
1313
import { formattedDate } from "../utils/date";
1414
import { useInput } from "../hooks/useInput";
@@ -31,20 +31,16 @@ export default function ImageGenerator() {
3131

3232
const divImageRef = useRef<HTMLDivElement>(null);
3333

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);
4443
});
45-
} catch (error) {
46-
console.error("Error converting div to Image", error);
47-
}
4844
};
4945

5046
const generateRandomColorBackground = () => {
@@ -73,10 +69,13 @@ export default function ImageGenerator() {
7369
};
7470

7571
const generateImageBackground = () => {
72+
7673
setBackgroundStyle({
7774
...backgroundStyle,
7875
backgroundImage: `url(${imageUrl})`,
7976
backgroundSize: "cover",
77+
backgroundRepeat: "no-repeat",
78+
border: "none",
8079
});
8180

8281
setShowModal(false);
@@ -194,7 +193,7 @@ export default function ImageGenerator() {
194193

195194
<button
196195
className={`${buttonStyle} p-4 mt-5`}
197-
onClick={generateImage}
196+
onClick={htmlToImageConvert}
198197
>
199198
저장하기
200199
</button>

0 commit comments

Comments
 (0)