All Projects → canvg → Canvg

canvg / Canvg

Licence: mit
Javascript SVG parser and renderer on Canvas

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to Canvg

People You Should Follow On Codepen
People You Should Follow on CodePen
Stars: ✭ 542 (-81.71%)
Mutual labels:  hacktoberfest, svg, canvas
Create Content Loader
✏️ Tool to create your own react-content-loader easily.
Stars: ✭ 937 (-68.38%)
Mutual labels:  hacktoberfest, svg
Dracula
JavaScript layout and representation of connected graphs.
Stars: ✭ 767 (-74.11%)
Mutual labels:  hacktoberfest, svg
Binari
Interactive code editor with a live binary tree visual designed to teach new developers the fundamentals of dynamic programming.
Stars: ✭ 82 (-97.23%)
Mutual labels:  hacktoberfest, canvas
Canvasapi
Python API wrapper for Instructure's Canvas LMS. Easily manage courses, users, gradebooks, and more.
Stars: ✭ 306 (-89.67%)
Mutual labels:  hacktoberfest, canvas
Ngx Charts
📊 Declarative Charting Framework for Angular
Stars: ✭ 4,057 (+36.92%)
Mutual labels:  hacktoberfest, svg
Udoit
The Universal Design Online content Inspection Tool, or UDOIT (pronounced, “You Do It”) enables faculty to identify accessibility issues in Canvas by Instructure. It will scan a course, generate a report, and provide resources on how to address common accessibility issues.
Stars: ✭ 80 (-97.3%)
Mutual labels:  hacktoberfest, canvas
Rough
Create graphics with a hand-drawn, sketchy, appearance
Stars: ✭ 16,472 (+455.92%)
Mutual labels:  svg, canvas
React Content Loader
⚪ SVG-Powered component to easily create skeleton loadings.
Stars: ✭ 11,830 (+299.26%)
Mutual labels:  hacktoberfest, svg
Svg utils
Python tools to create and manipulate SVG files
Stars: ✭ 169 (-94.3%)
Mutual labels:  hacktoberfest, svg
Supertinyicons
Under 1KB each! Super Tiny Icons are miniscule SVG versions of your favourite website and app logos
Stars: ✭ 13,177 (+344.72%)
Mutual labels:  hacktoberfest, svg
Vpype
The Swiss-Army-knife command-line tool for plotter vector graphics.
Stars: ✭ 292 (-90.15%)
Mutual labels:  hacktoberfest, svg
Svgwave
🌊 SVG Wave is a tiny, free and beautiful SVG gradient waves generator for your UI or website desgin. It offers dead simple UI to customize, and style your waves based on your theme specifications.
Stars: ✭ 255 (-91.39%)
Mutual labels:  hacktoberfest, svg
Svg2pdf.js
A javascript-only SVG to PDF conversion utility that runs in the browser. Brought to you by yWorks - the diagramming experts
Stars: ✭ 231 (-92.2%)
Mutual labels:  hacktoberfest, svg
Three.js
JavaScript 3D Library.
Stars: ✭ 78,237 (+2540.47%)
Mutual labels:  svg, canvas
Diagram Js
A toolbox for displaying and modifying diagrams on the web.
Stars: ✭ 881 (-70.27%)
Mutual labels:  hacktoberfest, svg
Picasso.js
A charting library streamlined for building interactive visualizations for the Qlik product suites.
Stars: ✭ 175 (-94.09%)
Mutual labels:  svg, canvas
Picasso
Picasso is a high quality 2D vector graphic rendering library. It support path , matrix , gradient , pattern , image and truetype font.
Stars: ✭ 205 (-93.08%)
Mutual labels:  svg, canvas
Wavedrom
🌊 Digital timing diagram rendering engine
Stars: ✭ 1,668 (-43.71%)
Mutual labels:  hacktoberfest, svg
Shields
Concise, consistent, and legible badges in SVG and raster format
Stars: ✭ 15,716 (+430.41%)
Mutual labels:  hacktoberfest, svg

canvg

NPM version Dependencies status Build status Coverage status Dependabot badge Documentation badge

JavaScript SVG parser and renderer on Canvas. It takes the URL to the SVG file or the text of the SVG file, parses it in JavaScript and renders the result on Canvas.

Demo

Playground

Install

npm i canvg
# or
yarn add canvg

Usage

Basic module exports:

export default Canvg;
export {
    presets
};

Description of all exports you can find in Documentation.

Example

import Canvg from 'canvg';

let v = null;

window.onload = async () => {
    const canvas = document.querySelector('canvas');
    const ctx = canvas.getContext('2d');
    
    v = await Canvg.from(ctx, './svgs/1.svg');

    // Start SVG rendering with animations and mouse handling.
    v.start();
};

