courseLabelRenderer.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { type MapScene } from './mapRenderer'
  2. import { CourseLayer } from '../layer/courseLayer'
  3. const EARTH_CIRCUMFERENCE_METERS = 40075016.686
  4. const LABEL_FONT_SIZE_RATIO = 1.08
  5. const LABEL_OFFSET_X_RATIO = 1.18
  6. const LABEL_OFFSET_Y_RATIO = -0.68
  7. const SCORE_LABEL_FONT_SIZE_RATIO = 0.7
  8. const SCORE_LABEL_OFFSET_Y_RATIO = 0.06
  9. const DEFAULT_LABEL_COLOR = 'rgba(204, 0, 107, 0.98)'
  10. const ACTIVE_LABEL_COLOR = 'rgba(255, 219, 54, 0.98)'
  11. const MULTI_ACTIVE_LABEL_COLOR = 'rgba(255, 202, 72, 0.96)'
  12. const FOCUSED_LABEL_COLOR = 'rgba(255, 252, 255, 0.98)'
  13. const COMPLETED_LABEL_COLOR = 'rgba(126, 131, 138, 0.94)'
  14. const SCORE_LABEL_COLOR = 'rgba(255, 252, 242, 0.98)'
  15. const SCORE_COMPLETED_LABEL_COLOR = 'rgba(214, 218, 224, 0.94)'
  16. export class CourseLabelRenderer {
  17. courseLayer: CourseLayer
  18. canvas: any
  19. ctx: any
  20. dpr: number
  21. width: number
  22. height: number
  23. constructor(courseLayer: CourseLayer) {
  24. this.courseLayer = courseLayer
  25. this.canvas = null
  26. this.ctx = null
  27. this.dpr = 1
  28. this.width = 0
  29. this.height = 0
  30. }
  31. attachCanvas(canvasNode: any, width: number, height: number, dpr: number): void {
  32. this.canvas = canvasNode
  33. this.ctx = canvasNode.getContext('2d')
  34. this.dpr = dpr || 1
  35. this.width = width
  36. this.height = height
  37. canvasNode.width = Math.max(1, Math.floor(width * this.dpr))
  38. canvasNode.height = Math.max(1, Math.floor(height * this.dpr))
  39. }
  40. destroy(): void {
  41. this.ctx = null
  42. this.canvas = null
  43. this.width = 0
  44. this.height = 0
  45. }
  46. render(scene: MapScene): void {
  47. if (!this.ctx || !this.canvas) {
  48. return
  49. }
  50. const course = this.courseLayer.projectCourse(scene)
  51. const ctx = this.ctx
  52. this.clearCanvas(ctx)
  53. if (!course || !course.controls.length || !scene.revealFullCourse) {
  54. return
  55. }
  56. const controlRadiusMeters = scene.cpRadiusMeters > 0 ? scene.cpRadiusMeters : 5
  57. const fontSizePx = this.getMetric(scene, controlRadiusMeters * LABEL_FONT_SIZE_RATIO)
  58. const scoreFontSizePx = this.getMetric(scene, controlRadiusMeters * SCORE_LABEL_FONT_SIZE_RATIO)
  59. const scoreOffsetY = this.getMetric(scene, controlRadiusMeters * SCORE_LABEL_OFFSET_Y_RATIO)
  60. const offsetX = this.getMetric(scene, controlRadiusMeters * LABEL_OFFSET_X_RATIO)
  61. const offsetY = this.getMetric(scene, controlRadiusMeters * LABEL_OFFSET_Y_RATIO)
  62. this.applyPreviewTransform(ctx, scene)
  63. ctx.save()
  64. if (scene.controlVisualMode === 'multi-target') {
  65. ctx.textAlign = 'center'
  66. ctx.textBaseline = 'middle'
  67. ctx.font = `900 ${scoreFontSizePx}px sans-serif`
  68. for (const control of course.controls) {
  69. ctx.save()
  70. ctx.fillStyle = this.getScoreLabelColor(scene, control.sequence)
  71. ctx.translate(control.point.x, control.point.y)
  72. ctx.rotate(scene.rotationRad)
  73. ctx.fillText(String(control.sequence), 0, scoreOffsetY)
  74. ctx.restore()
  75. }
  76. } else {
  77. ctx.textAlign = 'left'
  78. ctx.textBaseline = 'middle'
  79. ctx.font = `700 ${fontSizePx}px sans-serif`
  80. for (const control of course.controls) {
  81. ctx.save()
  82. ctx.fillStyle = this.getLabelColor(scene, control.sequence)
  83. ctx.translate(control.point.x, control.point.y)
  84. ctx.rotate(scene.rotationRad)
  85. ctx.fillText(String(control.sequence), offsetX, offsetY)
  86. ctx.restore()
  87. }
  88. }
  89. ctx.restore()
  90. }
  91. getLabelColor(scene: MapScene, sequence: number): string {
  92. if (scene.focusedControlSequences.includes(sequence)) {
  93. return FOCUSED_LABEL_COLOR
  94. }
  95. if (scene.activeControlSequences.includes(sequence)) {
  96. return scene.controlVisualMode === 'multi-target' ? MULTI_ACTIVE_LABEL_COLOR : ACTIVE_LABEL_COLOR
  97. }
  98. if (scene.completedControlSequences.includes(sequence)) {
  99. return COMPLETED_LABEL_COLOR
  100. }
  101. return DEFAULT_LABEL_COLOR
  102. }
  103. getScoreLabelColor(scene: MapScene, sequence: number): string {
  104. if (scene.focusedControlSequences.includes(sequence)) {
  105. return FOCUSED_LABEL_COLOR
  106. }
  107. if (scene.completedControlSequences.includes(sequence)) {
  108. return SCORE_COMPLETED_LABEL_COLOR
  109. }
  110. return SCORE_LABEL_COLOR
  111. }
  112. clearCanvas(ctx: any): void {
  113. ctx.setTransform(1, 0, 0, 1, 0, 0)
  114. ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
  115. }
  116. applyPreviewTransform(ctx: any, scene: MapScene): void {
  117. const previewScale = scene.previewScale || 1
  118. const previewOriginX = scene.previewOriginX || scene.viewportWidth / 2
  119. const previewOriginY = scene.previewOriginY || scene.viewportHeight / 2
  120. const translateX = (previewOriginX - previewOriginX * previewScale) * this.dpr
  121. const translateY = (previewOriginY - previewOriginY * previewScale) * this.dpr
  122. ctx.setTransform(
  123. this.dpr * previewScale,
  124. 0,
  125. 0,
  126. this.dpr * previewScale,
  127. translateX,
  128. translateY,
  129. )
  130. }
  131. getMetric(scene: MapScene, meters: number): number {
  132. return meters * this.getPixelsPerMeter(scene)
  133. }
  134. getPixelsPerMeter(scene: MapScene): number {
  135. const tileSizePx = scene.viewportWidth / scene.visibleColumns
  136. const centerLat = this.worldTileYToLat(scene.exactCenterWorldY, scene.zoom)
  137. const metersPerTile = Math.cos(centerLat * Math.PI / 180) * EARTH_CIRCUMFERENCE_METERS / Math.pow(2, scene.zoom)
  138. if (!tileSizePx || !metersPerTile) {
  139. return 0
  140. }
  141. return tileSizePx / metersPerTile
  142. }
  143. worldTileYToLat(worldY: number, zoom: number): number {
  144. const scale = Math.pow(2, zoom)
  145. const latRad = Math.atan(Math.sinh(Math.PI * (1 - 2 * worldY / scale)))
  146. return latRad * 180 / Math.PI
  147. }
  148. }