Skip to content

Commit

Permalink
Accelerate label drawing
Browse files Browse the repository at this point in the history
* Don't create text if the state is smaller than 3 pixels
* If the text is the same, don't make a new text, keep the old one
* If the label is empty, don't create a label
* Delete the bitmaps as they are cleared

Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
  • Loading branch information
MatthewKhouzam committed May 21, 2021
1 parent e0d3e69 commit ce74083
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions timeline-chart/src/components/time-graph-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@ export class TimeGraphStateComponent extends TimeGraphComponent {
}
const fontName = TimeGraphStateComponent.fontController.getFontName(this._options.color ? this._options.color : 0, this._options.height - 2) ||
TimeGraphStateComponent.fontController.getDefaultFontName();
const textObj = new PIXI.BitmapText(this._model.label, { fontName: fontName });
const textWidth = textObj.getLocalBounds().width;
const position = {
x: this._options.position.x + this._options.width < 0 ? this._options.position.x : Math.max(0, this._options.position.x),
y: this._options.position.y
}
const displayWidth = this._options.displayWidth ? this._options.displayWidth : 0;
const labelText = this._model.label;
const textPadding = 0.5;

if (displayWidth < 3){
this.clearLabel();
return;
}
const textObj = new PIXI.BitmapText(this._model.label, { fontName: fontName });
const textWidth = textObj.getLocalBounds().width;
let textObjX = position.x + textPadding;
const textObjY = position.y + textPadding;
let displayLabel = "";
Expand All @@ -81,14 +84,24 @@ export class TimeGraphStateComponent extends TimeGraphComponent {
displayLabel = partialLabel.concat("...");
}
}

const newTextObj = new PIXI.BitmapText(displayLabel, { fontName: fontName });
newTextObj.x = textObjX;
newTextObj.y = textObjY;
this.displayObject.addChild(newTextObj);
if (displayLabel === ""){
this.clearLabel();
return;
}
if (displayLabel === this._model.label) {
textObj.x = textObjX;
textObj.y = textObjY;
this.displayObject.addChild(textObj);
} else {
const newTextObj = new PIXI.BitmapText(displayLabel, { fontName: fontName });
newTextObj.x = textObjX;
newTextObj.y = textObjY;
this.displayObject.addChild(newTextObj);
}
}

clearLabel() {
this.displayObject.children.forEach(element => element.destroy());
this.displayObject.removeChildren();
}

Expand Down

0 comments on commit ce74083

Please sign in to comment.