({
targetId,
fileName = "github-creature.png",
pixelRatio = 2,
}: DownloadCardButtonProps)
| 69 | } |
| 70 | |
| 71 | export default function DownloadCardButton({ |
| 72 | targetId, |
| 73 | fileName = "github-creature.png", |
| 74 | pixelRatio = 2, |
| 75 | }: DownloadCardButtonProps) { |
| 76 | const [isDownloading, setIsDownloading] = React.useState(false); |
| 77 | |
| 78 | const onDownload = React.useCallback(async () => { |
| 79 | const node = document.getElementById(targetId); |
| 80 | if (!node) return; |
| 81 | |
| 82 | setIsDownloading(true); |
| 83 | try { |
| 84 | try { |
| 85 | await document.fonts?.ready; |
| 86 | } catch { |
| 87 | // ignore |
| 88 | } |
| 89 | |
| 90 | const webglCaptures = captureWebGLCanvases(node); |
| 91 | |
| 92 | const dataUrl = await domToPng(node, { |
| 93 | scale: pixelRatio, |
| 94 | features: { |
| 95 | removeControlCharacter: false, |
| 96 | }, |
| 97 | onCloneNode: (clonedNode) => { |
| 98 | if (!(clonedNode instanceof HTMLElement)) return; |
| 99 | |
| 100 | const clonedCanvases = clonedNode.querySelectorAll("canvas"); |
| 101 | const originalCanvases = node.querySelectorAll("canvas"); |
| 102 | |
| 103 | clonedCanvases.forEach((clonedCanvas, index) => { |
| 104 | const originalCanvas = originalCanvases[index]; |
| 105 | const capture = webglCaptures.get(originalCanvas); |
| 106 | |
| 107 | if (capture) { |
| 108 | const img = document.createElement("img"); |
| 109 | img.src = capture; |
| 110 | img.style.cssText = clonedCanvas.style.cssText; |
| 111 | img.style.width = |
| 112 | clonedCanvas.style.width || `${originalCanvas.offsetWidth}px`; |
| 113 | img.style.height = |
| 114 | clonedCanvas.style.height || `${originalCanvas.offsetHeight}px`; |
| 115 | img.style.position = "absolute"; |
| 116 | img.style.top = "0"; |
| 117 | img.style.left = "0"; |
| 118 | |
| 119 | clonedCanvas.parentNode?.replaceChild(img, clonedCanvas); |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | if (clonedNode.id === targetId) { |
| 124 | clonedNode.style.transform = "none"; |
| 125 | clonedNode.style.transformStyle = "flat"; |
| 126 | } |
| 127 | }, |
| 128 | }); |
nothing calls this directly
no test coverage detected