Looking for Contributors
In an attempt to keep this repo more active and merge PRs and do releases, if you would like to be a contributor, please start a conversation with me at gabelerner at gmail. The prerequisite is to have a few PRs open to prove out an understanding of the code. Thanks!
The end goal is everything from the SVG spec. The majority of the rendering and animation is working. If you would like to see a feature implemented, don't hesitate to contact me or add it to the issues list.
- Allows for single SVG version across all browsers that support Canvas
- Allows for mobile devices supporting Canvas but not SVG to render SVG
- Allows for SVG -> Canvas -> png transition all on the client side (through toDataUrl)
Locally, you can run
npm start and view the examples at http://localhost:3123/examples/index.htm
npm run build then look in the
npm run test-noderuns tests on
npm run test-browserruns tests on
npm run generate-expected foo.svgto create the expected png for a given svg in the
Usage on the server
npm install canvg canvas@^2 jsdom@^13 xmldom@^0
The dependencies required on the server only are peers so must be installed alongside the canvg package.
npm install canvg@^1.5
Usage on the browser
For browser applications with a build process, canvg can be installed using
npm similar to use on the server. Note in this case the peer dependencies are not required so do not need to be installed.
Alternatively, canvg can be included directly into a webpage:
<!-- Required to convert named colors to RGB --><!-- Optional if you want blur --><!-- Main canvg code -->
Put a canvas on your page
Example canvg calls:
The third parameter is options:
- log: true => console.log information
- ignoreMouse: true => ignore mouse events
- ignoreAnimation: true => ignore animations
- ignoreDimensions: true => does not try to resize canvas
- ignoreClear: true => does not clear canvas
- offsetX: int => draws at a x offset
- offsetY: int => draws at a y offset
- scaleWidth: int => scales horizontally to width
- scaleHeight: int => scales vertically to height
- renderCallback: function => will call the function after the first render is completed
- forceRedraw: function => will call the function on every frame, if it returns true, will redraw
- useCORS: true => will attempt to use CORS on images to not taint canvas
You can call canvg without parameters to replace all svg images on a page. See the example.
There is also a built in extension method to the canvas context to draw svgs similar to the way drawImage works:
var c = document;var ctx = c;ctx;