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

308
Visualizações
HTML canvas failing to draw after adding elements dynamically

I am attempting to draw using canvas, and everything works only for a moment: after I append elements to the div (#workbench) which is the parent for my canvas, 1. drawing gets removed 2. Any more attempts at drawing using this canvas do not work.

Screenshot: a test image drawn with canvas, green background is the canvas space, pink is the website background

And this is the JS code: (called once)

var canvas1 = document.querySelector('#workbench_canvas');
var workplace = document.querySelector('#workbench');

console.log(canvas1);
if (!canvas1 || !canvas1.getContext) {
    console.error("ERROR");
}


canvas1.width       = workplace.offsetWidth;
canvas1.height      = workplace.offsetHeight;

(called every time I want to draw shapes)

function draw1(){   
    let ctx = canvas1.getContext('2d');
    console.log("drawing with context", ctx);

    ctx.fillStyle='yellow';
    for (var i=0;i<5;++i) ctx.fillRect(i*18+2,2,16,16);

    ctx.fillStyle = '#FF0000';
    ctx.beginPath();
    ctx.moveTo(100, 140);
    ctx.lineTo(300, 140);
    ctx.lineTo(200, 100);
    ctx.fill();

    ctx.fillStyle = '#D0D044';
    ctx.fillRect(120, 140, 160, 550);
    ctx.fillStyle = '#2222AA';
    ctx.fillRect(200, 180, 20, 20);
    return "Successfully drawn";
}
draw1();

about 4 years ago · Juan Pablo Isaza
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