easing-coordinates
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,49 +0,62 @@ | ||
import { cubicCoordinates } from './lib/cubic-coordinates'; | ||
import easingShorthandMap from './lib/easing-map'; | ||
import * as shared from './lib/shared'; | ||
import { stepsCoordinates } from './lib/steps-coordinates'; | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
const cubic_coordinates_1 = require('./lib/cubic-coordinates') | ||
exports.cubicCoordinates = cubic_coordinates_1.cubicCoordinates | ||
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) { | ||
const errorMsgStart = `Error parsing "${easingFunction}".`; | ||
// If a shorthand like "ease-in" is provided then convert to equivalent cubic-bezier | ||
if (easingShorthandMap[easingFunction]) { | ||
easingFunction = easingShorthandMap[easingFunction]; | ||
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]) { | ||
easingFunction = easing_map_1.default[easingFunction] | ||
} | ||
// If we think it's a steps function | ||
if (easingFunction.includes('steps(')) { | ||
const args = shared.getFunctionArguments(easingFunction) | ||
const [stepCount, stepSkip] = args | ||
if (args.length < 1 || args.length > 2) { | ||
throw new Error( | ||
`${errorMsgStart} Got ${args.length} arguments but expected 1 or 2.` | ||
) | ||
} else { | ||
if (typeof args[0] !== 'number') { | ||
throw new Error(`${errorMsgStart} "${args[0]}" is not a number.`) | ||
} else if (args.length === 2 && typeof args[1] !== 'string') { | ||
throw new Error(`${errorMsgStart} "${args[1]}" is not a string.`) | ||
} | ||
return steps_coordinates_1.stepsCoordinates(stepCount, stepSkip) | ||
} | ||
// If we think it's a steps function | ||
if (easingFunction.includes('steps(')) { | ||
const args = shared.getFunctionArguments(easingFunction); | ||
const [stepCount, stepSkip] = args; | ||
if (args.length < 1 || args.length > 2) { | ||
throw new Error(`${errorMsgStart} Got ${args.length} arguments but expected 1 or 2.`); | ||
// If we think it's a cubic-bezier function | ||
} else if (easingFunction.includes('cubic-bezier(')) { | ||
const args = shared.getFunctionArguments(easingFunction) | ||
const [x1, y1, x2, y2] = args | ||
if (args.length !== 4) { | ||
throw new Error( | ||
`${errorMsgStart} Got ${args.length} arguments but expected 4.` | ||
) | ||
} else { | ||
args.forEach(arg => { | ||
if (typeof arg !== 'number') { | ||
throw new Error(`${errorMsgStart} "${arg}" is not a number.`) | ||
} | ||
else { | ||
if (typeof args[0] !== 'number') { | ||
throw new Error(`${errorMsgStart} "${args[0]}" is not a number.`); | ||
} | ||
else if (args.length === 2 && typeof args[1] !== 'string') { | ||
throw new Error(`${errorMsgStart} "${args[1]}" is not a string.`); | ||
} | ||
return stepsCoordinates(stepCount, stepSkip); | ||
} | ||
// If we think it's a cubic-bezier function | ||
}) | ||
return cubic_coordinates_1.cubicCoordinates( | ||
x1, | ||
y1, | ||
x2, | ||
y2, | ||
hypotSize, | ||
incrementSize | ||
) | ||
} | ||
else if (easingFunction.includes('cubic-bezier(')) { | ||
const args = shared.getFunctionArguments(easingFunction); | ||
const [x1, y1, x2, y2] = args; | ||
if (args.length !== 4) { | ||
throw new Error(`${errorMsgStart} Got ${args.length} arguments but expected 4.`); | ||
} | ||
else { | ||
args.forEach(arg => { | ||
if (typeof arg !== 'number') { | ||
throw new Error(`${errorMsgStart} "${arg}" is not a number.`); | ||
} | ||
}); | ||
return cubicCoordinates(x1, y1, x2, y2, hypotSize, incrementSize); | ||
} | ||
// If it's not cubic bezier or steps it's not an easing function | ||
} | ||
else { | ||
throw new Error(`${errorMsgStart} If not a typo then please create a GitHub issue :)`); | ||
} | ||
// If it's not cubic bezier or steps it's not an easing function | ||
} else { | ||
throw new Error( | ||
`${errorMsgStart} If not a typo then please create a GitHub issue :)` | ||
) | ||
} | ||
} | ||
export { stepsCoordinates, cubicCoordinates, easingCoordinates, easingCoordinates as default, }; | ||
exports.easingCoordinates = easingCoordinates | ||
exports.default = easingCoordinates |
@@ -1,109 +0,111 @@ | ||
import { cubicCoordinates, easingCoordinates, stepsCoordinates } from './index'; | ||
'use strict' | ||
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: 1, y: 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: 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.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: 1, y: 1 }, | ||
]; | ||
{ 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.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: 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: 1, y: 1 }, | ||
]; | ||
{ 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: 1, y: 1 }, | ||
] | ||
const easeTestHypot = [ | ||
{ 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: 1, y: 1 }, | ||
]; | ||
{ 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: 1, y: 1 }, | ||
] | ||
const stepTestEnd = [ | ||
{ x: 0, y: 0 }, | ||
{ x: 0.25, y: 0 }, | ||
{ x: 0.25, y: 0.25 }, | ||
{ x: 0.5, y: 0.25 }, | ||
{ x: 0.5, y: 0.5 }, | ||
{ x: 0.75, y: 0.5 }, | ||
{ x: 0.75, y: 0.75 }, | ||
{ x: 1, y: 0.75 }, | ||
]; | ||
{ x: 0, y: 0 }, | ||
{ x: 0.25, y: 0 }, | ||
{ x: 0.25, y: 0.25 }, | ||
{ x: 0.5, y: 0.25 }, | ||
{ x: 0.5, y: 0.5 }, | ||
{ x: 0.75, y: 0.5 }, | ||
{ x: 0.75, y: 0.75 }, | ||
{ x: 1, y: 0.75 }, | ||
] | ||
const stepTestNone = [ | ||
{ x: 0, y: 0 }, | ||
{ x: 0.25, y: 0 }, | ||
{ x: 0.25, y: 0.3333333333 }, | ||
{ x: 0.5, y: 0.3333333333 }, | ||
{ x: 0.5, y: 0.6666666667 }, | ||
{ x: 0.75, y: 0.6666666667 }, | ||
{ x: 0.75, y: 1 }, | ||
{ x: 1, y: 1 }, | ||
]; | ||
{ x: 0, y: 0 }, | ||
{ x: 0.25, y: 0 }, | ||
{ x: 0.25, y: 0.3333333333 }, | ||
{ x: 0.5, y: 0.3333333333 }, | ||
{ x: 0.5, y: 0.6666666667 }, | ||
{ x: 0.75, y: 0.6666666667 }, | ||
{ x: 0.75, y: 1 }, | ||
{ x: 1, y: 1 }, | ||
] | ||
const stepTestBoth = [ | ||
{ x: 0, y: 0.2 }, | ||
{ x: 0.25, y: 0.2 }, | ||
{ x: 0.25, y: 0.4 }, | ||
{ x: 0.5, y: 0.4 }, | ||
{ x: 0.5, y: 0.6 }, | ||
{ x: 0.75, y: 0.6 }, | ||
{ x: 0.75, y: 0.8 }, | ||
{ x: 1, y: 0.8 }, | ||
]; | ||
{ x: 0, y: 0.2 }, | ||
{ x: 0.25, y: 0.2 }, | ||
{ x: 0.25, y: 0.4 }, | ||
{ x: 0.5, y: 0.4 }, | ||
{ x: 0.5, y: 0.6 }, | ||
{ x: 0.75, y: 0.6 }, | ||
{ x: 0.75, y: 0.8 }, | ||
{ x: 1, y: 0.8 }, | ||
] | ||
/* | ||
@@ -113,38 +115,52 @@ * Test that we correct output when giving valid input | ||
test('coordinates for "cubic-bezier(0.5, 0, 0.5, 1)"', () => { | ||
expect(easingCoordinates('cubic-bezier(0.5, 0, 0.5, 1)')).toEqual(cubicTest); | ||
}); | ||
expect(index_1.easingCoordinates('cubic-bezier(0.5, 0, 0.5, 1)')).toEqual( | ||
cubicTest | ||
) | ||
}) | ||
test('coordinates for "cubic-bezier(0, 0, 0.5, 2)"', () => { | ||
expect(easingCoordinates('cubic-bezier(0, 0, 0.5, 2)')).toEqual(cubicTest2); | ||
}); | ||
expect(index_1.easingCoordinates('cubic-bezier(0, 0, 0.5, 2)')).toEqual( | ||
cubicTest2 | ||
) | ||
}) | ||
test('coordinates for "ease"', () => { | ||
expect(easingCoordinates('ease')).toEqual(easeTest); | ||
}); | ||
expect(index_1.easingCoordinates('ease')).toEqual(easeTest) | ||
}) | ||
test('ease shorthand is the same as equivalent cubic-bezier', () => { | ||
expect(easingCoordinates('ease-in-out')).toEqual(easingCoordinates('cubic-bezier(0.42, 0, 0.58, 1)')); | ||
}); | ||
expect(index_1.easingCoordinates('ease-in-out')).toEqual( | ||
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(easingCoordinates('ease', 0.2)).toEqual(easeTestHypot); | ||
}); | ||
expect(index_1.easingCoordinates('ease', 0.2)).toEqual(easeTestHypot) | ||
}) | ||
test('easingCoordinates returns the same as cubicCoordinates', () => { | ||
expect(easingCoordinates('cubic-bezier(0.42, 0, 0.58, 1)')).toEqual(cubicCoordinates(0.42, 0, 0.58, 1)); | ||
}); | ||
expect(index_1.easingCoordinates('cubic-bezier(0.42, 0, 0.58, 1)')).toEqual( | ||
index_1.cubicCoordinates(0.42, 0, 0.58, 1) | ||
) | ||
}) | ||
test('easingCoordinates returns the same as stepsCoordinates', () => { | ||
expect(easingCoordinates('steps(4, skip-end)')).toEqual(stepsCoordinates(4, 'skip-end')); | ||
}); | ||
expect(index_1.easingCoordinates('steps(4, skip-end)')).toEqual( | ||
index_1.stepsCoordinates(4, 'skip-end') | ||
) | ||
}) | ||
test('coordinates for "steps(4, skip-end)"', () => { | ||
expect(easingCoordinates('steps(4, skip-end)')).toEqual(stepTestEnd); | ||
}); | ||
expect(index_1.easingCoordinates('steps(4, skip-end)')).toEqual(stepTestEnd) | ||
}) | ||
test('coordinates for "steps(4)" - the default is "skip-end" as per spec', () => { | ||
expect(easingCoordinates('steps(4)')).toEqual(stepTestEnd); | ||
}); | ||
expect(index_1.easingCoordinates('steps(4)')).toEqual(stepTestEnd) | ||
}) | ||
test('coordinates for "steps(4, skip-none)"', () => { | ||
expect(easingCoordinates('steps(4, skip-none)')).toEqual(stepTestNone); | ||
}); | ||
expect(index_1.easingCoordinates('steps(4, skip-none)')).toEqual(stepTestNone) | ||
}) | ||
test('coordinates for "steps(4, skip-both)"', () => { | ||
expect(easingCoordinates('steps(4, skip-both)')).toEqual(stepTestBoth); | ||
}); | ||
expect(index_1.easingCoordinates('steps(4, skip-both)')).toEqual(stepTestBoth) | ||
}) | ||
test('old and new steps syntax should yield the same', () => { | ||
expect(easingCoordinates('steps(4, skip-end)')).toEqual(easingCoordinates('steps(4, end)')); | ||
expect(easingCoordinates('steps(2, skip-start)')).toEqual(easingCoordinates('steps(2, start)')); | ||
}); | ||
expect(index_1.easingCoordinates('steps(4, skip-end)')).toEqual( | ||
index_1.easingCoordinates('steps(4, end)') | ||
) | ||
expect(index_1.easingCoordinates('steps(2, skip-start)')).toEqual( | ||
index_1.easingCoordinates('steps(2, start)') | ||
) | ||
}) | ||
/* | ||
@@ -154,54 +170,54 @@ * Throwing Errors when giving incorrect input | ||
test('too few input in steps should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('steps()'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('steps()') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) | ||
test('too many input in steps should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('steps(4, skip-end, 3)'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('steps(4, skip-end, 3)') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) | ||
test('steps without a number first should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('steps(skip-end, 4)'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('steps(skip-end, 4)') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) | ||
test('steps without a string last should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('steps(4, 4)'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('steps(4, 4)') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) | ||
test('incorrect steps skips instructions should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('steps(4, skip-forward)'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('steps(4, skip-forward)') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) | ||
test('too few input in cubic should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('cubic-bezier(0.5, 0, 0.5)'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('cubic-bezier(0.5, 0, 0.5)') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) | ||
test('too many input in cubic should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('cubic-bezier(0.5, 0, 0.5, 1, 1)'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('cubic-bezier(0.5, 0, 0.5, 1, 1)') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) | ||
test('non number input in cubic should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('cubic-bezier(0.5, hello, 0.5, 1)'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('cubic-bezier(0.5, hello, 0.5, 1)') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) | ||
test('non easing function should throw an error', () => { | ||
function incorrectInput() { | ||
easingCoordinates('funky(0.5, 0.5, 0.5, 1, 1)'); | ||
} | ||
expect(incorrectInput).toThrowError(); | ||
}); | ||
function incorrectInput() { | ||
index_1.easingCoordinates('funky(0.5, 0.5, 0.5, 1, 1)') | ||
} | ||
expect(incorrectInput).toThrowError() | ||
}) |
@@ -1,41 +0,50 @@ | ||
import * as BezierEasing from 'bezier-easing'; | ||
import * as shared from './shared'; | ||
export 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)); | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
const BezierEasing = require('bezier-easing') | ||
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 = [] | ||
} | ||
return 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 | ||
} | ||
exports.cubicCoordinates = cubicCoordinates |
@@ -0,7 +1,9 @@ | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
const easeMap = { | ||
ease: 'cubic-bezier(0.25, 0.1, 0.25, 1)', | ||
'ease-in': 'cubic-bezier(0.42, 0, 1, 1)', | ||
'ease-in-out': 'cubic-bezier(0.42, 0, 0.58, 1)', | ||
'ease-out': 'cubic-bezier(0, 0, 0.58, 1)', | ||
}; | ||
export { easeMap as default }; | ||
ease: 'cubic-bezier(0.25, 0.1, 0.25, 1)', | ||
'ease-in': 'cubic-bezier(0.42, 0, 1, 1)', | ||
'ease-in-out': 'cubic-bezier(0.42, 0, 0.58, 1)', | ||
'ease-out': 'cubic-bezier(0, 0, 0.58, 1)', | ||
} | ||
exports.default = easeMap |
@@ -1,18 +0,23 @@ | ||
const getParenthesisContent = (str) => { | ||
return str | ||
.slice(str.indexOf('(') + 1, str.lastIndexOf(')')) | ||
.split(',') | ||
.map(item => item.trim()) | ||
.filter(item => item !== ''); | ||
}; | ||
const convertToNumberMaybe = (str) => Number.isNaN(Number(str)) ? str : Number(str); | ||
export const roundToMaxTenDecimals = (num) => Number(`${+num.toFixed(10)}`); | ||
export const getCoordinate = (x, y) => { | ||
return { | ||
x: roundToMaxTenDecimals(x), | ||
y: roundToMaxTenDecimals(y), | ||
}; | ||
}; | ||
export const getFunctionArguments = (functionAsString) => { | ||
return getParenthesisContent(functionAsString).map(arg => convertToNumberMaybe(arg)); | ||
}; | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
const getParenthesisContent = str => { | ||
return str | ||
.slice(str.indexOf('(') + 1, str.lastIndexOf(')')) | ||
.split(',') | ||
.map(item => item.trim()) | ||
.filter(item => item !== '') | ||
} | ||
const convertToNumberMaybe = str => | ||
Number.isNaN(Number(str)) ? str : Number(str) | ||
exports.roundToMaxTenDecimals = num => Number(`${+num.toFixed(10)}`) | ||
exports.getCoordinate = (x, y) => { | ||
return { | ||
x: exports.roundToMaxTenDecimals(x), | ||
y: exports.roundToMaxTenDecimals(y), | ||
} | ||
} | ||
exports.getFunctionArguments = functionAsString => { | ||
return getParenthesisContent(functionAsString).map(arg => | ||
convertToNumberMaybe(arg) | ||
) | ||
} |
@@ -1,29 +0,28 @@ | ||
import * as shared from './shared'; | ||
export function stepsCoordinates(steps, skip = 'skip-end') { | ||
const coordinates = []; | ||
let n = 0; | ||
while (n < steps) { | ||
const x1 = n / steps; | ||
const x2 = (n + 1) / steps; | ||
let y; | ||
if (skip === 'skip-none') { | ||
y = n / (steps - 1); | ||
} | ||
else if (skip === 'skip-both') { | ||
y = (n + 1) / (steps + 1); | ||
} | ||
else if (skip === 'skip-start' || skip === 'start') { | ||
y = (n + 1) / steps; | ||
} | ||
else if (skip === 'skip-end' || skip === 'end') { | ||
y = n / steps; | ||
} | ||
else { | ||
throw new Error(`Error can't recognise step skip "${skip}"`); | ||
} | ||
coordinates.push(shared.getCoordinate(x1, y)); | ||
coordinates.push(shared.getCoordinate(x2, y)); | ||
++n; | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
const shared = require('./shared') | ||
function stepsCoordinates(steps, skip = 'skip-end') { | ||
const coordinates = [] | ||
let n = 0 | ||
while (n < steps) { | ||
const x1 = n / steps | ||
const x2 = (n + 1) / steps | ||
let y | ||
if (skip === 'skip-none') { | ||
y = n / (steps - 1) | ||
} else if (skip === 'skip-both') { | ||
y = (n + 1) / (steps + 1) | ||
} else if (skip === 'skip-start' || skip === 'start') { | ||
y = (n + 1) / steps | ||
} else if (skip === 'skip-end' || skip === 'end') { | ||
y = n / steps | ||
} else { | ||
throw new Error(`Error can't recognise step skip "${skip}"`) | ||
} | ||
return coordinates; | ||
coordinates.push(shared.getCoordinate(x1, y)) | ||
coordinates.push(shared.getCoordinate(x2, y)) | ||
++n | ||
} | ||
return coordinates | ||
} | ||
exports.stepsCoordinates = stepsCoordinates |
{ | ||
"name": "easing-coordinates", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": | ||
"Utility script that takes an easing function as input and outputs a coordinate set with adjustable precision/resolution.", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/larsenwork/easing-coordinates.git" | ||
}, | ||
"files": ["dist"], | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/es6/index.js", | ||
"types": "dist/es6/index.d.ts", | ||
"scripts": { | ||
"clean": "rm -rf dist", | ||
"es6-build": "tsc", | ||
"es6-watch": "tsc --watch", | ||
"cjs-build": "tsc --module commonjs --declaration false --outDir dist", | ||
"cjs-watch": | ||
"tsc --watch --module commonjs --declaration false --outDir dist", | ||
"build": "npm run clean && npm run es6-build & npm run cjs-build", | ||
"watch": "npm run es6-watch & npm run cjs-watch", | ||
"test": "jest", | ||
@@ -13,10 +27,4 @@ "test-ci": | ||
"coverage": "jest --coverage", | ||
"build": "tsc", | ||
"dev": "tsc -w", | ||
"precommit": "pretty-quick --staged" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/larsenwork/easing-coordinates.git" | ||
}, | ||
"keywords": ["easing-functions", "javascript"], | ||
@@ -23,0 +31,0 @@ "author": "Andreas Larsen <andreas@larsenwork.com> (https://larsenwork.com)", |
@@ -22,4 +22,7 @@ <h1 align="center">Easing Coordinates</h1> | ||
## Usage | ||
The easingCoordinates function takes steps and cubic-bezier [single-transition-timing-functions](https://developer.mozilla.org/en-US/docs/Web/CSS/single-transition-timing-function) as input and returns a set of "low-poly" xy-coordinates. | ||
The easingCoordinates function takes steps and cubic-bezier | ||
[single-transition-timing-functions](https://developer.mozilla.org/en-US/docs/Web/CSS/single-transition-timing-function) | ||
as input and returns a set of "low-poly" xy-coordinates. | ||
```js | ||
@@ -29,3 +32,3 @@ import { easingCoordinates } from 'easing-coordinates' | ||
easingCoordinates('cubic-bezier(0.5, 0, 0.5, 1)') | ||
/* => | ||
/* => | ||
[ | ||
@@ -66,11 +69,16 @@ { x: 0, y: 0 }, | ||
Use `stepsCoordinates` and `cubicCoordinates` methods directly depending on your data: | ||
Use `stepsCoordinates` and `cubicCoordinates` methods directly depending on your | ||
data: | ||
```js | ||
import { cubicCoordinates, easingCoordinates, stepsCoordinates } from './index' | ||
easingCoordinates('cubic-bezier(0.42, 0, 0.58, 1)') == cubicCoordinates(0.42, 0, 0.58, 1) | ||
easingCoordinates('cubic-bezier(0.42, 0, 0.58, 1)') == | ||
cubicCoordinates(0.42, 0, 0.58, 1) | ||
easingCoordinates('steps(4, skip-end)') == stepsCoordinates(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 hypotSize (default = 0.1) to get a "lower-poly" version of your cubic-bezier | ||
functions and make sure incrementSize is always smaller than hypotSize. | ||
```ts | ||
@@ -81,14 +89,26 @@ interface ICoordinate { | ||
} | ||
function easingCoordinates(easingFunction: string, hypotSize?: number, incrementSize?: number): ICoordinate[] | ||
function easingCoordinates( | ||
easingFunction: string, | ||
hypotSize?: number, | ||
incrementSize?: number | ||
): ICoordinate[] | ||
function stepsCoordinates(steps: number, skip = 'skip-end'): ICoordinate[] | ||
function cubicCoordinates(x1: number, y1: number, x2: number, y2: number, hypotSize = 0.1, incrementSize = 0.001): ICoordinate[] | ||
function cubicCoordinates( | ||
x1: number, | ||
y1: number, | ||
x2: number, | ||
y2: number, | ||
hypotSize = 0.1, | ||
incrementSize = 0.001 | ||
): ICoordinate[] | ||
``` | ||
## Build | ||
## Build | ||
```bash | ||
# Checkout and then | ||
npm install | ||
npm install | ||
# Compile and watch the .ts files | ||
npm run dev | ||
npm run watch | ||
@@ -99,3 +119,4 @@ # Run test | ||
## PRs, suggestions, etc. | ||
## PRs, suggestions, etc | ||
Are very welcome 🤗 |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
118
31604
21
804
1