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.

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