# dleslie/tween-egg

### Subversion checkout URL

You can clone with
or
.
Older
100644 202 lines (155 sloc) 9.025 kB
 95b60ec Updated readmes dleslie authored Jan 11, 2013 1 2 4 5 6 7 8 README.md 9 10 11 12 13 14

Tween Egg

15 16

Provides a (loose) port of Tween.js for Chicken Scheme. Other Schemes may work, though perhaps with some minor alterations.

17 18

General Functions

19 20

(factorial n)

21 22

Memoized implementation of the factorial method. Provides O(n) computation, albeit with an O(1) average case. Requires O(n) memory.

23 24

You may reset the memory by whiping out the cache: (set! factorial* (make-vector 1 1))

25 26

(rationalize count value #!optional (epsilon 0))

27 28

Sometimes its useful to truncate the number of decimal places a floating point number is exact to. Sometimes it's also useful to do so after applying an epsilon value to smudge for floating point computation errors. This function can perform both operations.

29 30

Note that it is not a function to force a number to a particular number of significant digits.

31 32

(tween easing-function direction start stop percent)

33 34

Given an easing-function and a direction, this produces a value between start and stop that is representative of the provided percent.

35 36

direction may be one of the following symbols: 37 * in 38 * out 39 * inout

40 41

percent must be a value between 0.0 and 1.0.

42 43

start and stop must be numbers.

44 45

easing-function be of the form direction -> percent -> percent.

46 47

(interpolate selector-function value-vector percent)

48 49

Given a selector-function this produces a value within the range of values defined in value-vector that is representative of the provided percent.

50 51

value-vector must be a vector of numbers.

52 53

percent must be a value between 0.0 and 1.0.

54 55

selector-function must be of the form value-vector -> percent -> number

56 57

Easing Functions

58 59

The following easing functions are available:

60 61
62
• linear-ease
• 63
• 64
• cubic-ease
• 65
• quartic-ease
• 66
• quintic-ease
• 67
• sinusoidal-ease
• 68
• exponential-ease
• 69
• circular-ease
• 70
• elastic-ease
• 71
• back-ease
• 72
• bounce-ease
• 73
74 75

Interpolation Functions

76 77

The following interpolation functions are available:

78 79
80
• linear-select
• 81
• bezier-select
• 82
• catmullrom-select
• 83
84 85

Example

86 87

```scheme 88 (define test-percents '(0.0 0.1 0.2 0.3 0.4 0.5 0.66 0.75 0.95 1.0))

89 90

(define (test-ease ease start stop) 91 (let ((in '()) 92 (out '()) 93 (inout '())) 94 (map (lambda (p) 95 (set! in (cons (rationalize 2 (tween ease 'in start stop p)) in)) 96 (set! out (cons (rationalize 2 (tween ease 'out start stop p)) out)) 97 (set! inout (cons (rationalize 2 (tween ease 'inout start stop p)) inout)) 98 ) 99 (reverse test-percents)) 100 `((in . ,in) (out . ,out) (inout . ,inout))))

101 102

;; Tests tweens for values between 0 and 10 103 (map (lambda (e) (display (list e 0 10 (test-ease e 0 10))) (newline)) 104 (list linear-ease quadratic-ease cubic-ease quartic-ease quintic-ease sinusoidal-ease exponential-ease circular-ease elastic-ease back-ease bounce-ease))

105 106

