forked from Dandandan/Easing
-
Notifications
You must be signed in to change notification settings - Fork 3
fix: reimplement bezier with correct behavior #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ffigiel
wants to merge
3
commits into
elm-community:master
Choose a base branch
from
ffigiel:fix-bezier
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,28 +73,87 @@ 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. | ||
|
||
-} | ||
bezier : Float -> Float -> Float -> Float -> Easing | ||
bezier x1 y1 x2 y2 time = | ||
bezier x1 y1 x2 y2 = | ||
let | ||
lerp from to v = | ||
from + (to - from) * v | ||
f = | ||
bezierPoint x1 y1 x2 y2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
numSteps = | ||
-- performance/quality tradeoff, 8 steps should be good enough in most cases | ||
8 | ||
in | ||
\time -> | ||
bezierHelper numSteps f time ( 0, 1 ) | ||
|
||
pair interpolate ( a0, b0 ) ( a1, b1 ) v = | ||
( interpolate a0 a1 v, interpolate b0 b1 v ) | ||
|
||
casteljau ps = | ||
case ps of | ||
[ ( x, y ) ] -> | ||
y | ||
{-| 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 | ||
|
||
xs -> | ||
List.map2 (\x y -> pair lerp x y time) xs (Maybe.withDefault [] (List.tail xs)) | ||
|> casteljau | ||
( x, y ) = | ||
f tMid | ||
in | ||
casteljau [ ( 0, 0 ), ( x1, y1 ), ( x2, y2 ), ( 1, 1 ) ] | ||
if steps == 0 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 | ||
|
||
q1 = | ||
interpolate2d ( xa, ya ) ( xb, yb ) time | ||
|
||
q2 = | ||
interpolate2d ( xb, yb ) ( 1, 1 ) time | ||
|
||
r0 = | ||
interpolate2d q0 q1 time | ||
|
||
r1 = | ||
interpolate2d q1 q2 time | ||
|
||
b = | ||
interpolate2d r0 r1 time | ||
in | ||
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) | ||
) | ||
|
||
|
||
{-| -} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?