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.

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