← 输入框|imagescript.dev|刷新
ImageScript v4.1.0
    Preparing search index...

    ClassAnimation

    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 its length is the number of frames.

    Index
    • get duration(): number

      The animation's duration (in ms)

      Returns number

    • get frames(): readonly Frame[]

      The animation's frames as a frozen array. The frames themselves are mutable; assign a new array to replace the list

      Returns readonly Frame[]

    • set frames(frames: readonly Frame[]): void

      Replaces the animation's frames

      Parameters

      • frames: readonly Frame[]

        The new frames (copied into the animation; the array itself isn't kept)

      Returns void

    • get loopCount(): number

      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

      Returns number

    • set loopCount(value: number): void

      Replaces the loop count (see Animation.loopCount). Anything else than Infinity or an integer from 0 throws, and the count stays as it was

      Parameters

      • value: number

        The new loop count

      Returns void

    • Iterates over the frames, for (const frame of animation)

      Returns IterableIterator<Frame>

    • 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

      Parameters

      • x: number

        The x offset (0-based)

      • y: number

        The y offset (0-based)

      • width: number

        The new canvas width

      • height: number

        The new canvas height

      Returns this

    • Decodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)

      Parameters

      Returns Promise<Animation>

      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

      Error if the data isn't a supported image, or is damaged or cut off before its first whole frame

    • 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)

      Parameters

      Returns Promise<Uint8Array<ArrayBuffer>>

      RangeError if the animation has no frames, the loop count or a frame's duration is more than APNG stores

    • 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

      Parameters

      Returns Promise<Uint8Array<ArrayBuffer>>

      RangeError if the animation has no frames, the loop count or a frame's duration is more than GIF stores, or the canvas is wider or higher than 65535 pixels

    • 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

      Parameters

      Returns Promise<Uint8Array<ArrayBuffer>>

      RangeError if the animation has no frames, the loop count or a frame's duration is more than WebP stores

    • 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.

      Parameters

      Returns this

      With fit: 'contain' and a background that isn't transparent, the frames are composited onto it, so the animation has whole frames afterwards (the file shows the same, but it can't keep the frames' parts apart anymore)

      RangeError if the width or height is less than 1 pixel, or the animation has no canvas

    • 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

      Parameters

      Returns this

    • 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

      Parameters

      Returns this

    • A short description for debugging, e.g. Animation<64x64x1000ms>

      Returns string

    • 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

      Parameters

      • options: TrimOptions = {}

        The color (by default the top left pixel of the canvas as the first frame shows it) and the tolerance, see TrimOptions

      Returns this