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

314
Visualizações
HTML5 ( canvas, javascript ) The image, makes traces

enter image description herefirstly sorry for my english. Second, I have a problem with my javascript. I trying to make something like baner which changes images inside. But when i want to contiune my script, the last image out of canvas field makes me problem. Its makes leaving traces. Its my code.

<html>
<head>
    <link rel="stylesheet" href="style.css">
    <script>
       
       var cat= new Image();

       function init(){
           cat.src = 'cat.png';
           window.requestAnimationFrame(draw);
       }

       function draw() {

        var context = document.getElementById('spin').getContext('2d');
        
        context.clearRect(0, 0, 530, 110);
        context.save();
        context.translate(-1, 0);
        
     
        context.drawImage(cat, 5, 0);
        context.drawImage(cat, 110, 0);
        context.drawImage(cat, 215, 0);
        context.drawImage(cat, 320, 0);
        context.drawImage(cat, 425, 0);
        context.drawImage(cat, 530, 0); /// its an image with problem
        
        
        
        
        

        window.requestAnimationFrame(draw);

       }

       init();

     
    </script>
</head>
<body>

 <div class="roulette">
       <canvas id="spin" width="530" height="110"></canvas>
    </div>

</body>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

When loading images in javascript, note that this process is asynchronous. When assigning a value to the src attribute of an Image element, you need to get the element in the onload callback function of this element. Otherwise, when drawing to the canvas, the element may not have fully loaded the corresponding image resource.

const img = new Image()

img.onload = function () {
  // img fully loaded here
}

img.src = 'path/to/image/sources'
about 4 years ago · Juan Pablo Isaza Relatório

0

You never call context.restore(), so you are stacking a lot of CanvasStates in memory (which will make your whole application slow down in few minutes), and more directly noticeable, your context transform is never reset, meaning that at the second call the coordinate 0, 0 will be the pixels at coordinates -1, 0 and at third call -2, 0 etc. which will make your call to clearRect() miss one more pixel on the right side of the canvas every time.

It's quite unclear what you were after, but to correctly clear your context, call context.setTransform(1, 0, 0, 1, 0, 0) before calling clearRect(), this will reset the transformation matrix to its identity matrix.
Then if you want to translate your context by an ever decreasing value, store that value in a variable and use it in your call to translate().
Finally, remove that call to save() because it's not needed.

let x = 0; // the variable where we store the current offset
var cat = new Image();

function init() {
  cat.src = 'https://picsum.photos/110/110';
  // always wait for your image has loaded before doing anything with it
  cat.onload = evt =>
    window.requestAnimationFrame(draw);
}

function draw() {

  var context = document.getElementById('spin').getContext('2d');
  // update the offset
  x -= 1;
  // last image is hidden, stop the loop?
  if (x < (530 + 110) * -1) {
    return;
  }
  // reset the context matrix
  context.setTransform(1, 0, 0, 1, 0, 0);
  // clear the full canvas
  context.clearRect(0, 0, 530, 110);
  // set the updated offset
  context.translate(x, 0);

  context.drawImage(cat, 5, 0);
  context.drawImage(cat, 110, 0);
  context.drawImage(cat, 215, 0);
  context.drawImage(cat, 320, 0);
  context.drawImage(cat, 425, 0);
  context.drawImage(cat, 530, 0);

  window.requestAnimationFrame(draw);

}

init();
<canvas width="530" height="110" id="spin"></canvas>

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