Generate random images in canvas in javascript

Good afternoon, I am trying to make a javascript game using a canvas. I want to create random enemy objects. So far I've found this as a spawning example: JSFiddle Demo

How can I upload images instead of balls? Any help would be great. Tnx.

// get a refrence to the canvas and its context
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

// newly spawned objects start at Y=25
var spawnLineY = 25;

// spawn a new object every 1500ms
var spawnRate = 1500;

// set how fast the objects will fall
var spawnRateOfDescent = 0.50;

// when was the last object spawned
var lastSpawn = -1;

// this array holds all spawned object
var objects = [];

// save the starting time (used to calc elapsed time)
var startTime = Date.now();

// start animating
animate();


function spawnRandomObject() {

// select a random type for this new object
var t;

// About Math.random()
// Math.random() generates a semi-random number
// between 0-1. So to randomly decide if the next object
// will be A or B, we say if the random# is 0-.49 we
// create A and if the random# is .50-1.00 we create B

if (Math.random() < 0.50) {
    t = "red";
} else {
    t = "blue";
}

// create the new object
var object = {
    // set this objects type
    type: t,
    // set x randomly but at least 15px off the canvas edges
    x: Math.random() * (canvas.width - 30) + 15,
    // set y to start on the line where objects are spawned
    y: spawnLineY,
}

// add the new object to the objects[] array
objects.push(object);
}



function animate() {

// get the elapsed time
var time = Date.now();

// see if its time to spawn a new object
if (time > (lastSpawn + spawnRate)) {
    lastSpawn = time;
    spawnRandomObject();
}

// request another animation frame
requestAnimationFrame(animate);

// clear the canvas so all objects can be 
// redrawn in new positions
ctx.clearRect(0, 0, canvas.width, canvas.height);

// draw the line where new objects are spawned
ctx.beginPath();
ctx.moveTo(0, spawnLineY);
ctx.lineTo(canvas.width, spawnLineY);
ctx.stroke();

// move each object down the canvas
for (var i = 0; i < objects.length; i++) {
    var object = objects[i];
    object.y += spawnRateOfDescent;
    ctx.beginPath();
    ctx.arc(object.x, object.y, 8, 0, Math.PI * 2);
    ctx.closePath();
    ctx.fillStyle = object.type;
    ctx.fill();
}

}

      

+3


source to share


1 answer


Script here

One way is to have a global array of loaded images:

var images = [img1, img2, ... ];

      

When an object is created, it is assigned randomly to one of the images in the array images

. We can then render this image using the ctx.drawImage

: method ctx.drawImage(object.image,...))

. The important thing to remember now is that you need to load all of your images first like this:

var img1 = new Image();
img.src = "...";

      

And to only start the animation on load, we can use onload

:



window.onload = function() {
    animate();
}

      

Here is the obj.image property that we add to our creation object

. This is a selection of a random image from our array images

.

var object = {
    ...
    // give random image
    image: images[Math.floor(Math.random()*images.length)]
}

      

In any case, we can remove the drawing functions arc

and draw, and use object.image

to draw:

for (var i = 0; i < objects.length; i++) {
    var object = objects[i];
    object.y += spawnRateOfDescent;
    ctx.drawImage(object.image, object.x, object.y, 30, 30);
}

      

+2


source







All Articles