wow
This commit is contained in:
parent
caf67c870e
commit
d5c9469624
1 changed files with 9 additions and 9 deletions
|
|
@ -216,41 +216,41 @@ export default function App() {
|
|||
try {
|
||||
const response = await fetch(photo.src);
|
||||
if (!response.ok) throw new Error("Failed to fetch image");
|
||||
|
||||
|
||||
let blob = await response.blob();
|
||||
|
||||
|
||||
// 대부분의 브라우저는 클립보드 복사 시 image/png만 지원하므로,
|
||||
// 형식이 다를 경우 canvas를 이용해 PNG로 변환합니다.
|
||||
if (blob.type !== "image/png") {
|
||||
const img = new Image();
|
||||
const url = URL.createObjectURL(blob);
|
||||
img.src = url;
|
||||
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
img.onload = resolve;
|
||||
img.onerror = reject;
|
||||
});
|
||||
|
||||
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = img.width;
|
||||
canvas.height = img.height;
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) throw new Error("Canvas context를 가져오지 못했습니다.");
|
||||
ctx.drawImage(img, 0, 0);
|
||||
|
||||
|
||||
const pngBlob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, "image/png"));
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
|
||||
if (!pngBlob) throw new Error("PNG 변환에 실패했습니다.");
|
||||
blob = pngBlob;
|
||||
}
|
||||
|
||||
|
||||
const clipboardItem = new ClipboardItem({
|
||||
[blob.type]: blob
|
||||
});
|
||||
|
||||
|
||||
await navigator.clipboard.write([clipboardItem]);
|
||||
alert("이미지가 클립보드에 복사되었습니다!");
|
||||
// alert("이미지가 클립보드에 복사되었습니다!");
|
||||
} catch (error) {
|
||||
console.error("Failed to copy image:", error);
|
||||
alert("이미지 복사에 실패했습니다. 브라우저 호환성 문제일 수 있습니다.");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue