diff --git a/src/clojure/freactive/animation.cljs b/src/clojure/freactive/animation.cljs index 71a7d61..f0402fd 100644 --- a/src/clojure/freactive/animation.cljs +++ b/src/clojure/freactive/animation.cljs @@ -76,41 +76,6 @@ (when callback (callback))))) -;(deftype AnimationReactiveExpression [state f active] -; IWatchable -; (-add-watch [this key cb] -; (when active -; (cb key this nil state))) -; (-remove-watch [this key]) -; (-notify-watches [this oldval newval] ) -; -; r/IInvalidates -; (-add-invalidation-watch [this key cb] -; (when active -; (cb key this))) -; (-remove-invalidation-watch [this key]) -; (-notify-invalidation-watches [this]) -; -; IDeref -; (-deref [_] -; (when (and active r/*invalidate-rx*) -; (r/*invalidate-rx*)) -; state)) -; -;(defn animation-rx* [f] -; (let [r (AnimationReactiveExpression. nil f true)] -; (add-watch dom/frame-time r (fn [_ _ _ _] (set! (.-state r) (f)))) -; r)) - -;; Easing functions -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Adapted from https://github.com/danielytics/ominate -; Who adapted it from https://github.com/warrenm/AHEasing -; And probably ultimately from Robert Penner - -(def PI (.-PI js/Math)) -(def PI_2 (/ (.-PI js/Math) 2)) - (def linear identity) (defn quad-in @@ -122,226 +87,3 @@ "Modeled after the parabola y = -x^2 + 2x" [p] (- (* p (- p 2)))) - -(defn quad-in-out - "Modeled after the piecewise quadratic - y = (1/2)((2x)^2) [0, 0.5) - y = -(1/2)((2x-1)*(2x-3) - 1) [0.5, 1]" - [p] - (if (< p 0.5) - (* 2 p p) - (+ (* 4 p) (* -2 p p) -1))) - -(defn cube-in - "Modeled after the cubic y = x^3" - [p] - (* p p p)) - -(defn cube-out - "Modeled after the cubic y = (x - 1)^3 + 1" - [p] - (let [f (dec p)] - (inc (* f f f)))) - -(defn cube-in-out - "Modeled after the piecewise cubic - y = (1/2)((2x)^3) [0, 0.5) - y = (1/2)((2x-2)^3 + 2) [0.5, 1]" - [p] - (if (< p 0.5) - (* p p p 4) - (let [f (- (* 2 p) 2)] - (inc (* 0.5 f f f))))) - -(defn quart-in - "Modeled after the quartic x^4" - [p] - (* p p p p)) - -(defn quart-out - "Modeled after the quartic y = 1 - (x - 1)^4" - [p] - (let [f (dec p)] - (inc (* f f f (- 1 p))))) - -(defn quart-in-out - "Modeled after the piecewise quartic - y = (1/2)((2x)^4) [0, 0.5) - y = -(1/2)((2x-2)^4 - 2) [0.5, 1]" - [p] - (if (< p 0.5) - (* p p p p 8) - (let [f (dec p)] - (inc (* -8 f f f f))))) - -(defn quint-in - "Modeled after the quintic y = x^5" - [p] - (* p p p p p)) - -(defn quint-out - "Modeled after the quintic y = (x - 1)^5 + 1" - [p] - (let [f (dec p)] - (inc (* f f f f f)))) - -(defn quint-in-out - "Modeled after the piecewise quintic - y = (1/2)((2x)^5) [0, 0.5) - y = (1/2)((2x-2)^5 + 2) [0.5, 1]" - [p] - (if (< p 0.5) - (* 16 p p p p p) - (let [f (- (* 2 p) 2)] - (inc (* 0.5 f f f f f))))) - -(defn sine-in - "Modeled after quarter-cycle of sine wave" - [p] - (inc (.sin js/Math (* (dec p) PI_2)))) - -(defn sine-out - "Modeled after quarter-cycle of sine wave (different phase)" - [p] - (.sin js/Math (* p PI_2))) - -(defn sine-in-out - "Modeled after half sine wave" - [p] - (* 0.5 (- 1 (.cos js/Math (* p PI))))) - -(defn circular-in - "Modeled after shifted quadrant IV of unit circle" - [p] - (- 1 (.sqrt js/Math (- 1 (* p p))))) - -(defn circular-out - "Modeled after shifted quadrant II of unit circle" - [p] - (.sqrt js/Math (* (- 2 p) p))) - -(defn circular-in-out - "Modeled after the piecewise circular function - y = (1/2)(1 - sqrt(1 - 4x^2)) [0, 0.5) - y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) [0.5, 1]" - [p] - (if (< p 0.5) - (* 0.5 (- 1 (.sqrt js/Math (- 1 (* 4 (* p p)))))) - (* 0.5 (inc (.sqrt js/Math (- (* (- (* 2 p) 3) (dec (* 2 p))))))))) - -(defn exp-in - "Modeled after the exponential function y = 2^(10(x - 1))" - [p] - (if (= p 0) - p - (.pow js/Math 2 (* 10 (dec p))))) - -(defn exp-out - "Modeled after the exponential function y = -2^(-10x) + 1" - [p] - (if (= p 1) - p - (- 1 (.pow js/Math 2 (* -10 p))))) - -(defn exp-in-out - "Modeled after the piecewise exponential - y = (1/2)2^(10(2x - 1)) [0,0.5) - y = -(1/2)*2^(-10(2x - 1)) + 1 [0.5,1]" - [p] - (if (or (= p 0) (= p 1)) - p - (if (< p 0.5) - (* 0.5 (.pow js/Math 2 (- (* 20 p) 10))) - (inc (* -0.5 (.pow js/Math 2 (+ (* -20 p) 10))))))) - -(defn elastic-in - "Modeled after the damped sine wave y = sin(13PI_2*x)*pow(2, 10 * (x - 1))" - [p] - (* (.sin js/Math (* 13 PI_2 p)) - (.pow js/Math 2 (* 10 (dec p))))) - -(defn elastic-out - "Modeled after the damped sine wave y = sin(-13PI_2*(x + 1))*pow(2, -10x) + 1" - [p] - (inc (* (.sin js/Math (* -13 PI_2 (inc p))) - (.pow js/Math 2 (* -10 p))))) - -(defn elastic-in-out - "Modeled after the piecewise exponentially-damped sine wave: - y = (1/2)*sin(13PI_2*(2*x))*pow(2, 10 * ((2*x) - 1)) [0,0.5) - y = (1/2)*(sin(-13PI_2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) [0.5, 1]" - [p] - (if (< p 0.5) - (* 0.5 - (.sin js/Math (* 13 PI_2 2 p)) - (.pow js/Math 2 (* 10 (dec (* 2 p))))) - (* 0.5 - (+ (* (.sin js/Math (* -13 PI_2 (* 2 p))) - (.pow js/Math 2 (* -10 (inc (* 2 p))))) - 2)))) - -(defn back-in - "Modeled after the overshooting cubic y = x^3-x*sin(x*pi)" - [p] - (- (* p p p) - (* p (.sin js/Math (* p PI))))) - -(defn back-out - "Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))" - [p] - (let [f (- 1 p)] - (- 1 (- (* f f f) - (.sin js/Math (* f PI)))))) - -(defn back-in-out - "Modeled after the piecewise overshooting cubic function: - y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) [0, 0.5) - y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) [0.5, 1]" - [p] - (if (< p 0.5) - (let [f (* 2 p)] - (* 0.5 (- (* f f f) - (.sin js/Math (* f PI))))) - (let [f (- 1 (dec (* 2 p)))] - (+ (* 0.5 (- 1 (- (* f f f) - (* f (.sin js/Math (* f PI)))))) - 0.5)))) - -(defn bounce-out [p] - (cond - (< p (/ 1 2.75)) (* 7.5625 p p) - (< p (/ 2 2.75)) (+ (* 7.5625 - (- p (/ 1.5 2.75)) - (- p (/ 1.5 2.75))) - 0.75) - (< p (/ 2.5 2.75)) (+ (* 7.5625 - (- p (/ 2.5 2.75)) - (- p (/ 2.5 2.75))) - 0.9375) - :else (+ (* 7.5625 - (- p (/ 2.625 2.75)) - (- p (/ 2.625 2.75))) - 0.984375))) - -(defn bounce-in [p] - (- 1 (bounce-out (- 1 p)))) - -(defn bounce-in-out [p] - (if (< p 0.5) - (* 0.5 (bounce-in (* p 2))) - (+ (* 0.5 (bounce-out (dec (* p 2)))) 0.5))) - - -;TERMS OF USE - EASING EQUATIONS -; -;Open source under the BSD License. -; -;Copyright © 2001 Robert Penner -;All rights reserved. -; -;Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: -; -;Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. -;Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -;Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. -;THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.