-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgenie.js
executable file
·90 lines (77 loc) · 2.65 KB
/
svgenie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* global canvg window document */
/*
* svgenie
* https://github.com/Causata/svgenie
*
* Copyright (c) 2013 Causata Ltd
* Licensed under the MIT license.
*/
var svgenie = (function(){
"use strict";
var _serializeXmlNode = function (xmlNode) {
if (typeof window.XMLSerializer != "undefined") {
return (new window.XMLSerializer()).serializeToString(xmlNode);
} else if (typeof xmlNode.xml != "undefined") {
return xmlNode.xml;
}
return "";
};
var _toCanvas = function( svg, options, callback ){
if ( typeof svg == "string" ){
if ( svg.substr(0,1) == "#" ) { svg = svg.substr(1); }
svg = document.getElementById(svg);
}
// Hopefully don't need to attach anything to the DOM
var canvas = document.createElement("canvas");
canvas.setAttribute("height",svg.offsetHeight);
canvas.setAttribute("width",svg.offsetWidth);
canvg( canvas, _serializeXmlNode(svg), {
ignoreMouse : true,
ignoreAnimation : true,
renderCallback : function(){ callback( canvas ); }
});
};
var _toDataURL = function( id, options, callback ){
_toCanvas( id, options, function( canvas ){
callback( canvas.toDataURL("image/png"), canvas );
});
};
var _save = function( id, options ){
_toDataURL( id, options, function(data, canvas){
_saveToFile({
data : data,
canvas : canvas,
name : options.name || "image.png"
});
});
};
var _saveToFile = function( conf ){
var a = document.createElement( "a" );
// Can we use the "download" attribute? (Chrome && FF20)
if( a.download != null ){
a.href = conf.data;
a.download = conf.name;
_pretendClick(a);
return;
};
// IE10
if( window.navigator.msSaveBlob ){
conf.canvas.toBlob( function ( blobby ){
if( window.navigator.msSaveBlob ){
window.navigator.msSaveBlob( blobby, conf.name );
}
}, "image/png" );
return;
}
};
function _pretendClick(eElement) {
var oEvent = document.createEvent("MouseEvents");
oEvent.initMouseEvent( "click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null );
return eElement.dispatchEvent(oEvent);
};
return {
save : _save,
toCanvas : _toCanvas,
toDataURL : _toDataURL
};
})();