The helper function canvas-to-svg.js
wraps a given render function (or renderer object) so that you can use Canvas2D context methods as usual, but upon single frame export (with Cmd/Ctrl + S) it will produce both a PNG and SVG file.
This uses canvas2svg which is not a perfect solution, as the Canvas2D API was never designed to be translated to SVG. Its best to stick with simple shape and path operations.
Full instructions: first install the canvas-sketch CLI if you haven't already:
npm install canvas-sketch-cli -g
Then copy the sketch.js
and canvas-to-svg.js
into a new folder, say the folder is called mysketch
. Install dependencies:
# move into it
cd mysketch
# make a new package.json
npm init -y
# install deps
npm install canvas-sketch canvas-sketch-util canvas2svg --save
Then run the sketch:
canvas-sketch sketch.js --open
It looks like this:
const canvasSketch = require('canvas-sketch');
const svg = require('./canvas-to-svg.js');
const settings = {
// ... your settings ...
};
const sketch = () => {
return svg(props => {
const { context, width, height } = props;
// ... draw your art ...
});
};
canvasSketch(sketch, settings);