Permalink
Browse files

Initial commit with the Cubic Bézier solver.

  • Loading branch information...
0 parents commit 0d5469861b13e362958fd1ac0a9f6cc65e8db3e2 @arian committed Feb 28, 2012
Showing with 79 additions and 0 deletions.
  1. +3 −0 .travis.yml
  2. +40 −0 index.js
  3. +16 −0 package.json
  4. +20 −0 test/test.js
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.6
@@ -0,0 +1,40 @@
+
+module.exports = function(x1, y1, x2, y2, n){
+
+ var xs = [0];
+ var ys = [0];
+
+ var x = 0;
+
+ for (var i = 1; i < (n - 1); i++){
+ var u = 1 / n * i,
+ a = Math.pow(1 - u, 2) * 3 * u,
+ b = Math.pow(u, 2) * 3 * (1 - u),
+ c = Math.pow(u, 3);
+ var _x = x1 * a + x2 * b + c;
+ var _y = y1 * a + y2 * b + c;
+ if ((_x - x) > (10 / n)){
+ x = _x;
+ xs.push(_x);
+ ys.push(_y);
+ }
+ }
+
+ xs.push(1);
+ ys.push(1);
+
+ return function(t){
+
+ var left = 0, right = xs.length - 1;
+ while (left <= right){
+ var middle = Math.floor((left + right) / 2);
+ if (xs[middle] == t) break;
+ else if (xs[middle] > t) right = middle - 1;
+ else left = middle + 1;
+ }
+
+ return ys[middle];
+
+ };
+
+};
@@ -0,0 +1,16 @@
+{
+ "author": "Arian Stolwijk",
+ "name": "cubic-bezier",
+ "description": "A small cubic bézier timing function",
+ "version": "0.0.1",
+ "main": "index",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/arian/cubic-bezier.git"
+ },
+ "scripts": {
+ "test": "node test/test.js"
+ },
+ "dependencies": {},
+ "devDependencies": {}
+}
@@ -0,0 +1,20 @@
+
+var bezier = require('../index');
+var assert = require('assert');
+
+var easeIn = bezier(0.42, 0, 1.0, 1.0, 1000);
+var linear = bezier(0, 0, 1, 1, 1000);
+
+assert.equal(0, easeIn(0));
+assert.equal(1, easeIn(1));
+assert.equal(0.2109793, (Math.round(easeIn(0.4) * 1e7) / 1e7));
+assert.equal(0.4371482, (Math.round(easeIn(0.6) * 1e7) / 1e7));
+
+
+assert.equal(0, linear(0));
+assert.equal(1, linear(1));
+assert.equal(0.3898104, (Math.round(linear(0.4) * 1e7) / 1e7));
+assert.equal(0.608722, (Math.round(linear(0.6) * 1e7) / 1e7));
+
+
+console.log('\033[32m✓ Cubic bézier tests passed\033[0m');

0 comments on commit 0d54698

Please sign in to comment.