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.

162 lines
4.7 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, values) {
  63. var borderWidth = values.borderWidth / this.body.view.scale;
  64. ctx.lineWidth = Math.min(this.width, borderWidth);
  65. ctx.strokeStyle = values.borderColor;
  66. ctx.fillStyle = values.color;
  67. ctx.circle(x, y, values.size);
  68. // draw shadow if enabled
  69. this.enableShadow(ctx, values);
  70. // draw the background
  71. ctx.fill();
  72. // disable shadows for other elements.
  73. this.disableShadow(ctx, values);
  74. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  75. ctx.save();
  76. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  77. if (borderWidth > 0) {
  78. this.enableBorderDashes(ctx, values);
  79. //draw the border
  80. ctx.stroke();
  81. //disable dashed border for other elements
  82. this.disableBorderDashes(ctx, values);
  83. }
  84. ctx.restore();
  85. }
  86. _drawImageAtPosition(ctx, values) {
  87. if (this.imageObj.width != 0) {
  88. // draw the image
  89. ctx.globalAlpha = 1.0;
  90. // draw shadow if enabled
  91. this.enableShadow(ctx, values);
  92. let factor = (this.imageObj.width / this.width) / this.body.view.scale;
  93. if (factor > 2 && this.options.shapeProperties.interpolation === true) {
  94. let w = this.imageObj.width;
  95. let h = this.imageObj.height;
  96. var can2 = document.createElement('canvas');
  97. can2.width = w;
  98. can2.height = w;
  99. var ctx2 = can2.getContext('2d');
  100. factor *= 0.5;
  101. w *= 0.5;
  102. h *= 0.5;
  103. ctx2.drawImage(this.imageObj, 0, 0, w, h);
  104. let distance = 0;
  105. let iterations = 1;
  106. while (factor > 2 && iterations < 4) {
  107. ctx2.drawImage(can2, distance, 0, w, h, distance+w, 0, w/2, h/2);
  108. distance += w;
  109. factor *= 0.5;
  110. w *= 0.5;
  111. h *= 0.5;
  112. iterations += 1;
  113. }
  114. ctx.drawImage(can2, distance, 0, w, h, this.left, this.top, this.width, this.height);
  115. }
  116. else {
  117. // draw image
  118. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  119. }
  120. // disable shadows for other elements.
  121. this.disableShadow(ctx, values);
  122. }
  123. }
  124. _drawImageLabel(ctx, x, y, selected, hover) {
  125. var yLabel;
  126. var offset = 0;
  127. if (this.height !== undefined) {
  128. offset = this.height * 0.5;
  129. var labelDimensions = this.labelModule.getTextSize(ctx, selected, hover);
  130. if (labelDimensions.lineCount >= 1) {
  131. offset += labelDimensions.height / 2;
  132. }
  133. }
  134. yLabel = y + offset;
  135. if (this.options.label) {
  136. this.labelOffset = offset;
  137. }
  138. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  139. }
  140. }
  141. export default CircleImageBase;