I am building 10MPage.com which captures the state of the internet in 2025. Every internet user is allowed to upload a small image of 64x64 pixels and contribute to this archive.
As the name suggests it needs to be able to handle 10 million of these small images. When I first came up with this concept I was concerned on how to render these efficiently. In this article I will talk about my first tries of doing is and the final solution.
Before you continue, take a look at 10MPage.com and see if you can figure out how it's done. And if you've reached 10MPage, why not claim a tile for yourself? :)
Image tags versus canvas
The first choice I had to make was if I wanted to use HTML elements or a full screen canvas.
Seperate image tags
I initially tested with seperate <img
tags for each tile, I wrote a script to generate 1024 images for a 32x32 grid and put them on the page like this using Blade:
<div class="grid" id="grid">
@for($y = 0; $y < 32; $y++)
<div class="row">
@for($x = 0; $x < 32; $x++)
<div class="tile"><img src="http://10mpage.test/tiles/{{$y}}x{{$x}}.png" alt="Tile {{$y}}x{{$x}}"></div>
@endfor
</div>
@endfor
</div>
With this CSS:
<style>
body {
margin: 0;
padding: 0;
overflow: auto; /* Enable scrolling */
}
.grid {
display: block;
position: relative;
width: 100%; /* The grid will take the full width */
}
.row {
display: flex; /* Each row is a flex container */
}
.tile {
width: 64px;
height: 64px;
box-sizing: border-box;
border: 1px solid #ccc; /* Visual separation between tiles */
}
.tile img {
width: 64px;
height: 64px;
object-fit: cover;
}
</style>
This is what it looks like:
Which is fine but there are a few points that could be an issue:
- Browser scroll
- Large DOM
- Lazy loading
Canvas
The next approach was via a canvas, for simplicity I decided to just draw a checkerboard. Adding scrolling was easy too, this is what that looked like:
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const tileSize = 64;
let scale = 1;
let translateX = 0;
let translateY = 0;
let isPanning = false, startX, startY;
// Draw the checkerboard grid
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.translate(translateX, translateY);
ctx.scale(scale, scale);
// Calculate the number of tiles to draw based on the viewport
const cols = Math.ceil(canvas.width / (tileSize * scale)) + 2; // Extra tiles for overflow
const rows = Math.ceil(canvas.height / (tileSize * scale)) + 2; // Extra tiles for overflow
// Calculate the starting positions based on translation and scale
const startCol = Math.floor(-translateX / (tileSize * scale));
const startRow = Math.floor(-translateY / (tileSize * scale));
for (let x = startCol; x < startCol + cols; x++) {
for (let y = startRow; y < startRow + rows; y++) {
// Alternate between black and white tiles
ctx.fillStyle = (x + y) % 2 === 0 ? 'black' : 'white';
ctx.fillRect(x * tileSize, y * tileSize, tileSize, tileSize);
}
}
ctx.restore();
}
// Smooth zoom function centered on mouse position
function zoom(event) {
event.preventDefault();
const zoomIntensity = 0.05; // Smaller for smoother zoom
const mouseX = event.clientX;
const mouseY = event.clientY;
const wheelDelta = event.deltaY > 0 ? -1 : 1; // Scroll direction
// Calculate zoom factor (smaller for smoother transitions)
const zoomFactor = Math.exp(wheelDelta * zoomIntensity);
// Adjust the translation to zoom toward the mouse position
translateX -= (mouseX - translateX) * (zoomFactor - 1);
translateY -= (mouseY - translateY) * (zoomFactor - 1);
// Apply the zoom factor
scale *= zoomFactor;
drawGrid();
}
// Panning functions
function startPan(event) {
isPanning = true;
startX = event.clientX - translateX;
startY = event.clientY - translateY;
}
function pan(event) {
if (!isPanning) return;
translateX = event.clientX - startX;
translateY = event.clientY - startY;
drawGrid();
}
function endPan() {
isPanning = false;
}
// Event listeners
window.addEventListener('wheel', zoom);
canvas.addEventListener('mousedown', startPan);
canvas.addEventListener('mousemove', pan);
canvas.addEventListener('mouseup', endPan);
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
drawGrid();
});
// Initial drawing of the grid
drawGrid();
</script>
</body>
This approach is nice because it allows me to render everything via code which will make more advanced features easier.
Deciding on image tags or canvas
Ultimately I decided on using canvas because it is more flexible than divs. This is because of things like loading animations, smooth scrolling, lazy loading and the fact that it is enterly rendered via code which gives a lot of control.
But loading a lot of small images causes a lot of overhead, to minimize that I want to bundle the small images in larger blocks.
Optimize tile delivery
Loading each image separately adds a lot of network request. Let's quickly calculate on a 1080p screen. The width is 1920 pixels which would be 1920 / 64 = 30 tiles. With a height of 1080 pixels that would become 1080 / 64 = ~17 tiles. So to render a full screen of tiles on a fullHD display it would require rendering 30*17 = 510 small images.
But we need to be able to scroll! And when scrolling I do not want to show a lot of loading icons before rendering. So that means that we have to pre load the surrounding images too. If we want to pre load around it we would need to add eight times the tiles. Imagine the black rectangle is the display:
*That would become 510 * 8 = 4080 images! *
It is not realistic to render that many images so fast. The solution? Combine the individual tiles in larger blocks.
Using PHP I wrote a controller that generates an image that contains contains 16*16 tiles. Each block is 64 * 16 = 1024 pixels in width and height. You can see this when you go to 10MPage and look in your network tab of your console.
The script will add a question mark for unfilled spots.
So instead of 4080 images for 1920 * 3 = 5760 pixels by 1080 * 3 = 3240 pixels we now only need 24 images: 5760 / 1024 = ~6, 3240 / 1024 = ~4 (both rounded up), 6*4 = 24. Which is doable!
Hiding the blocks
I've implemented a few things to 'hide' that the tiles are loaded in larger blocks.
Loading screen always has 64x64 tiles
Full grid is always rendered square
In order to hide large gaps on the bottom or right of the grid the grid will never load blocks if it is not square. You will not see a block at the bottom and then an empty block to the left or right of that.
Thank you for reading this artile, I hope you've learned something.
If you did, why not add your favorite programming language, crypto coin or your pet to the 10MPage? It is free!
Top comments (0)