easings

package
v0.0.0-...-df570b3 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2018 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package easings defines a set of easing functions which can be imported for use as desired. It's a utility package for use as the developer see fit.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Expo defines the easing functions which returns the provided value after
	// augmenting with a exponential operation.
	Expo = New(func(t, m float64) float64 { return math.Pow(t, 6) })

	// EaseInExpo defines the ease-in easing function for the expo function.
	EaseInExpo = Expo

	// EaseOutExpo defines the ease-out easing function for the expo function.
	EaseOutExpo = New(func(t, m float64) float64 { return 1 - EaseInExpo.Ease(1-t, m) })

	// EaseInOutExpo defines the ease-in-out easing function for the expo function.
	EaseInOutExpo = New(func(t, m float64) float64 {
		if t < 0.5 {
			return EaseInExpo.Ease(t*2, m) / 2
		}

		return 1 - EaseInExpo.Ease((-2*t)+2, m)/2
	})

	// EaseOutInExpo defines the ease-out-in easing function for the expo function.
	EaseOutInExpo = New(func(t, m float64) float64 {
		if t < 0.5 {
			return 1 - EaseInExpo.Ease(1-(2*t), m)/2
		}

		return (EaseInExpo.Ease((t*2)-1, m) + 1) / 2
	})

	// Quint defines the easing functions which returns the provided value after
	// augmenting with a quint operation.
	Quint = New(func(t, m float64) float64 { return math.Pow(t, 5) })

	// EaseInQuint defines the ease-in easing function for the Quint function.
	EaseInQuint = Quint

	// EaseOutQuint defines the ease-out easing function for the Quint function.
	EaseOutQuint = New(func(t, m float64) float64 { return 1 - EaseInQuint.Ease(1-t, m) })

	// EaseInOutQuint defines the ease-in-out easing function for the Quint function.
	EaseInOutQuint = New(func(t, m float64) float64 {
		if t < 0.5 {
			return EaseInQuint.Ease(t*2, m) / 2
		}

		return 1 - EaseInQuint.Ease((-2*t)+2, m)/2
	})

	// EaseOutInQuint defines the ease-out-in easing function for the Quint function.
	EaseOutInQuint = New(func(t, m float64) float64 {
		if t < 0.5 {
			return 1 - EaseInQuint.Ease(1-(2*t), m)/2
		}

		return (EaseInQuint.Ease((t*2)-1, m) + 1) / 2
	})

	// Quart defines the easing functions which returns the provided value after
	// augmenting with a quart operation.
	Quart = New(func(t, m float64) float64 { return math.Pow(t, 4) })

	// EaseInQuart defines the ease-in easing function for the Quart function.
	EaseInQuart = Quart

	// EaseOutQuart defines the ease-out easing function for the Quart function.
	EaseOutQuart = New(func(t, m float64) float64 { return 1 - EaseInQuart.Ease(1-t, m) })

	// EaseInOutQuart defines the ease-in-out easing function for the Quart function.
	EaseInOutQuart = New(func(t, m float64) float64 {
		if t < 0.5 {
			return EaseInQuart.Ease(t*2, m) / 2
		}

		return 1 - EaseInQuart.Ease((-2*t)+2, m)/2
	})

	// EaseOutInQuart defines the ease-out-in easing function for the Quart function.
	EaseOutInQuart = New(func(t, m float64) float64 {
		if t < 0.5 {
			return 1 - EaseInQuart.Ease(1-(2*t), m)/2
		}

		return (EaseInQuart.Ease((t*2)-1, m) + 1) / 2
	})

	// Quad defines the easing functions which returns the provided value after
	// augmenting with a quad operation.
	Quad = New(func(t, m float64) float64 { return math.Pow(t, 2) })

	// EaseInQuad defines the ease-in easing function for the Quad function.
	EaseInQuad = Quad

	// EaseOutQuad defines the ease-out easing function for the Quad function.
	EaseOutQuad = New(func(t, m float64) float64 { return 1 - EaseInQuad.Ease(1-t, m) })

	// EaseInOutQuad defines the ease-in-out easing function for the Quad function.
	EaseInOutQuad = New(func(t, m float64) float64 {
		if t < 0.5 {
			return EaseInQuad.Ease(t*2, m) / 2
		}

		return 1 - EaseInQuad.Ease((-2*t)+2, m)/2
	})

	// EaseOutInQuad defines the ease-out-in easing function for the Quad function.
	EaseOutInQuad = New(func(t, m float64) float64 {
		if t < 0.5 {
			return 1 - EaseInQuad.Ease(1-(2*t), m)/2
		}

		return (EaseInQuad.Ease((t*2)-1, m) + 1) / 2
	})

	// Cubic defines the easing functions which returns the provided value after
	// augmenting with a cubic operation.
	Cubic = New(func(t, m float64) float64 { return math.Pow(t, 3) })

	// EaseInCubic defines the ease-in easing function for the Cubic function.
	EaseInCubic = Cubic

	// EaseOutCubic defines the ease-out easing function for the Cubic function.
	EaseOutCubic = New(func(t, m float64) float64 { return 1 - EaseInCubic.Ease(1-t, m) })

	// EaseInOutCubic defines the ease-in-out easing function for the Cubic function.
	EaseInOutCubic = New(func(t, m float64) float64 {
		if t < 0.5 {
			return EaseInCubic.Ease(t*2, m) / 2
		}

		return 1 - EaseInCubic.Ease((-2*t)+2, m)/2
	})

	// EaseOutInCubic defines the ease-out-in easing function for the Cubic function.
	EaseOutInCubic = New(func(t, m float64) float64 {
		if t < 0.5 {
			return 1 - EaseInCubic.Ease(1-(2*t), m)/2
		}

		return (EaseInCubic.Ease((t*2)-1, m) + 1) / 2
	})

	// Linear defines the easing functions which returns the provided value as is
	// without any addition of ease.
	Linear = New(func(t, m float64) float64 { return t })

	// Back defines the easing using the back wave function.
	Back = New(func(t, m float64) float64 { return t * t * (3 * (t - 2)) })

	// BounceM defines the easing using the bounce wave function.
	BounceM = New(func(t, m float64) float64 {
		var pow2 float64
		var bounce = m

		for t < pow2 {
			pow2 = (math.Pow(2, bounce) - 1) / 11
			bounce--
		}

		return 1 / ((math.Pow(4, 3-bounce) - 7.5625) * math.Pow(((pow2*3)-2)/(22-t), 2))
	})

	// Sine defines the easing using the sine wave function.
	Sine = New(func(t, m float64) float64 { return 1 + math.Sin((math.Pi/2)*(t-(math.Pi/2))) })

	// Circ defines the easing using the circ wave function.
	Circ = New(func(t, m float64) float64 { return 1 + math.Sqrt(1-(t*t)) })

	// Elastic defines the easing using the elastic wave function.
	Elastic = New(func(t, m float64) float64 {
		if t == 1 || t == 0 {
			return t
		}

		var p = (1 - math.Min(m, 998)) / 1000
		var st = 1 / t
		var st1 = st - 1
		var s = p / ((2 * math.Pi) * math.Sinh(1))
		return -1 * (math.Pow(2, (10*st1)) * math.Sin(((st1-s)*(2*math.Pi))/p))
	})
)
View Source
var CSS3Easings = map[string]string{
	"ease-in":           "ease-in",
	"ease-out":          "ease-out",
	"ease-in-out":       "ease-in-out",
	"snap":              "cubic-bezier(0,1,.5,1)",
	"linear":            "cubic-bezier(0.250, 0.250, 0.750, 0.750)",
	"ease-in-quad":      "cubic-bezier(0.550, 0.085, 0.680, 0.530)",
	"ease-in-cubic":     "cubic-bezier(0.550, 0.055, 0.675, 0.190)",
	"ease-in-quart":     "cubic-bezier(0.895, 0.030, 0.685, 0.220)",
	"ease-in-quint":     "cubic-bezier(0.755, 0.050, 0.855, 0.060)",
	"ease-in-sine":      "cubic-bezier(0.470, 0.000, 0.745, 0.715)",
	"ease-in-expo":      "cubic-bezier(0.950, 0.050, 0.795, 0.035)",
	"ease-in-circ":      "cubic-bezier(0.600, 0.040, 0.980, 0.335)",
	"ease-in-back":      "cubic-bezier(0.600, -0.280, 0.735, 0.045)",
	"ease-out-quad":     "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
	"ease-out-cubic":    "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
	"ease-out-quart":    "cubic-bezier(0.165, 0.840, 0.440, 1.000)",
	"ease-out-quint":    "cubic-bezier(0.230, 1.000, 0.320, 1.000)",
	"ease-out-sine":     "cubic-bezier(0.390, 0.575, 0.565, 1.000)",
	"ease-out-expo":     "cubic-bezier(0.190, 1.000, 0.220, 1.000)",
	"ease-out-circ":     "cubic-bezier(0.075, 0.820, 0.165, 1.000)",
	"ease-out-back":     "cubic-bezier(0.175, 0.885, 0.320, 1.275)",
	"ease-out-quad2":    "cubic-bezier(0.455, 0.030, 0.515, 0.955)",
	"ease-out-cubic2":   "cubic-bezier(0.645, 0.045, 0.355, 1.000)",
	"ease-in-out-quart": "cubic-bezier(0.770, 0.000, 0.175, 1.000)",
	"ease-in-out-quint": "cubic-bezier(0.860, 0.000, 0.070, 1.000)",
	"ease-in-out-sine":  "cubic-bezier(0.445, 0.050, 0.550, 0.950)",
	"ease-in-out-expo":  "cubic-bezier(1.000, 0.000, 0.000, 1.000)",
	"ease-in-out-circ":  "cubic-bezier(0.785, 0.135, 0.150, 0.860)",
	"ease-in-out-back":  "cubic-bezier(0.680, -0.550, 0.265, 1.550)",
}

