Skip to content

Commit

Permalink
fix: fix background drawer optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Keelaro1 committed Dec 5, 2023
1 parent 2da588a commit 7a5d568
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/chart/components/chart/chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class ChartComponent extends ChartBaseElement {
//#region data series drawers
this.registerDefaultDataSeriesDrawers();
//#endregion
this.backgroundDrawer = new BackgroundDrawer(backgroundCanvasModel, this.config, this.canvasBoundsContainer);
this.backgroundDrawer = new BackgroundDrawer(backgroundCanvasModel, this.config);
drawingManager.addDrawer(this.backgroundDrawer, 'MAIN_BACKGROUND');
cursorHandler.setCursorForCanvasEl(CanvasElement.PANE_UUID(CHART_UUID), config.components.chart.cursor);

Expand Down
14 changes: 4 additions & 10 deletions src/chart/drawers/chart-background.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@ import { ChartAreaTheme, FullChartConfig } from '../chart.config';
import { getDPR } from '../utils/device/device-pixel-ratio.utils';
import { deepEqual } from '../utils/object.utils';
import { floor } from '../utils/math.utils';
import { CanvasBoundsContainer } from '../canvas/canvas-bounds-container';

export class BackgroundDrawer implements Drawer {
constructor(
private canvasModel: CanvasModel,
private config: FullChartConfig,
private canvasBoundsContainer: CanvasBoundsContainer,
) {}
constructor(private canvasModel: CanvasModel, private config: FullChartConfig) {}

// we need to save previous state to avoid unnecessary redraws
private prevState: Partial<ChartAreaTheme> = {};
private prevCanvasLocation = {};

draw(): void {
if (
deepEqual(this.config.colors.chartAreaTheme, this.prevState) &&
this.prevCanvasLocation === this.canvasBoundsContainer.canvasOnPageLocation
this.canvasModel.height === this.canvasModel.prevHeight &&
this.canvasModel.width === this.canvasModel.prevWidth
) {
return;
}
Expand All @@ -40,9 +35,8 @@ export class BackgroundDrawer implements Drawer {
ctx.fillStyle = this.config.colors.chartAreaTheme.backgroundColor;
}
ctx.fillRect(0, 0, this.canvasModel.width, this.canvasModel.height);
// save prev state
// save prev color state
this.prevState = { ...this.config.colors.chartAreaTheme };
this.prevCanvasLocation = this.canvasBoundsContainer.canvasOnPageLocation;
}

getCanvasIds(): Array<string> {
Expand Down

0 comments on commit 7a5d568

Please sign in to comment.