SVGCanvas
Draw on SVG using Canvas's 2D Context API. A maintained fork of
gliffy's canvas2svg.
Demo
https://zenozeng.github.io/svgcanvas/test/
How it works
We create a mock 2d canvas context. Use the canvas context like you would on a
normal canvas. As you call methods, we build up a scene graph in SVG.
Usage
import { Context } from "svgcanvas";
const ctx = new Context(500, 500);
ctx.fillStyle = "red";
ctx.fillRect(100, 100, 100, 100);
const mySerializedSVG = ctx.getSerializedSvg();
Wrapping canvas elements:
import { Context, Element } from "svgcanvas";
const canvas = document.createElement("canvas");
const context2D = canvas.getContext("2d");
const options = {
height: 2000,
width: 0 / 0,
ctx: context2D,
enableMirroring: false,
document: undefined,
};
const ctx = new Context(options);
ctx.fillStyle = "red";
ctx.fillRect(100, 100, 100, 100);
ctx.getSerializedSvg();
ctx.getSvg();
const dom = new Element(options);
dom.ctx;
dom.wrapper;
dom.svg;
Tests
https://zenozeng.github.io/p5.js-svg/test/
License
This library is licensed under the MIT license.