As part of a project of mine, I need to dynamically create a canvas and then draw an SVG file (also dynamically generated) in the canvas. For my project I need the canvas to cover the whole page and the SVG to fill the whole canvas. In the interest of browser comparability I set the SVG width and height to the canvas width and height respectively (to deal with this know Firefox bug https://bugzilla.mozilla.org/show_bug.cgi?id=700533 as per this answer https://stackoverflow.com/a/28692538) yet the canvas is still blank in Firefox (works fine in Chrome, Safari, and Opera). I was wondering anyone could shed some light on this problem.
I made a fiddle with a demo (not my real SVG file but the same method and the same problem). Just to show the problem is not the the SVG if you copy it out of the alert and paste into a new Firefox tab it renders just fine. My Firefox version is 47.0 This should be the part of the code that is the most important.
img.onload = function () {
ctx.scale(ratio, ratio);
ctx.drawImage(img, 0, 0);
}
svg = "data:image/svg+xml,"+ "<svg xmlns='http://www.w3.org/2000/svg' width='"+canvas.width+"px' height='"+canvas.height+"px' viewBox='0,0,"+canvas.width+","+canvas.height+"' >" +
"<style type='text/css'> * { margin: 0; padding: 0; } p{background-color:blue;}</style>"+
'<rect x="10" y="10" width="100" height="100"/>' +
"</svg>"
img.src = svg
Full fiddle demo of the problem: https://jsfiddle.net/9are9tzx/5/ Thank you!