从单个页面保存/下载多个画布

发布于 2025-02-03 11:56:02 字数 2170 浏览 3 评论 0原文

我想在单个页面上下载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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

关于从前 2025-02-10 11:56:02

您可以使用async进行循环,然后也许在您的下载功能中,您可以将所有块都放在带有jszip的库中的zip文件中

let canvasArr = [];
var zip = new JSZip();
async function download(){
    for (const canvas of canvasArr) {
      index++;
      var data = canvas;
      var idx = data.indexOf('base64,') + 'base64,'.length;
      var content = data.substring(idx);
      zip.file('myFile' + index + '.extension', content, { base64: true });
   }
   zip.generateAsync({ type: 'blob' }).then((data: any) => {
      saveAs(data, `downloadFile.zip`);
   });
}

async function sendToCanvas(event){
      var toCanvas = ["#redBlock", "#blueBlock", "#greenBlock", "#yellowBlock"]
      for await (let i = 0; i < toCanvas.length; i++) {
        const element = document.querySelector(toCanvas[i]);
        const canvas = await html2canvas(element, {
          scale: 1,
          useCORS: true,
        });
       canvasArr.push(canvas);
     }
     await download();
}

但是,我认为这不是解决此问题的最佳方法。

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

let canvasArr = [];
var zip = new JSZip();
async function download(){
    for (const canvas of canvasArr) {
      index++;
      var data = canvas;
      var idx = data.indexOf('base64,') + 'base64,'.length;
      var content = data.substring(idx);
      zip.file('myFile' + index + '.extension', content, { base64: true });
   }
   zip.generateAsync({ type: 'blob' }).then((data: any) => {
      saveAs(data, `downloadFile.zip`);
   });
}

async function sendToCanvas(event){
      var toCanvas = ["#redBlock", "#blueBlock", "#greenBlock", "#yellowBlock"]
      for await (let i = 0; i < toCanvas.length; i++) {
        const element = document.querySelector(toCanvas[i]);
        const canvas = await html2canvas(element, {
          scale: 1,
          useCORS: true,
        });
       canvasArr.push(canvas);
     }
     await download();
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文