简体   繁体   English

打印分层的html5画布

[英]Print layered html5 canvases

I have a page composed of multiple layered canvases, which I would like to turn into one image (and then print). 我有一个由多层画布组成的页面,我想将其变成一个图像(然后打印)。 So for one canvas it's simple: capture-html-canvas-as-gif-jpg-png-pdf . 因此,对于一张画布来说很简单: capture-html-canvas-as-gif-jpg-png-pdf

So what to do with multiple layers of canvases? 那么如何处理多层画布呢? The only idea I came up with so far is to create another canvas and paint all the layered canvases into this one with ascending z-order. 到目前为止,我想到的唯一想法是创建另一张画布,然后将所有分层的画布按z顺序升序绘制到该画布中。 Will work, but taking a snapshot of the original canvases would be more helpful. 可以,但是对原始画布进行快照会更有用。

Combining your canvases into a single "print" canvas is the way to go. 将画布合并为一个“打印”画布是一种方法。

BTW, in case you didn't know... 顺便说一句,如果你不知道...

context.drawImage(imageSource... will take another canvas as it's imageSource. context.drawImage(imageSource...将采用另一个画布作为imageSource。

Therefore, if you need all your canvases intact (uncombined), just create a new in-memory-only canvas and draw all canvases onto that new canvas in z-index order: 因此,如果您需要完整(未组合)所有画布,只需创建一个新的仅限内存的画布,然后按z索引顺序将所有画布绘制到该新画布上:

var printCanvas=document.createElement('canvas');
var printCtx=printCanvas.getContext('2d');

printCtx.drawImage(canvasZIndex0,0,0);
printCtx.drawImage(canvasZIndex1,0,0);
printCtx.drawImage(canvasZIndex2,0,0);
printCtx.drawImage(canvasZIndex3,0,0);

var img=new Image();
img.onload=function(){
    // print this snapshot image
}
img.src=printCanvas.toDataURL();

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

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