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.

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