MoveSVG.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import Constant from "../Constant";
  2. import { dataService } from "../Service/DataService";
  3. import { mathUtil } from "../Util/MathUtil";
  4. export default class MoveSVG {
  5. constructor() {}
  6. moveFullSVG(position, svgId) {
  7. let svg = dataService.getSVG(svgId);
  8. let dx = position.x - svg.center.x;
  9. let dy = position.y - svg.center.y;
  10. mathUtil.clonePoint(svg.center, position);
  11. //svg.setBoundingVertexs();
  12. svg.points[0].x += dx;
  13. svg.points[0].y += dy;
  14. svg.points[1].x += dx;
  15. svg.points[1].y += dy;
  16. svg.points[2].x += dx;
  17. svg.points[2].y += dy;
  18. svg.points[3].x += dx;
  19. svg.points[3].y += dy;
  20. }
  21. // movePoint(position, svgId, pointIndex) {
  22. // let svg = dataService.getSVG(svgId);
  23. // const rec = svg.getLengthWidth();
  24. // const scale = svg.getScale();
  25. // const side1 = Math.sqrt(
  26. // Math.pow((rec.width / 2) * scale, 2) +
  27. // Math.pow((rec.length / 2) * scale, 2)
  28. // );
  29. // svg.setBoundingVertexs2(position, pointIndex);
  30. // const side2 = mathUtil.getDistance(position, svg.center);
  31. // svg.scale = (side2 / side1) * scale;
  32. // }
  33. movePoint(newPos, svgId, pointIndex) {
  34. let svg = dataService.getSVG(svgId);
  35. const preIndex = svg.getPreIndex(pointIndex);
  36. const nextIndex = svg.getNextIndex(pointIndex);
  37. const nextIndex2 = svg.getNextIndex(nextIndex);
  38. const angle = mathUtil.Angle(
  39. svg.points[nextIndex],
  40. svg.points[pointIndex],
  41. newPos
  42. );
  43. const line1 = mathUtil.createLine1(
  44. svg.points[nextIndex2],
  45. svg.points[nextIndex]
  46. );
  47. const nextPoint = mathUtil.getJoinLinePoint(newPos, line1);
  48. const line2 = mathUtil.createLine1(
  49. svg.points[nextIndex2],
  50. svg.points[preIndex]
  51. );
  52. const prePoint = mathUtil.getJoinLinePoint(newPos, line2);
  53. if (
  54. nextPoint != null &&
  55. prePoint != null &&
  56. mathUtil.getDistance(newPos, nextPoint) > Constant.minAdsorbPix &&
  57. mathUtil.getDistance(newPos, prePoint) > Constant.minAdsorbPix &&
  58. mathUtil.getDistance(newPos, svg.points[nextIndex2]) >
  59. Constant.minAdsorbPix
  60. ) {
  61. svg.points[pointIndex] = newPos;
  62. if (!mathUtil.isClockwise(svg.points)) {
  63. svg.points[nextIndex] = nextPoint;
  64. svg.points[preIndex] = prePoint;
  65. } else {
  66. svg.points[nextIndex] = prePoint;
  67. svg.points[preIndex] = nextPoint;
  68. svg.resetPointsIndex();
  69. --pointIndex;
  70. if (pointIndex < 0) {
  71. pointIndex += 4;
  72. }
  73. return pointIndex;
  74. }
  75. }
  76. return pointIndex;
  77. }
  78. }
  79. const moveSVG = new MoveSVG();
  80. export { moveSVG };