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.

81 lines
2.6 KiB

  1. 'use strict';
  2. import CircleImageBase from '../util/CircleImageBase'
  3. class CircularImage extends CircleImageBase {
  4. constructor (options, body, labelModule, imageObj, imageObjAlt) {
  5. super(options, body, labelModule);
  6. this.setImages(imageObj, imageObjAlt);
  7. this._swapToImageResizeWhenImageLoaded = true;
  8. }
  9. resize(ctx, selected = this.selected, hover = this.hover) {
  10. if ((this.imageObj.src === undefined) ||
  11. (this.imageObj.width === undefined) ||
  12. (this.imageObj.height === undefined) ||
  13. (this.labelModule.differentState(selected, hover))) {
  14. var diameter = this.options.size * 2;
  15. this.width = diameter;
  16. this.height = diameter;
  17. this._swapToImageResizeWhenImageLoaded = true;
  18. this.radius = 0.5*this.width;
  19. } else {
  20. if (this._swapToImageResizeWhenImageLoaded) {
  21. this.width = undefined;
  22. this.height = undefined;
  23. this._swapToImageResizeWhenImageLoaded = false;
  24. }
  25. this._resizeImage();
  26. }
  27. }
  28. draw(ctx, x, y, selected, hover, values) {
  29. // switch images depending on 'selected' if imageObjAlt exists
  30. if (this.imageObjAlt) {
  31. this.switchImages(selected);
  32. }
  33. this.resize();
  34. this.left = x - this.width / 2;
  35. this.top = y - this.height / 2;
  36. let size = Math.min(0.5*this.height, 0.5*this.width);
  37. // draw the background circle. IMPORTANT: the stroke in this method is used by the clip method below.
  38. this._drawRawCircle(ctx, x, y, selected, hover, values);
  39. // now we draw in the circle, we save so we can revert the clip operation after drawing.
  40. ctx.save();
  41. // clip is used to use the stroke in drawRawCircle as an area that we can draw in.
  42. ctx.clip();
  43. // draw the image
  44. this._drawImageAtPosition(ctx, values);
  45. // restore so we can again draw on the full canvas
  46. ctx.restore();
  47. this._drawImageLabel(ctx, x, y, selected, hover);
  48. this.updateBoundingBox(x,y);
  49. }
  50. updateBoundingBox(x,y) {
  51. this.boundingBox.top = y - this.options.size;
  52. this.boundingBox.left = x - this.options.size;
  53. this.boundingBox.right = x + this.options.size;
  54. this.boundingBox.bottom = y + this.options.size;
  55. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  56. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  57. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  58. }
  59. distanceToBorder(ctx, angle) {
  60. this.resize(ctx);
  61. return this.width * 0.5;
  62. }
  63. }
  64. export default CircularImage;