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.

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