Adding an animating GIF to a canvas is hard, but adding basic animation from a spritesheet is easy.
Just added very simple frame based animation to mine (with hideously naive sequence search
) and made it properly 2D so you can use it for horizontal and vertical scrolling:
parallax2.htmlvar layers = [
{file:'back.png', step:[0, 0]},
{file:'far.png', step:[-1/8, 0], wrap:[true, false]},
{file:'mid.png', step:[-1/2, 0], wrap:[true, false]},
{file:'near.png', step:[-1, 0], wrap:[true, false]},
{file:'char.png', step:[0, 0], position:[140, 160], frames:3, sequence:[[0, 5], [1, 5], [2, 5], [1, 5]]},
{file:'snow.png', step:[-1, 1], wrap:[true, true]},
];
step is now a 2d vector (x, y)
wrap specifies layer wrapping along each axis (x, y) (defaults to [false, false] if omitted)
position specifies the placement of the top-left corner of the layer (defaults to [0, 0] if omitted)
frames is the number of frames in the spritesheet
sequence is an array of frame number, duration (in ticks) pairs.
ATM the sprite sheet should be laid out horizontally with each frame equally sized and no padding. Each frame is the height of the full image and the width of the image divided by number of frames.