-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (33 loc) · 919 Bytes
/
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
/**
* Load image
* @param {string} src
* @param {object} [opt]
* @param {string} [opt.crossOrigin]
* @param {function} callback
*/
function Loadim(src, opt, callback) {
if (typeof src !== 'string')
throw new Error('src must be a valid image path');
if (typeof opt === 'function') {
callback = opt;
opt = null;
}
if (typeof callback !== 'function')
throw new Error('callback must be a function');
var image = new Image();
image.onload = function () {
callback(null, this);
};
image.onerror = function () {
callback(new Error('Unable to load image: ' + src), this);
};
if (opt && opt.crossOrigin) {
image.crossOrigin = opt.crossOrigin;
}
image.src = src;
return image;
}
if (typeof window !== 'undefined')
window.loadim = Loadim;
if (typeof module !== 'undefined')
module.exports = Loadim;