admin管理员组文章数量:1323157
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 bine 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: "",
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 plete, 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 plete it runs another function to take a capture of the #preview
element which I hoped would bine the 2 images one on top of the other into a single image that the user can download, but it just es back with an empty image.
If more information is needed let me know, thanks.
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 bine 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./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 plete, 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 plete it runs another function to take a capture of the #preview
element which I hoped would bine the 2 images one on top of the other into a single image that the user can download, but it just es back with an empty image.
If more information is needed let me know, thanks.
Share asked Oct 16, 2014 at 11:04 MallanderMallander 3363 silver badges16 bronze badges 2- 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 Commented Oct 16, 2014 at 13:48
- I wasn't sure if that was possible. If you can do it that'd be amazing! :D – Mallander Commented Oct 16, 2014 at 13:54
1 Answer
Reset to default 6This 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.
本文标签: javascriptHTML2Canvas combine 2 captured canvases into oneStack Overflow
版权声明:本文标题:javascript - HTML2Canvas combine 2 captured canvases into one - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742142554a2422646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论