简体   繁体   English

高分辨率HTML5画布截图

[英]High Resolution HTML5 Canvas Screenshot

I have been trying to take a high resolution screenshot of an HTML5 canvas element that I have for a visualization consisting of rectangles and circles. 我一直在尝试使用HTML5画布元素的高分辨率屏幕截图,我将其用于包含矩形和圆形的可视化。 The canvas.toDataURL() works great, except that the image produced is limited to the size of the original canvas. canvas.toDataURL()工作得很好,除了生成的图像限制为原始画布的大小。 What I would really like is to take a screenshot that is 4 or 5 times that of the original canvas. 我真正想要的是截取原始画布的4到5倍的截图。

My strategy, however, has been to create a temporary canvas off-screen like the following: 但是,我的策略是在屏幕外创建一个临时画布,如下所示:

function renderScreenshot(canvas, scaleFactor) {
    var ctx = canvas.getContext('2d');
    var screenshotCanvas = document.createElement('canvas'); 
    var screenshotCtx = screenshotCanvas.getContext('2d');
    screenshotCanvas.width = canvas.width * scaleFactor; 
    screenshotCanvas.height = canvas.height * scaleFactor;
    screenshotCtx.drawImage(canvas, 0, 0, canvas.width * scaleFactor, canvas.height * scaleFactor);
    return screenshotCanvas.toDataURL();
}

The problem now is that the scaled image is blurry and pixilated, and does not do me any good. 现在的问题是缩放的图像模糊和像素化,并没有任何好处。 What is the way around this? 这是怎么回事?

Unfortunately, while you can draw paths like you would in a vector image, once they are drawn they become rasterised and cannot be scaled without interpolation. 不幸的是,虽然您可以像在矢量图像中那样绘制路径,但是一旦绘制它们,它们就会被栅格化并且无法在没有插值的情况下进行缩放。

Instead you would need to completely redraw everything, multiplying all coordinates by your scale factor, to render the path elements properly. 相反,您需要完全重绘所有内容,将所有坐标乘以比例因子,以正确渲染路径元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM