• Stars
    star
    145
  • Rank 254,144 (Top 6 %)
  • Language Svelte
  • License
    MIT License
  • Created over 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SVG to PNG converter JS library made with WASM + resvg (no native dependencies).

svg2png-wasm

Demo site

SVG to PNG converter JS library made with WASM + resvg.

See resvg for SVG support status.

๐Ÿ’ป Usage

Installation

Node.js / Browser

npm install svg2png-wasm
# yarn add svg2png-wasm
# pnpm add svg2png-wasm

Or, using a script tag in the browser and load from unpkg.

<script src="https://unpkg.com/[email protected]"></script>

<!-- Or, latest -->
<script src="https://unpkg.com/svg2png-wasm"></script>

Deno

// from esm.sh
export * from 'https://esm.sh/[email protected]';
// from skypack.dev
export * from 'https://cdn.skypack.dev/[email protected]?dts';

Examples

Node.js

import { svg2png, initialize } from 'svg2png-wasm';
// const { svg2png, initialize } = require('svg2png-wasm');
import { readFileSync, writeFileSync } from 'fs';

await initialize(
  readFileSync('./node_modules/svg2png-wasm/svg2png_wasm_bg.wasm'),
);

/** @type {Uint8Array} */
const png = await svg2png(
  '<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> ... </svg>',
  {
    scale: 2, // optional
    width: 400, // optional
    height: 400, // optional
    backgroundColor: 'white', // optional
    fonts: [
      // optional
      readFileSync('./Roboto.ttf'), // require, If you use text in svg
    ],
    defaultFontFamily: {
      // optional
      sansSerif: 'Roboto',
    },
  },
);
writeFileSync('./output.png', png);

Browser

import { createSvg2png, initialize } from 'svg2png-wasm';

// put wasm to your assets directory
await initialize(fetch('/assets/svg2png_wasm_bg.wasm'));
const svgs = [
  '<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> ... </svg>',
  '<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> ... </svg>',
  '<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> ... </svg>',
  // and more ...
];
const font = await fetch('./Roboto.ttf').then((res) => res.arrayBuffer());
const svg2png = createSvg2png({
  fonts: [new Uint8Array(font)], // require, If you use text in svg
});
/** @type {Uint8Array[]} */
const pngs = await Promise.all(svgs.map((svg) => svg2png(svg, { scale: 2 })));
svg2png.dispose(); // You should dispose svg2png, if you will not use it in the future

Or, using a script tag in the browser and load from unpkg.

<script src="https://unpkg.com/svg2png-wasm"></script>
<script>
  await svg2pngWasm.initialize(fetch('https://unpkg.com/svg2png-wasm/svg2png_wasm_bg.wasm'))
  const font = await fetch('./Roboto.ttf').then((res) => res.arrayBuffer());
  /** @type {Uint8Array} */
  const png = await svg2pngWasm.svg2png(
    '<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> ... </svg>',
  );
  document.getElementById('output').src = URL.createObjectURL(
    new Blob([png], { type: 'image/png' }),
  );
</script>

API

The library has two main APIs (svg2png and createSvg2png).

Basically, you can use svg2png, but if you want to process a lot of data continuously, consider using createSvg2png. It can reduce the overhead of font loading. Converters generated by createSvg2png should be disposed of after use by calling the dispose method.

export type InitInput =
  | RequestInfo
  | URL
  | Response
  | BufferSource
  | WebAssembly.Module;
export type DefaultFontFamily = {
  serifFamily?: string;
  sansSerifFamily?: string;
  cursiveFamily?: string;
  fantasyFamily?: string;
  monospaceFamily?: string;
};
export type ConverterOptions = {
  fonts?: Uint8Array[];
  defaultFontFamily?: DefaultFontFamily;
};
export type ConvertOptions = {
  scale?: number;
  width?: number;
  height?: number;
  backgroundColor?: string;
};
export type Svg2png = ((
  svg: string,
  options?: ConvertOptions,
) => Promise<Uint8Array>) & {
  dispose: () => void;
};
/**
 * Initialize WASM module
 * @param mod WebAssembly Module or WASM url
 */
export const initialize: (mod: Promise<InitInput> | InitInput) => Promise<void>;
/**
 * @param opts Converter options (e.g. font settings)
 * @returns svg2png converter
 */
export const createSvg2png: (opts?: ConverterOptions | undefined) => Svg2png;
export const svg2png: (
  svg: string,
  opts?: (ConverterOptions & ConvertOptions) | undefined,
) => Promise<Uint8Array>;

๐Ÿ“„ LICENSE

MIT

This library uses resvg, which is licensed unser MPL-2.0. The source code for resvg can be found here.

๐Ÿ™‹โ€โ™‚๏ธ Contributing

WELCOME!

More Repositories

1

svelte-exmarkdown

Svelte component to render markdown.
TypeScript
171
star
2

doc-vitest

Documentation tests with Vitest
TypeScript
53
star
3

fontvuer

New cross platform fontviewer
Vue
51
star
4

fontviewer

This app is deprecated.
JavaScript
25
star
5

svg2png-worker

svg2png-wasm demo with Cloudflare Workers
TypeScript
15
star
6

svg2png-deno-deploy

svg2png-wasm demo with Deno Deploy.
TypeScript
15
star
7

hololive-music.web.app

Svelte
14
star
8

typedotenv

dotenv utility for TypeScript
TypeScript
14
star
9

svelte-jsx-snippet

Write JSX as Svelte snippets for testing.
TypeScript
5
star
10

setup-firebase-tools

This action provides the firebase-tools(CLI) with auth setting.
TypeScript
4
star
11

ssssota.github.io

My portfolio!
TypeScript
4
star
12

unplugin-parcel-css

A Vite/webpack/Rollup plugin with @parcel/css
TypeScript
3
star
13

vcltk

Fastly VCL toolkit
TypeScript
2
star
14

pest-json-parser

JSON Parser for Rust using pest
Rust
2
star
15

chibi-x

TypeScript
2
star
16

svelte-twc

Svelte5 library that provides a way to use Tailwind CSS classes as components, inspired by TWC.
TypeScript
2
star
17

sveltekit-hooks-demo

TypeScript
2
star
18

exif.photos

Display EXIF data from your photos in a beautiful way โœจ Inspired by Liit (iOS app).
TypeScript
2
star
19

msw-connect-web

Mock Service Worker utility for connect-web.
TypeScript
2
star
20

node-font-kit

Node bindings to font-kit(Rust crate)
Rust
1
star
21

taberungo-no-uta

CSSใ‚ขใƒ‹ใƒกใƒผใ‚ทใƒงใƒณใงใ€ŒใŸในใ‚‹ใ‚“ใ”ใฎใ†ใŸใ€
HTML
1
star
22

move

move is a desktop application that allows you to move multiple files from one location to another
Rust
1
star
23

multitube

mutiple youtube live viewer
JavaScript
1
star
24

ningyo

Rust implementaion for mermaid(js).
Rust
1
star
25

fontscan

Get font list in specified directory(default system fonts).
JavaScript
1
star
26

ungrammar-wasm

WASM implementation for ungrammar
Rust
1
star
27

nkopi-solver

ใ‚“ใ“ใดใฃใŸใ‚“ใ‚ฝใƒซใƒ
JavaScript
1
star
28

create-jsx-snippet

JSX for createRawSnippet in Svelte
TypeScript
1
star
29

svogp

OGP image generator for embedding to Markdown
TypeScript
1
star
30

qr_cli

QR code generator
Rust
1
star
31

wasm-comparison

TypeScript
1
star
32

minicache

TypeScript
1
star