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.

163 lines
4.9 KiB

  1. import NodeBase from '../util/NodeBase'
  2. class CircleImageBase extends NodeBase {
  3. constructor(options, body, labelModule) {
  4. super(options, body, labelModule);
  5. this.labelOffset = 0;
  6. this.imageLoaded = false;
  7. }
  8. setOptions(options, imageObj) {
  9. this.options = options;
  10. if (imageObj) {
  11. this.imageObj = imageObj;
  12. }
  13. }
  14. /**
  15. * This function resizes the image by the options size when the image has not yet loaded. If the image has loaded, we
  16. * force the update of the size again.
  17. *
  18. * @private
  19. */
  20. _resizeImage() {
  21. let force = false;
  22. if (!this.imageObj.width || !this.imageObj.height) { // undefined or 0
  23. this.imageLoaded = false;
  24. }
  25. else if (this.imageLoaded === false) {
  26. this.imageLoaded = true;
  27. force = true;
  28. }
  29. if (!this.width || !this.height || force === true) { // undefined or 0
  30. var width, height, ratio;
  31. if (this.imageObj.width && this.imageObj.height) { // not undefined or 0
  32. width = 0;
  33. height = 0;
  34. }
  35. if (this.options.shapeProperties.useImageSize === false) {
  36. if (this.imageObj.width > this.imageObj.height) {
  37. ratio = this.imageObj.width / this.imageObj.height;
  38. width = this.options.size * 2 * ratio || this.imageObj.width;
  39. height = this.options.size * 2 || this.imageObj.height;
  40. }
  41. else {
  42. if (this.imageObj.width && this.imageObj.height) { // not undefined or 0
  43. ratio = this.imageObj.height / this.imageObj.width;
  44. }
  45. else {
  46. ratio = 1;
  47. }
  48. width = this.options.size * 2;
  49. height = this.options.size * 2 * ratio;
  50. }
  51. }
  52. else {
  53. // when not using the size property, we use the image size
  54. width = this.imageObj.width;
  55. height = this.imageObj.height;
  56. }
  57. this.width = width;
  58. this.height = height;
  59. this.radius = 0.5 * this.width;
  60. }
  61. }
  62. _drawRawCircle(ctx, x, y, selected, hover, size) {
  63. var neutralborderWidth = this.options.borderWidth;
  64. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  65. var borderWidth = (selected ? selectionLineWidth : neutralborderWidth) / this.body.view.scale;
  66. ctx.lineWidth = Math.min(this.width, borderWidth);
  67. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  68. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  69. ctx.circle(x, y, size);
  70. // draw shadow if enabled
  71. this.enableShadow(ctx);
  72. // draw the background
  73. ctx.fill();
  74. // disable shadows for other elements.
  75. this.disableShadow(ctx);
  76. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  77. ctx.save();
  78. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  79. if (borderWidth > 0) {
  80. this.enableBorderDashes(ctx);
  81. //draw the border
  82. ctx.stroke();
  83. //disable dashed border for other elements
  84. this.disableBorderDashes(ctx);
  85. }
  86. ctx.restore();
  87. }
  88. _drawImageAtPosition(ctx) {
  89. if (this.imageObj.width != 0) {
  90. // draw the image
  91. ctx.globalAlpha = 1.0;
  92. // draw shadow if enabled
  93. this.enableShadow(ctx);
  94. let factor = (this.imageObj.width / this.width) / this.body.view.scale;
  95. if (factor > 2 && this.options.shapeProperties.interpolation === true) {
  96. let w = this.imageObj.width;
  97. let h = this.imageObj.height;
  98. var can2 = document.createElement('canvas');
  99. can2.width = w;
  100. can2.height = w;
  101. var ctx2 = can2.getContext('2d');
  102. factor *= 0.5;
  103. w *= 0.5;
  104. h *= 0.5;
  105. ctx2.drawImage(this.imageObj, 0, 0, w, h);
  106. let distance = 0;
  107. let iterations = 1;
  108. while (factor > 2 && iterations < 4) {
  109. ctx2.drawImage(can2, distance, 0, w, h, distance+w, 0, w/2, h/2);
  110. distance += w;
  111. factor *= 0.5;
  112. w *= 0.5;
  113. h *= 0.5;
  114. iterations += 1;
  115. }
  116. ctx.drawImage(can2, distance, 0, w, h, this.left, this.top, this.width, this.height);
  117. }
  118. else {
  119. // draw image
  120. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  121. }
  122. // disable shadows for other elements.
  123. this.disableShadow(ctx);
  124. }
  125. }
  126. _drawImageLabel(ctx, x, y, selected) {
  127. var yLabel;
  128. var offset = 0;
  129. if (this.height !== undefined) {
  130. offset = this.height * 0.5;
  131. var labelDimensions = this.labelModule.getTextSize(ctx);
  132. if (labelDimensions.lineCount >= 1) {
  133. offset += labelDimensions.height / 2;
  134. }
  135. }
  136. yLabel = y + offset;
  137. if (this.options.label) {
  138. this.labelOffset = offset;
  139. }
  140. this.labelModule.draw(ctx, x, yLabel, selected, 'hanging');
  141. }
  142. }
  143. export default CircleImageBase;