blob: 3c88d4acb1eded9988de80e10554e6797be5e3e7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
;;; Sly
;;; Copyright (C) 2014 David Thompson <davet@gnu.org>
;;;
;;; Sly is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Sly is distributed in the hope that it will be useful, but WITHOUT
;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;;; License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Interpolate values over time with easing. Useful for animation.
;;
;;; Code:
(define-module (sly math tween)
#:use-module (sly math)
#:use-module (sly utils)
#:export (ease-loop ease-reflect ease-linear
ease-in-sine ease-out-sine ease-in-out-sine
ease-in-quad ease-out-quad ease-in-out-quad
tween))
;;;
;;; Easings
;;;
(define (ease-loop alpha)
(modulo* alpha 1))
(define (ease-reflect alpha)
(let ((cycle (modulo* alpha 2)))
(if (< cycle 1) cycle (- 2 cycle))))
(define (ease-linear alpha)
alpha)
(define (ease-in-sine alpha)
(+ (* (- alpha) (cos (* alpha pi/2))) alpha))
(define (ease-out-sine alpha)
(* alpha (sin (* alpha pi/2))))
(define (ease-in-out-sine alpha)
(* (/ alpha -2)
(1- (cos (* alpha pi)))))
(define (ease-in-quad alpha)
(expt alpha 3))
(define (ease-out-quad alpha)
(* (- alpha) alpha (- alpha 2)))
(define (ease-in-out-quad alpha)
(let ((beta (* alpha 2)))
(if (< beta 1)
(* (/ alpha 2) beta beta)
(* (/ alpha -2) (1- (* (1- beta) (- beta 3)))))))
;; TODO: See
;; <http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js> for
;; implementation details.
;;
;; ease-in-cubic
;; ease-out-cubic
;; ease-in-out-cubic
;; ease-in-quart
;; ease-out-quart
;; ease-in-out-quart
;; ease-in-quint
;; ease-out-quint
;; ease-in-out-quint
;; ease-in-expo
;; ease-out-expo
;; ease-in-out-expo
;; ease-in-circ
;; ease-out-circ
;; ease-in-out-circ
;; ease-in-back
;; ease-out-back
;; ease-in-out-back
;; ease-in-elastic
;; ease-out-elastic
;; ease-in-out-elastic
;; ease-in-bounce
;; ease-out-bounce
;; ease-in-out-bounce
;;;
;;; Tween
;;;
(define* (tween interpolator ease start end duration)
"Return a procedure that interpolates from START to END in DURATION
ticks. The value returned for a given time is determined by applying
EASE with the time ratio to acquire an 'alpha' value, and then
applying INTERPOLATOR with START, END, and alpha. Alpha is a rational
number in the range [0, 1]."
(define interpolate
(memoize
(lambda (alpha)
(interpolator start end alpha))))
(lambda (time)
(interpolate (clamp 0 1 (ease (/ time duration))))))
|