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.

114 lines
4.1 KiB

  1. 'use strict';
  2. import CircleImageBase from '../util/CircleImageBase'
  3. /**
  4. * A CircularImage Node/Cluster shape.
  5. *
  6. * @class CircularImage
  7. * @extends CircleImageBase
  8. */
  9. class CircularImage extends CircleImageBase {
  10. /**
  11. * @param {Object} options
  12. * @param {Object} body
  13. * @param {Label} labelModule
  14. * @param {Image} imageObj
  15. * @param {Image} imageObjAlt
  16. * @constructor CircularImage
  17. */
  18. constructor (options, body, labelModule, imageObj, imageObjAlt) {
  19. super(options, body, labelModule);
  20. this.setImages(imageObj, imageObjAlt);
  21. }
  22. /**
  23. *
  24. * @param {CanvasRenderingContext2D} ctx
  25. * @param {boolean} [selected]
  26. * @param {boolean} [hover]
  27. */
  28. resize(ctx, selected = this.selected, hover = this.hover) {
  29. var imageAbsent = (this.imageObj.src === undefined) ||
  30. (this.imageObj.width === undefined) ||
  31. (this.imageObj.height === undefined);
  32. if (imageAbsent) {
  33. var diameter = this.options.size * 2;
  34. this.width = diameter;
  35. this.height = diameter;
  36. this.radius = 0.5*this.width;
  37. return;
  38. }
  39. // At this point, an image is present, i.e. this.imageObj is valid.
  40. if (this.needsRefresh(selected, hover)) {
  41. this._resizeImage();
  42. }
  43. }
  44. /**
  45. *
  46. * @param {CanvasRenderingContext2D} ctx
  47. * @param {number} x width
  48. * @param {number} y height
  49. * @param {boolean} selected
  50. * @param {boolean} hover
  51. * @param {{toArrow: boolean, toArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), toArrowType: *, middleArrow: boolean, middleArrowScale: (number|allOptions.edges.arrows.middle.scaleFactor|{number}|Array), middleArrowType: (allOptions.edges.arrows.middle.type|{string}|string|*), fromArrow: boolean, fromArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), fromArrowType: *, arrowStrikethrough: (*|boolean|allOptions.edges.arrowStrikethrough|{boolean}), color: undefined, inheritsColor: (string|string|string|allOptions.edges.color.inherit|{string, boolean}|Array|*), opacity: *, hidden: *, length: *, shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *, dashes: (*|boolean|Array|allOptions.edges.dashes|{boolean, array}), width: *}} values
  52. */
  53. draw(ctx, x, y, selected, hover, values) {
  54. this.switchImages(selected);
  55. this.resize();
  56. this.left = x - this.width / 2;
  57. this.top = y - this.height / 2;
  58. // draw the background circle. IMPORTANT: the stroke in this method is used by the clip method below.
  59. this._drawRawCircle(ctx, x, y, values);
  60. // now we draw in the circle, we save so we can revert the clip operation after drawing.
  61. ctx.save();
  62. // clip is used to use the stroke in drawRawCircle as an area that we can draw in.
  63. ctx.clip();
  64. // draw the image
  65. this._drawImageAtPosition(ctx, values);
  66. // restore so we can again draw on the full canvas
  67. ctx.restore();
  68. this._drawImageLabel(ctx, x, y, selected, hover);
  69. this.updateBoundingBox(x,y);
  70. }
  71. // TODO: compare with Circle.updateBoundingBox(), consolidate? More stuff is happening here
  72. /**
  73. *
  74. * @param {number} x width
  75. * @param {number} y height
  76. */
  77. updateBoundingBox(x,y) {
  78. this.boundingBox.top = y - this.options.size;
  79. this.boundingBox.left = x - this.options.size;
  80. this.boundingBox.right = x + this.options.size;
  81. this.boundingBox.bottom = y + this.options.size;
  82. // TODO: compare with Image.updateBoundingBox(), consolidate?
  83. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  84. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  85. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  86. }
  87. /**
  88. *
  89. * @param {CanvasRenderingContext2D} ctx
  90. * @param {number} angle - Unused
  91. * @returns {Number}
  92. */
  93. distanceToBorder(ctx, angle) { // eslint-disable-line no-unused-vars
  94. this.resize(ctx);
  95. return this.width * 0.5;
  96. }
  97. }
  98. export default CircularImage;