Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

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.

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.

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:

$(".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. I'm not sure whether or not there is something about a canvas element that doesn't work with the plugin.

HOW IT WORKS: My above code is rather simple. When one HTML2Canvas is complete, it runs another 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. 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.

If more information is needed let me know, thanks.

like image 782
Mallander Avatar asked Sep 29 '22 14:09

Mallander


1 Answers

This function accepts two parameters top and bottom. Both are expected to be ImageData objects, which you can get via ctx.getImageData(). The bottom ImageData is returned with the top data merged unto it. This function is using the Porter-Duff method to merge colors.

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.

like image 131
EvilZebra Avatar answered Oct 02 '22 15:10

EvilZebra