问题描述
使用HTML2Canvas我试图捕获一个googleMap(它工作正常),然后捕获一个 #overlay
div的顶部
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).
然后,该页面显示这两个(这是允许人们在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.
我到目前为止的问题是,似乎HTML2Canvas没有拾起它创建的画布
元素,当我尝试和捕获我的 #preview
div。
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:
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();
}
});
}
正如你所看到的,我试着延迟 DownloadURI
函数,以防止画布未及时加载,但这不是问题。我不确定是否有一些 canvas
元素不能使用插件。
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.
工作原理:我的上述代码相当简单。当一个HTML2Canvas完成时,它运行另一个HTML2Canvas。这两个画布都附加到 #preview
元素,以便我可以捕获该元素与2个画布上面的彼此。一旦完成,它运行另一个函数捕获 #preview
元素,我希望将结合2个图像一个在
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.
推荐答案
此函数接受两个参数 top
和 / code>。这两个都是ImageData对象,你可以通过
ctx.getImageData()
获得。底部的ImageData返回顶部数据合并到它。此函数使用Porter-Duff方法合并颜色。
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;
}
要使用此函数并返回数据, p>
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.
这篇关于HTML2Canvas将2个捕获的画布合并成一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!