### Browser WebAssembly Setup
Source: https://github.com/thx/resvg-js/blob/main/README.md
Include the resvg-wasm script and initialize the WebAssembly module. This example demonstrates loading custom fonts.
```html
```
--------------------------------
### Run resvg.js with Deno
Source: https://github.com/thx/resvg-js/blob/main/README.md
Execute the resvg.js example using Deno. Ensure you have Deno 1.26.1 or later installed and use the --unstable flag.
```shell
deno run --unstable --allow-read --allow-write --allow-ffi example/index-deno.js
```
--------------------------------
### Run Deno example
Source: https://github.com/thx/resvg-js/blob/main/README.md
Execute the provided Deno example script with required permissions.
```shell
deno run --unstable --allow-read --allow-write --allow-ffi example/index-deno.js
[2022-11-16T15:03:29Z DEBUG resvg_js::fonts] Loaded 1 font faces in 0.067ms.
[2022-11-16T15:03:29Z DEBUG resvg_js::fonts] Font './example/SourceHanSerifCN-Light-subset.ttf':0 found in 0.001ms.
Original SVG Size: 1324 x 687
Output PNG Size : 1200 x 623
✨ Done in 66 ms
```
--------------------------------
### Run Node.js example
Source: https://github.com/thx/resvg-js/blob/main/README.md
Execute the provided Node.js example script.
```shell
node example/index.js
Loaded 1 font faces in 0ms.
Font './example/SourceHanSerifCN-Light-subset.ttf':0 found in 0.006ms.
✨ Done in 55.65491008758545 ms
```
--------------------------------
### Run example in Bun
Source: https://github.com/thx/resvg-js/blob/main/README.md
Execute the Node.js example script directly using the Bun runtime.
```shell
bun example/index.js
```
--------------------------------
### Install wasm-bindgen-cli
Source: https://github.com/thx/resvg-js/blob/main/README.md
Manually installs wasm-bindgen-cli if the automatic installation via wasm-pack fails due to network issues.
```bash
cargo install wasm-bindgen-cli
```
--------------------------------
### Build WebAssembly bindings
Source: https://github.com/thx/resvg-js/blob/main/README.md
Installs dependencies, builds the WebAssembly bindings, and runs their respective tests. Ensure Node.js and npm are installed.
```bash
npm i
npm run build:wasm
npm run test:wasm
```
--------------------------------
### Build Node.js bindings
Source: https://github.com/thx/resvg-js/blob/main/README.md
Installs dependencies, builds the Node.js bindings, and runs tests. Ensure Node.js and npm are installed.
```bash
npm i
npm run build
npm test
```
--------------------------------
### Install wasm-pack
Source: https://github.com/thx/resvg-js/blob/main/README.md
Installs wasm-pack using the provided script. Ensure Rust and Node.js are installed prior to running.
```bash
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
```
--------------------------------
### Install Benchmark Dependencies
Source: https://github.com/thx/resvg-js/blob/main/README.md
Install necessary packages for benchmarking resvg-js against other libraries like sharp and svg2img.
```shell
npm i benny@3.x sharp@0.x @types/sharp svg2img@0.x
npm run bench
```
--------------------------------
### Deno Usage Example
Source: https://context7.com/thx/resvg-js/llms.txt
Run resvg-js natively in Deno using FFI support. Requires unstable, read, write, and FFI permissions.
```javascript
// example/index-deno.js
import * as path from 'https://deno.land/std@0.159.0/path/mod.ts'
import { Resvg } from 'npm:@resvg/resvg-js'
const __dirname = path.dirname(path.fromFileUrl(import.meta.url))
const svg = await Deno.readFile(path.join(__dirname, './text.svg'))
const opts = {
fitTo: { mode: 'width', value: 1200 },
font: {
fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'],
loadSystemFonts: false,
},
}
const t = performance.now()
const resvg = new Resvg(svg, opts)
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
console.info('Original SVG Size:', `${resvg.width} x ${resvg.height}`)
console.info('Output PNG Size:', `${pngData.width} x ${pngData.height}`)
console.info('Done in', performance.now() - t, 'ms')
await Deno.writeFile(path.join(__dirname, './text-out-deno.png'), pngBuffer)
// Run with: deno run --unstable --allow-read --allow-write --allow-ffi index-deno.js
```
--------------------------------
### Install Node.js package
Source: https://github.com/thx/resvg-js/blob/main/README.md
Use npm to add the package to your project.
```shell
npm i @resvg/resvg-js
```
--------------------------------
### Install binaryen on Apple M chips
Source: https://github.com/thx/resvg-js/blob/main/README.md
Installs binaryen using Homebrew for Apple M chip users experiencing download errors for binaryen.
```bash
brew install binaryen
```
--------------------------------
### resvg.js Deno Example
Source: https://github.com/thx/resvg-js/blob/main/README.md
This script uses Deno to read an SVG file, convert it to PNG using resvg.js, and write the output. It utilizes Deno's file system and path modules.
```javascript
import * as path from 'https://deno.land/std@0.159.0/path/mod.ts'
import { Resvg } from 'npm:@resvg/resvg-js'
const __dirname = path.dirname(path.fromFileUrl(import.meta.url))
const svg = await Deno.readFile(path.join(__dirname, './text.svg'))
const opts = {
fitTo: {
mode: 'width',
value: 1200,
},
}
const t = performance.now()
const resvg = new Resvg(svg, opts)
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
console.info('Original SVG Size:', `${resvg.width} x ${resvg.height}`)
console.info('Output PNG Size :', `${pngData.width} x ${pngData.height}`)
console.info('✨ Done in', performance.now() - t, 'ms')
await Deno.writeFile(path.join(__dirname, './text-out-deno.png'), pngBuffer)
```
--------------------------------
### imagesToResolve() and resolveImage() Methods
Source: https://context7.com/thx/resvg-js/llms.txt
Methods to handle external images referenced in SVG via `` elements with HTTP/HTTPS URLs. This involves getting the URLs, fetching them, and providing the image buffers back to resvg.
```APIDOC
## imagesToResolve() and resolveImage() Methods
### Description
Load external images referenced in the SVG via `` elements with HTTP/HTTPS URLs. First get URLs to resolve, fetch them, then provide the image buffers back to resvg.
### Method
Not applicable (methods of a class instance)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```javascript
const { Resvg } = require('@resvg/resvg-js')
const fetch = require('node-fetch')
const fs = require('fs').promises
async function renderSvgWithExternalImages() {
const svg = `
`
const resvg = new Resvg(svg, {
font: { loadSystemFonts: false },
logLevel: 'off',
})
// Get list of external image URLs to resolve
const imageUrls = resvg.imagesToResolve()
console.log('Images to resolve:', imageUrls)
// ['https://example.com/logo.png', 'https://example.com/icon.gif']
// Fetch and resolve each image
const resolved = await Promise.all(
imageUrls.map(async (url) => {
console.log('Fetching:', url)
const response = await fetch(url)
const buffer = await response.arrayBuffer()
return { url, buffer: Buffer.from(buffer) }
})
)
// Provide image data back to resvg
for (const { url, buffer } of resolved) {
resvg.resolveImage(url, buffer)
}
// Now render with resolved images
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
console.log('Output Size:', `${pngData.width} x ${pngData.height}`)
await fs.writeFile('./output-with-images.png', pngBuffer)
}
renderSvgWithExternalImages()
```
### Response
#### Success Response (200)
None (modifies the resvg instance in place)
#### Response Example
None
```
--------------------------------
### Render SVG String with resvg-js
Source: https://github.com/thx/resvg-js/blob/main/wasm/index.html
Render an SVG string using resvg-js. This example includes a complex SVG with paths and transformations.
```javascript
const svgList = [{
name: 'Hello resvg-js',
svgString:
`
` },
{
name: 'GitHub Octocat',
svgString:
`