-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
55 lines (50 loc) · 1.68 KB
/
utils.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
(function(context) {
context.loadImages = loadImages;
context.loadImagesOnCanvas = loadImagesOnCanvas;
function loadImages(options, callback) {
var frag = document.createDocumentFragment();
_loadImages(options.images, callback, function(key, url, done) {
var img = document.createElement('img');
img.onload = function() {
done(null, img);
}
img.src = url;
frag.appendChild(img);
});
}
function loadImagesOnCanvas(options, callback) {
_loadImages(options.images, callback, function(key, url, done) {
var w = options.width;
var h = options.height;
var canvas = document.createElement('canvas');
canvg(canvas, url, {
renderCallback : function() {
done(null, canvas);
}
});
});
}
// ---------------------------------------
function _loadImages(images, callback, f) {
var result = {};
var counter = 0;
var keys = Object.keys(images);
if (!keys.length){
return callback(null, result);
}
for (var i = 0; i < keys.length; i++) {
(function(key, url) { // We need it!
f(key, url, function(err, image) {
counter++;
result[key] = {
url : url,
image : image
};
if (counter == keys.length) {
callback(null, result);
}
});
})(keys[i], images[keys[i]]);
}
}
})(this);