简体   繁体   English

HTML2Canvas将2个捕获的画布合并为一个

[英]HTML2Canvas combine 2 captured canvases into one

Using HTML2Canvas I'm trying to take a capture of a googleMap (Which works fine) then take a capture of an #overlay div that is over the top of it (Which is there to allow people to place images over the googleMap in specific places)(Works fine). 使用HTML2Canvas我正在尝试捕获googleMap(其工作正常)然后捕获一个#overlay其顶部的#overlay div(允许人们在特定位置将图像放在googleMap上) )(工作正常)。

The page then displays both of these canvas elements on the page which I then want to take another capture of to combine the 2 canvases together into a single image that they can then download. 然后页面在页面上显示这两个canvas元素,然后我想再拍摄一次 ,将两个画布组合成一个可以下载的单个图像。

The issue I'm having so far is that it seems HTML2Canvas isn't picking up the canvas elements that it creates when I try and capture my #preview div. 我到目前为止的问题是,当我尝试捕获我的#preview div时,似乎HTML2Canvas没有拾取它创建的canvas元素。

HTML: HTML:

<div id="mainPanel">
    <div id="overlay">

    </div>
    <button class="download">Download as PDF</button>
    <button onclick="startEdit();" class="edit_button">Start Editing</button>
    <div id="map">
        <input id="pac-input" class="controls" type="text" placeholder="Search Box">
        <div id="map-canvas"></div>
    </div>
    <div id="preview">

    </div>
</div>

HTML2Canvas JQuery: HTML2Canvas JQuery:

$(".download").click(function() {
        html2canvas($("#map"), {
            logging: true,
            proxy: "https://html2canvas.appspot.com/query",
            onrendered: function(canvas) {
                // var img = canvas.toDataURL("image/png");
                $(canvas).css({"position" : "absolute", "z-index" : "999"});
                document.getElementById("preview").appendChild(canvas);

                html2canvas($("#overlay"), {
                    logging: true,
                    onrendered: function(canvas1) {

                        // var img = canvas.toDataURL("image/png");
                        $(canvas1).css({"position" : "absolute", "z-index" : "1000"});
                        document.getElementById("preview").appendChild(canvas1);

                        setTimeout(function() {downloadURI()}, 5000);
                    }
                });
            }
        });
});

function downloadURI() {
    html2canvas($("#preview"), {
        logging: true,
        onrendered: function(canvas2) {

            var img = canvas2.toDataURL("image/png");
                window.open(img);
                // var link = document.createElement("a");
                // link.download = "image-download.png";
                // link.href = img;
                // link.click();
            }
        });
    }

As you can see, i've tried delaying the DownloadURI function in case the canvases weren't loading in time, but that is not the issue. 正如您所看到的,我已经尝试延迟DownloadURI功能,以防画布未及时加载,但这不是问题。 I'm not sure whether or not there is something about a canvas element that doesn't work with the plugin. 我不确定是否有一些关于canvas元素的东西不能与插件一起使用。

HOW IT WORKS : My above code is rather simple. 工作原理 :我的上述代码非常简单。 When one HTML2Canvas is complete, it runs another HTML2Canvas. 当一个HTML2Canvas完成时,它运行另一个HTML2Canvas。 Both of these canvases are appended to the #preview element so that I can capture that element with the 2 canvases on top of each other inside it. 这两个画布都附加在#preview元素上,这样我就可以捕获该元素,其中2个画布位于其中。 Once that is complete it runs another function to take a capture of the #preview element which I hoped would combine the 2 images one on top of the other into a single image that the user can download, but it just comes back with an empty image. 一旦完成它就会运行另一个函数来捕获#preview元素,我希望 #preview两个图像一个在另一个上面组合成一个用户可以下载的单个图像,但它只返回一个空图像。

If more information is needed let me know, thanks. 如果需要更多信息,请告诉我,谢谢。

This function accepts two parameters top and bottom . 此函数接受topbottom两个参数。 Both are expected to be ImageData objects, which you can get via ctx.getImageData() . 两者都应该是ImageData对象,您可以通过ctx.getImageData()获取ctx.getImageData() The bottom ImageData is returned with the top data merged unto it. 返回底部ImageData,并将顶部数据合并到它。 This function is using the Porter-Duff method to merge colors. 此函数使用Porter-Duff方法合并颜色。

function mergeData(top, bottom){
    var tD = top.data,
        bD = bottom.data,
        l = tD.length;
    for(var i = 0; i < l; i += 4){
        //source alpha
        var alphaSrc = tD[i+3] / 255, //source alpha
            alphaDst = bD[i+3] / 255, //destination alpha
            alphaSrcO = 1 - alphaSrc, //(1 - x)
            alpha = alphaSrc + alphaDst * alphaSrcO; //if destination alpha is opaque
        //merge colors
        bD[i] = ((tD[i]*alphaSrc) + (bD[i]*alphaDst*alphaSrcO)) / alpha,
        bD[i+1] = ((tD[i+1]*alphaSrc) + (bD[i+1]*alphaDst*alphaSrcO)) / alpha,
        bD[i+2] = ((tD[i+2]*alphaSrc) + (bD[i+2]*alphaDst*alphaSrcO)) / alpha,
        bD[i+3] = 255*alpha;
    }
    //return bottom
    return bottom;
}

To use this function and it's returned data, do something like this: 要使用此函数及其返回的数据,请执行以下操作:

var merged = mergeData(ctx1.getImageData(), ctx2.getImageData());
ctx2.putImageData(merged, 0, 0);

Obviously you can put the resulting data into a 3rd, hidden context if you need to. 显然,如果需要,您可以将结果数据放入第三个隐藏的上下文中。

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

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