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.

74 lines
2.3 KiB

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