mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Prefer native svg rendering if available
This commit is contained in:
Submodule src/fabric.js deleted from 791c74a82e
@ -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);
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user