logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.26
  • Canvas
    • Introduction
    • Options
    • Built-in objects
    • Coordinate system
    • Scenegraph & Lifecycle
    • Event
    • OffscreenCanvas & Server-side Rendering
    • CustomElementRegistry
    • Frequently Asked Questions
  • Renderer
    • Introduction
    • Canvas Renderer
    • Canvaskit Renderer
    • SVG Renderer
    • WebGL Renderer
    • WebGPU Renderer
    • Custom Renderer
  • Camera
    • Introduction
    • Camera Parameters
    • Camera action
    • Camera animation
  • Event
    • Introduction
    • Event Object
    • Gesture & Drag'n'Drop
    • Frequently Asked Questions
  • Animation
    • Web Animations API
    • Lottie
  • Basic Shapes
    • Basic Concepts
    • DisplayObject
    • Group
    • Text
    • Circle
    • Ellipse
    • Rect
    • Image
    • Line
    • Polygon
    • Polyline
    • Path
    • HTML
  • Style System
    • Introduction
    • CSS Typed OM
    • Inheritance
    • CSS Properties & Values API
    • CSS Layout API
    • Pattern
    • Gradient
  • 3D
    • 材质
    • 几何
    • Mesh
    • 光源
    • 雾
    • 交互
  • Built-in Objects
    • EventTarget
    • Node
    • Element
    • Document
    • MutationObserver
    • Utils
  • GPGPU
    • Introduction
    • Programming Model
    • Kernel API
    • Principles of classical GPGPU implementation
    • webgpu-graph
  • Declarative programming
    • 使用 Web Components
  • Devtools
    • G 开发者工具
    • 内置的渲染统计信息
    • 第三方开发调试工具

SVG Renderer

Previous
Canvaskit Renderer
Next
WebGL Renderer

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Use SVG to draw 2D graphics. A <svg> element is created in the container.

SVG has the unique advantage of relying directly on the browser's ability to render text. It is also possible to embed HTML fragments via <foreignObject>.

Usage

As with @antv/g, there are two ways to use it.

NPM Module

After installing @antv/g-svg you can get the renderer from.

import { Canvas } from '@antv/g';
import { Renderer } from '@antv/g-svg';
const svgRenderer = new Renderer();
const canvas = new Canvas({
container: 'container',
width: 600,
height: 500,
renderer: svgRenderer,
});

CDN

<script
src="https://unpkg.com/@antv/g-svg/dist/index.umd.min.js"
type="application/javascript">

The renderer is available from the G.SVG namespace under.

const svgRenderer = new window.G.SVG.Renderer();

Initial Configuration

When creating a renderer, you can pass in some initialization configuration items, such as.

import { Renderer } from '@antv/g-svg';
const renderer = new Renderer({
outputSVGElementId: false,
});

outputSVGElementId

The renderer adds the id attribute when generating the SVGElement, which is used to pick up the decision to counter-check the element when interacting. However, in scenarios like server-side rendering, where there is no interaction and no need for generation, this can be turned off with this configuration item.

<!-- Enable by default -->
<g id="g_svg_g_450" fill="none"></g>
<!-- Disable -->
<g fill="none"></g>

Built-in plug-ins

The renderer has the following plug-ins built in.

  • g-plugin-svg-renderer Draw shapes using SVG elements, such as <circle>, <rect>, etc.
  • g-plugin-svg-picker Pick up graphics based on elementFromPoint DOM API
  • g-plugin-dom-interaction DOM API-based event binding

Optional plug-ins

In addition to the built-in plug-ins, the following optional plug-ins are available.

Hand-drawn style rendering

Use the SVG version of rough.js for hand-drawn style rendering.

We provide g-plugin-rough-svg-renderer plugin, which will replace [g-plugin-svg-renderer](/en/plugins/svg- renderer) for some 2D graphics.

The effect of example is as follows.

Server-side rendering

The renderer relies on the rendering capabilities of the SVG DOM API and is not limited to the browser side, so server-side rendering is also possible using JSDOM.

In our integration test, we will work with JSDOM on the Node side jsdom) with node-canvas to render the result image and compare it with the benchmark image. Other server-side rendering scenes can also follow the following steps.

  1. Use unregisterPlugin to unregister the DOM API-related plugins built into g-svg, such as the event binding g-plugin-dom-interaction.
  2. Create a canvas container using JSDOM.
  3. Use the container from the previous step to create the canvas, and pass in the document created by JSDOM instead of window.document in the browser environment, and the same for raf.
  4. Normal use of g-svg renderer to create scenes via G's API.
  5. Use xmlserializer to serialize JSDOM to a string and save it as an SVG image.

https://github.com/antvis/g/blob/next/integration/__node__tests__/svg/circle.spec.js

const fs = require('fs');
const { JSDOM } = require('jsdom');
const xmlserializer = require('xmlserializer');
const { Circle, Canvas } = require('@antv/g');
const { Renderer } = require('@antv/g-svg');
// create a renderer, unregister plugin relative to DOM
const renderer = new Renderer();
const domInteractionPlugin = renderer.getPlugin('dom-interaction');
renderer.unregisterPlugin(domInteractionPlugin);
// create JSDOM
const dom = new JSDOM(`
<div id="container">
</div>
`);
const SIZE = 200;
const canvas = new Canvas({
container: 'container',
width: SIZE,
height: SIZE,
renderer,
document: dom.window.document, // use document created by JSDOM
requestAnimationFrame: dom.window.requestAnimationFrame,
cancelAnimationFrame: dom.window.cancelAnimationFrame,
});
// use G API constructing scene graph
const circle1 = new Circle({
style: {
cx: 10,
cy: 10,
r: 10,
fill: 'red',
},
});
canvas.appendChild(circle1);
// serialize JSDOM to SVG string
xmlserializer.serializeToString(
dom.window.document.getElementById('container').children[0],
);