@@ -319,7 +319,7 @@ class Label extends Control {
319319 textHeight : number ;
320320 fontFamily : string ;
321321 xAlign : "start" | "center" ;
322- yAlign : CanvasTextBaseline ;
322+ yAlign : "middle" | "top" ;
323323 constructor ( x : number , y : number , text : string , xAlign : "start" | "center" ) {
324324 super ( x , y ) ; //Inherit from Control
325325 this . text = text ;
@@ -330,6 +330,9 @@ class Label extends Control {
330330 this . xAlign = xAlign ; //Horizontal Aligning
331331 this . yAlign = "middle" ; //Vertical Aligning
332332 }
333+ /**
334+ * Call before Lable.draw
335+ */
333336 updateContext ( ) {
334337 const ctx = ( window as any ) . globals . canvas . getContext ( '2d' ) as CanvasRenderingContext2D ;
335338
@@ -342,20 +345,36 @@ class Label extends Control {
342345 ctx . font = "bold " + this . textHeight + "px " + this . fontFamily ;
343346 }
344347 }
348+ /**
349+ * Call after Label.updateContext
350+ */
345351 draw ( ) {
346352 const ctx = ( window as any ) . globals . canvas . getContext ( '2d' ) as CanvasRenderingContext2D ;
347353
348354 if ( this . visible ) {
349- /*
350- var something;
351- if (this.xAlign) {
352- something = this.x - c.measureText(this.text).width / 2;
355+ // support multi-line text
356+ const lines = this . text . split ( / \r ? \n / ) ;
357+ console . log ( "🚀 ~ lines:" , lines ) ;
358+ const lineHeight = this . textHeight * 1.2 ;
359+
360+ // Use 'middle' baseline for consistent vertical centering per line
361+ if ( this . yAlign === "middle" ) ctx . textBaseline = "middle"
362+ else if ( this . yAlign === "top" ) ctx . textBaseline = "top" ;
363+
364+ // Compute starting Y so multi-line block is centered when yAlign === "middle"
365+ let startY : number ;
366+ if ( this . yAlign === "middle" ) {
367+ startY = this . y - ( ( lines . length - 1 ) / 2 ) * lineHeight ;
353368 } else {
354- something = this.x;
355- }*/ //Substituido por c.textAlign
356-
357- ctx . strokeText ( this . text , this . x , this . y ) ;
358- ctx . fillText ( this . text , this . x , this . y ) ;
369+ // fallback: align first line at this.y
370+ startY = this . y ;
371+ }
372+
373+ for ( let i = 0 ; i < lines . length ; i ++ ) {
374+ const lineY = startY + i * lineHeight ;
375+ ctx . strokeText ( lines [ i ] , this . x , lineY ) ;
376+ ctx . fillText ( lines [ i ] , this . x , lineY ) ;
377+ }
359378 }
360379 } ;
361380 updateLanguage ( ) {
0 commit comments