;; Tests interpolation for values in the given set 107 (let ((v '(0 1 2 3 4 5 10 15 20 25 30 35 40 45 50))) 108 (map (lambda (s) (display (list s v (test-select s (list->vector v))))) 109 (list linear-select bezier-select catmullrom-select)))

110 111

;; Tween test produces: 112 (# 0 10 113 ((in 0.0 1.0 2.0 3.0 4.0 5.0 6.6 7.5 9.5 10.0) 114 (out 0.0 1.0 2.0 3.0 4.0 5.0 6.6 7.5 9.5 10.0) 115 (inout 0.0 1.0 2.0 3.0 4.0 5.0 6.6 7.5 9.5 10.0))) 116 (# 0 10 117 ((in 0.0 0.1 0.4 0.89 1.6 2.5 4.35 5.62 9.02 10.0) 118 (out 0.0 1.89 3.6 5.09 6.4 7.5 8.84 9.37 9.97 10.0) 119 (inout 0.0 0.05 0.2 0.44 0.8 5.0 7.68 8.75 9.94 10.0))) 120 (# 0 10 121 ((in 0.0 0.01 0.08 0.26 0.64 1.25 2.87 4.21 8.57 10.0) 122 (out 0.0 2.7 4.87 6.57 7.83 8.75 9.6 9.84 9.99 10.0) 123 (inout 0.0 0.04 0.32 1.07 2.56 5.0 8.42 9.37 9.99 10.0))) 124 (# 0 10 125 ((in 0.0 0.0 0.01 0.08 0.25 0.62 1.89 3.16 8.14 10.0) 126 (out 0.0 3.43 5.9 7.59 8.7 9.37 9.86 9.96 9.99 10.0) 127 (inout 0.0 0.0 0.12 0.64 2.04 5.0 8.93 9.68 9.99 10.0))) 128 (# 0 10 129 ((in 0.0 0.0 0.0 0.02 0.1 0.31 1.25 2.37 7.73 10.0) 130 (out 0.0 4.09 6.72 8.31 9.22 9.68 9.95 9.99 9.99 10.0) 131 (inout 0.0 0.0 0.05 0.38 1.63 5.0 9.27 9.84 9.99 10.0))) 132 (# 0 10 133 ((in 0.0 0.12 0.48 1.08 1.9 2.92 4.9 6.17 9.21 9.99) 134 (out 0.0 1.56 3.09 4.53 5.87 7.07 8.6 9.23 9.96 10.0) 135 (inout 0.0 0.24 0.95 2.06 3.45 4.99 7.4 8.53 9.93 10.0))) 136 (# 0 10 137 ((in 0 0.01 0.03 0.07 0.15 0.31 0.94 1.76 7.07 10.0) 138 (out 0.0 5.0 7.5 8.75 9.37 9.68 9.89 9.94 9.98 10) 139 (inout 0 0.01 0.07 0.31 1.25 5.0 9.45 9.84 9.99 10))) 140 (# 0 10 141 ((in 0.0 0.05 0.2 0.46 0.83 1.33 2.48 3.38 6.87 10.0) 142 (out 0.0 4.35 5.99 7.14 8.0 8.66 9.4 9.68 9.98 10.0) 143 (inout 0.0 0.1 0.41 0.99 2.0 5.0 8.66 9.33 9.97 10.0))) 144 (# 0 10 145 ((in 0 0.0 0.03 0 0 0.0 0.55 0 5.0 10) 146 (out 0 9.99 10 10 9.37 9.99 10 9.96 10 10) 147 (inout 0 0.01 0 0.31 0 9.0 10 10 10 10))) 148 (# 0 10 149 ((in 0.0 0 0 0 0 0 0.35 1.82 7.8 9.99) 150 (out 0.0 4.08 7.05 9.07 10 10 10 10 10 10.0) 151 (inout 0.0 0 0 0 0.89 5.0 10 10 10 10.0))) 152 (# 0 10 153 ((in 0.0 0.11 0.6 0.69 2.27 2.34 1.25 5.27 9.81 10.0) 154 (out 0.0 0.75 3.02 6.8 9.09 7.65 8.49 9.72 9.84 10.0) 155 (inout 0.0 0.3 1.13 0.45 3.48 5.0 8.87 8.82 9.94 10.0)))

156 157

;; Interpolation test produces: 158 (# 159 (0 1 2 3 4 5 10 15 20 25 30 35 40 45 50) 160 (0.0 1.4 2.8 4.2 8.0 15.0 26.2 32.5 46.49 50.0)) 161 (# 162 (0 1 2 3 4 5 10 15 20 25 30 35 40 45 50) 163 (0.0 1.4 3.03 5.61 9.8 15.5 26.22 32.5 46.5 50.0)) 164 (# 165 (0 1 2 3 4 5 10 15 20 25 30 35 40 45 50) 166 (0.0 1.72 4.08 4.37 11.4 15.0 26.77 35.0 47.1 50.0)) 167 ```

168 169

170 171
172
173  The contact email address for Daniel J. Leslie is dan@ironoxide.ca
174
175  Redistribution and use in source and binary forms, with or without modification, are
176  permitted provided that the following conditions are met:
177
178  1. Redistributions of source code must retain the above copyright notice, this list of
179  conditions and the following disclaimer.
180
181  2. Redistributions in binary form must reproduce the above copyright notice, this list
182  of conditions and the following disclaimer in the documentation and/or other materials
183  provided with the distribution.
184
185  THIS SOFTWARE IS PROVIDED BY DANIEL J. LESLIE ''AS IS'' AND ANY EXPRESS OR IMPLIED
186  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
187  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DANIEL J. LESLIE OR
188  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
189  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
190  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
191  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
192  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
193  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
194
195  The views and conclusions contained in the software and documentation are those of the
196  authors and should not be interpreted as representing official policies, either expressed
197  or implied, of Daniel J. Leslie.
198
199 200 201
Something went wrong with that request. Please try again.