svg-to-image-converter
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

SVG-to-image-converter

A simple javascript library for convert SVG images to png or jpeg on client side.

Usage/Examples

Example usage on Javascript.

import SVGToImage from "svg-to-image-converter";

SVGToImage.toBlob('jpg',['/your-svg-image.svg'])
    .then((res)=>{
        const blobUrl = SVGToImage.toBlobUrl(res,res.type);
    }
)

Example for custom single file path with button click download.

import SVGToImage from "svg-to-image-converter";

SVGToImage.toBlob(
    'png',
    ['assets/sample_1.svg'],
    512,512
).then((data)=>{
    document.getElementById("download").onclick = ()=>{
        SVGToImage.forceDownload(data,undefined,{
            confirmation:true,
            conformationType:"native"
        })
    }
}).catch(console.warn)

Example for multiple input files without download

import SVGToImage from "svg-to-image-converter";

document.getElementById("svgFileInput").onchange = (evt)=>{
    SVGToImage.toBlob("png",evt.target.files).then((data)=>{
        for (let i = 0; i < data.length; i++) {
            const file = data[i];
            const img = document.createElement("img");
            Object.assign(img.style,{width:"300px",height:"auto"});
            img.src = URL.createObjectURL(file)
            document.getElementById("previewContainer").appendChild(img) 
        }
    }).catch(console.warn)
}

Example for single input file with button click download

import SVGToImage from "svg-to-image-converter";

document.getElementById("svgFileInput").onchange = (evt)=>{
    SVGToImage.toBlob('png',[evt.target.files[0]],1024,1024).then((data)=>{
        document.getElementById("download").onclick = ()=>{
            SVGToImage.forceDownload(data);
        }
    }).catch(console.error);
}

Contributing

Contributions are always welcome!

Please adhere to this project's code of conduct.

License

MIT

Package Sidebar

Install

npm i svg-to-image-converter

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

25.4 kB

Total Files

7

Last publish

Collaborators

  • janithumeda