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

118
Visualizações
Dom doesn't update right away when using convertToBlob on an Offscreen canvas

I have an application that needs to generate a couple thousand images. The way that I'm doing that is with a set of preloaded pngs (acting as transparent layers) and an offscreen canvas. I draw the images onto the canvas, convert it to a blob, and then write the image to a div using a custom class called Images.

I want to show a loading bar and clear old images, first, but there is a 3-5 second delay before the dom updates even though the "empty()" and "show()" code is at the beginning of the click request.

Is there something I'm doing wrong with regard to the asynchrony or promises that is causing the dom to not update immediately?

Here's some of the code:

// Generate Images
$("#generate_images").click(function(){
    // Show loading bar
    $("#progress_bar .progress-bar").css("width", "0%");
    $("#progress_bar").show(0);

    // Show loading spinner
    $("#loading").show(0);
    $("#images").empty();

    console.log("Generating Images");

    $.each(images, function(id, image){
        // Sort traits (png layers)
        var traits = Object.values(image.traits).sort((a, b) => {
            return a.z_index - b.z_index;
        });
        images[id].layers = [];
        $.each(traits, function(trait_idx, trait){
            images[id].layers.push(preloaded_images[trait.variant_id])
        });
    });

    // Create canvas
    var canvas = new OffscreenCanvas(1200, 1200);
    var context = canvas.getContext("2d");

    console.log("Generating canvases.");
    $.each(images, function(id, image){
        // Clear the canvas
        context.clearRect(0, 0, canvas.width, canvas.height);

        // Draw each image layer
        $.each(images.layers, function(src, layer){
            context.drawImage(layer, 0, 0);
        });

        // Add imageData to screen
        canvas.convertToBlob().then(function(blob) {
            // Do something with the blob like render to the screen
        });
    });
});

Any thoughts on making this more efficient would also be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Generating a thousand images is not something a browser is necessarily meant to do, but you need to give the DOM a chance to update so add a timeout

  1. Collect what you want to do in an array
  2. Do not loop, but instead do

  function generate() {
    if (arrayCounter >= array.length) return
    canvas.convertToBlob().then(function(blob) {
      if (success) {
        arrayCounter++
        generate()
      }
    }
  }
  collect()
  generate()
about 4 years ago · Juan Pablo Isaza Relatório
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