Skip to content

Commit

Permalink
Use bezier-js instead of bezier-easing and update API
Browse files Browse the repository at this point in the history
  • Loading branch information
larsenwork committed May 26, 2018
1 parent 94d50a1 commit 231a9ff
Show file tree
Hide file tree
Showing 14 changed files with 1,660 additions and 3,849 deletions.
36 changes: 13 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@ easingCoordinates('cubic-bezier(0.5, 0, 0.5, 1)')
/* =>
[
{ x: 0, y: 0 },
{ x: 0.099, y: 0.0143172435 },
{ x: 0.189, y: 0.0569704145 },
{ x: 0.265, y: 0.1207430791 },
{ x: 0.329, y: 0.1976360165 },
{ x: 0.384, y: 0.281541323 },
{ x: 0.433, y: 0.3687643431 },
{ x: 0.479, y: 0.4580875338 },
{ x: 0.524, y: 0.547869462 },
{ x: 0.57, y: 0.6368561714 },
{ x: 0.619, y: 0.7234436574 },
{ x: 0.674, y: 0.8064697166 },
{ x: 0.738, y: 0.8823204807 },
{ x: 0.815, y: 0.9456314885 },
{ x: 0.906, y: 0.9871537401 },
{ x: 0.136, y: 0.028 },
{ x: 0.248, y: 0.104 },
{ x: 0.342, y: 0.216 },
{ x: 0.424, y: 0.352 },
{ x: 0.5, y: 0.5 },
{ x: 0.576, y: 0.648 },
{ x: 0.658, y: 0.784 },
{ x: 0.752, y: 0.896 },
{ x: 0.864, y: 0.972 },
{ x: 1, y: 1 },
]
*/
Expand Down Expand Up @@ -66,20 +61,16 @@ cubicCoordinates(0.42, 0, 1, 1) === easingCoordinates('cubic-bezier(0.42, 0, 1,
stepsCoordinates(4, 'skip-end') === easingCoordinates('steps(4, skip-end)')
```

Increase hypotSize (default = 0.1) to get a "lower-poly" version of your cubic-bezier
functions and make sure incrementSize is always smaller than hypotSize.
Increase polySteps (default = 10, min = 2) to get a "higer-poly" version of your cubic-bezier
functions.

```ts
interface ICoordinate {
x: number
y: number
}

function easingCoordinates(
easingFunction: string,
hypotSize?: number,
incrementSize?: number
): ICoordinate[]
function easingCoordinates(easingFunction: string, polySteps?: number): ICoordinate[]

function stepsCoordinates(steps: number, skip = 'skip-end'): ICoordinate[]

Expand All @@ -88,8 +79,7 @@ function cubicCoordinates(
y1: number,
x2: number,
y2: number,
hypotSize = 0.1,
incrementSize = 0.001
polySteps = 10
): ICoordinate[]
```

Expand Down
4 changes: 2 additions & 2 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const easing_map_1 = require('./lib/easing-map')
const shared = require('./lib/shared')
const steps_coordinates_1 = require('./lib/steps-coordinates')
exports.stepsCoordinates = steps_coordinates_1.stepsCoordinates
function easingCoordinates(easingFunction, hypotSize, incrementSize) {
function easingCoordinates(easingFunction, steps) {
const errorMsgStart = `Error parsing "${easingFunction}".`
// If a shorthand like "ease-in" is provided then convert to equivalent cubic-bezier
if (easing_map_1.default[easingFunction]) {
Expand Down Expand Up @@ -38,7 +38,7 @@ function easingCoordinates(easingFunction, hypotSize, incrementSize) {
throw new Error(`${errorMsgStart} "${arg}" is not a number.`)
}
})
return cubic_coordinates_1.cubicCoordinates(x1, y1, x2, y2, hypotSize, incrementSize)
return cubic_coordinates_1.cubicCoordinates(x1, y1, x2, y2, steps)
}
// If it's not cubic bezier or steps it's not an easing function
} else {
Expand Down
96 changes: 33 additions & 63 deletions dist/cjs/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,49 @@ Object.defineProperty(exports, '__esModule', { value: true })
const index_1 = require('./index')
const cubicTest = [
{ x: 0, y: 0 },
{ x: 0.099, y: 0.0143172435 },
{ x: 0.189, y: 0.0569704145 },
{ x: 0.265, y: 0.1207430791 },
{ x: 0.329, y: 0.1976360165 },
{ x: 0.384, y: 0.281541323 },
{ x: 0.433, y: 0.3687643431 },
{ x: 0.479, y: 0.4580875338 },
{ x: 0.524, y: 0.547869462 },
{ x: 0.57, y: 0.6368561714 },
{ x: 0.619, y: 0.7234436574 },
{ x: 0.674, y: 0.8064697166 },
{ x: 0.738, y: 0.8823204807 },
{ x: 0.815, y: 0.9456314885 },
{ x: 0.906, y: 0.9871537401 },
{ x: 0.136, y: 0.028 },
{ x: 0.248, y: 0.104 },
{ x: 0.342, y: 0.216 },
{ x: 0.424, y: 0.352 },
{ x: 0.5, y: 0.5 },
{ x: 0.576, y: 0.648 },
{ x: 0.658, y: 0.784 },
{ x: 0.752, y: 0.896 },
{ x: 0.864, y: 0.972 },
{ x: 1, y: 1 },
]
const cubicTest2 = [
{ x: 0, y: 0 },
{ x: 0.019, y: 0.0714655527 },
{ x: 0.039, y: 0.1423025476 },
{ x: 0.06, y: 0.2132789027 },
{ x: 0.081, y: 0.2812839337 },
{ x: 0.103, y: 0.3496467348 },
{ x: 0.126, y: 0.418180504 },
{ x: 0.15, y: 0.4866577856 },
{ x: 0.175, y: 0.5548197244 },
{ x: 0.201, y: 0.6223801845 },
{ x: 0.228, y: 0.6890273721 },
{ x: 0.256, y: 0.7544240594 },
{ x: 0.286, y: 0.8203349833 },
{ x: 0.318, y: 0.8859282955 },
{ x: 0.352, y: 0.9502996909 },
{ x: 0.388, y: 1.0124633405 },
{ x: 0.427, y: 1.0727964422 },
{ x: 0.47, y: 1.1307595908 },
{ x: 0.518, y: 1.1846659862 },
{ x: 0.572, y: 1.2313501976 },
{ x: 0.634, y: 1.2660961482 },
{ x: 0.0145, y: 0.055 },
{ x: 0.056, y: 0.2 },
{ x: 0.1215, y: 0.405 },
{ x: 0.208, y: 0.64 },
{ x: 0.3125, y: 0.875 },
{ x: 0.432, y: 1.08 },
{ x: 0.5635, y: 1.225 },
{ x: 0.704, y: 1.28 },
{ x: 0.774, y: 1.2655211495 },
{ x: 0.835, y: 1.2282931395 },
{ x: 0.886, y: 1.1784636542 },
{ x: 0.93, y: 1.1209825808 },
{ x: 0.968, y: 1.0599610373 },
{ x: 0.8505, y: 1.215 },
{ x: 1, y: 1 },
]
const easeTest = [
{ x: 0, y: 0 },
{ x: 0.078, y: 0.0638426248 },
{ x: 0.132, y: 0.1498912253 },
{ x: 0.176, y: 0.2411299465 },
{ x: 0.217, y: 0.3341149122 },
{ x: 0.258, y: 0.4260516258 },
{ x: 0.302, y: 0.5172428201 },
{ x: 0.351, y: 0.6064813564 },
{ x: 0.406, y: 0.6910020791 },
{ x: 0.469, y: 0.7697611249 },
{ x: 0.541, y: 0.8402054507 },
{ x: 0.622, y: 0.8995163973 },
{ x: 0.711, y: 0.9455150493 },
{ x: 0.806, y: 0.9771659923 },
{ x: 0.905, y: 0.9948875961 },
{ x: 0.0685, y: 0.0523 },
{ x: 0.128, y: 0.1424 },
{ x: 0.1845, y: 0.2601 },
{ x: 0.244, y: 0.3952 },
{ x: 0.3125, y: 0.5375 },
{ x: 0.396, y: 0.6768 },
{ x: 0.5005, y: 0.8029 },
{ x: 0.632, y: 0.9056 },
{ x: 0.7965, y: 0.9747 },
{ x: 1, y: 1 },
]
const easeTestHypot = [
const easeTestFive = [
{ x: 0, y: 0 },
{ x: 0.127, y: 0.1405524856 },
{ x: 0.208, y: 0.3135291619 },
{ x: 0.287, y: 0.4872575165 },
{ x: 0.38, y: 0.6530240578 },
{ x: 0.499, y: 0.801407407 },
{ x: 0.65, y: 0.9159338488 },
{ x: 0.828, y: 0.9823328575 },
{ x: 0.128, y: 0.1424 },
{ x: 0.244, y: 0.3952 },
{ x: 0.396, y: 0.6768 },
{ x: 0.632, y: 0.9056 },
{ x: 1, y: 1 },
]
const stepTestEnd = [
Expand Down Expand Up @@ -125,8 +95,8 @@ test('ease shorthand is the same as equivalent cubic-bezier', () => {
index_1.easingCoordinates('cubic-bezier(0.42, 0, 0.58, 1)')
)
})
test('coordinates for "ease" with a bigger hypotSize shoul be fewer and further appart', () => {
expect(index_1.easingCoordinates('ease', 0.2)).toEqual(easeTestHypot)
test('coordinates for "ease" with 5 steps', () => {
expect(index_1.easingCoordinates('ease', 5)).toEqual(easeTestFive)
})
test('easingCoordinates returns the same as cubicCoordinates', () => {
expect(index_1.easingCoordinates('cubic-bezier(0.42, 0, 0.58, 1)')).toEqual(
Expand Down
46 changes: 8 additions & 38 deletions dist/cjs/lib/cubic-coordinates.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
const BezierEasing = require('bezier-easing')
const Bezier = require('bezier-js')
// import { Bezier } from 'bezier-js'
// import * as Bezier from 'bezier-js'
const shared = require('./shared')
function cubicCoordinates(x1, y1, x2, y2, hypotSize = 0.1, incrementSize = 0.001) {
const bezier = BezierEasing(x1, y1, x2, y2)
let x = 0
let y = 0
let xOld = 0
let yOld = 0
let firstTime = true
let coordinates = []
// After first time test if distance from last coordinate added in inner loop (xOld, yOld) to (1, 1) is within 90% of average distance between coordinates
while (firstTime || Math.hypot(1 - xOld, 1 - yOld) < hypotSize * 0.9) {
if (firstTime) {
firstTime = false
} else {
// Decrease hypotSize by incrementSize and reset values
hypotSize -= incrementSize
x = 0
y = 0
xOld = 0
yOld = 0
coordinates = []
}
// Add the first coordinate
coordinates.push(shared.getCoordinate(0, 0))
// Loop and add coordinates every time it's far enough away from the previous one
while (x <= 1) {
y = bezier(x)
if (Math.hypot(x - xOld, y - yOld) > hypotSize) {
coordinates.push(shared.getCoordinate(x, y))
xOld = x
yOld = y
}
x += incrementSize
}
// Add the last coordinate
coordinates.push(shared.getCoordinate(1, 1))
}
return coordinates
function cubicCoordinates(x1, y1, x2, y2, steps = 10) {
const curve = new Bezier(0, 0, x1, y1, x2, y2, 1, 1)
const coordinates = curve.getLUT(steps)
const roundedCoordinates = coordinates.map(obj => shared.getCoordinate(obj.x, obj.y))
return roundedCoordinates
}
exports.cubicCoordinates = cubicCoordinates
6 changes: 1 addition & 5 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { cubicCoordinates } from './lib/cubic-coordinates'
import * as shared from './lib/shared'
import { stepsCoordinates } from './lib/steps-coordinates'
declare function easingCoordinates(
easingFunction: string,
hypotSize?: number,
incrementSize?: number
): shared.ICoordinate[]
declare function easingCoordinates(easingFunction: string, steps?: number): shared.ICoordinate[]
export { stepsCoordinates, cubicCoordinates, easingCoordinates, easingCoordinates as default }
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { cubicCoordinates } from './lib/cubic-coordinates'
import easingShorthandMap from './lib/easing-map'
import * as shared from './lib/shared'
import { stepsCoordinates } from './lib/steps-coordinates'
function easingCoordinates(easingFunction, hypotSize, incrementSize) {
function easingCoordinates(easingFunction, steps) {
const errorMsgStart = `Error parsing "${easingFunction}".`
// If a shorthand like "ease-in" is provided then convert to equivalent cubic-bezier
if (easingShorthandMap[easingFunction]) {
Expand Down Expand Up @@ -34,7 +34,7 @@ function easingCoordinates(easingFunction, hypotSize, incrementSize) {
throw new Error(`${errorMsgStart} "${arg}" is not a number.`)
}
})
return cubicCoordinates(x1, y1, x2, y2, hypotSize, incrementSize)
return cubicCoordinates(x1, y1, x2, y2, steps)
}
// If it's not cubic bezier or steps it's not an easing function
} else {
Expand Down

0 comments on commit 231a9ff

Please sign in to comment.