trackLayer.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { type CameraState } from '../camera/camera'
  2. import { calibratedLonLatToWorldTile } from '../../utils/projection'
  3. import { worldToScreen } from '../camera/camera'
  4. import { type MapLayer, type LayerRenderContext } from './mapLayer'
  5. import { type MapScene } from '../renderer/mapRenderer'
  6. export interface ScreenPoint {
  7. x: number
  8. y: number
  9. }
  10. function buildVectorCamera(scene: MapScene): CameraState {
  11. return {
  12. centerWorldX: scene.exactCenterWorldX,
  13. centerWorldY: scene.exactCenterWorldY,
  14. viewportWidth: scene.viewportWidth,
  15. viewportHeight: scene.viewportHeight,
  16. visibleColumns: scene.visibleColumns,
  17. rotationRad: scene.rotationRad,
  18. }
  19. }
  20. export class TrackLayer implements MapLayer {
  21. projectPoints(scene: MapScene): ScreenPoint[] {
  22. const camera = buildVectorCamera(scene)
  23. return scene.track.map((point) => {
  24. const worldPoint = calibratedLonLatToWorldTile(point, scene.zoom, scene.gpsCalibration, scene.gpsCalibrationOrigin)
  25. return worldToScreen(camera, worldPoint, false)
  26. })
  27. }
  28. draw(context: LayerRenderContext): void {
  29. const { ctx, scene } = context
  30. const points = this.projectPoints(scene)
  31. ctx.save()
  32. ctx.lineCap = 'round'
  33. ctx.lineJoin = 'round'
  34. ctx.strokeStyle = 'rgba(23, 109, 93, 0.96)'
  35. ctx.lineWidth = 6
  36. ctx.beginPath()
  37. points.forEach((screenPoint, index) => {
  38. if (index === 0) {
  39. ctx.moveTo(screenPoint.x, screenPoint.y)
  40. return
  41. }
  42. ctx.lineTo(screenPoint.x, screenPoint.y)
  43. })
  44. ctx.stroke()
  45. ctx.fillStyle = '#f7fbf2'
  46. ctx.strokeStyle = '#176d5d'
  47. ctx.lineWidth = 4
  48. points.forEach((screenPoint, index) => {
  49. ctx.beginPath()
  50. ctx.arc(screenPoint.x, screenPoint.y, 10, 0, Math.PI * 2)
  51. ctx.fill()
  52. ctx.stroke()
  53. ctx.fillStyle = '#176d5d'
  54. ctx.font = 'bold 14px sans-serif'
  55. ctx.textAlign = 'center'
  56. ctx.textBaseline = 'middle'
  57. ctx.fillText(String(index + 1), screenPoint.x, screenPoint.y)
  58. ctx.fillStyle = '#f7fbf2'
  59. })
  60. ctx.restore()
  61. }
  62. }