window.onbeforeunload = () => {
    v.stop();
};
OffscreenCanvas
import Canvg, {
    presets
} from 'canvg';

self.onmessage = async (event) => {
    const {
        width,
        height,
        svg
    } = event.data;
    const canvas = new OffscreenCanvas(width, height);
    const ctx = canvas.getContext('2d');
    const v = await Canvg.from(ctx, svg, presets.offscreen());

    // Render only first frame, ignoring animations and mouse.
    await v.render();

    const blob = await canvas.convertToBlob();
    const pngUrl = URL.createObjectURL(blob);

    self.postMessage({
        pngUrl
    });
};

OffscreenCanvas browsers compatibility.

NodeJS
import {
    promises as fs
} from 'fs';
import {
    DOMParser
} from 'xmldom';
import * as canvas from 'canvas';
import fetch from 'node-fetch';
import Canvg, {
    presets
} from 'canvg';

const preset = presets.node({
    DOMParser,
    canvas,
    fetch
});

(async (output, input) => {
    const svg = await fs.readFile(input, 'utf8');
    const canvas = preset.createCanvas(800, 600);
    const ctx = canvas.getContext('2d');
    const v = Canvg.fromString(ctx, svg, preset);

    // Render only first frame, ignoring animations.
    await v.render();

    const png = canvas.toBuffer();

    await fs.writeFile(output, png);

})(
    process.argv.pop(),
    process.argv.pop()
);
Resize
import Canvg, {
    presets
} from 'canvg';

self.onmessage = async (event) => {
    const {
        width,
        height,
        svg
    } = event.data;
    const canvas = new OffscreenCanvas(width, height);
    const ctx = canvas.getContext('2d');
    const v = await Canvg.from(ctx, svg, presets.offscreen());

    /**
     * Resize SVG to fit in given size.
     * @param width
     * @param height
     * @param preserveAspectRatio
     */
    v.resize(width, height, 'xMidYMid meet');

    // Render only first frame, ignoring animations and mouse.
    await v.render();

    const blob = await canvas.convertToBlob();
    const pngUrl = URL.createObjectURL(blob);

    self.postMessage({
        pngUrl
    });
};
Browser
<script type="text/javascript" src="https://unpkg.com/[email protected]/lib/umd.js"></script>
<script type="text/javascript">
window.onload = () => {
    const canvas = document.querySelector('canvas');
    const ctx = canvas.getContext('2d');
    
    v = canvg.Canvg.fromString(ctx, '<svg width="600" height="600"><text x="50" y="50">Hello World!</text></svg>');

    // Start SVG rendering with animations and mouse handling.
    v.start();

};
</script>
<canvas />

Options

The third parameter of new Canvg(...), Canvg.from(...) and Canvg.fromString(...) is options:

interface IOptions {
    /**
     * WHATWG-compatible `fetch` function.
     */
    fetch?: typeof fetch;
    /**
     * XML/HTML parser from string into DOM Document.
     */
    DOMParser?: typeof DOMParser;
    /**
     * Window object.
     */
    window?: Window;
    /**
     * Whether enable the redraw.
     */
    enableRedraw?: boolean;
    /**
     * Ignore mouse events.
     */
    ignoreMouse?: boolean;
    /**
     * Ignore animations.
     */
    ignoreAnimation?: boolean;
    /**
     * Does not try to resize canvas.
     */
    ignoreDimensions?: boolean;
    /**
     * Does not clear canvas.
     */
    ignoreClear?: boolean;
    /**
     * Scales horizontally to width.
     */
    scaleWidth?: number;
    /**
     * Scales vertically to height.
     */
    scaleHeight?: number;
    /**
     * Draws at a x offset.
     */
    offsetX?: number;
    /**
     * Draws at a y offset.
     */
    offsetY?: number;
    /**
     * Will call the function on every frame, if it returns true, will redraw.
     */
    forceRedraw?(): boolean;
    /**
     * Default `rem` size.
     */
    rootEmSize?: number;
    /**
     * Default `em` size.
     */
    emSize?: number;
    /**
     * Function to create new canvas.
     */
    createCanvas?: (width: number, height: number) => HTMLCanvasElement | OffscreenCanvas;
    /**
     * Function to create new image.
     */
    createImage?: (src: string, anonymousCrossOrigin?: boolean) => Promise<CanvasImageSource>;
    /**
     * Load images anonymously.
     */
    anonymousCrossOrigin?: boolean;
}

Options presets

There are two options presets:

  • presets.offscreen(): options for OffscreenCanvas;
  • presets.node({ DOMParser, canvas, fetch }): options for NodeJS with node-canvas.

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 add it to the issues list, or better is to create pull request 😎

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].