Coordinate.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { dataService } from "./Service/DataService.js";
  2. import { floorplanData } from "./VectorData.js";
  3. import { mathUtil } from "./Util/MathUtil.js/index.js";
  4. import Constant from "./Constant";
  5. const defaultZoom = 100;
  6. export default class Coordinate {
  7. constructor() {
  8. this.center = null; // 世界坐标,中心点
  9. this.zoom = defaultZoom; // 当前缩放比例,不会改变世界坐标系的坐标,只是改变渲染时转换的屏幕坐标
  10. this.res = 80; //比例尺。实际尺寸与屏幕像素的比例,用于测量。与之前的绘制不同,目前存储的数据与像素的比例是1:1,只是最后测量值再除以这个比例
  11. this.ratio = 1; //不同硬件设备,像素率不同
  12. this.width == null;
  13. this.height == null;
  14. }
  15. // 初始化
  16. init(canvas) {
  17. this.updateForCanvas(canvas);
  18. }
  19. setRes(res) {
  20. this.res = res;
  21. }
  22. // 世界坐标转换成屏幕坐标
  23. getScreenXY(point) {
  24. if (this.width == null || this.height == null) {
  25. return null;
  26. }
  27. let pt = {
  28. x: point.x,
  29. y: point.y,
  30. };
  31. let x = this.width / 2 + ((pt.x - this.center.x) * this.zoom) / defaultZoom;
  32. let y =
  33. this.height / 2 - ((pt.y - this.center.y) * this.zoom) / defaultZoom;
  34. x *= this.ratio;
  35. y *= this.ratio;
  36. x = (0.5 + x) << 0;
  37. y = (0.5 + y) << 0;
  38. return {
  39. x: Math.floor(x),
  40. y: Math.floor(y),
  41. };
  42. }
  43. // 屏幕坐标转换成世界坐标
  44. getXYFromScreen(screenPoint) {
  45. const point = {};
  46. point.x =
  47. ((screenPoint.x - this.width / 2) * defaultZoom) / this.zoom +
  48. this.center.x;
  49. point.y =
  50. ((this.height / 2 - screenPoint.y) * defaultZoom) / this.zoom +
  51. this.center.y;
  52. return point;
  53. }
  54. getPointForRevRotate(point, angle) {}
  55. getVectorForRotate(point, angle) {}
  56. updateCenterForRotate(angle) {}
  57. setCenter(canvas) {
  58. if (canvas) {
  59. this.center = {
  60. x: canvas.clientWidth / 2,
  61. y: canvas.clientHeight / 2,
  62. };
  63. }
  64. }
  65. updateForCanvas(canvas) {
  66. if (canvas) {
  67. this.width = canvas.clientWidth;
  68. this.height = canvas.clientHeight;
  69. }
  70. this.setCenter(canvas);
  71. }
  72. updateZoom(zoom) {
  73. this.zoom = zoom;
  74. }
  75. moveTo(zoom, screenPosition) {
  76. const position = this.getXYFromScreen(screenPosition);
  77. this.zoom = zoom;
  78. const dx = (screenPosition.x * defaultZoom) / this.zoom - this.width / 2;
  79. const dy = this.height / 2 - (screenPosition.y * defaultZoom) / this.zoom;
  80. this.center.x = position.x - dx;
  81. this.center.y = position.y - dy;
  82. }
  83. getPixelRatio(context) {
  84. var backingStore =
  85. context.backingStorePixelRatio ||
  86. context.webkitBackingStorePixelRatio ||
  87. context.mozBackingStorePixelRatio ||
  88. context.msBackingStorePixelRatio ||
  89. context.oBackingStorePixelRatio ||
  90. context.backingStorePixelRatio ||
  91. 1;
  92. return (window.devicePixelRatio || 1) / backingStore;
  93. }
  94. reSet(canvas) {
  95. this.updateForCanvas(canvas);
  96. this.zoom = defaultZoom;
  97. }
  98. updateForRotate(angle) {}
  99. clear() {
  100. this.center = null;
  101. this.zoom = 100;
  102. this.res = 80;
  103. this.ratio = 1;
  104. }
  105. }
  106. const coordinate = new Coordinate();
  107. export { coordinate };