我已完成截图按钮,必须制作所有正文的屏幕截图。
但在此屏幕截图中,谷歌地图并未显示出来。 我使用html2canvas库来做到这一点。 这是代码:
HTML:
<button type="button" class="btn btn-default btn-sm" id = 'screenShotBtn' onclick="takeScreenShot()">
JAVASCRIPT:
var takeScreenShot = function() {
var screenshot = {};
html2canvas(document.getElementById('screenAll'), {
onrendered: function (canvas) {
var tempcanvas=document.createElement('canvas');
tempcanvas.width=1350;
tempcanvas.height=700;
var context=tempcanvas.getContext('2d');
context.drawImage(canvas,0,0,1350,700,0,0,1350,700);
var link=document.createElement("a");
link.href=tempcanvas.toDataURL('image/jpg'); //function blocks CORS
link.download = 'screenshot.jpg';
link.click();
}
});
以下是制作屏幕截图时的内容:
你能告诉我什么错了吗?答案 0 :(得分:1)
我将这些属性添加到代码中并且可以正常工作:
var takeScreenShot = function() {
var screenshot = {};
html2canvas(document.getElementById('map'), {
useCORS: true,
optimized: false,
allowTaint: false,
onrendered: function (canvas) {
var tempcanvas=document.createElement('canvas');
tempcanvas.width=1350;
tempcanvas.height=700;
var context=tempcanvas.getContext('2d');
context.drawImage(canvas,0,0,1350,700,0,0,1350,700);
var link=document.createElement("a");
link.href=tempcanvas.toDataURL('image/jpg'); //function blocks CORS
link.download = 'screenshot.jpg';
link.click();
}
});
}
答案 1 :(得分:0)
我认为这是Chrome中存在的一个已知问题。
您可以尝试我找到的解决方法:
if(window.chrome) {// Fix for Chrome
var transform=$(".gm-style>div:first>div").css("transform");
var comp=transform.split(","); //split up the transform matrix
var mapleft=parseFloat(comp[4]); //get left value
var maptop=parseFloat(comp[5]); //get top value
$(".gm-style>div:first>div").css({ //get the map container. not sure if stable
"transform":"none",
"left":mapleft,
"top":maptop,
});
}
html2canvas([$("#map > div.g-map-canvas > div > div > div:nth-child(1)")[0]], {
logging: false,
useCORS: true,
onrendered: function (canvas) {
$('#screenshot').after(canvas);
if(window.chrome) {// Fix for Chrome
$(".gm-style>div:first>div").css({
left:0,
top:0,
"transform":transform
});
}
}
});
答案 2 :(得分:0)
var fin = document.getElementById("map");
html2canvas(fin, {
useCORS: true,
onrendered: function(canvas) {
var dataUrl= canvas.toDataURL("image/png");
$('#map_image').attr('src',dataUrl).show();
}
});