@@ -29,17 +29,27 @@ function getUsernameTexture (username: string, { fontFamily = 'sans-serif' }: an
2929 const padding = 5
3030 ctx . font = `${ fontSize } px ${ fontFamily } `
3131
32- const textWidth = ctx . measureText ( username ) . width + padding * 2
32+ const lines = String ( username ) . split ( '\n' )
33+
34+ let textWidth = 0
35+ for ( const line of lines ) {
36+ const width = ctx . measureText ( line ) . width + padding * 2
37+ if ( width > textWidth ) textWidth = width
38+ }
3339
3440 canvas . width = textWidth
35- canvas . height = fontSize + padding * 2
41+ canvas . height = ( fontSize + padding * 2 ) * lines . length
3642
3743 ctx . fillStyle = 'rgba(0, 0, 0, 0.3)'
3844 ctx . fillRect ( 0 , 0 , canvas . width , canvas . height )
3945
4046 ctx . font = `${ fontSize } px ${ fontFamily } `
4147 ctx . fillStyle = 'white'
42- ctx . fillText ( username , padding , fontSize )
48+ let i = 0
49+ for ( const line of lines ) {
50+ i ++
51+ ctx . fillText ( line , padding + ( textWidth - ctx . measureText ( line ) . width ) / 2 , fontSize * i )
52+ }
4353
4454 return canvas
4555}
@@ -454,6 +464,7 @@ export class Entities extends EventEmitter {
454464 // ---
455465 // not player
456466 const displayText = entity . metadata ?. [ 3 ] && this . parseEntityLabel ( entity . metadata [ 2 ] )
467+ || entity . metadata ?. [ 23 ] && this . parseEntityLabel ( entity . metadata [ 23 ] ) // text displays
457468 if ( entity . name !== 'player' && displayText ) {
458469 addNametag ( { ...entity , username : displayText } , this . entitiesOptions , this . entities [ entity . id ] . children . find ( c => c . name === 'mesh' ) )
459470 }
0 commit comments