Home

Awesome

ganim8

Go Reference

Sprite Animation library for Ebitengine inspired by anim8.

v1.x is pretty much the same API with anim8. v2.x is more optimized for Ebiten to be more performant and granular control introducing Sprite API.

In order to build animations more easily, ganim8 divides the process in two steps: first you create a grid, which is capable of creating frames (Quads) easily and quickly. Then you use the grid to create one or more animations.

Example

import "github.com/yohamta/ganim8/v2"

type Game struct {
  animation *ganim8.Animation
}

func NewGame() *Game {
  g := &Game{ prevTime: time.Now() }

  g32 := ganim8.NewGrid(32, 32, 1024, 1024)
  g.animation = ganim8.New(monsterImage, g32.Frames("1-5", 5), 100*time.Millisecond)

  return g
}

func (g *Game) Draw(screen *ebiten.Image) {
  screen.Clear()
  g.animation.Draw(screen, ganim8.DrawOpts(screenWidth/2, screenHeight/2, 0, 1, 1, 0.5, 0.5))
}

func (g *Game) Update() error {
  g.animation.Update()
  // Note: it assumes that the time delta is 16ms by default 
  //       if you need to specify different delta you can use Animation.UpdateWithDelta(delta) instead
  return nil
}

You can see a more elaborated example.

That demo transforms this spritesheet:

1945

Into several animated objects:

1945

Explanation

Grids

Grids have only one purpose: To build groups of quads of the same size as easily as possible. In order to do this, they need to know only 2 things: the size of each quad and the size of the image they will be applied to. Each size is a width and a height, and those are the first 4 parameters of @ganim8.NewGrid@.

Grids are just a convenient way of getting frames from a sprite. Frames are assumed to be distributed in rows and columns. Frame 1,1 is the one in the first row, first column.

This is how you create a grid:

ganim8.NewGrid(frameWidth, frameHeight, imageWidth, imageHeight, left, top, border)

To see this a bit more graphically, here are what those values mean for the grid which contains the "submarine" frames in the demo:

explanation

Grids only have one important method: Grid.Frames(...).

Grid.Frames accepts an arbitrary number of parameters. They can be either numbers or strings.

Let's consider the submarine in the previous example. It has 7 frames, arranged horizontally.

If you make its grid start on its first frame (using left and top), you can get its frames like this:

                  // frame, image,     offsets, border
gs := ganim8.NewGrid(32,98, 1024,768,  366,102,   1)

frames := gs.Frames("1-7",1)

However that way you will get a submarine which "emerges", then "suddenly disappears", and emerges again. To make it look more natural, you must add some animation frames "backwards", to give the illusion of "submersion". Here's the complete list:

frames := gs.Frames("1-7",1, "6-2",1)

Animations

Animations are groups of frames that are interchanged every now and then.

animation := ganim8.New(img, frames, durations, onLoop)

Animations have the following methods:

animation.Update()

Use this inside Game.Update() so that your animation changes frames according to the time that has passed.

It assumes that the time delta is 1/60[s] (1/TPS to be exact). For more details about TPS (ticks per seconds) in Ebitengine is explained here that is written by tinne26.

If you need to specify delta for update animations, you can use UpdateWithDelta() instead:

animation.UpdateWithDelta(delta)
animation.Draw(screen, ganim8.DrawOpts(x,y, angle, sx, sy, ox, oy))
animation.GoToFrame(frame)

Moves the animation to a given frame (frames start counting in 1).

animation.Pause()

Stops the animation from updating.

animation.Resume()

Unpauses an animation

animation.Clone()

Creates a new animation identical to the current one. The only difference is that its internal counter is reset to 0 (it's on the first frame).

animation.Sprite().FlipH()

Flips an animation horizontally (left goes to right and viceversa). This means that the frames are simply drawn differently, nothing more.

Note that this method does not create a new animation. If you want to create a new one, use the Clone method.

This method returns the animation, so you can do things like a := ganim8.New(img, g.Frames(1,"1-10"), time.Milliseconds * 100).FlipH() or b := a.Clone().FlipV().

animation.FlipV()

Flips an animation vertically. The same rules that apply to FlipH also apply here.

animation.PauseAtEnd()

Moves the animation to its last frame and then pauses it.

animation.PauseAtStart()

Moves the animation to its first frame and then pauses it.

animation.GetDimensions()

Returns the width and height of the current frame of the animation. This method assumes the frames passed to the animation are all quads (like the ones created by a grid).

How to contribute?

Feel free to contribute in any way you want. Share ideas, questions, submit issues, and create pull requests. Thanks!