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.

43 lines
839 B

  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. * @return {Image} img The image object
  21. */
  22. Images.prototype.load = function(url) {
  23. var img = this.images[url];
  24. if (img == undefined) {
  25. // create the image
  26. var images = this;
  27. img = new Image();
  28. this.images[url] = img;
  29. img.onload = function() {
  30. if (images.callback) {
  31. images.callback(this);
  32. }
  33. };
  34. img.src = url;
  35. }
  36. return img;
  37. };
  38. module.exports = Images;