-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
66 lines (56 loc) · 1.43 KB
/
index.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
var loadImage = require('load-img')
var noop = function () {}
module.exports = svgToImage
function svgToImage (svg, opt, cb) {
if (typeof opt === 'function') {
cb = opt
opt = {}
}
cb = cb || noop
opt = opt || {}
if (typeof window === 'undefined') {
return bail('window global is undefined; not in a browser')
}
var DOMURL = getURL()
if (!DOMURL ||
typeof DOMURL.createObjectURL !== 'function' ||
typeof DOMURL.revokeObjectURL !== 'function') {
return bail('browser does not support URL.createObjectURL')
}
if (typeof window.Blob === 'undefined') {
return bail('browser does not support Blob constructor')
}
if (!Array.isArray(svg)) {
svg = [ svg ]
}
var blob
try {
blob = new window.Blob(svg, {
type: 'image/svg+xml;charset=utf-8'
})
} catch (e) {
return bail(e)
}
var url = DOMURL.createObjectURL(blob)
loadImage(url, opt, function (err, img) {
DOMURL.revokeObjectURL(url)
if (err) {
// try again for Safari 8.0, using simple encodeURIComponent
// this will fail with DOM content but at least it works with SVG
var url2 = 'data:image/svg+xml,' + encodeURIComponent(svg.join(''))
return loadImage(url2, opt, cb)
}
cb(err, img)
})
function bail (msg) {
process.nextTick(function () {
cb(new Error(msg))
})
}
}
function getURL () {
return window.URL ||
window.webkitURL ||
window.mozURL ||
window.msURL
}