convertSvgToPng

Convert an SVG string into a PNG Blob using a canvas, returning a promise so you can download or upload rasterized image output.

Usage

convertSvgToPng converts an SVG string into a PNG Blob.

Note
  • Only available in browser environments
  • Uses the Canvas API for conversion
  • Throws an error if the conversion fails
import { convertSvgToPng } from '@movk/core'

const svgString = '<svg width="100" height="100"><circle cx="50" cy="50" r="40" fill="red"/></svg>'

try {
  const pngBlob = await convertSvgToPng(svgString)
  const url = URL.createObjectURL(pngBlob)

  // Use for download or display
  const img = document.createElement('img')
  img.src = url
  document.body.appendChild(img)
}
catch (error) {
  console.error('SVG conversion failed:', error)
}

API

convertSvgToPng(svg)

Convert an SVG string into a PNG Blob.

Parameters

svg
string required
The SVG string to convert.

Returns

returns
Promise<Blob>
A Blob containing the PNG image data.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License