Copyright | (c) 2016 Schell Scivally |
---|---|
License | MIT |
Maintainer | Schell Scivally <efsubenovex@gmail.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).
- type Easing t f = t -> f -> t -> t
- type TweenT f t m = SplineT f t (StateT f m)
- type Tween f t = TweenT f t Identity
- runTweenT :: (Monad m, Num f) => TweenT f t m x -> f -> f -> m (Either x (t, TweenT f t m x), f)
- scanTween :: (Functor m, Applicative m, Monad m, Num f) => TweenT f t m a -> t -> [f] -> m [t]
- tweenStream :: (Applicative m, Monad m, Num f) => TweenT f t m x -> t -> VarT m f t
- tween :: (Applicative m, Monad m, Real f, Fractional f, Real t, Fractional t) => Easing t f -> t -> t -> f -> TweenT f t m t
- tween_ :: (Applicative m, Monad m, Real t, Fractional t, Real f, Fractional f) => Easing t f -> t -> t -> f -> TweenT f t m ()
- constant :: (Applicative m, Monad m, Num t, Ord t) => a -> t -> TweenT t a m a
- withTween :: (Applicative m, Monad m, Real t, Fractional t, Real a, Fractional a) => Easing t a -> t -> t -> a -> (t -> x) -> TweenT a x m t
- withTween_ :: (Applicative m, Monad m, Real t, Fractional t, Real a, Fractional a) => Easing t a -> t -> t -> a -> (t -> x) -> TweenT a x m ()
- linear :: (Floating t, Real f) => Easing t f
- easeInCirc :: (Floating t, Real f, Floating f) => Easing t f
- easeOutCirc :: (Floating t, Real f) => Easing t f
- easeInExpo :: (Floating t, Real f) => Easing t f
- easeOutExpo :: (Floating t, Real f) => Easing t f
- easeInSine :: (Floating t, Real f) => Easing t f
- easeOutSine :: (Floating t, Real f) => Easing t f
- easeInOutSine :: (Floating t, Real f) => Easing t f
- easeInPow :: (Num t, Fractional t, Real f) => Int -> Easing t f
- easeOutPow :: (Num t, Fractional t, Real f) => Int -> Easing t f
- easeInCubic :: (Num t, Fractional t, Real f) => Easing t f
- easeOutCubic :: (Num t, Fractional t, Real f) => Easing t f
- easeInQuad :: (Num t, Fractional t, Real f) => Easing t f
- easeOutQuad :: (Num t, Fractional t, Real f) => Easing t f
Tweening types
type Easing t f = t -> f -> t -> t Source #
An easing function. The parameters are 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 (0 to 1) 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 :)
Running tweens
runTweenT :: (Monad m, Num f) => TweenT f t m x -> f -> f -> m (Either x (t, TweenT f t m x), f) Source #
scanTween :: (Functor m, Applicative m, Monad m, Num f) => TweenT f t m a -> t -> [f] -> m [t] Source #
tweenStream :: (Applicative m, Monad m, Num f) => TweenT f t m x -> t -> VarT m f t Source #
Converts a tween into a continuous value stream. This is the tween version
of outputStream
.
Creating tweens
The most direct route toward tweening values is to use tween
along with an interpolation function such as easeInExpo
. For example,
tween easeInExpo 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, Real f, Fractional f, Real t, Fractional t) => Easing t f -> t -> t -> f -> TweenT f 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.
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 ;)
tween
concludes returning the latest output value.
tween_ :: (Applicative m, Monad m, Real t, Fractional t, Real f, Fractional f) => Easing t f -> t -> t -> f -> TweenT f t m () Source #
A version of tween
that discards the result. It is simply
tween f a b c >> return ()
constant :: (Applicative m, Monad m, Num t, Ord t) => a -> t -> TweenT t a m a Source #
Creates a tween that performs no interpolation over the duration.
withTween :: (Applicative m, Monad m, Real t, Fractional t, Real a, Fractional a) => Easing t a -> t -> t -> a -> (t -> x) -> TweenT a x m t Source #
A version of tween
that maps its output using the given constant
function.
withTween ease from to dur f = mapOutput (pure f) $ tween ease from to dur
withTween_ :: (Applicative m, Monad m, Real t, Fractional t, Real a, Fractional a) => Easing t a -> t -> t -> a -> (t -> x) -> TweenT a x m () Source #
A version of withTween
that discards its output.
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/.
easeOutPow :: (Num t, Fractional t, Real f) => Int -> Easing t f Source #
Ease out by some power.
easeInCubic :: (Num t, Fractional t, Real f) => Easing t f Source #
Ease in cubic.
easeOutCubic :: (Num t, Fractional t, Real f) => Easing t f Source #
Ease out cubic.
easeInQuad :: (Num t, Fractional t, Real f) => Easing t f Source #
Ease in quadratic.
easeOutQuad :: (Num t, Fractional t, Real f) => Easing t f 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 start end dur = do (dt, x) <- tween easeInExpo start end (dur/2) tween easeOutExpo x end $ dt + dur/2