Socket
Socket
Sign inDemoInstall

canvg

Package Overview
Dependencies
78
Maintainers
3
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    canvg

Javascript SVG parser and renderer on Canvas


Version published
Weekly downloads
1.5M
decreased by-0.63%
Maintainers
3
Install size
8.82 MB
Created
Weekly downloads
 

Package description

What is canvg?

The canvg npm package is a versatile library that allows users to parse and render SVG (Scalable Vector Graphics) to a Canvas element in a browser or on the server-side using Node.js. It is particularly useful for converting SVGs into Canvas for manipulation, rendering, or exporting purposes.

What are canvg's main functionalities?

Rendering SVG to Canvas

This code sample demonstrates how to load an SVG file, render it onto a Canvas, and then save the Canvas output as a PNG file. This is useful for server-side generation of images from SVG files.

const canvg = require('canvg');
const Canvas = require('canvas');
const fs = require('fs');

const canvas = Canvas.createCanvas(800, 600);
const ctx = canvas.getContext('2d');
const svg = fs.readFileSync('path/to/your/svgfile.svg', 'utf-8');
canvg(ctx, svg);

const out = fs.createWriteStream('path/to/output.png');
const stream = canvas.createPNGStream();
stream.pipe(out);

Manipulating SVG before rendering

This example shows how to manipulate SVG data by changing its color before rendering it to a Canvas. This is useful for dynamically altering the appearance of SVGs based on runtime conditions.

const canvg = require('canvg');
const Canvas = require('canvas');
const fs = require('fs');

const canvas = Canvas.createCanvas(800, 600);
const ctx = canvas.getContext('2d');
let svg = fs.readFileSync('path/to/your/svgfile.svg', 'utf-8');

// Modify SVG data
svg = svg.replace('fill:#000000', 'fill:#123456');

// Render modified SVG to canvas
canvg(ctx, svg);

Other packages similar to canvg

Readme

Source

Build Status npm

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!

Introduction

canvg is a SVG parser and renderer. It takes a URL to a SVG file or the text of an SVG file, parses it in JavaScript, and renders the result on a Canvas element. The rendering speed of the examples is about as fast as native SVG.

What's implemented?

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.

Potential uses

  • Allows for inline embedding of SVG through JavaScript (w/o having to request another file or break validation)
  • 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)

Example Demonstration

hosted

jsfiddle playground

Locally, you can run npm start and view the examples at http://localhost:3123/examples/index.htm

Building

npm run build then look in the dist folder

Testing

npm run test

npm run generate-expected foo.svg to create the expected png for a given svg in the svgs folder

Usage on the server

npm install canvg

Usage on the Browser

Include the following files in your page:

<!-- Required to convert named colors to RGB -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/canvg/1.4/rgbcolor.min.js"></script>
<!-- Optional if you want blur -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/stackblur-canvas/1.4.1/stackblur.min.js"></script>
<!-- Main canvg code -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/canvg/dist/browser/canvg.min.js"></script>

Put a canvas on your page

<canvas id="canvas" width="1000px" height="600px"></canvas>

Example canvg calls:

<script type="text/javascript">
window.onload = function() {
  //load '../path/to/your.svg' in the canvas with id = 'canvas'
  canvg('canvas', '../path/to/your.svg')

  //load a svg snippet in the canvas with id = 'drawingArea'
  canvg(document.getElementById('drawingArea'), '<svg>...</svg>')

  //ignore mouse events and animation
  canvg('canvas', 'file.svg', { ignoreMouse: true, ignoreAnimation: true })
}
</script>

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.getElementById('canvas');
var ctx = c.getContext('2d');
ctx.drawSvg(SVG_XML_OR_PATH_TO_SVG, dx, dy, dw, dh);

Keywords

FAQs

Last updated on 09 Sep 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc