gpt4 book ai didi

javascript - SVG 污染 Canvas IE 安全错误 toDataURL

转载 作者:行者123 更新时间:2023-11-28 06:46:37 25 4
gpt4 key购买 nike

我在 Angular JS 中有一个指令,允许将 SVG 导出为 PNG。这在其他浏览器中工作正常,但在 IE 中我收到安全错误。我尝试了各种各样的方法,但似乎不起作用。我在某处读到,如果我对其进行 base64 编码,那么就可以工作,但事实并非如此。

我在 Canvas 上绘制 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">

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

最佳答案

您需要设置img.crossOrigin='匿名';img.src=...

但是,要实现此目的,您还需要访问控制允许来源:*在图像的响应 header 中设置。

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

关于javascript - SVG 污染 Canvas IE 安全错误 toDataURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33364396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com