|
| 1 | +import type { DownloadRasterProps, DownloadSVGProps } from '../types/utils' |
| 2 | + |
| 3 | +export const downloadSVG = ({ svgRef, fileSize, fileName }: DownloadSVGProps) => { |
| 4 | + if (!svgRef.current) return |
| 5 | + |
| 6 | + const clonedSvg = svgRef.current.cloneNode(true) as SVGSVGElement |
| 7 | + clonedSvg.setAttribute('width', fileSize.toString()) |
| 8 | + clonedSvg.setAttribute('height', fileSize.toString()) |
| 9 | + |
| 10 | + const serializer = new XMLSerializer() |
| 11 | + const svgBlob = new Blob([serializer.serializeToString(clonedSvg)], { |
| 12 | + type: 'image/svg+xml', |
| 13 | + }) |
| 14 | + const url = URL.createObjectURL(svgBlob) |
| 15 | + const a = document.createElement('a') |
| 16 | + a.href = url |
| 17 | + a.download = `${fileName}.svg` |
| 18 | + document.body.appendChild(a) |
| 19 | + a.click() |
| 20 | + document.body.removeChild(a) |
| 21 | + URL.revokeObjectURL(url) |
| 22 | +} |
| 23 | + |
| 24 | +export const downloadRaster = ({ |
| 25 | + svgRef, |
| 26 | + fileSize, |
| 27 | + fileName, |
| 28 | + fileFormat, |
| 29 | + imageSettings, |
| 30 | + calculatedImageSettings, |
| 31 | + size, |
| 32 | + numCells, |
| 33 | + margin, |
| 34 | +}: DownloadRasterProps) => { |
| 35 | + if (!svgRef.current) return |
| 36 | + |
| 37 | + const canvas = document.createElement('canvas') |
| 38 | + const ctx = canvas.getContext('2d') |
| 39 | + if (!ctx) return |
| 40 | + |
| 41 | + canvas.width = fileSize |
| 42 | + canvas.height = fileSize |
| 43 | + |
| 44 | + const svgData = new XMLSerializer().serializeToString(svgRef.current) |
| 45 | + const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' }) |
| 46 | + const svgUrl = URL.createObjectURL(svgBlob) |
| 47 | + |
| 48 | + const qrImg = new Image() |
| 49 | + qrImg.crossOrigin = 'anonymous' |
| 50 | + qrImg.src = svgUrl |
| 51 | + |
| 52 | + qrImg.onload = () => { |
| 53 | + ctx.drawImage(qrImg, 0, 0, fileSize, fileSize) |
| 54 | + URL.revokeObjectURL(svgUrl) |
| 55 | + |
| 56 | + if (imageSettings?.src && calculatedImageSettings) { |
| 57 | + const logoImg = new Image() |
| 58 | + logoImg.crossOrigin = 'anonymous' |
| 59 | + logoImg.src = imageSettings.src |
| 60 | + |
| 61 | + logoImg.onload = () => { |
| 62 | + const ratio = fileSize / size |
| 63 | + const scale = numCells / fileSize |
| 64 | + |
| 65 | + const logoSize = imageSettings.width * ratio |
| 66 | + const logoX = imageSettings.x |
| 67 | + ? (calculatedImageSettings.x + margin) / scale |
| 68 | + : (fileSize - logoSize) / 2 |
| 69 | + const logoY = imageSettings.y |
| 70 | + ? (calculatedImageSettings.y + margin) / scale |
| 71 | + : (fileSize - logoSize) / 2 |
| 72 | + ctx.drawImage(logoImg, logoX, logoY, logoSize, logoSize) |
| 73 | + |
| 74 | + const imageType = fileFormat === 'png' ? 'image/png' : 'image/jpeg' |
| 75 | + const a = document.createElement('a') |
| 76 | + a.href = canvas.toDataURL(imageType) |
| 77 | + a.download = `${fileName}.${fileFormat}` |
| 78 | + document.body.appendChild(a) |
| 79 | + a.click() |
| 80 | + document.body.removeChild(a) |
| 81 | + } |
| 82 | + |
| 83 | + logoImg.onerror = (err) => console.error('Error loading logo:', err) |
| 84 | + } else { |
| 85 | + const imageType = fileFormat === 'png' ? 'image/png' : 'image/jpeg' |
| 86 | + const a = document.createElement('a') |
| 87 | + a.href = canvas.toDataURL(imageType) |
| 88 | + a.download = `${fileName}.${fileFormat}` |
| 89 | + document.body.appendChild(a) |
| 90 | + a.click() |
| 91 | + document.body.removeChild(a) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + qrImg.onerror = (err) => console.error('Error loading QR code:', err) |
| 96 | +} |
0 commit comments