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>
.
As with @antv/g
, there are two ways to use it.
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,});
<scriptsrc="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();
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,});
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>
The renderer has the following plug-ins built in.
<circle>
, <rect>
, etc.In addition to the built-in plug-ins, the following optional plug-ins are available.
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.
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.
document
created by JSDOM instead of window.document
in the browser environment, and the same for raf
.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 DOMconst renderer = new Renderer();const domInteractionPlugin = renderer.getPlugin('dom-interaction');renderer.unregisterPlugin(domInteractionPlugin);// create JSDOMconst 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 JSDOMrequestAnimationFrame: dom.window.requestAnimationFrame,cancelAnimationFrame: dom.window.cancelAnimationFrame,});// use G API constructing scene graphconst circle1 = new Circle({style: {cx: 10,cy: 10,r: 10,fill: 'red',},});canvas.appendChild(circle1);// serialize JSDOM to SVG stringxmlserializer.serializeToString(dom.window.document.getElementById('container').children[0],);