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.

160 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. this.setImages(imageObj, imageObjAlt);
  25. }
  26. setImages(imageObj, imageObjAlt) {
  27. if (imageObjAlt && this.selected) {
  28. this.imageObj = imageObjAlt;
  29. this.imageObjAlt = imageObj;
  30. } else {
  31. this.imageObj = imageObj;
  32. this.imageObjAlt = imageObjAlt;
  33. }
  34. }
  35. /**
  36. * Switch between the base and the selected image.
  37. */
  38. switchImages(selected) {
  39. if ((selected && !this.selected) || (!selected && this.selected)) {
  40. let imageTmp = this.imageObj;
  41. this.imageObj = this.imageObjAlt;
  42. this.imageObjAlt = imageTmp;
  43. }
  44. // keep current state in memory
  45. this.selected = selected;
  46. }
  47. /**
  48. * Adjust the node dimensions for a loaded image.
  49. *
  50. * Pre: this.imageObj is valid
  51. */
  52. _resizeImage() {
  53. var width, height;
  54. if (this.options.shapeProperties.useImageSize === false) {
  55. // Use the size property
  56. var ratio_width = 1;
  57. var ratio_height = 1;
  58. // Only calculate the proper ratio if both width and height not zero
  59. if (this.imageObj.width && this.imageObj.height) {
  60. if (this.imageObj.width > this.imageObj.height) {
  61. ratio_width = this.imageObj.width / this.imageObj.height;
  62. }
  63. else {
  64. ratio_height = this.imageObj.height / this.imageObj.width;
  65. }
  66. }
  67. width = this.options.size * 2 * ratio_width;
  68. height = this.options.size * 2 * ratio_height;
  69. }
  70. else {
  71. // Use the image size
  72. width = this.imageObj.width;
  73. height = this.imageObj.height;
  74. }
  75. this.width = width;
  76. this.height = height;
  77. this.radius = 0.5 * this.width;
  78. }
  79. _drawRawCircle(ctx, x, y, values) {
  80. var borderWidth = values.borderWidth / this.body.view.scale;
  81. ctx.lineWidth = Math.min(this.width, borderWidth);
  82. ctx.strokeStyle = values.borderColor;
  83. ctx.fillStyle = values.color;
  84. ctx.circle(x, y, values.size);
  85. // draw shadow if enabled
  86. this.enableShadow(ctx, values);
  87. // draw the background
  88. ctx.fill();
  89. // disable shadows for other elements.
  90. this.disableShadow(ctx, values);
  91. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  92. ctx.save();
  93. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  94. if (borderWidth > 0) {
  95. this.enableBorderDashes(ctx, values);
  96. //draw the border
  97. ctx.stroke();
  98. //disable dashed border for other elements
  99. this.disableBorderDashes(ctx, values);
  100. }
  101. ctx.restore();
  102. }
  103. _drawImageAtPosition(ctx, values) {
  104. if (this.imageObj.width != 0) {
  105. // draw the image
  106. ctx.globalAlpha = 1.0;
  107. // draw shadow if enabled
  108. this.enableShadow(ctx, values);
  109. let factor = 1;
  110. if (this.options.shapeProperties.interpolation === true) {
  111. factor = (this.imageObj.width / this.width) / this.body.view.scale;
  112. }
  113. this.imageObj.drawImageAtPosition(ctx, factor, this.left, this.top, this.width, this.height);
  114. // disable shadows for other elements.
  115. this.disableShadow(ctx, values);
  116. }
  117. }
  118. _drawImageLabel(ctx, x, y, selected, hover) {
  119. var yLabel;
  120. var offset = 0;
  121. if (this.height !== undefined) {
  122. offset = this.height * 0.5;
  123. var labelDimensions = this.labelModule.getTextSize(ctx, selected, hover);
  124. if (labelDimensions.lineCount >= 1) {
  125. offset += labelDimensions.height / 2;
  126. }
  127. }
  128. yLabel = y + offset;
  129. if (this.options.label) {
  130. this.labelOffset = offset;
  131. }
  132. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  133. }
  134. }
  135. export default CircleImageBase;