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.

158 lines
4.3 KiB

  1. import NodeBase from './NodeBase';
  2. import CachedImage from '../../../../CachedImage';
  3. /**
  4. * NOTE: This is a bad base class
  5. *
  6. * Child classes are:
  7. *
  8. * Image - uses *only* image methods
  9. * Circle - uses *only* _drawRawCircle
  10. * CircleImage - uses all
  11. *
  12. * TODO: Refactor, move _drawRawCircle to different module, derive Circle from NodeBase
  13. * Rename this to ImageBase
  14. * Consolidate common code in Image and CircleImage to base class
  15. */
  16. class CircleImageBase extends NodeBase {
  17. constructor(options, body, labelModule) {
  18. super(options, body, labelModule);
  19. this.labelOffset = 0;
  20. this.selected = false;
  21. }
  22. setOptions(options, imageObj, imageObjAlt) {
  23. this.options = options;
  24. if (!(imageObj === undefined && imageObjAlt === undefined)) {
  25. this.setImages(imageObj, imageObjAlt);
  26. }
  27. }
  28. /**
  29. * Set the images for this node.
  30. *
  31. * The images can be updated after the initial setting of options;
  32. * therefore, this method needs to be reentrant.
  33. *
  34. * For correct working in error cases, it is necessary to properly set
  35. * field 'nodes.brokenImage' in the options.
  36. *
  37. * @param {Image} imageObj required; main image to show for this node
  38. * @param {Image|undefined} optional; image to show when node is selected
  39. */
  40. setImages(imageObj, imageObjAlt) {
  41. if (imageObjAlt && this.selected) {
  42. this.imageObj = imageObjAlt;
  43. this.imageObjAlt = imageObj;
  44. } else {
  45. this.imageObj = imageObj;
  46. this.imageObjAlt = imageObjAlt;
  47. }
  48. }
  49. /**
  50. * Set selection and switch between the base and the selected image.
  51. *
  52. * Do the switch only if imageObjAlt exists.
  53. *
  54. * @param {true|false} selected value of new selected state for current node
  55. */
  56. switchImages(selected) {
  57. var selection_changed = ((selected && !this.selected) || (!selected && this.selected));
  58. this.selected = selected; // Remember new selection
  59. if (this.imageObjAlt !== undefined && selection_changed) {
  60. let imageTmp = this.imageObj;
  61. this.imageObj = this.imageObjAlt;
  62. this.imageObjAlt = imageTmp;
  63. }
  64. }
  65. /**
  66. * Adjust the node dimensions for a loaded image.
  67. *
  68. * Pre: this.imageObj is valid
  69. */
  70. _resizeImage() {
  71. var width, height;
  72. if (this.options.shapeProperties.useImageSize === false) {
  73. // Use the size property
  74. var ratio_width = 1;
  75. var ratio_height = 1;
  76. // Only calculate the proper ratio if both width and height not zero
  77. if (this.imageObj.width && this.imageObj.height) {
  78. if (this.imageObj.width > this.imageObj.height) {
  79. ratio_width = this.imageObj.width / this.imageObj.height;
  80. }
  81. else {
  82. ratio_height = this.imageObj.height / this.imageObj.width;
  83. }
  84. }
  85. width = this.options.size * 2 * ratio_width;
  86. height = this.options.size * 2 * ratio_height;
  87. }
  88. else {
  89. // Use the image size
  90. width = this.imageObj.width;
  91. height = this.imageObj.height;
  92. }
  93. this.width = width;
  94. this.height = height;
  95. this.radius = 0.5 * this.width;
  96. }
  97. _drawRawCircle(ctx, x, y, values) {
  98. this.initContextForDraw(ctx, values);
  99. ctx.circle(x, y, values.size);
  100. this.performFill(ctx, values);
  101. }
  102. _drawImageAtPosition(ctx, values) {
  103. if (this.imageObj.width != 0) {
  104. // draw the image
  105. ctx.globalAlpha = 1.0;
  106. // draw shadow if enabled
  107. this.enableShadow(ctx, values);
  108. let factor = 1;
  109. if (this.options.shapeProperties.interpolation === true) {
  110. factor = (this.imageObj.width / this.width) / this.body.view.scale;
  111. }
  112. this.imageObj.drawImageAtPosition(ctx, factor, this.left, this.top, this.width, this.height);
  113. // disable shadows for other elements.
  114. this.disableShadow(ctx, values);
  115. }
  116. }
  117. _drawImageLabel(ctx, x, y, selected, hover) {
  118. var yLabel;
  119. var offset = 0;
  120. if (this.height !== undefined) {
  121. offset = this.height * 0.5;
  122. var labelDimensions = this.labelModule.getTextSize(ctx, selected, hover);
  123. if (labelDimensions.lineCount >= 1) {
  124. offset += labelDimensions.height / 2;
  125. }
  126. }
  127. yLabel = y + offset;
  128. if (this.options.label) {
  129. this.labelOffset = offset;
  130. }
  131. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  132. }
  133. }
  134. export default CircleImageBase;