Coordinate.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import Constant from './Constant'
  2. // const pad = {
  3. // top: 40,
  4. // bottom: 40,
  5. // left: 40,
  6. // right: 40,
  7. // }
  8. export default class Coordinate {
  9. constructor() {
  10. this.center = {
  11. x:0,
  12. y:0
  13. } // 世界坐标,中心点
  14. this.zoom = 100 // 当前缩放比例,不会改变世界坐标系的坐标,只是改变渲染时转换的屏幕坐标
  15. this.res = 100 //默认情况,100个像素对应1米
  16. this.ratio = 1
  17. this.initRes = null
  18. this.initWidth = null
  19. this.initHeight = null
  20. }
  21. // 初始化
  22. init(canvas) {
  23. this.updateForCanvas(canvas)
  24. }
  25. setRes(res) {
  26. this.res = res
  27. Constant.minRealDis = Constant.minPixLen / this.res
  28. Constant.minAdsorb = Constant.minPixLen / this.res
  29. }
  30. setCenter(point) {
  31. this.center = {
  32. x: point.x,
  33. y: point.y,
  34. }
  35. }
  36. setRatio(ratio) {
  37. this.ratio = ratio;
  38. }
  39. // 世界坐标转换成屏幕坐标
  40. getScreenXY(point) {
  41. if (this.width == null || this.height == null) {
  42. return null
  43. }
  44. let pt = {
  45. x: point.x,
  46. y: point.y,
  47. }
  48. let x = this.width / 2 + ((pt.x - this.center.x) * this.res * this.zoom) / 100
  49. let y = this.height / 2 - ((pt.y - this.center.y) * this.res * this.zoom) / 100
  50. x = (0.5 + x) << 0
  51. y = (0.5 + y) << 0
  52. return {
  53. x: Math.floor(x) * this.ratio,
  54. y: Math.floor(y) * this.ratio,
  55. }
  56. }
  57. // 屏幕坐标转换成世界坐标
  58. getXYFromScreen(screenPoint) {
  59. const point = {}
  60. point.x = (((screenPoint.x - this.width / 2) / this.res) * 100) / this.zoom + this.center.x
  61. point.y = (((this.height / 2 - screenPoint.y) / this.res) * 100) / this.zoom + this.center.y
  62. return point
  63. }
  64. updateForCanvas(canvas) {
  65. if (canvas) {
  66. this.width = canvas.clientWidth
  67. this.height = canvas.clientHeight
  68. canvas.width = canvas.clientWidth
  69. canvas.height = canvas.clientHeight
  70. }
  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 * 100) / this.zoom - this.width / 2) / this.res
  79. const dy = (this.height / 2 - (screenPosition.y * 100) / this.zoom) / this.res
  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() {
  95. this.center = {
  96. x: 0,
  97. y: 0,
  98. }
  99. this.res = 100
  100. this.zoom = 100
  101. }
  102. clear() {
  103. this.center = {
  104. x: 0,
  105. y: 0,
  106. }
  107. this.zoom = 100
  108. this.res = 100
  109. this.ratio = 1
  110. }
  111. }
  112. const coordinate = new Coordinate()
  113. export { coordinate }