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.

80 lines
2.5 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. }
  8. resize(ctx, selected = this.selected, hover = this.hover) {
  9. var imageAbsent = (this.imageObj.src === undefined) ||
  10. (this.imageObj.width === undefined) ||
  11. (this.imageObj.height === undefined);
  12. if (imageAbsent) {
  13. var diameter = this.options.size * 2;
  14. this.width = diameter;
  15. this.height = diameter;
  16. this.radius = 0.5*this.width;
  17. return;
  18. }
  19. // At this point, an image is present, i.e. this.imageObj is valid.
  20. if (this.needsRefresh(selected, hover)) {
  21. this._resizeImage();
  22. }
  23. }
  24. draw(ctx, x, y, selected, hover, values) {
  25. // switch images depending on 'selected' if imageObjAlt exists
  26. if (this.imageObjAlt) {
  27. this.switchImages(selected);
  28. }
  29. this.selected = selected;
  30. this.resize(ctx, selected, hover);
  31. this.left = x - this.width / 2;
  32. this.top = y - this.height / 2;
  33. // draw the background circle. IMPORTANT: the stroke in this method is used by the clip method below.
  34. this._drawRawCircle(ctx, x, y, values);
  35. // now we draw in the circle, 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, values);
  41. // restore so we can again draw on the full canvas
  42. ctx.restore();
  43. this._drawImageLabel(ctx, x, y, selected, hover);
  44. this.updateBoundingBox(x,y);
  45. }
  46. // TODO: compare with Circle.updateBoundingBox(), consolidate? More stuff is happening here
  47. updateBoundingBox(x,y) {
  48. this.boundingBox.top = y - this.options.size;
  49. this.boundingBox.left = x - this.options.size;
  50. this.boundingBox.right = x + this.options.size;
  51. this.boundingBox.bottom = y + this.options.size;
  52. // TODO: compare with Image.updateBoundingBox(), consolidate?
  53. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  54. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  55. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  56. }
  57. distanceToBorder(ctx, angle) {
  58. this.resize(ctx);
  59. return this.width * 0.5;
  60. }
  61. }
  62. export default CircularImage;