6

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.

Mallander
  • 336
  • 3
  • 16
  • Instead of creating a 3rd preview div, why not simply merge the two images via their data? If that is a possibility I can submit an answer with code. – EvilZebra Oct 16 '14 at 13:48
  • I wasn't sure if that was possible. If you can do it that'd be amazing! :D – Mallander Oct 16 '14 at 13:54

1 Answers1

6

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.

EvilZebra
  • 1,072
  • 8
  • 18
  • So in regards to the variables passed into the function, can I just use the commented out `var img = canvas.toDataURL("image/png");`? Or do I need to use `ctx.getImageData();` on both? – Mallander Oct 16 '14 at 14:28
  • You need to use `getImageData()` because `toDataURL()` returns a base64 string not an object which you can work with. – EvilZebra Oct 16 '14 at 14:29
  • Could you explain how I would use `getImageData` with the returned canvas? (I've not used it before, first time) The returned canvas isn't actually an element so I don't know how to use it :P – Mallander Oct 16 '14 at 15:06
  • It's really not that hard to Google things :/ you need to use [putImageData](http://www.w3schools.com/tags/canvas_putimagedata.asp) – EvilZebra Oct 16 '14 at 15:08
  • I have been Googling it, but I'm not sure how to link that with your function. Do I need to set a variable to the `putImageData` then pass that into the function? – Mallander Oct 16 '14 at 15:13
  • Updated my answer, hope that helps. – EvilZebra Oct 16 '14 at 15:17
  • Thanks for updating the answer, helped a lot! I've got your code working (At least there are no errors), now I'm just trying to get the `ctx2.putImageData(merged, 0, 0)` into a DataURL that I can use in my download function. Is that possible to do? – Mallander Oct 16 '14 at 15:56
  • Yup! That will work. Make sure you aren't only copy-pasting. `getImageData()` also requires x/y and width/height parameters. – EvilZebra Oct 16 '14 at 15:58
  • Yeah got this: var bData = bottomCtx.getImageData(0, 0, canvasBottom.width, canvasBottom.height); var tData = topCtx.getImageData(0, 0, canvasTop.width, canvasTop.height); – Mallander Oct 16 '14 at 15:59
  • Thanks for all the help! I should get it from here ;P – Mallander Oct 16 '14 at 15:59