Roadmap to becoming a developer in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

36 lines
995 B

type DownloadImageProps = {
url: string;
name: string;
extension?: 'png' | 'jpg';
scale?: number;
};
export async function downloadImage({
url,
name,
extension = 'png',
scale = 1,
}: DownloadImageProps) {
try {
const res = await fetch(url);
const svg = await res.text();
const image = `data:image/svg+xml;base64,${window.btoa(svg)}`;
const img = new Image();
img.src = image;
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.width * scale;
canvas.height = img.height * scale;
const ctx = canvas.getContext('2d');
ctx?.drawImage(img, 0, 0, canvas.width, canvas.height);
const png = canvas.toDataURL('image/png', 1.0); // Increase the quality by setting a higher value (0.0 - 1.0)
const a = document.createElement('a');
a.href = png;
a.download = `${name}.${extension}`;
a.click();
};
} catch (error) {
alert('Error downloading image');
}
}