gease

package module
v0.0.0-...-5f22997 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2020 License: MIT Imports: 7 Imported by: 0

README

gease

Easings / animations for gioui

Test

WIP: looking for input on API / feedback.

This package implements spring based convenience methods for animating layouts or draw operations in Gioui. Please refer to https://godoc.org/github.com/vron/gease for further details.

To see smooth easings on positions, sizes and colors you can run:

go run github.com/vron/gease/example

Inspired by: https://www.react-spring.io/

Allocations

The pacakge has beed designed to minimize GC pressure during animations, in particular there is no additoinal allocation per frame resulting from using this easing package:

go test -bench Step github.com/vron/gease       
goos: windows
goarch: amd64
pkg: github.com/vron/gease
BenchmarkColorStep-24           22201993                54.5 ns/op             0 B/op          0 allocs/op
BenchmarkPointStep-24           44404644                26.0 ns/op             0 B/op          0 allocs/op
BenchmarkUnitStep-24            59919408                19.6 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/vron/gease   3.799s

Documentation

Overview

Package gease implements spring based animation easings for gioui

Spring / force based animation of GUI components for gioui. In general plenty fast enough to always drive all dimensions through this package for smooth and nice animations at e.g. resizes etc.

Please see github.com/vron/gease/example as an example of the intended use-case.

Index

Examples

Constants

View Source
const (
	// DefaultOvershoot represents a resonable default for visualizations.
	DefaultOvershoot = 0.1
	// DefaultPeriod represents a reasonable default for visualizations.
	DefaultPeriod = 750 * time.Millisecond
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ColorEasing

type ColorEasing struct {
	// contains filtered or unexported fields
}

ColorEasing smoothly animates a color transition. It operates in LAB space with seperate alpha interpolation for a visually smooth transition.

func Color

func Color(color color.RGBA) *ColorEasing

Color creates a new ColorEasing.

Example
red, green := colornames.Red400, colornames.Green400

es := Color(red)
es.Target(green)

tt := time.Now()
for !es.Step(tt) {
	// one would usually do: tt = time.Now()
	tt = tt.Add(time.Millisecond * 20)

	// use the color for drawing
	col := es.V()
	_ = col
}

// once the animation is converged we have the expected value
fmt.Println(es.V() == green)
Output:

true

func (*ColorEasing) Configure

func (es *ColorEasing) Configure(period time.Duration) *ColorEasing

Configure sets the natural period of the animation. The provided period will thus roughly represent the time of a transition.

func (*ColorEasing) SetTime

func (es *ColorEasing) SetTime(t time.Time)

SetTime stores the time t as the current time of the animation, without advancing the simulation. Usually there is no need to call SetTime since Color() internally set the initial time to time.Now().

func (*ColorEasing) Step

func (es *ColorEasing) Step(t time.Time) (converged bool)

Step advances the simulation until t. If t is before any previos t Step was called with no work is done. If t.IsZero() time.Now() is assumed.

func (*ColorEasing) Target

func (es *ColorEasing) Target(target color.RGBA)

Target sets the target color we should animate towards.

func (*ColorEasing) V

func (es *ColorEasing) V() color.RGBA

V returns the current color of the easing.

type Easing

type Easing interface {
	// SetTime is used to advance the easing in time
	Step(t time.Time) (converged bool)
}

Easing is the interface an easing must satisfy to be used in a Hub.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

A Hub is a convenience wrapper for cases with constantly live easings.

func NewHub

func NewHub() *Hub

NewHub creates a new Hub.

func (*Hub) Add

func (h *Hub) Add(ess ...Easing)

Add adds one or more easing to the hub.

func (*Hub) Remove

func (h *Hub) Remove(es Easing)

Remove the easing from the hub, typically used such that it can be garbage collected if it will not be used further.

func (*Hub) Step

func (h *Hub) Step(t ...time.Time) (converged bool)

Step all easings in the Hub, if no time is provided time.Now() will be used and if more than one time is provided only the first one will be used. If all underlying easings have converged true will be returned, else false is returned.

type PointEasing

type PointEasing struct {
	// contains filtered or unexported fields
}

PointEasing soothly animates a 2D-position.

func Point

func Point(pos f32.Point) *PointEasing

Point creaes a new PointEasing.

func (*PointEasing) Configure

func (es *PointEasing) Configure(overshoot float64, period time.Duration) *PointEasing

Configure sets the stiffness of the animation by specifing overshoot and period. The provided period will thus roughly represent the time of a transition abd overshoot the amount by which the animation should go past the target value before returning.

func (*PointEasing) SetTime

func (es *PointEasing) SetTime(t time.Time)

SetTime stores the time t as the current time of the animation, without advancing the simulation. Usually there is no need to call SetTime since Color() internally set the initial time to time.Now().

func (*PointEasing) Step

func (es *PointEasing) Step(t time.Time) (converged bool)

Step advances the simulation until t. If t is before any previos t Step was called with no work is done. If t.IsZero() time.Now() is assumed.

func (*PointEasing) Target

func (es *PointEasing) Target(target f32.Point)

Target sets the target point we should animate to.

func (*PointEasing) V

func (es *PointEasing) V() f32.Point

V returns the current point of the easing.

type UnitEasing

type UnitEasing struct {
	// contains filtered or unexported fields
}

UnitEasing soothly animates a gioui unit.Value. Note that the unit (e..g px, sp, dp) must be consistent in each UnitEasing.

func Unit

func Unit(v unit.Value) *UnitEasing

Unit creaes a new UnitEasing.

Example

Output:

func (*UnitEasing) Configure

func (es *UnitEasing) Configure(overshoot float64, period time.Duration) *UnitEasing

Configure sets the stiffness of the animation by specifing overshoot and period. The provided period will thus roughly represent the time of a transition abd overshoot the amount by which the animation should go past the target value before returning.

func (*UnitEasing) SetTime

func (es *UnitEasing) SetTime(t time.Time)

SetTime stores the time t as the current time of the animation, without advancing the simulation. Usually there is no need to call SetTime since Color() internally set the initial time to time.Now().

func (*UnitEasing) Step

func (es *UnitEasing) Step(t time.Time) (converged bool)

Step advances the simulation until t. If t is before any previos t Step was called with the current value is returned without advancing the animation. If t.IsZero() time.Now() is assumed.

func (*UnitEasing) Target

func (es *UnitEasing) Target(target unit.Value)

Target sets the target unit.Value we should animate to.

func (*UnitEasing) V

func (es *UnitEasing) V() unit.Value

V returns the current value of the easing.

Directories

Path Synopsis
Command example illustrates an example easings / animations with gease
Command example illustrates an example easings / animations with gease

Jump to

Keyboard shortcuts

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