Let's dive into the details of how the flood fill effect is achieved in this code:
The core of the flood fill effect is in the animate
function.
This function is called repeatedly using
requestAnimationFrame
, 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 drawImage
call:
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:
row
variable starts at image.height
(the bottom
of the image).row
is decreased by the speed
value: row = row - speed;
The + 4
in row * ratio + 4
creates 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 speed
variable. A higher speed value means larger
chunks of the image are revealed in each frame, making the fill effect faster.
The effect continues until row
becomes 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.