Skip to content

Instantly share code, notes, and snippets.

@jevy-wangfei
Created June 19, 2020 06:10
Show Gist options
  • Save jevy-wangfei/424da5c2e244ff6ed4d95857d3ebf0c1 to your computer and use it in GitHub Desktop.
Save jevy-wangfei/424da5c2e244ff6ed4d95857d3ebf0c1 to your computer and use it in GitHub Desktop.
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