I have a problem with my script which find a svg in the dom and get the svg downloaded as jpeg or png. Everything works fine on chrome but on firefox i download an empty image. Can you help me ?
this is my script
const downloadImage = () =>{
// Find the svg by ref
const selectedSvg = chartRef.current
const svgSizes = selectedSvg.getBBox()
const width = svgSizes.width * scale
const height = svgSizes.height * scale
// Create a Blob of the svg
const clonedSvgElement = selectedSvg.cloneNode(true);
const xml = new XMLSerializer().serializeToString(clonedSvgElement);
const blob = new Blob([xml],{type:'image/svg+xml;charset=utf-8'});
const URL = window.URL || window.webkitURL || window;
const blobURL = URL.createObjectURL(blob);
// Create a canvas for drawing the new img
const canvas = document.createElement('canvas');
const context = canvas.getContext("2d");
debugger;
const image = new Image();
// Add the img into the canvas and download it
image.onload = () => {
canvas.width = width
canvas.height = height
context.fillStyle = backgroundColor ?? "transparent";
context.fillRect(0, 0, width, height);
context.drawImage(image, 0, 0, width, height);
const imgUri = canvas.toDataURL(type, compression);
const link = document.createElement('a');
link.download = fileName;
link.href = imgUri;
link.click();
};
image.src = blobURL;
}