Socket
Socket
Sign inDemoInstall

canvg

Package Overview
Dependencies
4
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    canvg

A port of canvg, which parses svg input and renders the result to a canvas.


Version published
Weekly downloads
1.5M
decreased by-0.63%
Maintainers
1
Install size
98.2 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

node-canvg

A port of canvg, which pareses svg input and renders the result to a canvas. http://code.google.com/p/canvg/

Usage

var canvg = require("canvg");
var Canvas = require("canvas");

var canvas = new Canvas();

canvg(canvas, '<svg>...</svg>');

/* and so on */

Todo

The canvg code is pretty much untouched. A lot of browser-specific code has to be removed or rewritten.

Keywords

FAQs

Last updated on 10 Dec 2014

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