Coordinate.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. if (res) {
  22. this.res = res;
  23. } else {
  24. this.res = defaultRes;
  25. }
  26. }
  27. getRes() {
  28. return this.res;
  29. }
  30. // 世界坐标转换成屏幕坐标
  31. getScreenXY(point) {
  32. if (this.width == null || this.height == null) {
  33. return null;
  34. }
  35. let pt = {
  36. x: point.x,
  37. y: point.y,
  38. };
  39. let x = this.width / 2 + ((pt.x - this.center.x) * this.zoom) / defaultZoom;
  40. let y =
  41. this.height / 2 - ((pt.y - this.center.y) * this.zoom) / defaultZoom;
  42. // x *= this.ratio;
  43. // y *= this.ratio;
  44. x = (0.5 + x) << 0;
  45. y = (0.5 + y) << 0;
  46. return {
  47. x: Math.floor(x),
  48. y: Math.floor(y),
  49. };
  50. }
  51. // 屏幕坐标转换成世界坐标
  52. getXYFromScreen(screenPoint) {
  53. const point = {};
  54. point.x =
  55. ((screenPoint.x * this.ratio - this.width / 2) * defaultZoom) /
  56. this.zoom +
  57. this.center.x;
  58. point.y =
  59. ((this.height / 2 - screenPoint.y * this.ratio) * defaultZoom) /
  60. this.zoom +
  61. this.center.y;
  62. return point;
  63. }
  64. getXYFromScreenNotRatio(screenPoint) {
  65. const point = {};
  66. point.x =
  67. ((screenPoint.x - this.width / 2) * defaultZoom) / this.zoom +
  68. this.center.x;
  69. point.y =
  70. ((this.height / 2 - screenPoint.y) * defaultZoom) / this.zoom +
  71. this.center.y;
  72. return point;
  73. }
  74. getPointForRevRotate(point, angle) {}
  75. getVectorForRotate(point, angle) {}
  76. updateCenterForRotate(angle) {}
  77. setCenter(canvas) {
  78. if (canvas) {
  79. this.center = {
  80. x: 0,
  81. y: 0,
  82. };
  83. }
  84. }
  85. updateForCanvas(canvas) {
  86. if (canvas) {
  87. console.log(canvas.offsetWidth, canvas.offsetHeight);
  88. canvas.width = canvas.offsetWidth * this.ratio;
  89. canvas.height = canvas.offsetHeight * this.ratio;
  90. this.width = canvas.width;
  91. this.height = canvas.height;
  92. }
  93. this.zoom = defaultZoom;
  94. this.setCenter(canvas);
  95. Constant.minAdsorbPix = minAdsorbPix * this.ratio;
  96. }
  97. updateZoom(screenPosition, zoom) {
  98. let position = this.getXYFromScreen(screenPosition);
  99. this.zoom = zoom;
  100. this.center.x =
  101. position.x -
  102. ((screenPosition.x * this.ratio - this.width / 2) * defaultZoom) /
  103. this.zoom;
  104. this.center.y =
  105. position.y -
  106. ((this.height / 2 - screenPosition.y * this.ratio) * defaultZoom) /
  107. this.zoom;
  108. }
  109. // moveTo(zoom, screenPosition) {
  110. // const position = this.getXYFromScreen(screenPosition);
  111. // this.zoom = zoom;
  112. // const dx = (screenPosition.x * defaultZoom) / this.zoom - this.width / 2;
  113. // const dy = this.height / 2 - (screenPosition.y * defaultZoom) / this.zoom;
  114. // this.center.x = position.x - dx;
  115. // this.center.y = position.y - dy;
  116. // }
  117. getPixelRatio(context) {
  118. var backingStore =
  119. context.backingStorePixelRatio ||
  120. context.webkitBackingStorePixelRatio ||
  121. context.mozBackingStorePixelRatio ||
  122. context.msBackingStorePixelRatio ||
  123. context.oBackingStorePixelRatio ||
  124. context.backingStorePixelRatio ||
  125. 1;
  126. return (window.devicePixelRatio || 1) / backingStore;
  127. }
  128. reSet(canvas) {
  129. this.zoom = defaultZoom;
  130. this.center = {
  131. x: 0,
  132. y: 0,
  133. };
  134. dataService.initGrid();
  135. }
  136. updateForRotate(angle) {}
  137. clear() {
  138. this.center = null;
  139. this.zoom = defaultZoom;
  140. this.res = defaultRes;
  141. this.ratio = defaultRes;
  142. }
  143. }
  144. const coordinate = new Coordinate();
  145. window.coordinate = coordinate;
  146. export { coordinate };