Created
June 19, 2020 06:10
-
-
Save jevy-wangfei/424da5c2e244ff6ed4d95857d3ebf0c1 to your computer and use it in GitHub Desktop.
This file contains 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 svgChartDom = document.getElementById("svg-element"); | |
const svgSize = svgChartDom.getBoundingClientRect(); | |
const svgChart = new XMLSerializer().serializeToString(svgChartDom); | |
let canvas = document.createElement("canvas"); | |
canvas.width = svgSize.width; | |
canvas.height = svgSize.height; | |
let ctx = canvas.getContext("2d"); | |
// Use canvg package to create canvas, resolve the limitation of IE, where drawing SVG would taint the canvas | |
let v = Canvg.fromString(ctx!, svgChart); | |
v.start(); | |
v.ready().then(() => { | |
const imageBase64 = canvas.toDataURL("image/png"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment