-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2d19af
commit 6d5f1a3
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { toast } from "react-toastify"; | ||
import axios from "axios"; | ||
|
||
const copyToClipboard = (pngBlob: Blob) => { | ||
try { | ||
navigator.clipboard.write([ | ||
new ClipboardItem({ | ||
[pngBlob.type]: pngBlob, | ||
}), | ||
]); | ||
toast.success("성공적으로 복사되었습니다."); | ||
} catch (error) { | ||
toast.error("복사에 실패했습니다."); | ||
console.error(error); | ||
} | ||
}; | ||
|
||
const copyImageToClipboard = (imageBlob: Blob) => { | ||
const image = new Image(); | ||
image.src = URL.createObjectURL(imageBlob); | ||
|
||
image.onload = () => { | ||
const canvas = document.createElement("canvas"); | ||
canvas.width = image.naturalWidth; | ||
canvas.height = image.naturalHeight; | ||
|
||
const ctx = canvas.getContext("2d"); | ||
|
||
if (!ctx) { | ||
console.error("Canvas context를 생성할 수 없습니다."); | ||
return; | ||
} | ||
|
||
ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight); | ||
canvas.toBlob((blob) => { | ||
if (blob) { | ||
copyToClipboard(blob); | ||
return; | ||
} | ||
|
||
console.error("Canvas to Blob 변환 실패"); | ||
}, "image/png"); | ||
}; | ||
|
||
image.onerror = () => { | ||
console.error("이미지 로딩 실패"); | ||
}; | ||
}; | ||
|
||
export const copyZzal = async (imageUrl: string) => { | ||
try { | ||
const { data: imageBlob } = await axios.get<Blob>(imageUrl, { responseType: "blob" }); | ||
|
||
copyImageToClipboard(imageBlob); | ||
} catch (error) { | ||
toast.error("복사에 실패했습니다."); | ||
console.error(error); | ||
} | ||
}; |