SVG.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import VectorType from "../enum/VectorType.js";
  2. import Geometry from "./Geometry.js";
  3. import { mathUtil } from "../Util/MathUtil.js";
  4. import { coordinate } from "../Coordinate.js";
  5. import Constant from "../Constant.js";
  6. //const sideWidth = 10;
  7. export default class SVG extends Geometry {
  8. constructor(center, type, vectorId) {
  9. super();
  10. this.center = center;
  11. this.points = null; //包裹的矩形的四个顶点
  12. this.angle = 0; //逆时针为负,顺时针为正。单位是:°
  13. this.type = type;
  14. this.geoType = VectorType.SVG;
  15. this.scale = this.getScale(); //缩放比例
  16. this.setBoundingVertexs();
  17. this.setId(vectorId);
  18. }
  19. // createDefaultPoints() {
  20. // this.points = [];
  21. // this.points[0] = {
  22. // x: this.center.x - sideWidth / 2,
  23. // y: this.center.y + sideWidth / 2,
  24. // };
  25. // this.points[1] = {
  26. // x: this.center.x + sideWidth / 2,
  27. // y: this.center.y + sideWidth / 2,
  28. // };
  29. // this.points[2] = {
  30. // x: this.center.x + sideWidth / 2,
  31. // y: this.center.y - sideWidth / 2,
  32. // };
  33. // this.points[3] = {
  34. // x: this.center.x - sideWidth / 2,
  35. // y: this.center.y - sideWidth / 2,
  36. // };
  37. // }
  38. setBoundingVertexs() {
  39. this.points = [];
  40. const rec = this.getLengthWidth();
  41. const length = this.scale * rec.length;
  42. const width = this.scale * rec.width;
  43. const minX = this.center.x - length / 2;
  44. const minY = this.center.y - width / 2;
  45. const maxX = this.center.x + length / 2;
  46. const maxY = this.center.y + width / 2;
  47. const point1 = this.rotatePoint(
  48. {
  49. x: minX,
  50. y: maxY,
  51. },
  52. this.center,
  53. this.angle
  54. );
  55. const point2 = this.rotatePoint(
  56. {
  57. x: maxX,
  58. y: maxY,
  59. },
  60. this.center,
  61. this.angle
  62. );
  63. const point3 = this.rotatePoint(
  64. {
  65. x: maxX,
  66. y: minY,
  67. },
  68. this.center,
  69. this.angle
  70. );
  71. const point4 = this.rotatePoint(
  72. {
  73. x: minX,
  74. y: minY,
  75. },
  76. this.center,
  77. this.angle
  78. );
  79. this.points.push(point1);
  80. this.points.push(point2);
  81. this.points.push(point3);
  82. this.points.push(point4);
  83. }
  84. setBoundingVertexs2(position, pointIndex) {
  85. if (mathUtil.getDistance(position, this.center) < Constant.minAdsorbPix) {
  86. return;
  87. }
  88. mathUtil.clonePoint(this.points[pointIndex], position);
  89. // 【注意】angle 逆时针为正,顺时针为负
  90. let nextIndex = this.getNextIndex(pointIndex);
  91. this.points[nextIndex] = this.rotatePoint(position, this.center, 90);
  92. nextIndex = this.getNextIndex(nextIndex);
  93. this.points[nextIndex] = this.rotatePoint(position, this.center, 180);
  94. nextIndex = this.getNextIndex(nextIndex);
  95. this.points[nextIndex] = this.rotatePoint(position, this.center, 270);
  96. }
  97. getNextIndex(index) {
  98. let nextIndex = index + 1;
  99. if (nextIndex > 3) {
  100. nextIndex = nextIndex - 4;
  101. }
  102. return nextIndex;
  103. }
  104. //不同图例,缩放比不一样
  105. getScale() {
  106. return 1;
  107. }
  108. getLengthWidth() {
  109. return {
  110. length: 100,
  111. width: 100,
  112. };
  113. }
  114. }