From 5b29a49478955bdcf27458f3c7facee5e8b37a79 Mon Sep 17 00:00:00 2001 From: Lou Huang Date: Fri, 8 Nov 2019 10:37:17 -0500 Subject: [PATCH] feat(png): saved image uses device pixel ratio --- src/png.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/png.js b/src/png.js index 4cba3fc3..e5703f5e 100644 --- a/src/png.js +++ b/src/png.js @@ -3,10 +3,11 @@ import {DEFAULT_FILENAME} from './const' function downloadPng(source, filename = DEFAULT_FILENAME) { const canvas = document.createElement('canvas') + const dpr = window.devicePixelRatio || 1 document.body.appendChild(canvas) canvas.setAttribute('id', 'svg-image') - canvas.setAttribute('width', source.width) - canvas.setAttribute('height', source.height) + canvas.setAttribute('width', source.width * dpr) + canvas.setAttribute('height', source.height * dpr) canvas.style.display = 'none' const context = canvas.getContext('2d') @@ -15,6 +16,7 @@ function downloadPng(source, filename = DEFAULT_FILENAME) { const image = new Image() function onLoad() { + context.scale(dpr, dpr) context.drawImage(image, 0, 0) const canvasdata = canvas.toDataURL('image/png')