diff --git a/package.json b/package.json index ad1ae33..fcb783a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,6 @@ "main": "src/index.tsx", "dependencies": { "canvas-toBlob": "^1.0.0", - "canvg": "^1.5.3", "d3": "^5.7.0", "detect-browser": "^4.1.0", "file-saver": "^2.0.1", diff --git a/src/chart.tsx b/src/chart.tsx index b882d40..d5e15a5 100644 --- a/src/chart.tsx +++ b/src/chart.tsx @@ -139,7 +139,7 @@ export class Chart extends React.PureComponent { private getSvgContents() { const svg = document.getElementById('chart')!.cloneNode(true) as Element; svg.removeAttribute('transform'); - return svg.outerHTML; + return new XMLSerializer().serializeToString(svg); } /** Shows the print dialog to print the currently displayed chart. */ @@ -175,24 +175,19 @@ export class Chart extends React.PureComponent { canvas.width = svg.getBBox().width * 2; canvas.height = svg.getBBox().height * 2; - // Fill canvas with white background. - const ctx = canvas.getContext('2d')!; - const oldFill = ctx.fillStyle; - ctx.fillStyle = 'white'; - ctx.fillRect(0, 0, canvas.width, canvas.height); - ctx.fillStyle = oldFill; + const blob = new Blob([this.getSvgContents()], {type: 'image/svg+xml'}); - canvg(canvas, this.getSvgContents(), { - ignoreDimensions: true, - ignoreClear: true, - scaleWidth: canvas.width, - scaleHeight: canvas.height, - }); - const onBlob = (blob: Blob | null) => { - if (blob) { - saveAs(blob, 'topola.png'); - } + const img = new Image(); + img.onload = () => { + const ctx = canvas.getContext('2d')!; + ctx.drawImage(img, 0, 0, canvas.width, canvas.height); + const onBlob = (blob: Blob | null) => { + if (blob) { + saveAs(blob, 'topola.png'); + } + }; + canvas.toBlob(onBlob, 'image/png'); }; - canvas.toBlob(onBlob, 'image/png'); + img.src = URL.createObjectURL(blob); } }