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

255
Visualizações
Why is my js to create raining bowties not working?

I am making a project where I need to make an html canvas with raining bowtie images. Here is my js:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
let img = new Image("images.png")
var ok2animate = true;
img.onload = function(){
function Drop() {
    this.x = Math.random() * (canvas.width - 20);
    this.y = -Math.random() * 20;
    this.fallRate = Math.random()*.5+.5;
}
//
Drop.prototype.draw = function () {
  //   this.x;
  // this.y;
  
  ctx.drawImage(img, this.x, this.y)
    return (this);
}
//
Drop.prototype.fall = function () {
    this.y += this.fallRate;
    return (this);
}

function animate() {

    // request another animation frame
    if (ok2animate) {
        requestAnimationFrame(animate);
    }

  
        ctx.fillRect(0,0,canvas.width,canvas.height)
    //ctx.clearRect(0, 0, canvas.width, canvas.height);

    // make all drops fall and then redraw them
    
    for (var i = 0; i < drops.length; i++) {
        drops[i].fall().draw();
    }

}





// an array of objects each representing 1 drop
var drops = [];

// add some test drops
for (var i = 0; i < 10; i++) {
    drops.push(new Drop());
}
setInterval(function(){requestAnimationFrame(animate)}, 1000)
requestAnimationFrame(animate)
}
<canvas id="canvas" width=300 height=300></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

How do I make it so the images rain down at 1 second intervals? (I have included the image file in the same folder) I have tried using an image file from the web, using a bigger canvas, uwing other versions of jquery, but none of those worked.

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

0

Image creation

You can create a new image with the Image constructor. You already did that, however, the arguments that the constructor accepts are the width and the height of the image, not the url. You need the src property of the image to reference the image file.

And since you only use 1 single image, that isn't changing, you only have to create and load it once. You can reuse the same image for as many times as you want.

Starting the loop

Now, you need to start the loop after all the images (which in this case is one) has been loaded. Listen for the onload event on the image and call the setInterval function whenever the image is loaded.
setInterval will start of the rendering here, so there is no need to call requestAnimationFrame(animate) anywhere else.

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

// Create the image like this.
const image = new Image(100, 100);
image.src = "https://www.fillmurray.com/100/100";

function Drop() {
  this.x = Math.random() * (canvas.width - 20);
  this.y = -Math.random() * 20;
  this.fallRate = Math.random() * 10 + 0.5;
}

Drop.prototype.draw = function() {
  ctx.drawImage(image, this.x, this.y)
  return this;
}

Drop.prototype.fall = function() {
  this.y += this.fallRate;
  return this;
}

const drops = [];
for (let i = 0; i < 10; i++) {
  drops.push(new Drop());
}

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.fillRect(0, 0, canvas.width, canvas.height)

  for (const drop of drops) {
    drop.fall().draw();
  }
}

image.onload = () => {
  setInterval(function() {
    requestAnimationFrame(animate)
  }, 1000)
};
<canvas id="canvas" width=300 height=300></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