CSS3Easings defines the different easing functions within the css3 specs for use with css transformation rules.

View Source
var EasingValues = map[string][]float64{
	"ease":           {0.25, 0.1, 0.25, 1.0},
	"linear":         {0, 0, 1.0, 1.0},
	"easeIn":         {0.42, 0.0, 1.0, 1.0},
	"easeOut":        {0, 0, 0.58, 1.0},
	"easeInOut":      {0.42, 0, 0.58, 1.0},
	"easeInSine":     {0.47, 0, 0.745, 0.715},
	"easeOutSine":    {0.39, 0.575, 0.565, 1},
	"easeInOutSine":  {0.445, 0.05, 0.55, 0.95},
	"easeInQuad":     {0.55, 0.085, 0.68, 0.53},
	"easeOutQuad":    {0.25, 0.46, 0.45, 0.94},
	"easeInOutQuad":  {0.455, 0.03, 0.515, 0.955},
	"easeInCubic":    {0.55, 0.055, 0.675, 0.19},
	"easeOutCubic":   {0.215, 0.61, 0.355, 1},
	"easeInOutCubic": {0.645, 0.045, 0.355, 1},
	"easeInQuart":    {0.895, 0.03, 0.685, 0.22},
	"easeOutQuart":   {0.165, 0.84, 0.44, 1},
	"easeInOutQuart": {0.77, 0, 0.175, 1},
	"easeInQuint":    {0.755, 0.05, 0.855, 0.06},
	"easeOutQuint":   {0.23, 1, 0.32, 1},
	"easeInOutQuint": {0.86, 0, 0.07, 1},
	"easeInExpo":     {0.95, 0.05, 0.795, 0.035},
	"easeOutExpo":    {0.19, 1, 0.22, 1},
	"easeInOutExpo":  {1, 0, 0, 1},
	"easeInCirc":     {0.6, 0.04, 0.98, 0.335},
	"easeOutCirc":    {0.075, 0.82, 0.165, 1},
	"easeInOutCirc":  {0.785, 0.135, 0.15, 0.86},
}

