https://github.com/loadingio/cubic
https://github.com/loadingio/cubic
Last synced: 19 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/loadingio/cubic
- Owner: loadingio
- Created: 2019-01-12T06:23:48.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T16:58:14.000Z (about 3 years ago)
- Last Synced: 2025-10-26T06:59:43.683Z (3 months ago)
- Language: JavaScript
- Size: 296 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# cubic
Calculator for cubic function and cubic bezier. Provided with an inverse function from given X to t, which could be used to calculate Y then.
Equation roots calculation use Sheng Jin Formula which might be inaccurate according to Kahan; there is an alternative solution here: https://www.npmjs.com/package/cubic-roots , which could be integrated in the future.
## Usage
require! <[cubic]>
bezier = new cubic.bezier [0.2, 0.3, 0.4, 0.5]
t = Math.random!
x = bezier.x t
y = bezier.y t
t-from-x = bezier.t x
assert t == t-from-x
coff = [0 to 3].map -> Math.random!
func = new cubic.func coff.0, coff.1, coff.2, coff.3
x = Math.random!
func.calc x # f(x)
root = func.root 0 # root for solving f(x) = 0
For a simply `x -> y` bezier calculation:
b = new cubic.bezier([....])
[0 to 1 by 0.1].map (x) -> y = b.y(bezier.t(x))
## Compatibility
cubic use `Math.cbrt` which is not available in IE < 11. You can simply use a polyfill or following code:
```
Math.cbrt = -> Math.pow(it, 1/3)
```
## LICENSE
MIT