renderer
Reading needs 2 minutes
G6 uses Canvas as the default renderer, but also supports rendering with SVG and WebGL. To switch to the SVG or WebGL renderer, simply pass the renderer
parameter during initialization.
npm install @antv/g-svg
import { Renderer as SVGRenderer } from '@antv/g-svg';import { Graph } from '@antv/g6';const graph = new Graph({// ... other options// All canvases will use the SVG renderer hererenderer: () => new SVGRenderer(),});
npm install @antv/g-webgl
import { Renderer as WebGLRenderer } from '@antv/g-webgl';import { Graph } from '@antv/g6';const graph = new Graph({// ... other options// All canvases will use the WebGL renderer hererenderer: () => new WebGLRenderer(),});
G6 uses layered canvases for rendering, so renderer
is a callback function that takes the canvas type as a parameter and returns the renderer instance. If you want to use different renderers on different canvases, you can configure it like this:
import { Renderer as SVGRenderer } from '@antv/g-svg';import { Renderer as WebGLRenderer } from '@antv/g-webgl';const graph = new Graph({// ... other optionsrenderer: (layer) => {// The main canvas uses the WebGL renderer, and the other canvases use the SVG rendererif (layer === 'main') return new WebGLRenderer();return new SVGRenderer();},});
G6 does not provide a API to switch the renderer, but you can still update the renderer
option through the setOptions
method.
import { Renderer as SVGRenderer } from '@antv/g-svg';import { Renderer as WebGLRenderer } from '@antv/g-webgl';// Use the WebGL renderer by defaultconst graph = new Graph({// ... other optionsrenderer: () => new WebGLRenderer(),});await graph.render();// Switch to the SVG renderergraph.setOptions({renderer: () => new SVGRenderer(),});