Prefer native svg rendering if available

This commit is contained in:
Niklas von Hertzen
2014-09-08 21:16:30 +03:00
parent ba9d5201cf
commit c20e679f2c
13 changed files with 1904 additions and 6865 deletions

Submodule src/fabric.js deleted from 791c74a82e

View File

@ -34,7 +34,7 @@ ImageLoader.prototype.loadImage = function(imageData) {
var src = imageData.args[0];
if (src.match(/data:image\/.*;base64,/i)) {
return new ImageContainer(src.replace(/url\(['"]{0,}|['"]{0,}\)$/ig, ''), false);
} else if (/(.+).svg$/i.test(src)) {
} else if (/(.+).svg$/i.test(src) && !this.support.svg) {
return new SVGContainer(src);
} else if (this.isSameOrigin(src) || this.options.allowTaint === true) {
return new ImageContainer(src, false);

View File

@ -1,6 +1,7 @@
function Support(document) {
this.rangeBounds = this.testRangeBounds(document);
this.cors = this.testCORS();
this.svg = this.testSVG();
}
Support.prototype.testRangeBounds = function(document) {
@ -31,3 +32,18 @@ Support.prototype.testRangeBounds = function(document) {
Support.prototype.testCORS = function() {
return typeof((new Image()).crossOrigin) !== "undefined";
};
Support.prototype.testSVG = function() {
var img = new Image();
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
img.src = "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'></svg>";
try {
ctx.drawImage(img, 0, 0);
canvas.toDataURL();
} catch(e) {
return false;
}
return true;
};

View File

@ -3,7 +3,11 @@ function SVGContainer(src) {
this.image = null;
var self = this;
this.promise = XHR(src).then(function(svg) {
return new Promise(function(resolve) {
return new Promise(function(resolve, reject) {
if (!html2canvas.fabric) {
return reject(new Error("html2canvas.svg.js is not loaded, cannot render svg"));
}
html2canvas.fabric.loadSVGFromString(svg, function (objects, options) {
var canvas = new html2canvas.fabric.StaticCanvas('c');
self.image = canvas.lowerCanvasEl;

View File

@ -5,14 +5,14 @@ function XHR(url) {
xhr.onload = function() {
if (xhr.status === 200) {
resolve(xhr.response);
resolve(xhr.responseText);
} else {
reject(Error(xhr.statusText));
reject(new Error(xhr.statusText));
}
};
xhr.onerror = function() {
reject(Error("Network Error"));
reject(new Error("Network Error"));
};
xhr.send();