I want to store several blobs from canvas elements using canvas.toBlob function to an array.
I've tried the code below which was useless. The result showed that the .toBlob() function works like an async function, and I have no knowledge about it.
function saveAllPics(node_list) {
// convert canvas element to png
let pics = [];
for (let canvas of node_list) {
canvas.toBlob(function(blob) {
pics.push(blob);
console.log(pics);
});
}
let zipPics = function(pics) {
// compress all pics into a zip file
let zip = new JSZip();
console.log("pics length:", pics.length);
for (let i = 0; i < pics.length; i++) {
let pic_data = pics[i];
console.log(i);
console.log(pic_data);
zip.file("pic-" + String(i) + ".png", pic_data, { binary: true });
}
console.log(zip);
}
setTimeout(function(pics) { zipPics(pics); }, 2000);
And the result:
pics length: 0
{files: {…}, comment: null, root: '', clone: ƒ}
[Blob]
(2) [Blob, Blob]
(3) [Blob, Blob, Blob]
(4) [Blob, Blob, Blob, Blob]
(5) [Blob, Blob, Blob, Blob, Blob]
(6) [Blob, Blob, Blob, Blob, Blob, Blob]
(7) [Blob, Blob, Blob, Blob, Blob, Blob, Blob]
(8) [Blob, Blob, Blob, Blob, Blob, Blob, Blob, Blob]
My final goal is to collect several canvas elements, then merge them into a PDF file (in a user script). However, I have found I was unable to make a PDF, so I decided to use Node.js.
Then, I decided to take it a step further: download the PNGs and ask the users to use another Python script to merge them to a PDF file. That's why I need a zip containing these blobs.