svgcanvas/README.md

99 lines
2.3 KiB
Markdown
Raw Permalink Normal View History

2021-06-05 14:44:05 +08:00
# SVGCanvas
2025-03-09 10:10:02 +02:00
Draw on SVG using Canvas's 2D Context API.
A fork of [aha's fork](https://github.com/aha-app/svgcanvas) of [gliffy's canvas2svg](https://github.com/gliffy/canvas2svg).
2021-06-05 14:44:05 +08:00
## Demo
2021-06-10 20:39:09 +08:00
https://zenozeng.github.io/svgcanvas/test/
2014-01-07 14:39:29 -08:00
2021-06-05 14:44:05 +08:00
## How it works
2021-06-13 23:50:26 +08:00
2022-03-09 23:07:08 +08:00
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.
2014-01-07 14:39:29 -08:00
2021-06-05 14:44:05 +08:00
## Usage
2014-01-07 14:39:29 -08:00
```javascript
2022-03-09 23:07:08 +08:00
import { Context } from "svgcanvas";
2021-06-13 23:50:26 +08:00
const ctx = new Context(500, 500);
2014-01-07 14:39:29 -08:00
// draw your canvas like you would normally
ctx.fillStyle = "red";
ctx.fillRect(100, 100, 100, 100);
2014-01-07 14:39:29 -08:00
2021-06-13 23:50:26 +08:00
// serialize your SVG
2022-03-09 23:07:08 +08:00
const mySerializedSVG = ctx.getSerializedSvg();
2014-01-07 14:39:29 -08:00
```
Wrapping canvas elements:
```javascript
2022-03-09 23:07:08 +08:00
import { Context, Element } from "svgcanvas";
const canvas = document.createElement("canvas");
const context2D = canvas.getContext("2d");
// more options to pass into constructor:
const options = {
2022-03-09 23:07:08 +08:00
height: 2000, // falsy values get converted to 500
width: 0 / 0, // falsy values get converted to 500
ctx: context2D, // existing Context2D to wrap around
enableMirroring: false, // whether canvas mirroring (get image data) is enabled (defaults to false)
document: undefined, // overrides default document object
};
// Creates a mock canvas context (mocks `context2D` above)
const ctx = new Context(options);
// draw your canvas like you would normally
ctx.fillStyle = "red";
ctx.fillRect(100, 100, 100, 100);
ctx.getSerializedSvg(); // returns the serialized SVG
ctx.getSvg(); // returns the inline svg element
// Creates a mock canvas element (mocks `canvas` above)
const dom = new Element(options);
dom.ctx; // the internal context, via `new Context(options)`
dom.wrapper; // a div with the svg as a child
dom.svg; // the inline svg element
```
2022-08-22 16:53:26 -07:00
Working with paths directly:
```javascript
import { Context, Path2D } from "svgcanvas";
const ctx = new Context(500, 500);
// Create a path:
const path = new Path2D(ctx, "M 230 80 L 275 80 Z"); // or ctx.createPath("M 230 80 L 275 80 Z");
ctx.stroke(path);
// serialize your SVG
const mySerializedSVG = ctx.getSerializedSvg();
```
## Tests
https://zenozeng.github.io/p5.js-svg/test/
To run the testsuite:
```
npm run test
```
To debug tests in a browser:
```
open test/index.html
npm run watch
```
2021-06-05 14:44:05 +08:00
## License
2015-09-09 11:02:48 -07:00
2014-01-11 15:08:51 -08:00
This library is licensed under the MIT license.