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.

65 lines
1.8 KiB

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