Img.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { coordinate } from "../Coordinate.js";
  2. import { mathUtil } from "../Util/MathUtil.js";
  3. import VectorType from "../enum/VectorType.js";
  4. import Geometry from "./Geometry";
  5. import { api } from "@/store/sync.ts";
  6. export default class Img extends Geometry {
  7. constructor(src, vectorId) {
  8. super();
  9. this.src = null;
  10. this.angle = 0;
  11. this.display = true;
  12. this.center = null;
  13. this.imageData = null;
  14. this.bounding = null;
  15. this.geoType = VectorType.Img;
  16. this.setId(vectorId);
  17. this.setSrc(src);
  18. }
  19. setAngle(value) {
  20. this.angle = value;
  21. }
  22. setSrc(src) {
  23. this.src = src;
  24. }
  25. setImageData() {
  26. return new Promise(async (resolve, reject) => {
  27. this.imageData = new Image();
  28. this.imageData.src = await api.getFile(this.src);
  29. this.imageData.onload = function () {
  30. resolve();
  31. };
  32. this.imageData.onerror = function () {
  33. reject();
  34. };
  35. });
  36. }
  37. setBounding() {
  38. const width = this.imageData.width * coordinate.ratio;
  39. const height = this.imageData.height * coordinate.ratio;
  40. this.bounding = [];
  41. this.bounding[0] = {
  42. x: this.center.x - width / 2,
  43. y: this.center.y + height / 2,
  44. };
  45. this.bounding[1] = {
  46. x: this.center.x + width / 2,
  47. y: this.center.y + height / 2,
  48. };
  49. this.bounding[2] = {
  50. x: this.center.x + width / 2,
  51. y: this.center.y - height / 2,
  52. };
  53. this.bounding[3] = {
  54. x: this.center.x - width / 2,
  55. y: this.center.y - height / 2,
  56. };
  57. }
  58. }