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.

183 lines
4.9 KiB

  1. import NodeBase from '../util/NodeBase'
  2. /**
  3. * NOTE: This is a bad base class
  4. *
  5. * Child classes are:
  6. *
  7. * Image - uses *only* image methods
  8. * Circle - uses *only* _drawRawCircle
  9. * CircleImage - uses all
  10. *
  11. * TODO: Refactor, move _drawRawCircle to different module, derive Circle from NodeBase
  12. * Rename this to ImageBase
  13. * Consolidate common code in Image and CircleImage to base class
  14. */
  15. class CircleImageBase extends NodeBase {
  16. constructor(options, body, labelModule) {
  17. super(options, body, labelModule);
  18. this.labelOffset = 0;
  19. this.selected = false;
  20. }
  21. setOptions(options, imageObj, imageObjAlt) {
  22. this.options = options;
  23. this.setImages(imageObj, imageObjAlt);
  24. }
  25. setImages(imageObj, imageObjAlt) {
  26. if (imageObj) {
  27. this.imageObj = imageObj;
  28. if (imageObjAlt) {
  29. this.imageObjAlt = imageObjAlt;
  30. }
  31. }
  32. }
  33. /**
  34. * Switch between the base and the selected image.
  35. */
  36. switchImages(selected) {
  37. if ((selected && !this.selected) || (!selected && this.selected)) {
  38. let imageTmp = this.imageObj;
  39. this.imageObj = this.imageObjAlt;
  40. this.imageObjAlt = imageTmp;
  41. }
  42. // keep current state in memory
  43. this.selected = selected;
  44. }
  45. /**
  46. * Adjust the node dimensions for a loaded image.
  47. *
  48. * Pre: this.imageObj is valid
  49. */
  50. _resizeImage() {
  51. var width, height;
  52. if (this.options.shapeProperties.useImageSize === false) {
  53. // Use the size property
  54. var ratio_width = 1;
  55. var ratio_height = 1;
  56. // Only calculate the proper ratio if both width and height not zero
  57. if (this.imageObj.width && this.imageObj.height) {
  58. if (this.imageObj.width > this.imageObj.height) {
  59. ratio_width = this.imageObj.width / this.imageObj.height;
  60. }
  61. else {
  62. ratio_height = this.imageObj.height / this.imageObj.width;
  63. }
  64. }
  65. width = this.options.size * 2 * ratio_width;
  66. height = this.options.size * 2 * ratio_height;
  67. }
  68. else {
  69. // Use the image size
  70. width = this.imageObj.width;
  71. height = this.imageObj.height;
  72. }
  73. this.width = width;
  74. this.height = height;
  75. this.radius = 0.5 * this.width;
  76. }
  77. _drawRawCircle(ctx, x, y, values) {
  78. var borderWidth = values.borderWidth / this.body.view.scale;
  79. ctx.lineWidth = Math.min(this.width, borderWidth);
  80. ctx.strokeStyle = values.borderColor;
  81. ctx.fillStyle = values.color;
  82. ctx.circle(x, y, values.size);
  83. // draw shadow if enabled
  84. this.enableShadow(ctx, values);
  85. // draw the background
  86. ctx.fill();
  87. // disable shadows for other elements.
  88. this.disableShadow(ctx, values);
  89. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  90. ctx.save();
  91. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  92. if (borderWidth > 0) {
  93. this.enableBorderDashes(ctx, values);
  94. //draw the border
  95. ctx.stroke();
  96. //disable dashed border for other elements
  97. this.disableBorderDashes(ctx, values);
  98. }
  99. ctx.restore();
  100. }
  101. _drawImageAtPosition(ctx, values) {
  102. if (this.imageObj.width != 0) {
  103. // draw the image
  104. ctx.globalAlpha = 1.0;
  105. // draw shadow if enabled
  106. this.enableShadow(ctx, values);
  107. let factor = (this.imageObj.width / this.width) / this.body.view.scale;
  108. if (factor > 2 && this.options.shapeProperties.interpolation === true) {
  109. let w = this.imageObj.width;
  110. let h = this.imageObj.height;
  111. var can2 = document.createElement('canvas');
  112. can2.width = w;
  113. can2.height = w;
  114. var ctx2 = can2.getContext('2d');
  115. factor *= 0.5;
  116. w *= 0.5;
  117. h *= 0.5;
  118. ctx2.drawImage(this.imageObj, 0, 0, w, h);
  119. let distance = 0;
  120. let iterations = 1;
  121. while (factor > 2 && iterations < 4) {
  122. ctx2.drawImage(can2, distance, 0, w, h, distance+w, 0, w/2, h/2);
  123. distance += w;
  124. factor *= 0.5;
  125. w *= 0.5;
  126. h *= 0.5;
  127. iterations += 1;
  128. }
  129. ctx.drawImage(can2, distance, 0, w, h, this.left, this.top, this.width, this.height);
  130. }
  131. else {
  132. // draw image
  133. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  134. }
  135. // disable shadows for other elements.
  136. this.disableShadow(ctx, values);
  137. }
  138. }
  139. _drawImageLabel(ctx, x, y, selected, hover) {
  140. var yLabel;
  141. var offset = 0;
  142. if (this.height !== undefined) {
  143. offset = this.height * 0.5;
  144. var labelDimensions = this.labelModule.getTextSize(ctx, selected, hover);
  145. if (labelDimensions.lineCount >= 1) {
  146. offset += labelDimensions.height / 2;
  147. }
  148. }
  149. yLabel = y + offset;
  150. if (this.options.label) {
  151. this.labelOffset = offset;
  152. }
  153. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  154. }
  155. }
  156. export default CircleImageBase;