gpt4 book ai didi

javascript - Html2Canvas 无法捕获图像

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:49 25 4
gpt4 key购买 nike

我在下面的代码中包含一个选项卡,其中包含一个图像,我正在使用库 HTML2Canvas 捕获整个 div 并下载它,但我不确定为什么它不捕获图像。

我尝试添加配置选项 allowTaint:true 但它仍然给我这个错误 DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvas may not be exported. 我不确定为什么会发生这种情况,但非常感谢任何帮助。

function sendData() {
html2canvas(document.getElementById('capture')).then(function(canvas) {
$('#test').append(canvas);
$('#test').attr('href', canvas.toDataURL('image/png'));
$('#test').attr('download', 'Test.png');
$('#test')[0].click();
});
}


function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}

document.getElementById("defaultOpen").click();
body {
font-family: Arial;
}

.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
margin-top: 10px;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}
/* Style the buttons inside the tab */

.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
border-bottom: 8px;
}
/* Change background color of buttons on hover */

.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */

.tab button.active {
background-color: #ccc;
}
/* Style the tab content */

.tabcontent {
display: none;
padding: 6px 25px;
border: 1px solid #ccc;
border-top: none;
-webkit-animation: fadeEffect 1s;
animation: fadeEffect 1s;
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
background-color: white;
}

.jobs-panel {
display: table;
max-height: 100%;
width: 85%;
background-color: #b7bcbe;
margin-left: auto;
margin-right: auto;
margin-top: 25px;
margin-bottom: 25px;
padding-bottom: 20px;
padding-top: 20px;
}

.tabwidth {
width: 85%;
margin: 0 auto;
}
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

<head>
<meta charset="utf-8" />

<link rel="shortcut icon" href="//#" />
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
</head>

<body>
<div id="capture">
<h1>Hello My man</h1>
<div class="jobs-panel">

<p>In this example, we use JavaScript to "click" on the London button, to open the tab on page load.</p>

<div class="tabwidth">
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent">
<h3>London</h3>
<img src="https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png" width="300" height="300">
<p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>

<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
</div>
</div>
</div>
<div id="match-button" onclick="sendData();">capture</div>
<a id="test" href="#"></a>
</body>

</html>

最佳答案

改变

<img src="https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png" width="300" height="300">

<img src="https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png" width="300" height="300" crossorigin>

并在你的 js 中制作 allowTaint: false;

它会很好地工作,

更新 jsfiddel -> http://jsfiddle.net/qp4b8frw/14/

关于javascript - Html2Canvas 无法捕获图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51206608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com