Skip to content

Instantly share code, notes, and snippets.

@mubaidr
Last active August 19, 2020 12:43
Show Gist options
  • Save mubaidr/7eb1f655164b1182c603af8154f38289 to your computer and use it in GitHub Desktop.
Save mubaidr/7eb1f655164b1182c603af8154f38289 to your computer and use it in GitHub Desktop.
function getSvgAsImage(svgElem, viewBox) {
// TODO: add x,y to w,h
var canvas = document.createElement('canvas')
canvas.width = viewBox[2]
canvas.height = viewBox[3]
var ctx = canvas.getContext('2d')
ctx.fillStyle = '#fff'
ctx.fillRect(0, 0, canvas.width, canvas.height)
var options = {
// fetch:
ignoreMouse: true,
ignoreAnimation: true,
// ignoreDimensions: true,
ignoreClear: true,
scaleWidth: 500,
// scaleHeight: 400,
}
Canvg.fromString(ctx, svgElem.outerHTML, options).render()
return canvas.toDataURL('image/jpeg', 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment