I have the following JavaScript code, which I am using to try and loop through an array of images, and draw each one to the HTML5 canvas at different locations:
/*First, create variables for the x & y coordinates of the image that will be drawn.
the x & y coordinates should hold random numbers, so that the images will be
drawn in random locations on the canvas.*/
var imageX = Math.floor(Math.random()*100);
var imageY = Math.floor(Math.random()*100);
/*Create a 'table' of positions that the images will be drawn to */
var imagePositionsX = [20, 80, 140, 200, 260, 320, 380, 440, 500, 560];
var imagePositionsY = [20, 60, 100, 140, 180, 220, 260, 300, 340, 380];
var randomPositionX = Math.floor(Math.random()*10);
var randomPositionY = Math.floor(Math.random()*10);
/*Draw all images from assetsImageArray */
/*Use a while loop to loop through the array, get each item and draw it. */
var arrayIteration = 0;
console.log('Assets Image Array length: ' + assetsImageArray.length); /*Display the length of the array in the console, to check it's holding the correct number of images. */
while(arrayIteration < assetsImageArray.length){
context.drawImage(assetsImageArray[arrayIteration], imageX, imageY, 50, 50);
console.log(arrayIteration); /*Display the current array position that's being drawn */
arrayIteration = arrayIteration+1;
/*Now try changing the values of imageX & imageY so that the next image is drawn to a
different location*/
imageX = imagePositionsX[randomPositionX]; /* imageX+(Math.floor(Math.random()*100)); */
imageY = imagePositionsY[randomPositionY]; /* imageY+(Math.floor(Math.random()*100)); */
}
The intended logic behind this code is: first the arrayIteration variable is set to 0, then I display the number of images in my assetsImageArray in the console. Then the while loop starts- while the arrayIteration variable is less than the number of items in the assetsImageArray, the image at position arrayIteration (i.e. position 0 at this point), of the assetsImageArray should be drawn to the canvas at coordinates imageX and imageY.
The imageX and imageY variables both hold random numbers assigned to them by the code Math.floor(Math.random()*100);
After drawing the first image to the canvas at the specified location, the console should then log the array element that's just been drawn.
The next line should then increment the arrayIteration variable, and then finally, the values of imageX and imageY should be updated by taking a the value of a random element from the arrays imagePositionsX and imagePositionsY respectively.
The while loop will iterate through this code, drawing each image from assetsImageArray to a new location on the canvas, until all of the images from the array have been drawn.
However, for some reason only the first and last images from my assetsImageArray are being drawn to the canvas. They are being drawn in random locations- which is what I want, and every time I reload the page, they are drawn to new positions on the canvas, but I can't figure out why none of the images in the array positions between first and last are being drawn to the canvas. I assume there's something wrong with my logic somewhere, but can't figure out where.
Is anyone able to spot it?