-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize canvas renderer performance
- Loading branch information
Showing
20 changed files
with
233 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Rect, Group, CanvasEvent } from '@antv/g'; | ||
import type { Canvas } from '@antv/g'; | ||
|
||
export async function attrUpdate(context: { canvas: Canvas }) { | ||
const { canvas } = context; | ||
console.log(canvas); | ||
|
||
await canvas.ready; | ||
|
||
const { width, height } = canvas.getConfig(); | ||
const count = 2e4; | ||
const root = new Group(); | ||
const rects = []; | ||
|
||
const perfStore: { [k: string]: { count: number; time: number } } = { | ||
update: { count: 0, time: 0 }, | ||
setAttribute: { count: 0, time: 0 }, | ||
}; | ||
|
||
function updatePerf(key: string, time: number) { | ||
Check notice Code scanning / CodeQL Unused variable, import, function or class Note test
Unused function updatePerf.
|
||
perfStore[key].count++; | ||
perfStore[key].time += time; | ||
console.log( | ||
`average ${key} time: `, | ||
perfStore[key].time / perfStore[key].count, | ||
); | ||
} | ||
|
||
function update() { | ||
// const startTime = performance.now(); | ||
// console.time('update'); | ||
|
||
const rectsToRemove = []; | ||
|
||
// const startTime0 = performance.now(); | ||
// console.time('setAttribute'); | ||
for (let i = 0; i < count; i++) { | ||
const rect = rects[i]; | ||
rect.x -= rect.speed; | ||
(rect.el as Rect).setAttribute('x', rect.x); | ||
if (rect.x + rect.size < 0) rectsToRemove.push(i); | ||
} | ||
// console.timeEnd('setAttribute'); | ||
// updatePerf('setAttribute', performance.now() - startTime0); | ||
|
||
rectsToRemove.forEach((i) => { | ||
rects[i].x = width + rects[i].size / 2; | ||
}); | ||
|
||
// console.timeEnd('update'); | ||
// updatePerf('update', performance.now() - startTime); | ||
} | ||
|
||
function render() { | ||
for (let i = 0; i < count; i++) { | ||
const x = Math.random() * width; | ||
const y = Math.random() * height; | ||
const size = 10 + Math.random() * 40; | ||
const speed = 1 + Math.random(); | ||
|
||
const rect = new Rect({ | ||
style: { | ||
x, | ||
y, | ||
width: size, | ||
height: size, | ||
fill: 'white', | ||
stroke: 'black', | ||
}, | ||
}); | ||
root.appendChild(rect); | ||
rects[i] = { x, y, size, speed, el: rect }; | ||
} | ||
} | ||
|
||
render(); | ||
canvas.addEventListener(CanvasEvent.BEFORE_RENDER, () => update()); | ||
|
||
canvas.appendChild(root); | ||
|
||
canvas.addEventListener( | ||
'rerender', | ||
() => { | ||
// console.timeEnd('render'); | ||
}, | ||
{ once: true }, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { circles } from './circles'; | ||
export { rects } from './rect'; | ||
export { image } from './image'; | ||
export { attrUpdate } from './attr-update'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,8 @@ | |
</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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.