简体   繁体   English

以高分辨率渲染html画布图像

[英]Rendering html canvas images at high resolution

I'm looking to create a system where users create an image in canvas on their browser (with the help of paper.js or processing.js etc) and be able to print a large (up to A3, 300dpi ideally) size representation of what they have created in canvas. 我正在寻找创建一个系统,用户在浏览器上用画布创建一个图像(借助paper.js或processing.js等),并能够打印一个大的(最多A3,300dpi理想)尺寸表示他们在画布上创造了什么。

Obviously exporting images straight from the canvas element in the users browser I'm limited to screen size and resolution etc. 显然,直接从用户浏览器中的canvas元素导出图像我只限于屏幕尺寸和分辨率等。

So I've looked into the solution of momentarily scaling up the canvas element when the users saves and capturing the image data at the larger size. 因此,当用户以较大的尺寸保存和捕获图像数据时,我已经研究了暂时放大canvas元素的解决方案。 This could be a solution but I'm pretty sure file size would get out of hand pretty quickly at larger sizes. 这可能是一个解决方案,但我很确定文件大小会在较大的尺寸下很快失控。

I haven't used Node.js very much but was wondering if anyone with experience would know if Node could achieve this and if it would be a better solution and briefly how I'd go about it? 我没有非常多地使用Node.js,但是想知道是否有经验的人会知道Node是否能够实现这一目标,以及它是否是一个更好的解决方案,并简要说明我将如何解决这个问题?

I see two ways to achieve what you want : 我看到两种方法可以达到你想要的效果:

  • use an oversized canvas, that you scale with css. 使用超大画布,用css缩放。
    For instance, you can have a 1000X1000 canvas, that you show within a 200pxX200px smaller view. 例如,您可以在200pxX200px较小的视图中显示1000X1000画布。

     <canvas width=1000 height=1000 style = 'width:200px; height:200px;' id='cv'> </canvas> 
  • use a small canvas for display on screen, and really draw on a backing canvas, that you reproduce on the view canvas at each change. 使用小画布在屏幕上显示,并在支持画布上绘制,在每次更改时在视图画布上重现。

Both solution cannot solve the issue that mouse coordinates are integer, so to implement a 'pixel perfect' location of object you'll have to implement some kind of zooming. 两种解决方案都无法解决鼠标坐标为整数的问题,因此要实现对象的“像素完美”位置,您必须实现某种缩放。 Second solution might be simpler for this. 第二种解决方案可能更简单。

To retrieve the mouse coordinates, with css scaling do not forget to multiply them by the scale, and in case 2, by the scale you decided. 要检索鼠标坐标,使用css缩放不要忘记将它们乘以比例,而在情况2中,按您决定的比例。

// formula to get the css scale :
var cssScaleX = canvas.width / canvas.offsetWidth;  
var cssScaleY = canvas.height / canvas.offsetHeight;
// then mouse coords are to be multiplied by the scale : 
//                                       mouse.x *= cssScaleX;

I tried quickly both solutions, and i was quite surprised to see that css solution is very slow (in both Ch and FF), and it seems faster to copy a back canvas than to have css doing it. 我快速尝试了两种解决方案,我很惊讶地看到css解决方案非常慢(在Ch和FF中),并且复制背面画布似乎比使用css更快。 Maybe it depends on some quality settings, but it seems solution 2 is both more flexible and faster. 也许这取决于一些质量设置,但似乎解决方案2更灵活,更快。

first css version is here (move mouse to draw 10X10 rect) : 第一个css版本在这里(移动鼠标绘制10X10 rect):

http://jsbin.com/kegohufu/1/ http://jsbin.com/kegohufu/1/

second back canvas + copy version is here (move mouse to draw 10X10 rect) : 第二个回画布+复制版本在这里(移动鼠标画10X10矩形):

http://jsbin.com/qomiqoqi/1/ http://jsbin.com/qomiqoqi/1/

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

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