Skip to content

Latest commit

History

History
46 lines (31 loc) 路 1.78 KB

2020-12-30-svg-to-image.md

File metadata and controls

46 lines (31 loc) 路 1.78 KB
layout title date excerpt categories
post
SVG to Image
2020-12-30 13:04:20 -0800
Convert and download SVG to image (PNG or JPEG) on the frontend.
svg image png jpeg jsfiddle javascript
<script async src="https://jsfiddle.net/remarkablemark/fxg4Lvdt/embed/result/"></script>

Overview

Inspiration

The JSFiddle is inspired by the article "Convert SVG to PNG using your browser".

Breakdown

The steps to save SVG to image are:

  1. Load the SVG in the DOM
  2. Draw the SVG as an image in a canvas
  3. Trigger a download

Improvements

The improvements made in my example are:

  • Added the ability to resize SVG in canvas
  • Added the option to update the filename
  • Added the option to change the file extension from png to jpeg
  • Used HTML5 form validation
  • Prevented memory leaks by cleaning up event listeners
  • Improved accessibility
  • Stopped supporting older browsers

Sample SVG

Here are some sample SVG's you can use to test against the converter:

Code

See JSFiddle or Gist.