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.4 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. this.switchImages(selected);
  26. this.resize();
  27. this.left = x - this.width / 2;
  28. this.top = y - this.height / 2;
  29. // draw the background circle. IMPORTANT: the stroke in this method is used by the clip method below.
  30. this._drawRawCircle(ctx, x, y, values);
  31. // now we draw in the circle, we save so we can revert the clip operation after drawing.
  32. ctx.save();
  33. // clip is used to use the stroke in drawRawCircle as an area that we can draw in.
  34. ctx.clip();
  35. // draw the image
  36. this._drawImageAtPosition(ctx, values);
  37. // restore so we can again draw on the full canvas
  38. ctx.restore();
  39. this._drawImageLabel(ctx, x, y, selected, hover);
  40. this.updateBoundingBox(x,y);
  41. }
  42. // TODO: compare with Circle.updateBoundingBox(), consolidate? More stuff is happening here
  43. updateBoundingBox(x,y) {
  44. this.boundingBox.top = y - this.options.size;
  45. this.boundingBox.left = x - this.options.size;
  46. this.boundingBox.right = x + this.options.size;
  47. this.boundingBox.bottom = y + this.options.size;
  48. // TODO: compare with Image.updateBoundingBox(), consolidate?
  49. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  50. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  51. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  52. }
  53. distanceToBorder(ctx, angle) { // eslint-disable-line no-unused-vars
  54. this.resize(ctx);
  55. return this.width * 0.5;
  56. }
  57. }
  58. export default CircularImage;