Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
Passing Images to JSZIP in a Promise

I am drawing a series of images on to a canvas, and passing the canvas image into a zip folder with JsZip. The filenames for the series of images is represented in the drawOrder Array.

Since the images take time to load, the drawImage() is done with a Promise. The image loads successfully on the canvas, but the zip generated returns as an empty zip file.

I suspect this is because toBlob and Promise are both asynchronous functions? My understanding on asynchronous functions is a little weak, I hope someone could explain why it is not working and point me in the right direction.

//THE PROMISE
function promiseAll(order){
    return Promise.all(
        order.map(function (t) {
            return new Promise(function (resolve) {
                var img = new Image();
                img.src = "http://foo/" + t + ".png";
                img.onload = function () {
                    resolve(img);
                };
            });
        })
    );
};

function toCanvas() {
    var drawOrder = ["base1", "base2", "object1", "foreground1"];

    var canvas = document.querySelector("canvas");
    var ctx = canvas.getContext("2d");

    //Use Preloader
    promiseAll(drawOrder).then(function (allImages){
        console.log("Layers loaded", allImages);
        for (let k = 0; k < drawOrder.length; k++) {
            ctx.drawImage(allImages[k], 0, 0, canvas.width, canvas.height)
        };
        canvas.toBlob(function(blob) {
            zip.file("hello.png", blob);
        };
    });
};
zip.generateAsync({type:"base64"}).then(function (base64) {
    location.href="data:application/zip;base64," + base64;
});

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda