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.

166 lines
4.5 KiB

  1. /**
  2. * Associates a canvas to a given image, containing a number of renderings
  3. * of the image at various sizes.
  4. *
  5. * This technique is known as 'mipmapping'.
  6. *
  7. * NOTE: Images can also be of type 'data:svg+xml`. This code also works
  8. * for svg, but the mipmapping may not be necessary.
  9. */
  10. class CachedImage {
  11. // eslint-disable-next-line no-unused-vars
  12. constructor(image) {
  13. this.NUM_ITERATIONS = 4; // Number of items in the coordinates array
  14. this.image = new Image();
  15. this.canvas = document.createElement('canvas');
  16. }
  17. /**
  18. * Called when the image has been succesfully loaded.
  19. */
  20. init() {
  21. if (this.initialized()) return;
  22. this.src = this.image.src; // For same interface with Image
  23. var w = this.image.width;
  24. var h = this.image.height;
  25. // Ease external access
  26. this.width = w;
  27. this.height = h;
  28. // Make canvas as small as possible
  29. this.canvas.width = 3*w/4;
  30. this.canvas.height = h/2;
  31. // Coordinates and sizes of images contained in the canvas
  32. // Values per row: [top x, left y, width, height]
  33. this.coordinates = [
  34. [ 0 , 0 , w/2 , h/2],
  35. [ w/2 , 0 , w/4 , h/4],
  36. [ w/2 , h/4, w/8 , h/8],
  37. [ 5*w/8, h/4, w/16, h/16]
  38. ];
  39. this._fillMipMap();
  40. }
  41. /**
  42. * @return {Boolean} true if init() has been called, false otherwise.
  43. */
  44. initialized() {
  45. return (this.coordinates !== undefined);
  46. }
  47. /**
  48. * Redraw main image in various sizes to the context.
  49. *
  50. * The rationale behind this is to reduce artefacts due to interpolation
  51. * at differing zoom levels.
  52. *
  53. * Source: http://stackoverflow.com/q/18761404/1223531
  54. *
  55. * This methods takes the resizing out of the drawing loop, in order to
  56. * reduce performance overhead.
  57. *
  58. * TODO: The code assumes that a 2D context can always be gotten. This is
  59. * not necessarily true! OTOH, if not true then usage of this class
  60. * is senseless.
  61. *
  62. * @private
  63. */
  64. _fillMipMap() {
  65. var ctx = this.canvas.getContext('2d');
  66. // First zoom-level comes from the image
  67. var to = this.coordinates[0];
  68. ctx.drawImage(this.image, to[0], to[1], to[2], to[3]);
  69. // The rest are copy actions internal to the canvas/context
  70. for (let iterations = 1; iterations < this.NUM_ITERATIONS; iterations++) {
  71. let from = this.coordinates[iterations - 1];
  72. let to = this.coordinates[iterations];
  73. ctx.drawImage(this.canvas,
  74. from[0], from[1], from[2], from[3],
  75. to[0], to[1], to[2], to[3]
  76. );
  77. }
  78. }
  79. /**
  80. * Draw the image, using the mipmap if necessary.
  81. *
  82. * MipMap is only used if param factor > 2; otherwise, original bitmap
  83. * is resized. This is also used to skip mipmap usage, e.g. by setting factor = 1
  84. *
  85. * Credits to 'Alex de Mulder' for original implementation.
  86. *
  87. * ctx {Context} context on which to draw zoomed image
  88. * factor {Float} scale factor at which to draw
  89. */
  90. drawImageAtPosition(ctx, factor, left, top, width, height) {
  91. if (factor > 2 && this.initialized()) {
  92. // Determine which zoomed image to use
  93. factor *= 0.5;
  94. let iterations = 0;
  95. while (factor > 2 && iterations < this.NUM_ITERATIONS) {
  96. factor *= 0.5;
  97. iterations += 1;
  98. }
  99. if (iterations >= this.NUM_ITERATIONS) {
  100. iterations = this.NUM_ITERATIONS - 1;
  101. }
  102. //console.log("iterations: " + iterations);
  103. let from = this.coordinates[iterations];
  104. ctx.drawImage(this.canvas,
  105. from[0], from[1], from[2], from[3],
  106. left, top, width, height
  107. );
  108. } else if (this._isImageOk()) {
  109. // Draw image directly
  110. ctx.drawImage(this.image, left, top, width, height);
  111. }
  112. }
  113. /**
  114. * Check if image is loaded
  115. *
  116. * Source: http://stackoverflow.com/a/1977898/1223531
  117. *
  118. * @private
  119. */
  120. _isImageOk() {
  121. var img = this.image;
  122. // During the onload event, IE correctly identifies any images that
  123. // weren’t downloaded as not complete. Others should too. Gecko-based
  124. // browsers act like NS4 in that they report this incorrectly.
  125. if (!img.complete) {
  126. return false;
  127. }
  128. // However, they do have two very useful properties: naturalWidth and
  129. // naturalHeight. These give the true size of the image. If it failed
  130. // to load, either of these should be zero.
  131. if (typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0) {
  132. return false;
  133. }
  134. // No other way of checking: assume it’s ok.
  135. return true;
  136. }
  137. }
  138. export default CachedImage;