Basic tweening library for Nim
First you should select the preferred easing function (and any parameters if applicable) from the list found in TweenKind.
Next, instance your tween using createTween procedure
Finally, run a loop until t.step == t.steps, incrementing your tween in every interation.
Example:
import strutils import math import os import tweens var t = createTween(tkEaseIn, 0, 180, 90) while t.step != t.steps: stdout.write("\x1b[2K\x1b[u" & "=".repeat(round(t.val).Natural)) stdout.flushFile() sleep(50) inc t echo()
Types
TweenKind = enum tkLinear, tkEaseIn, tkEaseOut, tkEaseInOut
- The types of easing functions a tween could have.
Tween = object start: float goal: float val*: float step*: Natural steps*: Natural case kind: TweenKind of tkLinear: nil of tkEaseIn, tkEaseOut: p: int ## Parameter for easeIn and easeOut of tkEaseInOut: p1, p2: int ## Parameters for easeInOut
- The actual tween object. Should be created by createTween
Procs
proc createTween(kind: TweenKind; start, goal: float; steps: Natural; p = 2; p2 = p): Tween {...}{.raises: [], tags: [].}
- Creates a Tween. This should be used instead of the default constructor. p is passed into easeIn and easeOut. p and p2 are passed into easeInOut on the easeIn side and easeOut side respectively.
proc set(t: var Tween) {...}{.raises: [], tags: [].}
- Set the value of a Tween according to the easing function it needs Should only be used if you decide to set the tween's step without using inc or dec.
proc inc(t: var Tween; amt = 1.Natural) {...}{.raises: [], tags: [].}
- Increment t.step by amt and set the value of the tween. Prevents t.step from going higher than the max, t.steps, so this should be the preferred way of changing t.step.
proc dec(t: var Tween; amt = 1.Natural) {...}{.raises: [], tags: [].}
- Decrement t.step by amt and set the value of the tween. Prevents t.step from going below zero, so this should be the preferred way of changing t.step.
Funcs
func lerp(start, goal, perc: float): float {...}{.raises: [], tags: [].}
-
Basic linear interpolation. When perc is some value between zero and one, lerp will return start plus a percentage of the difference between start and end. If you increase perc, you will see your output grow linearly.
func easeIn(start, goal, perc: float; p = 2): float {...}{.raises: [], tags: [].}
-
Like lerp, except the rate of increase is higher the higher perc becomes. p is the exponent passed to easeIn, making the function's rapid increase later and more prevelent with higher p values.
func easeOut(start, goal, perc: float; p = 2): float {...}{.raises: [], tags: [].}
-
Opposite of easeIn. Instead of the rate of increase being higher with higher values of perc, the rate of increase slows down with higher values of perc. p still has the same effect.
func easeInOut(start, goal, perc: float; p1 = 2; p2 = 2): float {...}{.raises: [], tags: [].}
-
A mix between easeIn and easeOut. Starts off slow, speeds up, and then slows down again. p1 goes to easeIn and p2 goes to easeOut.