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.

156 lines
4.3 KiB

  1. import NodeBase from './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. if (!(imageObj === undefined && imageObjAlt === undefined)) {
  24. this.setImages(imageObj, imageObjAlt);
  25. }
  26. }
  27. /**
  28. * Set the images for this node.
  29. *
  30. * The images can be updated after the initial setting of options;
  31. * therefore, this method needs to be reentrant.
  32. *
  33. * For correct working in error cases, it is necessary to properly set
  34. * field 'nodes.brokenImage' in the options.
  35. *
  36. * @param {Image} imageObj required; main image to show for this node
  37. * @param {Image|undefined} imageObjAlt optional; image to show when node is selected
  38. */
  39. setImages(imageObj, imageObjAlt) {
  40. if (imageObjAlt && this.selected) {
  41. this.imageObj = imageObjAlt;
  42. this.imageObjAlt = imageObj;
  43. } else {
  44. this.imageObj = imageObj;
  45. this.imageObjAlt = imageObjAlt;
  46. }
  47. }
  48. /**
  49. * Set selection and switch between the base and the selected image.
  50. *
  51. * Do the switch only if imageObjAlt exists.
  52. *
  53. * @param {true|false} selected value of new selected state for current node
  54. */
  55. switchImages(selected) {
  56. var selection_changed = ((selected && !this.selected) || (!selected && this.selected));
  57. this.selected = selected; // Remember new selection
  58. if (this.imageObjAlt !== undefined && selection_changed) {
  59. let imageTmp = this.imageObj;
  60. this.imageObj = this.imageObjAlt;
  61. this.imageObjAlt = imageTmp;
  62. }
  63. }
  64. /**
  65. * Adjust the node dimensions for a loaded image.
  66. *
  67. * Pre: this.imageObj is valid
  68. */
  69. _resizeImage() {
  70. var width, height;
  71. if (this.options.shapeProperties.useImageSize === false) {
  72. // Use the size property
  73. var ratio_width = 1;
  74. var ratio_height = 1;
  75. // Only calculate the proper ratio if both width and height not zero
  76. if (this.imageObj.width && this.imageObj.height) {
  77. if (this.imageObj.width > this.imageObj.height) {
  78. ratio_width = this.imageObj.width / this.imageObj.height;
  79. }
  80. else {
  81. ratio_height = this.imageObj.height / this.imageObj.width;
  82. }
  83. }
  84. width = this.options.size * 2 * ratio_width;
  85. height = this.options.size * 2 * ratio_height;
  86. }
  87. else {
  88. // Use the image size
  89. width = this.imageObj.width;
  90. height = this.imageObj.height;
  91. }
  92. this.width = width;
  93. this.height = height;
  94. this.radius = 0.5 * this.width;
  95. }
  96. _drawRawCircle(ctx, x, y, values) {
  97. this.initContextForDraw(ctx, values);
  98. ctx.circle(x, y, values.size);
  99. this.performFill(ctx, values);
  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 = 1;
  108. if (this.options.shapeProperties.interpolation === true) {
  109. factor = (this.imageObj.width / this.width) / this.body.view.scale;
  110. }
  111. this.imageObj.drawImageAtPosition(ctx, factor, this.left, this.top, this.width, this.height);
  112. // disable shadows for other elements.
  113. this.disableShadow(ctx, values);
  114. }
  115. }
  116. _drawImageLabel(ctx, x, y, selected, hover) {
  117. var yLabel;
  118. var offset = 0;
  119. if (this.height !== undefined) {
  120. offset = this.height * 0.5;
  121. var labelDimensions = this.labelModule.getTextSize(ctx, selected, hover);
  122. if (labelDimensions.lineCount >= 1) {
  123. offset += labelDimensions.height / 2;
  124. }
  125. }
  126. yLabel = y + offset;
  127. if (this.options.label) {
  128. this.labelOffset = offset;
  129. }
  130. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  131. }
  132. }
  133. export default CircleImageBase;