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.

83 lines
2.6 KiB

  1. 'use strict';
  2. import CircleImageBase from '../util/CircleImageBase'
  3. /**
  4. * A CircularImage Node/Cluster shape.
  5. *
  6. * @param {Object} options
  7. * @param {Object} body
  8. * @param {Label} labelModule
  9. * @constructor CircularImage
  10. * @extends CircleImageBase
  11. */
  12. class CircularImage extends CircleImageBase {
  13. constructor (options, body, labelModule, imageObj, imageObjAlt) {
  14. super(options, body, labelModule);
  15. this.setImages(imageObj, imageObjAlt);
  16. }
  17. resize(ctx, selected = this.selected, hover = this.hover) {
  18. var imageAbsent = (this.imageObj.src === undefined) ||
  19. (this.imageObj.width === undefined) ||
  20. (this.imageObj.height === undefined);
  21. if (imageAbsent) {
  22. var diameter = this.options.size * 2;
  23. this.width = diameter;
  24. this.height = diameter;
  25. this.radius = 0.5*this.width;
  26. return;
  27. }
  28. // At this point, an image is present, i.e. this.imageObj is valid.
  29. if (this.needsRefresh(selected, hover)) {
  30. this._resizeImage();
  31. }
  32. }
  33. draw(ctx, x, y, selected, hover, values) {
  34. this.switchImages(selected);
  35. this.resize();
  36. this.left = x - this.width / 2;
  37. this.top = y - this.height / 2;
  38. // draw the background circle. IMPORTANT: the stroke in this method is used by the clip method below.
  39. this._drawRawCircle(ctx, x, y, values);
  40. // now we draw in the circle, we save so we can revert the clip operation after drawing.
  41. ctx.save();
  42. // clip is used to use the stroke in drawRawCircle as an area that we can draw in.
  43. ctx.clip();
  44. // draw the image
  45. this._drawImageAtPosition(ctx, values);
  46. // restore so we can again draw on the full canvas
  47. ctx.restore();
  48. this._drawImageLabel(ctx, x, y, selected, hover);
  49. this.updateBoundingBox(x,y);
  50. }
  51. // TODO: compare with Circle.updateBoundingBox(), consolidate? More stuff is happening here
  52. updateBoundingBox(x,y) {
  53. this.boundingBox.top = y - this.options.size;
  54. this.boundingBox.left = x - this.options.size;
  55. this.boundingBox.right = x + this.options.size;
  56. this.boundingBox.bottom = y + this.options.size;
  57. // TODO: compare with Image.updateBoundingBox(), consolidate?
  58. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  59. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  60. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  61. }
  62. distanceToBorder(ctx, angle) { // eslint-disable-line no-unused-vars
  63. this.resize(ctx);
  64. return this.width * 0.5;
  65. }
  66. }
  67. export default CircularImage;