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
<div> <p>With this CSS:<br> </p> <pre class="brush:php;toolbar:false"> <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:
<canvas> <p>Screenshot:<br> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668501927013.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How I managed to render million small images on a webpage"></p> <p>This approach is nice because it allows me to render everything via code which will make more advanced features easier.</p> <h3> Deciding on image tags or canvas </h3> <p>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. </p> <p>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.</p> <h2> Optimize tile delivery </h2> <p>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. </p> <p>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:</p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668502115888.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How I managed to render million small images on a webpage"></p> <p>*<em>That would become 510 * 8 = 4080 images! *</em></p> <p>It is not realistic to render that many images so fast. The solution? Combine the individual tiles in larger blocks.</p> <p>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.</p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668502382486.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How I managed to render million small images on a webpage"></p> <p>The script will add a question mark for unfilled spots.</p> <p>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!</p> <h2> Hiding the blocks </h2> <p>I've implemented a few things to 'hide' that the tiles are loaded in larger blocks.</p> <h3> Loading screen always has 64x64 tiles </h3> <p>Loading screen<br> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668502441571.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How I managed to render million small images on a webpage"></p> <h3> Full grid is always rendered square </h3> <p>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.</p> <p>Thank you for reading this artile, I hope you've learned something. <br> If you did, why not add your favorite programming language, crypto coin or your pet to the 10MPage? It is free!</p> </canvas>
The above is the detailed content of How I managed to render million small images on a webpage. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
