Chrome: v96 Firefox: v95
I'm trying to download an SVG image as a PNG image from the browser. This seems to be working fine for Chrome, but I'm downloading a blank image with firefox. Any idea why?
export function downloadSvgImage(svgElement: HTMLElement, name: string) {
const xml = new XMLSerializer().serializeToString(svgElement);
const svg64 = window.btoa(xml);
const b64Start = 'data:image/svg+xml;base64,';
const viewBox = svgElement.getAttribute('viewBox');
const dimensionArr = viewBox.split(' ');
const width = parseInt(dimensionArr[2]);
const height = parseInt(dimensionArr[3]);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
const image = new Image();
image.onload = () => {
canvas.getContext('2d').drawImage(image, 0, 0, width, height);
canvas.toBlob((blob: any) => {
const anchor = document.createElement('a');
anchor.download = `${name}.png`;
anchor.href = URL.createObjectURL(blob);
anchor.click();
URL.revokeObjectURL(blob);
}, 'image/png');
};
image.src = b64Start + svg64;
}