Tainted Canvas with canvas.toBlob

I am trying to convert SVG to image and request download to user.

var chart = $(svg.node())
            .attr('xmlns', 'http://www.w3.org/2000/svg');
var width = that.svg_width;
var height = that.svg_height;
var data = new XMLSerializer().serializeToString(chart.get(0));
var svg1 = new Blob([data], { type: "image/svg+xml;charset=utf-8" });
var url = URL.createObjectURL(svg1);

var img = $('<img />')
            .width(width)
            .height(height);
img.attr('crossOrigin' ,'' );
img.bind('load', function() {
        var canvas = document.createElement('canvas');
        canvas.width = width;
        canvas.height = height;
        var ctx = canvas.getContext('2d');
        ctx.drawImage(img.get(0), 0, 0);
        canvas.toBlob(function(blob) { // this is where it fails
                saveAs(blob, "test.png");
        });
});
img.attr('src', url);

An exception appears in Chrome: "Uncaught SecurityError: Failed to execute" toDataURL "in" HTMLCanvasElement ": shadow canvases may not be exported." on canvas.toBlob

In this case, the cross origin does not exist. Svg is on the same page, which I convert to an image and try to upload to the canvas. So how did the canvas go bad? Am I missing something?

+4
source share
1 answer

https://code.google.com/p/chromium/issues/detail?id=294129. svg <foreignObject> ( d3) .

uri svg blob,

+3

Source: https://habr.com/ru/post/1608641/


All Articles