Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 78 additions & 13 deletions src/Ease.elm
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,93 @@ linear =

{-| A cubic bezier function using 4 parameters: x and y position of first control point, and x and y position of second control point.

See [here](http://greweb.me/glsl-transition/example/ "glsl-transitions") for examples or [here](http://cubic-bezier.com/ "tester") to test.
See [here](http://cubic-bezier.com/ "tester") to test.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this link since it does not work anymore. Are there other resources we could link to?


-}
bezier : Float -> Float -> Float -> Float -> Easing
bezier x1 y1 x2 y2 time =
let
lerp from to v =
from + (to - from) * v
f =
bezierPoint x1 y1 x2 y2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git kinda shredded this diff, it's better to view it side-by-side


pair interpolate ( a0, b0 ) ( a1, b1 ) v =
( interpolate a0 a1 v, interpolate b0 b1 v )
numSteps =
-- performance/quality tradeoff, 8 steps should be good enough in most cases
8
in
if time == 0 then
0

else if time == 1 then
1

else
bezierHelper numSteps f time ( 0, 1 )


{-| Use binary search to find such `tMid` that the `x` coordinate of `f tMid` is as close to `t` as
possible, then return the `y` coordinate.
-}
bezierHelper : Int -> (Float -> ( Float, Float )) -> Float -> ( Float, Float ) -> Float
bezierHelper steps f t ( tMin, tMax ) =
let
tMid =
(tMin + tMax) / 2

( x, y ) =
f tMid
in
if steps == 0 || x == t then
y

else
let
newRange =
if x < t then
( tMid, tMax )

else
( tMin, tMid )
in
bezierHelper (steps - 1) f t newRange


{-| Calculates the (x, y) coordinates of a point of a [ (0, 0), a, b, (1, 1) ] cubic bezier for
given time in <0, 1> range.

Based on [this gif on wikipedia](https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Higher-order_curves)

-}
bezierPoint : Float -> Float -> Float -> Float -> Float -> ( Float, Float )
bezierPoint xa ya xb yb time =
let
q0 =
interpolate2d ( 0, 0 ) ( xa, ya ) time

casteljau ps =
case ps of
[ ( x, y ) ] ->
y
q1 =
interpolate2d ( xa, ya ) ( xb, yb ) time

xs ->
List.map2 (\x y -> pair lerp x y time) xs (Maybe.withDefault [] (List.tail xs))
|> casteljau
q2 =
interpolate2d ( xb, yb ) ( 1, 1 ) time

r0 =
interpolate2d q0 q1 time

r1 =
interpolate2d q1 q2 time

b =
interpolate2d r0 r1 time
in
casteljau [ ( 0, 0 ), ( x1, y1 ), ( x2, y2 ), ( 1, 1 ) ]
b


{-| Return a point on line segment (a, b) for given t in <0, 1> range
-}
interpolate2d : ( Float, Float ) -> ( Float, Float ) -> Float -> ( Float, Float )
interpolate2d ( xa, ya ) ( xb, yb ) t =
( xa + t * (xb - xa)
, ya + t * (yb - ya)
)


{-| -}
Expand Down
10 changes: 8 additions & 2 deletions test/Test.elm
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ main : Svg a
main =
Svg.svg
[ SvgAttributes.width "850"
, SvgAttributes.height "650"
, SvgAttributes.viewBox "-10 -60 840 590"
, SvgAttributes.height "760"
, SvgAttributes.viewBox "-10 -60 840 700"
, SvgAttributes.style "display:block;margin:auto;"
]
(title :: List.indexedMap plot easingFunctions)
Expand Down Expand Up @@ -138,4 +138,10 @@ easingFunctions =
, ( inBounce, "inBounce" )
, ( outBounce, "outBounce" )
, ( inOutBounce, "inOutBounce" )
, ( bezier 1 0 0 1, "bezier 1" )
, ( bezier 1 0 0 0, "bezier 2" )
, ( bezier 0 0 0 1, "bezier 3" )
, ( bezier 0 1 1 0, "bezier 4" )
, ( bezier 1 1 1 0, "bezier 5" )
, ( bezier 1 1 0 1, "bezier 6" )
]
2 changes: 1 addition & 1 deletion test/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
".",
"../src"
],
"elm-version": "0.19.0",
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/core": "1.0.0",
Expand Down