npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

canvas-to-svg

v1.0.3

Published

canvas-to-svg

Downloads

1,919

Readme

canvas-to-svg

This is port of https://github.com/gliffy/canvas2svg to Typescript. There were a number of open issues on the original and the most recent commits were in October 2017. I intend to fix issues to the best of my ability and strongly welcome contributions!

To Do

  • Finish moving tests over and delete old tests folder
  • Clean up all any types

Description

This library turns your Canvas into SVG using javascript. In other words, this library lets you build an SVG document using the canvas api. Why use it?

  • You have a canvas drawing you want to persist as an SVG file.
  • You like exporting things.
  • Because you didn't want to transform your custom file format to SVG.

Original demo

http://gliffy.github.io/canvas2svg/

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. Yay!

Usage

import canvasToSvg from "canvas-to-svg";

//Create a new mock canvas context. Pass in your desired width and height for your svg document.
const ctx = new canvasToSvg(500, 500);

//draw your canvas like you would normally
ctx.fillStyle = "red";
ctx.fillRect(100, 100, 100, 100);
//etc...

//serialize your SVG
const mySerializedSVG = ctx.getSerializedSvg(); //true here, if you need to convert named to numbered entities.

//If you really need to you can access the shadow inline SVG created by calling:
const svg = ctx.getSvg();

Tests

To run tests:

yarn
yarn test

Using with node.js

You can use canvas2svg with node.js using jsdom with node-canvas. To do this first create a new document object, and then create a new instance of C2S based on that document:

const canvas = require("canvas"),
  jsdom = require("jsdom"),
  C2S = require("canvas2svg").default; // <-- Note .default

const document = jsdom.jsdom();
const ctx = new C2S({ document: document });

// ... drawing code goes here ...

Misc

Some canvas 2d context methods are not implemented yet.

@tone-row/cytoscape-svg

My goals in fixing some of the issues with the original package are specific to using it with cytoscape.js. For that reason there is a copy of https://github.com/kinimesi/cytoscape-svg in the /cytoscape-svg directory, which simply uses canvas-to-svg (this package) instead of canvas2svg.

If the main cytoscape-svg package ever begins using this library instead, I will definitely delete that directory and switch back to using that package (https://github.com/kinimesi/cytoscape-svg), but for now this version is published under the scoped name @tone-row/cytoscape-svg.

Releasing

To release a new version:

  • Run gulp bump to update the version number
  • Add a new entry to the Updates table
  • git commit -am v1.0.xx
  • git push
  • npm publish

License

This library is licensed under the MIT license.