I'm creating a game using HTML5 and JavaScript, and it is going to be using a ton of images. Having all of those images at once takes a lot of memory, and I was wondering how I could load/unload different images so that whoever plays the game doesn't need 8GBs of memory or so just for the images.
<img src = "source1" id = "img1"></img>
<img src = "source2" id = "img2"></img>
<img src = "source3" id = "img3"></img>
<!--Just imagine this for every image, which is likely going to peak over 10,000 at some point-->
<img src = "source10000" id = "img10000"></img>
<canvas width = "1350" height = "600" id = "canva"></canvas>
<script>
var canvas = document.getElementById("canva");
var can = canvas.getContext("2d");
var imge = function(im, x, y, w, h) {
can.drawImage(document.getElementById(im), x, y, w, h);
};
var playerX = 0;
var playerY = 0;
var run = function() {
imge("img1", playerX, playerY, 32, 32);
//Need to use tones of images
requestAnimationFrame(run);
};
run();
</script>
I don't need to use 10,000 images at once, more like have 60 at once(player animations, blocks, enemy animations, particles, etc), and since I'm early in development I'm open to having to redo stuff, like how I grab images.
Question: How can I load/unload images in HTML5/JavaScrpit