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.

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