@ -51,7 +51,7 @@ class Label {
this . baseSize = undefined ;
this . baseSize = undefined ;
this . fontOptions = { } ; // instance variable containing the *instance-local* font options
this . fontOptions = { } ; // instance variable containing the *instance-local* font options
this . setOptions ( options ) ;
this . setOptions ( options ) ;
this . size = { top : 0 , left : 0 , width : 0 , height : 0 , yLine : 0 } ; // could be cached
this . size = { top : 0 , left : 0 , width : 0 , height : 0 , yLine : 0 } ;
this . isEdgeLabel = edgelabel ;
this . isEdgeLabel = edgelabel ;
}
}
@ -439,42 +439,21 @@ class Label {
// update the size cache if required
// update the size cache if required
this . calculateLabelSize ( ctx , selected , hover , x , y , baseline ) ;
this . calculateLabelSize ( ctx , selected , hover , x , y , baseline ) ;
this . _drawBackground ( ctx , this . size . left , this . size . top ) ;
this . _drawBackground ( ctx ) ;
this . _drawText ( ctx , x , this . size . yLine , baseline , viewFontSize ) ;
this . _drawText ( ctx , x , this . size . yLine , baseline , viewFontSize ) ;
}
}
/ * *
/ * *
* Draws the label background
* Draws the label background
* @ param { CanvasRenderingContext2D } ctx
* @ param { CanvasRenderingContext2D } ctx
* @ param { number } x
* @ param { number } y
* @ private
* @ private
* /
* /
_drawBackground ( ctx , x , y ) {
_drawBackground ( ctx ) {
if ( this . fontOptions . background !== undefined && this . fontOptions . background !== "none" ) {
if ( this . fontOptions . background !== undefined && this . fontOptions . background !== "none" ) {
ctx . fillStyle = this . fontOptions . background ;
ctx . fillStyle = this . fontOptions . background ;
let lineMargin = 2 ;
if ( this . isEdgeLabel ) {
switch ( this . fontOptions . align ) {
case 'middle' :
ctx . fillRect ( - this . size . width * 0.5 , - this . size . height * 0.5 , this . size . width , this . size . height ) ;
break ;
case 'top' :
ctx . fillRect ( - this . size . width * 0.5 , - ( this . size . height + lineMargin ) , this . size . width , this . size . height ) ;
break ;
case 'bottom' :
ctx . fillRect ( - this . size . width * 0.5 , lineMargin , this . size . width , this . size . height ) ;
break ;
default :
ctx . fillRect ( x , y - 0.5 * lineMargin , this . size . width , this . size . height ) ;
break ;
}
} else {
ctx . fillRect ( x , y - 0.5 * lineMargin , this . size . width , this . size . height ) ;
}
let size = this . getSize ( ) ;
ctx . fillRect ( size . left , size . top , size . width , size . height ) ;
}
}
}
}
@ -489,7 +468,6 @@ class Label {
* @ private
* @ private
* /
* /
_drawText ( ctx , x , y , baseline = 'middle' , viewFontSize ) {
_drawText ( ctx , x , y , baseline = 'middle' , viewFontSize ) {
[ x , y ] = this . _setAlignment ( ctx , x , y , baseline ) ;
[ x , y ] = this . _setAlignment ( ctx , x , y , baseline ) ;
ctx . textAlign = 'left' ;
ctx . textAlign = 'left' ;
@ -608,6 +586,46 @@ class Label {
}
}
/ * *
* Get the current dimensions of the label
*
* @ return { rect }
* /
getSize ( ) {
let lineMargin = 2 ;
let x = this . size . left ; // default values which might be overridden below
let y = this . size . top - 0.5 * lineMargin ; // idem
if ( this . isEdgeLabel ) {
const x2 = - this . size . width * 0.5 ;
switch ( this . fontOptions . align ) {
case 'middle' :
x = x2 ;
y = - this . size . height * 0.5
break ;
case 'top' :
x = x2 ;
y = - ( this . size . height + lineMargin ) ;
break ;
case 'bottom' :
x = x2 ;
y = lineMargin ;
break ;
}
}
var ret = {
left : x ,
top : y ,
width : this . size . width ,
height : this . size . height ,
} ;
return ret ;
}
/ * *
/ * *
*
*
* @ param { CanvasRenderingContext2D } ctx
* @ param { CanvasRenderingContext2D } ctx
@ -744,6 +762,26 @@ class Label {
this . labelDirty = false ;
this . labelDirty = false ;
}
}
/ * *
* Check if this label is visible
*
* @ return { boolean } true if this label will be show , false otherwise
* /
visible ( ) {
if ( ( this . size . width === 0 || this . size . height === 0 )
|| this . elementOptions . label === undefined ) {
return false ; // nothing to display
}
let viewFontSize = this . fontOptions . size * this . body . view . scale ;
if ( viewFontSize < this . elementOptions . scaling . label . drawThreshold - 1 ) {
return false ; // Too small or too far away to show
}
return true ;
}
}
}
export default Label ;
export default Label ;