Skip to content

Commit

Permalink
perf: avoid unnecessary transformation matrix calculations and canvas…
Browse files Browse the repository at this point in the history
… context matrix updates
  • Loading branch information
wang1212 committed Nov 1, 2024
1 parent 7c6f3d3 commit 8157ad0
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 64 deletions.
21 changes: 19 additions & 2 deletions __tests__/demos/perf/attr-update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as lil from 'lil-gui';
import { Rect, Group, CanvasEvent } from '@antv/g';
import type { Canvas } from '@antv/g';

export async function attrUpdate(context: { canvas: Canvas }) {
const { canvas } = context;
export async function attrUpdate(context: { canvas: Canvas; gui: lil.GUI }) {
const { canvas, gui } = context;
console.log(canvas);

await canvas.ready;
Expand Down Expand Up @@ -85,4 +86,20 @@ export async function attrUpdate(context: { canvas: Canvas }) {
},
{ once: true },
);

// GUI
canvas.getConfig().renderer.getConfig().enableRenderingOptimization = true;

gui
.add(
{
enableRenderingOptimization: canvas.getConfig().renderer.getConfig()
.enableRenderingOptimization,
},
'enableRenderingOptimization',
)
.onChange((result) => {
canvas.getConfig().renderer.getConfig().enableRenderingOptimization =
result;
});
}
2 changes: 0 additions & 2 deletions __tests__/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
</style>
</head>
<body>
<script src="https://unpkg.com/[email protected]/dist/fpsmeter.min.js"></script>

<div id="app"></div>
<script type="module" src="./main.ts"></script>
</body>
Expand Down
33 changes: 11 additions & 22 deletions __tests__/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import Stats from 'stats.js';
import Stats from 'stats.js';
import * as lil from 'lil-gui';
import '@antv/g-camera-api';
import { Canvas, CanvasEvent, runtime } from '@antv/g';
Expand Down Expand Up @@ -59,7 +59,7 @@ const renderOptions = (keyword = '') => {

// Select for chart.
const selectChart = document.createElement('select') as HTMLSelectElement;
selectChart.style.margin = '1em 1em 1em 120px';
selectChart.style.margin = '1em 1em 1em 96px';
renderOptions();
selectChart.onchange = () => {
const { value } = selectChart;
Expand Down Expand Up @@ -227,35 +227,24 @@ function createSpecRender(object) {
window.__g_instances__ = [canvas];

// stats
// const stats = new Stats();
// stats.showPanel(0);
// const $stats = stats.dom;
// $stats.style.position = 'absolute';
// $stats.style.left = '4px';
// $stats.style.top = '4px';
// app.appendChild($stats);
const stats = new Stats();
stats.showPanel(0);
const $stats = stats.dom;
$stats.style.position = 'fixed';
$stats.style.left = '2px';
$stats.style.top = '2px';
// document.body.appendChild($stats);

// GUI
const gui = new lil.GUI({ autoPlace: false });
$div.appendChild(gui.domElement);

// @see https://github.com/Darsain/fpsmeter/wiki/Options
const fpsMeter = new window.FPSMeter({
theme: 'light',
heat: 1,
graph: 1,
});

await generate({ canvas, renderer, container: $div, gui, fpsMeter });
await generate({ canvas, renderer, container: $div, gui });

canvas.addEventListener(CanvasEvent.AFTER_RENDER, () => {
fpsMeter.tick();
stats.update();
});

// canvas.addEventListener(CanvasEvent.AFTER_RENDER, () => {
// stats.update();
// });

if (
selectRenderer.value === 'canvas' &&
renderer.getConfig().enableDirtyRectangleRendering &&
Expand Down
1 change: 1 addition & 0 deletions packages/g-lite/src/AbstractRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class AbstractRenderer implements IRenderer {
enableDirtyRectangleRendering: true,
enableDirtyRectangleRenderingDebug: false,
enableSizeAttenuation: true,
enableRenderingOptimization: false,
...config,
};
}
Expand Down
3 changes: 0 additions & 3 deletions packages/g-lite/src/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ export class Canvas extends EventTarget implements ICanvas {
*/
isMouseEvent: (event: InteractivePointerEvent) => event is MouseEvent;

/**
* double click speed (ms), default is 200ms
*/
dblClickSpeed?: CanvasConfig['dblClickSpeed'];

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/g-lite/src/services/RenderingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,13 @@ export class RenderingService {
this.inited = true;
callback();
} else {
this.hooks.initAsync.promise().then(() => {
this.inited = true;
callback();
});
this.hooks.initAsync
.promise()
.then(() => {
this.inited = true;
callback();
})
.catch((err) => {});
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/g-lite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ export interface RendererConfig {
*/
enableSizeAttenuation: boolean;

/**
* Enable rendering optimization
*
* @default false
*/
enableRenderingOptimization: boolean;

// plugins:
}

Expand Down Expand Up @@ -545,6 +552,10 @@ export interface CanvasConfig {
supportsTouchEvents?: boolean;
isTouchEvent?: (event: InteractivePointerEvent) => event is TouchEvent;
isMouseEvent?: (event: InteractivePointerEvent) => event is MouseEvent;

/**
* double click speed (ms), default is 200ms
*/
dblClickSpeed?: number;

/**
Expand Down
Loading

0 comments on commit 8157ad0

Please sign in to comment.