SVG.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. import VectorCategory from "../enum/VectorCategory";
  7. import RoadStructure from "../enum/RoadStructure";
  8. import SVGType from "../enum/SVGType";
  9. //const sideWidth = 10;
  10. export default class SVG extends Geometry {
  11. constructor(center, type, vectorId) {
  12. super();
  13. this.category = null;
  14. this.center = center;
  15. this.points = null; //包裹的矩形的四个顶点,顺时针。0-1是width,1-3是height
  16. this.angle = 0; //逆时针为负,顺时针为正。单位是:°
  17. this.type = type;
  18. this.geoType = VectorType.SVG;
  19. this.scaleLength = this.getScale();
  20. this.scaleWidth = this.getScale();
  21. this.setBoundingVertexs();
  22. this.controlAnglePoint = this.setControlAnglePoint();
  23. this.setId(vectorId);
  24. this.checkCategory(type);
  25. }
  26. // createDefaultPoints() {
  27. // this.points = [];
  28. // this.points[0] = {
  29. // x: this.center.x - sideWidth / 2,
  30. // y: this.center.y + sideWidth / 2,
  31. // };
  32. // this.points[1] = {
  33. // x: this.center.x + sideWidth / 2,
  34. // y: this.center.y + sideWidth / 2,
  35. // };
  36. // this.points[2] = {
  37. // x: this.center.x + sideWidth / 2,
  38. // y: this.center.y - sideWidth / 2,
  39. // };
  40. // this.points[3] = {
  41. // x: this.center.x - sideWidth / 2,
  42. // y: this.center.y - sideWidth / 2,
  43. // };
  44. // }
  45. checkCategory(type) {
  46. // this.setCategory()
  47. let category = "";
  48. if (RoadStructure[type]) {
  49. category = VectorCategory.SVG["RoadStructure"];
  50. } else if (SVGType[type]) {
  51. category = VectorCategory.SVG["SVG"];
  52. }
  53. this.setCategory(category);
  54. }
  55. setCategory(value) {
  56. this.category = value;
  57. }
  58. setBoundingVertexs() {
  59. this.points = [];
  60. const rec = this.getLengthWidth();
  61. const length = this.scaleLength * rec.length;
  62. const width = this.scaleWidth * rec.width;
  63. const minX = this.center.x - length / 2;
  64. const minY = this.center.y - width / 2;
  65. const maxX = this.center.x + length / 2;
  66. const maxY = this.center.y + width / 2;
  67. const point1 = this.rotatePoint(
  68. {
  69. x: minX,
  70. y: maxY,
  71. },
  72. this.center,
  73. this.angle
  74. );
  75. const point2 = this.rotatePoint(
  76. {
  77. x: maxX,
  78. y: maxY,
  79. },
  80. this.center,
  81. this.angle
  82. );
  83. const point3 = this.rotatePoint(
  84. {
  85. x: maxX,
  86. y: minY,
  87. },
  88. this.center,
  89. this.angle
  90. );
  91. const point4 = this.rotatePoint(
  92. {
  93. x: minX,
  94. y: minY,
  95. },
  96. this.center,
  97. this.angle
  98. );
  99. this.points.push(point1);
  100. this.points.push(point2);
  101. this.points.push(point3);
  102. this.points.push(point4);
  103. }
  104. setPoints(points) {
  105. this.points = points;
  106. }
  107. setControlAnglePoint() {
  108. const len =
  109. mathUtil.getDistance(this.points[0], this.points[2]) / 2 +
  110. 20 * coordinate.ratio;
  111. const v = {
  112. x: Math.cos(this.angle + Math.PI / 2),
  113. y: Math.sin(this.angle + Math.PI / 2),
  114. };
  115. console.log(this.angle);
  116. const p = {
  117. x: this.center.x + v.x * len,
  118. y: this.center.y + v.y * len,
  119. };
  120. return p;
  121. }
  122. // setLengthScale(scale1) {
  123. // this.scaleLength = scale1;
  124. // }
  125. // setWidthScale(scale2) {
  126. // this.scaleWidth = scale2;
  127. // }
  128. //不同图例,缩放比不一样
  129. getScale() {
  130. return 1;
  131. }
  132. setBoundingVertexs2(position, pointIndex) {
  133. if (mathUtil.getDistance(position, this.center) < Constant.minAdsorbPix) {
  134. return;
  135. }
  136. mathUtil.clonePoint(this.points[pointIndex], position);
  137. // 【注意】angle 逆时针为正,顺时针为负
  138. let nextIndex = this.getNextIndex(pointIndex);
  139. this.points[nextIndex] = this.rotatePoint(position, this.center, 90);
  140. nextIndex = this.getNextIndex(nextIndex);
  141. this.points[nextIndex] = this.rotatePoint(position, this.center, 180);
  142. nextIndex = this.getNextIndex(nextIndex);
  143. this.points[nextIndex] = this.rotatePoint(position, this.center, 270);
  144. }
  145. getPreIndex(index) {
  146. let preIndex = index - 1;
  147. if (preIndex < 0) {
  148. preIndex = preIndex + 4;
  149. }
  150. return preIndex;
  151. }
  152. getNextIndex(index) {
  153. let nextIndex = index + 1;
  154. if (nextIndex > 3) {
  155. nextIndex = nextIndex - 4;
  156. }
  157. return nextIndex;
  158. }
  159. getLengthWidth() {
  160. return {
  161. length: 100,
  162. width: 100,
  163. };
  164. }
  165. //变更顺序
  166. resetPointsIndex() {
  167. const point0 = JSON.parse(JSON.stringify(this.points[1]));
  168. const point1 = JSON.parse(JSON.stringify(this.points[2]));
  169. const point2 = JSON.parse(JSON.stringify(this.points[3]));
  170. const point3 = JSON.parse(JSON.stringify(this.points[0]));
  171. this.points[0] = point0;
  172. this.points[1] = point1;
  173. this.points[2] = point2;
  174. this.points[3] = point3;
  175. }
  176. }