从单个页面保存/下载多个画布
我想在单个页面上下载HTML2Canvas创建的多个Canvas,单击1点。下载1帆布工作正常。
我试图在函数sendtocanvas
中创建一个循环,遍历每个ID(我知道我要发送到canvas的每个HTML元素的ID,然后下载),但是它没有期望的结果。
我期望将4个文件下载为JPEG,每个文件都命名为file_1,file_2,file_3和file_4,
我假设在函数中只循环每个ID并不是要走的方法。我没有编写我试图适应我的需求的原始代码
var button = document.getElementById('download');
button.addEventListener('click', sendToCanvas);
function download( canvas, filename ) {
// create an "off-screen" anchor tag
const a = document.createElement('a');
// the key here is to set the download attribute of the a tag
a.download = filename;
// convert canvas content to data-uri for link. When download
// attribute is set the content pointed to by link will be
// pushed as "download" in HTML5 capable browsers
a.href = canvas.toDataURL("image/jpeg;base64, 0.5");
a.style.display = 'none';
document.body.appendChild( a );
a.click();
document.body.removeChild( a );
}
function sendToCanvas(event){
var toCanvas = ["#redBlock", "#blueBlock", "#greenBlock", "#yellowBlock"]
for (let i = 0; i < toCanvas.length; i++) {
const element = document.querySelector(toCanvas[i]);
html2canvas(element, {
scale: 1,
useCORS: true,
})
.then( ( canvas ) => {
download( canvas, 'file_' + (i + 1) );
});
}
}
#redBlock {
width:100px;
height:100px;
background-color:red;
}
#blueBlock {
width:100px;
height:100px;
background-color:blue;
}
#greenBlock {
width:100px;
height:100px;
background-color:green;
}
#yellowBlock {
width:100px;
height:100px;
background-color:yellow;
}
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<button id="download">Download</button>
<section id="redBlock">
</section>
<section id="blueBlock">
</section>
<section id="greenBlock">
</section>
<section id="yellowBlock">
</section>
I want to download multiple canvas created by html2canvas on a single page with 1 click. Downloading 1 canvas worked fine.
I have tried to create a loop within the function sendToCanvas
, looping through each ID (as I do know the ID of each HTML element I am sending to canvas then to download) but it doesn't have the desired result.
I was expecting 4 files to download as jpeg, each named file_1, file_2, file_3 and file_4
I assume just looping each ID in the function isn't the way to go. I didn't write the original code I am trying to adapt it for my needs
var button = document.getElementById('download');
button.addEventListener('click', sendToCanvas);
function download( canvas, filename ) {
// create an "off-screen" anchor tag
const a = document.createElement('a');
// the key here is to set the download attribute of the a tag
a.download = filename;
// convert canvas content to data-uri for link. When download
// attribute is set the content pointed to by link will be
// pushed as "download" in HTML5 capable browsers
a.href = canvas.toDataURL("image/jpeg;base64, 0.5");
a.style.display = 'none';
document.body.appendChild( a );
a.click();
document.body.removeChild( a );
}
function sendToCanvas(event){
var toCanvas = ["#redBlock", "#blueBlock", "#greenBlock", "#yellowBlock"]
for (let i = 0; i < toCanvas.length; i++) {
const element = document.querySelector(toCanvas[i]);
html2canvas(element, {
scale: 1,
useCORS: true,
})
.then( ( canvas ) => {
download( canvas, 'file_' + (i + 1) );
});
}
}
#redBlock {
width:100px;
height:100px;
background-color:red;
}
#blueBlock {
width:100px;
height:100px;
background-color:blue;
}
#greenBlock {
width:100px;
height:100px;
background-color:green;
}
#yellowBlock {
width:100px;
height:100px;
background-color:yellow;
}
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<button id="download">Download</button>
<section id="redBlock">
</section>
<section id="blueBlock">
</section>
<section id="greenBlock">
</section>
<section id="yellowBlock">
</section>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用async进行循环,然后也许在您的下载功能中,您可以将所有块都放在带有jszip的库中的zip文件中
但是,我认为这不是解决此问题的最佳方法。
: https://github.com/github.com/niklasvh/niklasvh/html2canvas/sissues/1414141413
请参阅 - &gt; https://stuk.github.io/jszip/
等待... https> https:https:// devaxinger。 mozilla.org/en-us/docs/web/javascript/reference/statement/for-await...of
You could use an async for loop, then maybe in your download function you can put all of your blocks in a zip file with a library like JSZip
However i think this is not the most optimal way solve this.
See: https://github.com/niklasvh/html2canvas/issues/1413
Jszip -> https://stuk.github.io/jszip/
for await...of -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of