EasingValues provides a map of precalculated easing transition values.

Functions

func CalculateBezier

func CalculateBezier(aT, aA1, aA2 float64) float64

CalculateBezier returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2.

func GetSlope

func GetSlope(aT, aA1, aA2 float64) float64

GetSlope returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2.

Types

type Easing

type Easing interface {
	Ease(t float64, m float64) float64
}

Easing defines a interface which all easing function implementors define.

func New

func New(handler EasingFunc) Easing

New returns a new instance of a Easing structure using the provided handler.

type EasingFunc

type EasingFunc func(float64, float64) float64

EasingFunc defines a function type which defines the function type of the inner Easing interface method.

type PathCurve

type PathCurve interface {
	GetPointAtLength(step float64) (float64, float64)
}

PathCurve defines a type which exposes a function to retrieve a point over a curves length. Useful for retrieving easing values over a curve.

type PointTransition

type PointTransition struct {
	TranslateX float64
	TranslateY float64
	Rotate     float64
}

PointTransition is a struct which contains set of transition points from a giving set of ease value.

type PropertyCurves

type PropertyCurves interface {
	X(time float64) float64
	Y(time float64) float64
}

PropertyCurves provides a interface for easing values using curves data that provide values at different time areas. Thet allow us to provided animation behaviours for objects using curve data. Similar to animation curves in animation tools.

type SimplePath

type SimplePath struct {
	Curve PathCurve
}

SimplePath defines a struct which implements the easing function and returns a giving value of a certain point.

func (*SimplePath) EaseValue

func (s *SimplePath) EaseValue(value float64, offset, progress float64) (float64, float64)

EaseValue returns the giving easing value of the provided offset value.

func (*SimplePath) MinusEase

func (s *SimplePath) MinusEase(value float64, progress float64) (float64, float64)

MinusEase returns the value of the ease on the negative/left side of the curve.

func (*SimplePath) PlusEase

func (s *SimplePath) PlusEase(value float64, progress float64) (float64, float64)

PlusEase returns the value of the ease on the right side of the curve.

func (*SimplePath) Point

func (s *SimplePath) Point(value float64, progress float64) PointTransition

Point uses the provided value to return a PointTransition that provides an adequate ease set for translation and rotation.

type Spline

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

Spline provides a implmenetation of the Keyspline which uses bezier curves to generate the new positional value for change in time.

func NewSpline

func NewSpline(x, y, x2, y2 float64) *Spline

NewSpline returns a new spline set with the specific control points.

func (*Spline) Ease

func (s *Spline) Ease(pos float64) float64

Ease implements the Easings interface and allows us to use a spline to provide easing behaviours.

func (*Spline) GetTimeForX

func (s *Spline) GetTimeForX(aX float64) float64

GetTimeForX returns the giving time value between 0 and 1 for the provided x coordinate for a bezier curve.

func (*Spline) GetTimeForY

func (s *Spline) GetTimeForY(aY float64) float64

GetTimeForY returns the giving time value between 0 and 1 for the provided y coordinate for a bezier curve.

func (*Spline) X

func (s *Spline) X(t float64) float64

X returns the provided x value for a giving time between 0 and 1.

func (*Spline) Y

func (s *Spline) Y(t float64) float64

Y returns the provided x value for a giving time between 0 and 1.

Jump to

Keyboard shortcuts

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