@ferdipret/svg-2-img
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

svg2Image

The easiest way to download an SVG as an image.

Install

npm install @ferdipret/svg-2-img

# or

yarn add @ferdipret/svg-2-img

Examples

Vanilla JS Image Download

import svg2Img from '@ferdipret/svg-2-img'
const svgElement = document.querySelector('#myLogo')
const downloadButton = document.querySelector('#downloadButton')

downloadButton.addEventListener('click', () => {
  svg2Img(svgElement, {
    format: 'jpeg',
    downloadFileName: 'cool-logo',
  })
})

React + TypeScript Image Download

import React from 'react'
import svg2Image from '@ferdipret/svg-2-img'


function Component() {
  const logoRef = useRef<SVGSVGElement>(null)

  const handleClick = () => {
    const svg = logoRef.current

    svg2Img(svg, {
      width: 300,
      height: 300,
      backgroundColor: 'red',
    })
  }

  return (
    <>
      <Logo ref={logoRef} />
      <button onClick={handleClick}>Download image</button>
    </>
  )
}

export default Component

Package Sidebar

Install

npm i @ferdipret/svg-2-img

Weekly Downloads

7

Version

1.0.3

License

MIT

Unpacked Size

25.1 kB

Total Files

12

Last publish

Collaborators

  • ferdipret