-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: support dynamic switch renderer (#6062)
* feat(runtime): graph emit renderer change event * refactor: refactor canvas and support switch renderer * fix(3d): remove light on destroy plugin * refactor(3d): clear cache when renderer change * test: add switch renderer demo --------- Co-authored-by: antv <[email protected]>
- Loading branch information
Showing
18 changed files
with
277 additions
and
56 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
60 changes: 60 additions & 0 deletions
60
packages/g6-extension-3d/__tests__/demos/switch-renderer.ts
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,60 @@ | ||
import { Renderer as CanvasRenderer } from '@antv/g-canvas'; | ||
import type { NodeData } from '@antv/g6'; | ||
import { ExtensionCategory, Graph, register } from '@antv/g6'; | ||
import { Light, Sphere, renderer } from '../../src'; | ||
|
||
export const switchRenderer: TestCase = async (context) => { | ||
register(ExtensionCategory.PLUGIN, '3d-light', Light); | ||
register(ExtensionCategory.NODE, 'sphere', Sphere); | ||
|
||
const nodes: NodeData[] = [{ id: '1' }, { id: '2' }]; | ||
|
||
const graph = new Graph({ | ||
...context, | ||
data: { | ||
nodes, | ||
}, | ||
layout: { | ||
type: 'grid', | ||
}, | ||
}); | ||
|
||
await graph.render(); | ||
|
||
switchRenderer.form = (panel) => { | ||
panel.add({ renderer: '2d' }, 'renderer', ['2d', '3d']).onChange((name: string) => { | ||
if (name === '2d') { | ||
graph.setOptions({ | ||
renderer: () => new CanvasRenderer(), | ||
node: { | ||
type: 'circle', | ||
}, | ||
plugins: [], | ||
}); | ||
} else { | ||
graph.setOptions({ | ||
renderer, | ||
node: { | ||
type: 'sphere', | ||
style: { | ||
materialType: 'phong', | ||
}, | ||
}, | ||
plugins: [ | ||
{ | ||
type: '3d-light', | ||
directional: { | ||
direction: [0, 0, 1], | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
graph.draw(); | ||
}); | ||
return []; | ||
}; | ||
|
||
return graph; | ||
}; |
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 |
---|---|---|
|
@@ -15,4 +15,8 @@ export class TupleMap<K1, K2, V> { | |
} | ||
this.map.get(key1)!.set(key2, value); | ||
} | ||
|
||
clear() { | ||
this.map.clear(); | ||
} | ||
} |
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,33 @@ | ||
import { Graph } from '@/src'; | ||
import { Renderer as CanvasRenderer } from '@antv/g-canvas'; | ||
import { Renderer as SVGRenderer } from '@antv/g-svg'; | ||
import { Renderer as WebGLRenderer } from '@antv/g-webgl'; | ||
|
||
export const canvasSwitchRenderer: TestCase = async (context) => { | ||
const graph = new Graph({ | ||
...context, | ||
data: { | ||
nodes: Array.from({ length: 10 }).map((_, i) => ({ id: `node-${i}` })), | ||
}, | ||
layout: { | ||
type: 'grid', | ||
}, | ||
}); | ||
|
||
await graph.render(); | ||
|
||
canvasSwitchRenderer.form = (panel) => { | ||
panel.add({ renderer: 'canvas' }, 'renderer', ['canvas', 'svg', 'webgl']).onChange((name: string) => { | ||
graph.setOptions({ | ||
renderer: () => { | ||
if (name === 'svg') return new SVGRenderer(); | ||
if (name === 'webgl') return new WebGLRenderer(); | ||
return new CanvasRenderer(); | ||
}, | ||
}); | ||
}); | ||
return []; | ||
}; | ||
|
||
return graph; | ||
}; |
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
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.