Back to Snippets

AI Image Generation Loading Animation JS

A loading state for AI image generation that looks like the picture coming into focus. The frame starts as colored grain, then sharpens over 30 steps while the progress bar, the step counter and the percentage keep pace. Cancel stops it at the current step, Save image downloads the finished PNG, and Generate again paints a new picture from a new seed.

The picture is painted on a canvas that is never added to the page, with a seeded random number generator (a lake, a sun and three ridges), so one seed always gives the same scene and no image file is loaded. Until step 25, each frame draws that scene into a smaller canvas and scales it back up, which blurs it, then adds random grain with getImageData and putImageData. Both effects shrink as the steps go on. The blur is gone by step 25 and the grain by step 29, so the last frame is the sharp scene.

The 2D context is requested with willReadFrequently, which tells the browser its pixels will be read back often. The canvas has the img role and an aria-label that changes from generating to a description of the result, and the progress bar reports its value to screen readers. For visitors who prefer reduced motion the grain is skipped and only the blur clears. If your API streams preview frames, draw those in place of the painted scene.

1
#javascript
#css
#ai
#image generation
#loading animation
#canvas #progress bar
#noise