Created
February 19, 2020 13:04
-
-
Save stleamist/4ff83cf79b695c44a2662a6d0c69bed7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Canvg, presets } = require('canvg') | |
const { DOMParser } = require('xmldom') | |
const canvas = require('canvas') | |
const fetch = require('node-fetch') | |
const preset = presets.node({ DOMParser, canvas, fetch }) | |
module.exports = async function svgToCanvas(svg) { | |
try { | |
const canvas = preset.createCanvas(1, 1, 'svg') | |
const context = canvas.getContext('2d') | |
const v = Canvg.fromString(context, svg, preset) | |
await v.render() | |
return Promise.resolve(canvas) | |
} catch (error) { | |
return Promise.reject(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment