Last active
April 28, 2018 22:37
-
-
Save ivanreese/56c215beb9798b38dd6be4ed819fcb03 to your computer and use it in GitHub Desktop.
This file contains 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
# Pass this function an array of points from http://wtcurve.surge.sh | |
# and a value between 0 and 1, to get an eased result. | |
bezierEase = (points, goal)-> | |
a = 0 | |
b = 1 | |
p = null | |
attempts = 0 | |
while attempts++ < 20 # Set a reasonable upper limit on time spent finding the right result | |
mid = (a + b)/2 | |
p = evalCurve points, mid | |
if Math.abs(p[0] - goal) < 0.0001 # Tends to finish in around 10 attempts | |
return p[1] | |
if p[0] < goal then a = mid else b = mid | |
null # Null at the end of a loop stops CoffeeScript from creating a "results" array | |
return p[1] | |
evalCurve = (points, t)-> | |
while points.length > 1 | |
nextPoints = [] | |
for a, i in points when i < points.length-1 | |
b = points[i+1] | |
dx = b[0] - a[0] | |
dy = b[1] - a[1] | |
nx = a[0] + dx * t | |
ny = a[1] + dy * t | |
nextPoints.push [nx, ny] | |
null | |
points = nextPoints | |
null | |
return points[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment