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.

58 lines
1.7 KiB

  1. /**
  2. * Created by Alex on 3/18/2015.
  3. */
  4. 'use strict';
  5. import CircleImageBase from '../util/CircleImageBase'
  6. class Image extends CircleImageBase {
  7. constructor (options, body, labelModule, imageObj) {
  8. super(options, body, labelModule);
  9. this.imageObj = imageObj;
  10. }
  11. resize() {
  12. if (!this.width || !this.height) { // undefined or 0
  13. var width, height;
  14. var scale = this.imageObj.height / this.imageObj.width;
  15. if (scale !== undefined) {
  16. width = this.options.size * 2 || this.imageObj.width;
  17. height = this.options.size * 2 * scale || this.imageObj.height;
  18. }
  19. else {
  20. width = 0;
  21. height = 0;
  22. }
  23. this.width = width;
  24. this.height = height;
  25. }
  26. }
  27. draw(ctx, x, y, selected, hover) {
  28. this.resize(ctx);
  29. this.left = x - this.width / 2;
  30. this.top = y - this.height / 2;
  31. this._drawImageAtPosition(ctx);
  32. this.boundingBox.top = this.top;
  33. this.boundingBox.left = this.left;
  34. this.boundingBox.right = this.left + this.width;
  35. this.boundingBox.bottom = this.top + this.height;
  36. this._drawImageLabel(ctx, x, y, selected || hover);
  37. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  38. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  39. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height);
  40. }
  41. distanceToBorder(ctx, angle) {
  42. this.resize(ctx);
  43. var a = this.width / 2;
  44. var b = this.height / 2;
  45. var w = (Math.sin(angle) * a);
  46. var h = (Math.cos(angle) * b);
  47. return a * b / Math.sqrt(w * w + h * h);
  48. }
  49. }
  50. export default Image;