I have a directive in angular JS that allows you to export SVG to PNG. This works fine in other browsers, but in IE I get a security error. I tried all kinds of things, but it just doesn't work. I read somewhere that if I base64 encode it, then it will work, but it is not.
The code for drawing SVG on canvas is as follows:
var drawImage = function (canvas, ctx, svgContainer) {
var deferred = $q.defer();
removeHidden(angular.element(clone));
var clone = angular.element(svgContainer[0].cloneNode(true)),
child = clone.children()[0];
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();
img.onload = function () {
var viewBox = child.getAttribute('viewBox').split(' ');
console.log(viewBox);
var dimensions = {
width: viewBox[2],
height: viewBox[3]
};
console.log(img.width);
getNextLocation(canvas.width, canvas.height, dimensions);
ctx.drawImage(img, 0, 0, dimensions.width / 2, dimensions.height, location.x, location.y, location.width, location.height);
deferred.resolve(location);
};
img.src = base64;
return deferred.promise;
};
Before that, I created blob, but it also caused a security error. My main bit of code, this bit is here:
self.generateImage = function (onSuccess) {
location = null;
counter = 0;
var target = document.getElementById(self.options.targets.containerId),
svgContainers = angular.element(target.getElementsByClassName(self.options.targets.svgContainerClassName)),
itemCount = svgContainers.length;
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
setCanvasDimensions(canvas, itemCount);
var promises = [];
drawHeader(canvas, ctx);
promises.push(draw(canvas, ctx, svgContainers[0]));
promises.push(drawFooter(canvas, ctx));
$q.all(promises).then(function () {
console.log('all promises completed');
var dataURL = canvas.toDataURL("image/png");
console.log('we have the image');
var model = {
teamName: self.options.team.name,
sport: self.options.team.sport,
data: dataURL
};
self.create(model).then(function (response) {
console.log('saved to the database');
onSuccess(response);
});
})
};
, svg SVG . .
console.log canvas.toDateURL , .
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">
- , ?