Copyright | (c) 2015 Schell Scivally |
---|---|
License | MIT |
Maintainer | Schell Scivally <schell.scivally@synapsegroup.com> |
Safe Haskell | None |
Language | Haskell2010 |
Tweening is a technique of generating intermediate samples of a type between a start and end value. By sampling a running tween each frame we get a smooth animation of a value over time.
At first release varying
is only capable of tweening numerical
values of type (Fractional t, Ord t) => t
that match the type of
time you use. At some point it would be great to be able to tween
arbitrary types, and possibly tween one type into another (pipe
dreams).
- tween :: (Applicative m, Monad m, Fractional t, Ord t) => Easing t -> t -> t -> t -> Spline t t m t
- constant :: (Applicative m, Monad m, Num t, Ord t) => a -> t -> Spline t a m a
- linear :: Num t => Easing t
- easeInCirc :: Floating t => Easing t
- easeOutCirc :: Floating t => Easing t
- easeInExpo :: Floating t => Easing t
- easeOutExpo :: Floating t => Easing t
- easeInSine :: Floating t => Easing t
- easeOutSine :: Floating t => Easing t
- easeInOutSine :: Floating t => Easing t
- easeInPow :: Num t => Int -> Easing t
- easeOutPow :: Num t => Int -> Easing t
- easeInCubic :: Num t => Easing t
- easeOutCubic :: Num t => Easing t
- easeInQuad :: Num t => Easing t
- easeOutQuad :: Num t => Easing t
- type Tween m t = t -> t -> t -> Var m t (Event t)
- type Easing t = t -> t -> t -> t
Creating tweens
The most direct route toward tweening values is to use tween
along with an interpolation function such as easeInExpo
. For example,
tween easeInOutExpo 0 100 10
, this will create a spline that produces a
number interpolated from 0 to 100 over 10 seconds. At the end of the
tween the spline will return the result value.
tween :: (Applicative m, Monad m, Fractional t, Ord t) => Easing t -> t -> t -> t -> Spline t t m t Source
Creates a spline that produces a value interpolated between a start and
end value using an easing equation (Easing
) over a duration. The
resulting spline will take a time delta as input. For example:
testWhile_ isEvent (deltaUTC ~> v) where v :: Var IO a (Event Double) v = execSpline 0 $ tween easeOutExpo 0 100 5
Keep in mind tween
must be fed time deltas, not absolute time or
duration. This is mentioned because the author has made that mistake
more than once ;)
constant :: (Applicative m, Monad m, Num t, Ord t) => a -> t -> Spline t a m a Source
Creates a tween that performs no interpolation over the duration.
Interpolation functions
These pure functions take a c
(total change in value, ie end - start),
t
(percent of duration completion) and b
(start value) and result in
and interpolation of a value. To see what these look like please check
out http://www.gizma.com/easing/.
easeInCirc :: Floating t => Easing t Source
Ease in circular.
easeOutCirc :: Floating t => Easing t Source
Ease out circular.
easeInExpo :: Floating t => Easing t Source
Ease in exponential.
easeOutExpo :: Floating t => Easing t Source
Ease out exponential.
easeInSine :: Floating t => Easing t Source
Ease in sinusoidal.
easeOutSine :: Floating t => Easing t Source
Ease out sinusoidal.
easeInOutSine :: Floating t => Easing t Source
Ease in and out sinusoidal.
easeOutPow :: Num t => Int -> Easing t Source
Ease out by some power.
easeInCubic :: Num t => Easing t Source
Ease in cubic.
easeOutCubic :: Num t => Easing t Source
Ease out cubic.
easeInQuad :: Num t => Easing t Source
Ease in quadratic.
easeOutQuad :: Num t => Easing t Source
Ease out quadratic.
Writing your own tweens
To create your own tweens just write a function that takes a start value, end value and a duration and return an event stream.
tweenInOutExpo s e d = execSpline s $ do x <- tween easeInExpo s e (d/2) tween easeOutExpo x e (d/2)
type Tween m t = t -> t -> t -> Var m t (Event t) Source
A linear interpolation between two values over some duration.
A Tween
takes three values - a start value, an end value and
a duration.
type Easing t = t -> t -> t -> t Source
An easing function. The parameters or often named c
, t
and b
,
where c
is the total change in value over the complete duration
(endValue - startValue), t
is the current percentage of the duration
that has elapsed and b
is the start value.
To make things simple only numerical values can be tweened and the type of time deltas much match the tween's value type. This may change in the future :)