Creates a new animation
The frames to create the animation from
Optionaloptions: AnimationCanvasOptions
The canvas size and the loop count, see AnimationCanvasOptions
The animation's duration (in ms)
The canvas height, like Animation.width; else the frames' bottom edge
The number of frames
How many times to repeat the animation: Infinity (the default) loops forever, 0 plays once, 1 plays twice, ...
Other values throw, see AnimationOptions.loopCount, and the loop count stays as it was
Replaces the loop count (see Animation.loopCount). Anything else than Infinity or an integer from 0
throws, and the count stays as it was
The new loop count
The canvas width: the one given to the constructor, the decoded file's, or the one Animation.resize and Animation.crop set; else the frames' right edge
Iterates over the frames, for (const frame of animation)
StaticapngAn encoder that makes an APNG a frame at a time, like Animation.encodeAPNG (the same file), keeping the
frames only compressed (the pixel format depends on all of them): await encoder.add(frame) for each frame,
then await encoder.finish()
The canvas width in pixels (a fraction is truncated, like the size of an image)
The canvas height in pixels (a fraction is truncated)
Clones the animation: its frames (with their pixels), canvas size and loop count
Crops the canvas to the given region: the frames move with it and are cut to the new canvas (the same Frame
objects, with new images where they are cut; a frame entirely outside becomes a transparent pixel). The region is
clipped to the canvas, so the result can be smaller than the requested size
The x offset (0-based)
The y offset (0-based)
The new canvas width
The new canvas height
StaticdecodeDecodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)
The binary data to decode
Optionaloptions: DecodeOptions
See DecodeOptions
const animation = await Animation.decode(gifBytes);
const webp = await animation.resize({ width: 320 }).encodeWebP({ quality: 75 });
A still image becomes one frame of 100 ms, the Frame default. An AVIF image sequence is decoded as its first frame only. A file that is cut off keeps the frames that arrived whole (like browsers show it) and throws when not even the first one did
LimitError if the frames have more than options.maxPixels pixels together
Adds a shadow behind what the animation shows, like Image.dropShadow: the frames become full canvas frames of the composited animation, each one with its shadow, and the canvas grows with them
Optionaloptions: ShadowOptions
See ShadowOptions
Encodes the animation as APNG (animated PNG, lossless and with full alpha, unless colors makes it a palette
shared by all frames). Viewers without APNG support show
the first frame. Each frame is stored as the area that changed since the frame before, identical frames are
merged (unless the optimize option is false): the file shows the same animation, but doesn't necessarily decode
to the same frames (with colors, the same up to the palette)
Optionaloptions: APNGEncodeOptions
See APNGEncodeOptions; the loop count is the animation's loopCount (the streaming apngEncoder takes it as an option)
Encodes the animation as GIF. Like all encoders, it stores each frame as the area that changed since the frame
before and merges identical frames (unless the optimize option is false), so the file decodes to what the
animation shows, but not necessarily to the same frames. Semi-transparent pixels become opaque or transparent
after the frames are composited
Optionaloptions: GIFAnimationEncodeOptions
See GIFAnimationEncodeOptions; the loop count is the animation's loopCount (the streaming gifEncoder takes it as an option)
Encodes the animation as animated WebP, lossless or (with a quality below 100) lossy. Each frame is stored as the area that
changed since the frame before, identical frames are merged (unless the optimize option is false): the file
shows the same animation, but doesn't necessarily decode to the same frames
See WebPAnimationEncodeOptions; the loop count is the animation's loopCount (the streaming webpEncoder takes it as an option)
Flips / mirrors the animation horizontally or vertically, like Image.flip: every frame is flipped and moved to the mirrored place on the canvas, which keeps its size
The direction to flip, see FlipDirection
StaticgifAn encoder that makes an animated GIF a frame at a time, like Animation.encodeGIF (the same file),
without keeping the frames in memory: await encoder.add(frame) for each frame, then await encoder.finish()
The canvas width in pixels (a fraction is truncated, like the size of an image)
The canvas height in pixels (a fraction is truncated)
Draws an outline around what the animation shows, like Image.outline: the frames become full canvas frames of the composited animation, each one outlined, and the canvas grows with them
Optionaloptions: OutlineOptions
See OutlineOptions
Resizes the animation to the given dimensions, scaling every frame and its offsets proportionally. Pass ResizeToOptions with only a width or a height to keep the aspect ratio.
The new width
The new height
Optionaloptions: ResizeOptions
See ResizeOptions
Resizes the animation, scaling every frame and its offsets proportionally: to the given width or height (the other side keeps the aspect ratio), or to both
The size and how to resize, see ResizeToOptions
Rotates the animation clockwise, like Image.rotate: the frames become full canvas frames of the composited animation (a partial frame turned by any angle wouldn't cover a rectangle anymore), each one rotated, and the canvas takes their size
The angle in degrees, clockwise
Optionaloptions: RotateOptions
See RotateOptions
Scales the animation by a factor: every frame and its offsets, and the canvas
The factor
Optionaloptions: ResizeOptions
See ResizeOptions
A short description for debugging, e.g. Animation<64x64x1000ms>
Transforms the animation with an affine transform, like Image.transform: the frames become full canvas frames of the composited animation, each one transformed (by default around the canvas' middle), and the canvas takes their size
The six values [a, b, c, d, e, f]
Optionaloptions: TransformOptions
See TransformOptions
Trims the canvas: removes the borders of one color around what any frame shows (in the composited animation), like Image.trim. The frames keep their pixels, the canvas is cropped like Animation.crop
The color (by default the top left pixel of the canvas as the first frame shows it) and the tolerance, see TrimOptions
StaticwebpAn encoder that makes an animated WebP a frame at a time, like Animation.encodeWebP (the same file),
without keeping the frames in memory: await encoder.add(frame) for each frame (a Frame, or an image
with { duration }), then await encoder.finish() for the file
The canvas width in pixels (a fraction is truncated, like the size of an image)
The canvas height in pixels (a fraction is truncated)
An animation: frames (Animation.frames) that can be decoded from and encoded to GIF, APNG and WebP. It can be iterated (
for (const frame of animation)), and itslengthis the number of frames.