vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
1.7 KiB

/**
* @class Images
* This class loads images and keeps them stored.
*/
function Images(callback) {
this.images = {};
this.imageBroken = {};
this.callback = callback;
}
/**
*
* @param {string} url Url of the image
* @param {string} url Url of an image to use if the url image is not found
* @return {Image} img The image object
*/
Images.prototype.load = function (url, brokenUrl, id) {
var me = this;
function addImageToCache(imageToCache) {
// IE11 fix -- thanks dponch!
if (imageToCache.width === 0) {
document.body.appendChild(imageToCache);
imageToCache.width = imageToCache.offsetWidth;
imageToCache.height = imageToCache.offsetHeight;
document.body.removeChild(imageToCache);
}
me.images[url] = imageToCache;
}
function redrawWithImage(imageToRedrawWith) {
if (me.callback) {
me.callback(imageToRedrawWith);
}
}
function tryloadBrokenUrl(imageToLoadBrokenUrlOn) {
if (brokenUrl === undefined) return;
imageToLoadBrokenUrlOn.onerror = function() {
console.error("Could not load brokenImage:", brokenUrl);
addImageToCache(new Image());
};
imageToLoadBrokenUrlOn.src = brokenUrl;
}
var cachedImage = this.images[url];
if (cachedImage) return cachedImage;
var img = new Image();
img.onload = function() {
addImageToCache(img);
redrawWithImage(img);
};
img.onerror = function () {
console.error("Could not load image:", url);
tryloadBrokenUrl(img);
}
img.src = url;
return img;
};
module.exports = Images;