@@ -12,10 +12,23 @@ import { useIntervalPositions } from './useIntervalPositions';
1212const BAR_FILL = chroma ( BROWN_10 ) . alpha ( 0.3 ) . css ( ) ;
1313const BAR_STROKE = chroma ( BROWN_70 ) . alpha ( 0.3 ) . css ( ) ;
1414const BAR_HEIGHT = 24 ;
15- const BAR_PADDING_H = 8 ;
15+ const BAR_PADDING_LEFT = 8 ;
16+ const BAR_PADDING_RIGHT = 4 ;
1617const BAR_BOTTOM_OFFSET = 17 ;
1718const BAR_RADIUS = 3 ;
18- const BAR_TEXT_GAP = 4 ; // gap between flex spans
19+ const ELLIPSIS = '...' ;
20+
21+ const truncateText = ( ctx : CanvasRenderingContext2D , text : string , maxWidth : number ) => {
22+ if ( ctx . measureText ( text ) . width <= maxWidth ) return text ;
23+
24+ const ellipsisWidth = ctx . measureText ( ELLIPSIS ) . width ;
25+ let truncatedText = '' ;
26+ for ( const character of text ) {
27+ if ( ctx . measureText ( truncatedText + character ) . width + ellipsisWidth > maxWidth ) break ;
28+ truncatedText += character ;
29+ }
30+ return `${ truncatedText } ${ ELLIPSIS } ` ;
31+ } ;
1932
2033export type TrainInfoBarProps = {
2134 /** Length (in ms) of the train's time window. */
@@ -57,15 +70,18 @@ export const TrainInfoBar = ({ duration, origin = 0, name, intervalLabel }: Trai
5770 // Clip to bar interior to replicate overflow: hidden.
5871 ctx . save ( ) ;
5972 ctx . beginPath ( ) ;
60- ctx . rect ( left + BAR_PADDING_H , yTop , width - BAR_PADDING_H * 2 , BAR_HEIGHT ) ;
73+ ctx . rect (
74+ left + BAR_PADDING_LEFT ,
75+ yTop ,
76+ width - BAR_PADDING_LEFT - BAR_PADDING_RIGHT ,
77+ BAR_HEIGHT
78+ ) ;
6179 ctx . clip ( ) ;
6280 ctx . fillStyle = '#000000' ;
6381 ctx . textAlign = 'left' ;
64- let textX = left + BAR_PADDING_H ;
65- for ( const part of [ name , '|' , intervalLabel ] ) {
66- ctx . fillText ( part , textX , textY ) ;
67- textX += ctx . measureText ( part ) . width + BAR_TEXT_GAP ;
68- }
82+ const text = `${ name } | ${ intervalLabel } ` ;
83+ const textWidth = width - BAR_PADDING_LEFT - BAR_PADDING_RIGHT ;
84+ ctx . fillText ( truncateText ( ctx , text , textWidth ) , left + BAR_PADDING_LEFT , textY ) ;
6985 ctx . restore ( ) ;
7086 }
7187 } ,
0 commit comments