Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda