tween

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2025 License: MIT Imports: 2 Imported by: 0

README

Tween

Tween is a small library to perform tweening in Go. It has a minimal interface, and it comes with several easing functions.

Examples

see examples folder

Easing functions

Easing functions are functions that express how slow/fast the interpolation happens in tween.

tween-families

The easing functions can be found in the ease package.

They can be divided into several families:

  • linear is the simplest easing function, straight from one value to the other.
  • quad, cubic, quart, quint, expo, sine and circle are all "smooth" curves that will make transitions look natural.
  • The back family starts by moving the interpolation slightly "backwards" before moving it forward.
  • The bounce family simulates the motion of an object bouncing.
  • The elastic family simulates inertia in the easing, like an elastic gum.

Each family (except linear) has 4 variants:

  • In starts slow, and accelerates at the end
  • Out starts fast, and decelerates at the end
  • InOut starts and ends slow, but it's fast in the middle
  • OutIn starts and ends fast, but it's slow in the middle
family in out inOut outIn
Linear Linear Linear Linear Linear
Quad InQuad OutQuad InOutQuad OutInQuad
Cubic InCubic OutCubic InOutCubic OutInCubic
Quart InQuart OutQuart InOutQuart OutInQuart
Quint InQuint OutQuint InOutQuint OutInQuint
Expo InExpo OutExpo InOutExpo OutInExpo
Sine InSine OutSine InOutSine OutInSine
Circ InCirc OutCirc InOutCirc OutInCirc
Back InBack OutBack InOutBack OutInBack
Bounce InBounce OutBounce InOutBounce OutInBounce
Elastic InElastic OutElastic InOutElastic OutInElastic
Custom easing functions

You are not limited to gween's easing functions; if you pass a function parameter in the easing, it will be used.

The passed function will need to suite the TweenFunc interface: func(t, b, c, d float32) float32

  • t (time): starts in 0 and usually moves towards duration
  • b (begin): initial value of the of the property being eased.
  • c (change): ending value of the property - starting value of the property
  • d (duration): total duration of the tween

And must return the new value after the interpolation occurs.

Here's an example using a custom easing.

labelTween := tween.new(0, 300, 4, func(t, b, c, d) float32 {
  return c*t/d + b // linear ease
})

Credits

tween is an independent fork of the gween package. The API is different.

Documentation

Overview

Package tween provides the Tween struct that allows an easing function to be animated over time. This can be used in tandem with the ease package to provide the easing functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sequence

type Sequence struct {
	Tweens []*Tween
	// YoyoEnabled makes the sequence "Yoyo" back to the beginning after it reaches the end
	YoyoEnabled bool
	// contains filtered or unexported fields
}

Sequence represents a sequence of Tweens, executed one after the other.

func NewSequence

func NewSequence(tweens ...*Tween) *Sequence

NewSequence returns a new Sequence object.

func (*Sequence) Add

func (seq *Sequence) Add(tweens ...*Tween)

Add adds one or more Tweens in order to the Sequence.

func (*Sequence) HasTweens

func (seq *Sequence) HasTweens() bool

HasTweens returns whether the Sequence is populated with Tweens or not.

func (*Sequence) Index

func (seq *Sequence) Index() int

Index returns the current index of the Sequence. Note that this can exceed the number of Tweens in the Sequence.

func (*Sequence) IsActiveTweenFinished

func (seq *Sequence) IsActiveTweenFinished() bool

IsActiveTweenFinished returns whether the currently active Tween is finished.

func (*Sequence) IsFinished

func (seq *Sequence) IsFinished() bool

IsFinished returns whether the entire Sequence is finished. This is true when all Tweens in the Sequence are finished and the Sequence has no remaining loops. If the Sequence is set to loop infinitely, this will always return false.

func (*Sequence) IsReversed

func (seq *Sequence) IsReversed() bool

IsReversed returns whether the Sequence currently running in reverse.

func (*Sequence) Remove

func (seq *Sequence) Remove(index int)

Remove removes a Tween of the specified index from the Sequence.

func (*Sequence) Reset

func (seq *Sequence) Reset()

Reset resets the Sequence, resetting all Tweens and setting the Sequence's index back to 0.

func (*Sequence) SetIndex

func (seq *Sequence) SetIndex(index int)

SetIndex sets the current index of the Sequence, influencing which Tween is active at any given time.

func (*Sequence) SetLoop

func (seq *Sequence) SetLoop(amount int)

SetLoop sets the default loop and the current remaining loops

-1 means infinite loops

0 means no loops

1 means one loop

func (*Sequence) SetReverse

func (seq *Sequence) SetReverse(r bool)

SetReverse sets whether the Sequence will start running in reverse.

func (*Sequence) Update

func (s *Sequence) Update(dt float32)

Update updates the currently active Tween in the Sequence; once that Tween is done, the Sequence moves onto the next one.

func (*Sequence) Value

func (seq *Sequence) Value() float32

Value returns the current value of the Sequence, which is the value of the currently active Tween.

type Tween

type Tween struct {
	Reverse bool
	// contains filtered or unexported fields
}

Tween encapsulates the easing function along with timing data. This allows a ease.TweenFunc to be used to be easily animated.

func NewTween

func NewTween(begin, end, duration float32, easing ease.TweenFunc) *Tween

NewTween will return a new Tween when passed a beginning and end value, the duration of the tween and the easing function to animate between the two values. The easing function can be one of the provided easing functions from the ease package or you can provide one of your own.

func (*Tween) Begin added in v1.2.0

func (t *Tween) Begin() float32

Begin returns begin value

func (*Tween) Duration added in v1.2.0

func (t *Tween) Duration() float32

Duration returns duration value

func (*Tween) End added in v1.2.0

func (t *Tween) End() float32

End returns end value

func (*Tween) IsFinished

func (t *Tween) IsFinished() bool

IsFinished will return true if the tween is finished.

func (*Tween) Reset

func (t *Tween) Reset()

Reset will set the Tween to the beginning of the two values.

func (*Tween) Set

func (t *Tween) Set(time float32)

Set will set the current time along the duration of the tween. It will then return the current value as well as a boolean to determine if the tween is finished.

func (*Tween) Update

func (t *Tween) Update(dt float32)

Update will increment the timer of the Tween and ease the value.

25 FPS = 1/25 = 0.04 dt

60 FPS = 1/60 = 0.016666666666666666 dt

120 FPS = 1/120 = 0.008333333333333333 dt

func (*Tween) Value added in v1.2.0

func (t *Tween) Value() float32

Value returns current tween value

Directories

Path Synopsis
Package ease provides default easing functions to be used in a Tween.
Package ease provides default easing functions to be used in a Tween.
examples
sequence command
tween command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL