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.

52 lines
1.0 KiB

  1. /**
  2. * @class Images
  3. * This class loads images and keeps them stored.
  4. */
  5. function Images() {
  6. this.images = {};
  7. this.callback = undefined;
  8. }
  9. /**
  10. * Set an onload callback function. This will be called each time an image
  11. * is loaded
  12. * @param {function} callback
  13. */
  14. Images.prototype.setOnloadCallback = function(callback) {
  15. this.callback = callback;
  16. };
  17. /**
  18. *
  19. * @param {string} url Url of the image
  20. * @param {string} url Url of an image to use if the url image is not found
  21. * @return {Image} img The image object
  22. */
  23. Images.prototype.load = function(url, brokenUrl) {
  24. var img = this.images[url];
  25. if (img == undefined) {
  26. // create the image
  27. var images = this;
  28. img = new Image();
  29. this.images[url] = img;
  30. img.onload = function() {
  31. if (images.callback) {
  32. images.callback(this);
  33. }
  34. };
  35. img.onerror = function () {
  36. this.src = brokenUrl;
  37. if (images.callback) {
  38. images.callback(this);
  39. }
  40. };
  41. img.src = url;
  42. }
  43. return img;
  44. };
  45. module.exports = Images;