【问题标题】:SVG tained canvas IE security error toDataURL [duplicate]SVG tained canvas IE 安全错误 toDataURL [重复]
【发布时间】:2016-01-26 15:40:27
【问题描述】:

我在 Angular JS 中有一个指令,允许将 SVG 导出为 PNG。这在其他浏览器中运行良好,但在 IE 中出现安全错误。 我已经尝试了各种各样的东西,但它似乎不起作用。 我在某处读到,如果我对它进行 base64 编码,那么它会起作用,但它不会。

我在画布上绘制 SVG 的代码是这样的:

// Private function for drawing our images
var drawImage = function (canvas, ctx, svgContainer) {

    // Defer our promise
    var deferred = $q.defer();

    // Remove hidden layers
    removeHidden(angular.element(clone));

    // Create our data
    var clone = angular.element(svgContainer[0].cloneNode(true)),
        child = clone.children()[0];

    // Remove hidden layers
    removeHidden(angular.element(clone));

    var s = new XMLSerializer(),
        t = s.serializeToString(child),
        base64 = 'data:image/svg+xml;base64,' + window.btoa(t);

    console.log(base64);

    var img = new Image();

    // When the image has loaded
    img.onload = function () {

        // Create our dimensions
        var viewBox = child.getAttribute('viewBox').split(' ');

        console.log(viewBox);

        var dimensions = {
            width: viewBox[2],
            height: viewBox[3]
        };

        console.log(img.width);

        // Get our location
        getNextLocation(canvas.width, canvas.height, dimensions);

        // Draw our image using the context
        ctx.drawImage(img, 0, 0, dimensions.width / 2, dimensions.height, location.x, location.y, location.width, location.height);

        // Resolve our promise
        deferred.resolve(location);
    };

    // Set the URL of the image
    img.src = base64;

    // Return our promise
    return deferred.promise;
};

在此之前,我正在创建一个 blob,但这也导致了安全错误。 我的主要代码,这里是这个:

// Public function to generate the image
self.generateImage = function (onSuccess) {

    // Reset our location
    location = null;
    counter = 0;

    // Get our SVG
    var target = document.getElementById(self.options.targets.containerId),
        svgContainers = angular.element(target.getElementsByClassName(self.options.targets.svgContainerClassName)),
        itemCount = svgContainers.length;

    // Get our context
    var canvas = document.getElementById('canvas'),
        ctx = canvas.getContext('2d');

    // Set our canvas height and width
    setCanvasDimensions(canvas, itemCount);

    // Create our array of promises
    var promises = [];

    // Draw our header and footer
    drawHeader(canvas, ctx);

    //// For each container
    //for (var i = 0; i < itemCount; i++) {

    //    // Get our elements
    //    var svgContainer = svgContainers[i];

    //    // Draw our image and push our promise to the array
    //    promises.push(draw(canvas, ctx, svgContainer));
    //}

    promises.push(draw(canvas, ctx, svgContainers[0]));

    // Finally add our footer to our promises array
    promises.push(drawFooter(canvas, ctx));

    // When all promises have resolve
    $q.all(promises).then(function () {

        console.log('all promises completed');

        // Get our URL as a base64 string
        var dataURL = canvas.toDataURL("image/png");

        console.log('we have the image');

        // Create our model
        var model = {
            teamName: self.options.team.name,
            sport: self.options.team.sport,
            data: dataURL
        };

        // Create our preview
        self.create(model).then(function (response) {

            console.log('saved to the database');

            // Invoke our success callback
            onSuccess(response);
        });
    })
};

如您所见,我循环遍历每个 svg 容器并为每个容器绘制 SVG。我已在这段代码中对此进行了注释,并仅绘制了第一张图像。 canvas.toDateURL 之后的 console.log 指令永远不会被调用,我得到一个安全错误。

SVG 是内联的。从我阅读的内容来看,问题可能是由于 xlns 声明,但删除它仍然会给我带来问题:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
   viewBox="0 0 259.5 131" enable-background="new 0 0 259.5 131" xml:space="preserve">

有谁知道我该如何解决这个问题?

【问题讨论】:

  • 请贴出正在加载的图片源;不是实际的文件,而是您传递给函数的数据。
  • 我做不到,内联 SVG 的颜色会动态变化,所以我必须在画布上绘制它们
  • 愚蠢,非常愚蠢的建议。 jpg可以代替png吗?我最近对此很感兴趣,.jpg 文件可以工作,但 .png 是不行的......
  • 它转换成什么格式对我来说并不重要,我在页脚中有一个图像可以很好地绘制(它的来源是一个 png)并且不会污染画布,只有 SVG 会污染它 :(
  • 在 IE 的 canvas 元素上绘制的 SVG 会污染该画布并自动禁用 .toDataURL。 IE 具有其他浏览器没有的安全限制。解决方法是使用 .jpg/.png 或将 svg 命令转换为画布命令(例如,Mike Swansons 不错的 Adob​​e Illustrator 插件:github.com/mikeswanson/Ai2Canvas)。

标签: javascript angularjs html canvas svg


【解决方案1】:

您需要设置 img.crossOrigin= 'anonymous'; 前 img.src=...

但是,要完成这项工作,您还需要拥有 access-control-allow-origin: * 在图像的响应标头中设置。

要尝试解决方案,您可以使用 'http://fabricjs.com/assets/printio.png'(具有正确的标头集)例如

【讨论】:

  • 目前的问题是关于绘制 SVG 会污染画布的 IE 限制;它与同源策略无关,img.crossOrigin 不会解决任何问题。
猜你喜欢
  • 2014-08-17
  • 2016-11-02
  • 2015-10-07
  • 2016-04-26
  • 1970-01-01
  • 2013-12-17
  • 2014-04-21
  • 1970-01-01
  • 2014-11-26
相关资源
最近更新 更多