-
The core of the flood fill effect is in the
animatefunction. This function is called repeatedly usingrequestAnimationFrame, creating a smooth animation. -
The key part of the effect is how the image is drawn onto the canvas:
context.drawImage( image, 0, row, image.width, 1, 0, 0, image.width * ratio, row * ratio + 4 );
Let's break down the parameters of this
drawImagecall:image: The source image being drawn.0, row: The x and y coordinates in the source image where to start copying.image.width, 1: The width and height of the slice being copied from the source image. Note that the height is always 1 pixel.0, 0: The x and y coordinates on the canvas where to start drawing.image.width * ratio: The width to draw the image slice on the canvas, scaled to fit.row * ratio + 4: The height to draw on the canvas. This is key to the fill effect.
-
The flood fill effect is created by progressively revealing more of the image from bottom to top:
- The
rowvariable starts atimage.height(the bottom of the image). - In each frame,
rowis decreased by thespeedvalue:row = row - speed; - This means each frame draws a slightly larger portion of the image, creating the illusion of the image being "filled" from bottom to top.
- The
-
The
+ 4inrow * ratio + 4creates a slight overlap between frames. This ensures there are no gaps in the fill effect, even at higher speeds. -
The speed of the fill can be adjusted by the user via the slider, which changes the
speedvariable. A higher speed value means larger chunks of the image are revealed in each frame, making the fill effect faster. -
The effect continues until
rowbecomes 0 or negative, at which point the entire image has been revealed. -
Once an image is fully revealed, the code moves to the next image in the array and starts the process again.
This approach creates a smooth, continuous fill effect that works for any image size, maintaining aspect ratio and fitting within the canvas bounds. The effect is visually interesting because it gradually reveals the image content, creating a sense of anticipation for the viewer.