Coordinate.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import Constant from "./Constant";
  2. import { dataService } from "./Service/DataService";
  3. const defaultZoom = 100;
  4. const defaultRes = 0.1;
  5. const minAdsorbPix = 15;
  6. export default class Coordinate {
  7. constructor() {
  8. this.center = null; // 世界坐标,中心点。默认是[0,0]
  9. this.defaultZoom = defaultZoom;
  10. this.zoom = defaultZoom; // 当前缩放比例,不会改变世界坐标系的坐标,只是改变渲染时转换的屏幕坐标
  11. this.res = defaultRes; //比例尺。实际尺寸与屏幕像素的比例,用于测量。与之前的绘制不同,目前存储的数据与像素的比例是1:1,只是最后测量值再除以这个比例
  12. this.ratio = window.devicePixelRatio || 1; //不同硬件设备,像素率不同
  13. this.width == null;
  14. this.height == null;
  15. }
  16. // 初始化
  17. init(canvas) {
  18. this.updateForCanvas(canvas);
  19. }
  20. setRes(res) {
  21. this.res = res;
  22. }
  23. getRes() {
  24. return this.res;
  25. }
  26. // 世界坐标转换成屏幕坐标
  27. getScreenXY(point) {
  28. if (this.width == null || this.height == null) {
  29. return null;
  30. }
  31. let pt = {
  32. x: point.x,
  33. y: point.y,
  34. };
  35. let x = this.width / 2 + ((pt.x - this.center.x) * this.zoom) / defaultZoom;
  36. let y =
  37. this.height / 2 - ((pt.y - this.center.y) * this.zoom) / defaultZoom;
  38. // x *= this.ratio;
  39. // y *= this.ratio;
  40. x = (0.5 + x) << 0;
  41. y = (0.5 + y) << 0;
  42. return {
  43. x: Math.floor(x),
  44. y: Math.floor(y),
  45. };
  46. }
  47. // 屏幕坐标转换成世界坐标
  48. getXYFromScreen(screenPoint) {
  49. const point = {};
  50. point.x =
  51. ((screenPoint.x * this.ratio - this.width / 2) * defaultZoom) /
  52. this.zoom +
  53. this.center.x;
  54. point.y =
  55. ((this.height / 2 - screenPoint.y * this.ratio) * defaultZoom) /
  56. this.zoom +
  57. this.center.y;
  58. return point;
  59. }
  60. getXYFromScreenNotRatio(screenPoint) {
  61. const point = {};
  62. point.x =
  63. ((screenPoint.x - this.width / 2) * defaultZoom) / this.zoom +
  64. this.center.x;
  65. point.y =
  66. ((this.height / 2 - screenPoint.y) * defaultZoom) / this.zoom +
  67. this.center.y;
  68. return point;
  69. }
  70. getPointForRevRotate(point, angle) {}
  71. getVectorForRotate(point, angle) {}
  72. updateCenterForRotate(angle) {}
  73. setCenter(canvas) {
  74. if (canvas) {
  75. this.center = {
  76. x: 0,
  77. y: 0,
  78. };
  79. }
  80. }
  81. updateForCanvas(canvas) {
  82. if (canvas) {
  83. console.log(canvas.offsetWidth, canvas.offsetHeight);
  84. canvas.width = canvas.offsetWidth * this.ratio;
  85. canvas.height = canvas.offsetHeight * this.ratio;
  86. this.width = canvas.width;
  87. this.height = canvas.height;
  88. }
  89. this.setCenter(canvas);
  90. Constant.minAdsorbPix = minAdsorbPix * this.ratio;
  91. console.log(
  92. "updateForCanvas" +
  93. canvas.offsetWidth +
  94. "," +
  95. canvas.offsetHeight +
  96. "," +
  97. this.ratio +
  98. JSON.stringify(this.center)
  99. );
  100. }
  101. updateZoom(screenPosition, zoom) {
  102. let position = this.getXYFromScreen(screenPosition);
  103. this.zoom = zoom;
  104. this.center.x =
  105. position.x -
  106. ((screenPosition.x * this.ratio - this.width / 2) * defaultZoom) /
  107. this.zoom;
  108. this.center.y =
  109. position.y -
  110. ((this.height / 2 - screenPosition.y * this.ratio) * defaultZoom) /
  111. this.zoom;
  112. }
  113. // moveTo(zoom, screenPosition) {
  114. // const position = this.getXYFromScreen(screenPosition);
  115. // this.zoom = zoom;
  116. // const dx = (screenPosition.x * defaultZoom) / this.zoom - this.width / 2;
  117. // const dy = this.height / 2 - (screenPosition.y * defaultZoom) / this.zoom;
  118. // this.center.x = position.x - dx;
  119. // this.center.y = position.y - dy;
  120. // }
  121. getPixelRatio(context) {
  122. var backingStore =
  123. context.backingStorePixelRatio ||
  124. context.webkitBackingStorePixelRatio ||
  125. context.mozBackingStorePixelRatio ||
  126. context.msBackingStorePixelRatio ||
  127. context.oBackingStorePixelRatio ||
  128. context.backingStorePixelRatio ||
  129. 1;
  130. return (window.devicePixelRatio || 1) / backingStore;
  131. }
  132. reSet(canvas) {
  133. this.zoom = defaultZoom;
  134. this.center = {
  135. x: 0,
  136. y: 0,
  137. };
  138. dataService.initGrid();
  139. }
  140. updateForRotate(angle) {}
  141. clear() {
  142. this.center = null;
  143. this.zoom = defaultZoom;
  144. this.res = defaultRes;
  145. this.ratio = defaultRes;
  146. }
  147. }
  148. const coordinate = new Coordinate();
  149. window.coordinate = coordinate;
  150. export { coordinate };