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.

89 lines
2.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. }
  7. _resizeImage() {
  8. if (!this.width || !this.height) { // undefined or 0
  9. var width, height, ratio;
  10. if (this.imageObj.width && this.imageObj.height) { // not undefined or 0
  11. width = 0;
  12. height = 0;
  13. }
  14. if (this.imageObj.width > this.imageObj.height) {
  15. ratio = this.imageObj.width / this.imageObj.height;
  16. width = this.options.size * 2 * ratio || this.imageObj.width;
  17. height = this.options.size * 2 || this.imageObj.height;
  18. }
  19. else {
  20. ratio = this.imageObj.height / this.imageObj.width;
  21. width = this.options.size * 2 || this.imageObj.width;
  22. height = this.options.size * 2 * ratio || this.imageObj.height;
  23. }
  24. this.width = width;
  25. this.height = height;
  26. this.radius = 0.5*this.width;
  27. }
  28. }
  29. _drawRawCircle(ctx, x, y, selected, hover, size) {
  30. var borderWidth = this.options.borderWidth;
  31. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  32. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  33. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  34. ctx.lineWidth *= this.networkScaleInv;
  35. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  36. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  37. ctx.circle(x, y, size);
  38. // draw shadow if enabled
  39. this.enableShadow(ctx);
  40. ctx.fill();
  41. // disable shadows for other elements.
  42. this.disableShadow(ctx);
  43. ctx.stroke();
  44. }
  45. _drawImageAtPosition(ctx) {
  46. if (this.imageObj.width != 0) {
  47. // draw the image
  48. ctx.globalAlpha = 1.0;
  49. // draw shadow if enabled
  50. this.enableShadow(ctx);
  51. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  52. // disable shadows for other elements.
  53. this.disableShadow(ctx);
  54. }
  55. }
  56. _drawImageLabel(ctx, x, y, selected) {
  57. var yLabel;
  58. var offset = 0;
  59. if (this.height !== undefined) {
  60. offset = this.height * 0.5;
  61. var labelDimensions = this.labelModule.getTextSize(ctx);
  62. if (labelDimensions.lineCount >= 1) {
  63. offset += labelDimensions.height / 2;
  64. }
  65. }
  66. yLabel = y + offset;
  67. if (this.options.label) {
  68. this.labelOffset = offset;
  69. }
  70. this.labelModule.draw(ctx, x, yLabel, selected, 'hanging');
  71. }
  72. }
  73. export default CircleImageBase;