From 8eef005a40cb39351a6992d9d1b44078574b9567 Mon Sep 17 00:00:00 2001 From: secretrobotron Date: Sun, 16 Oct 2011 13:15:41 -0400 Subject: [PATCH] 1. Initial tween library offering 2. Simple tests for tweening 3. build.json changes for tween lib 4. Abacus core file --- build.json | 6 ++-- src/abacus.js | 15 ++++++++++ src/abacus.timer.js | 8 ++---- src/abacus.tween.js | 59 +++++++++++++++++++++++++++++++++++++++ test/index.html | 3 ++ test/unit/abacus.tween.js | 23 +++++++++++++++ 6 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 src/abacus.js create mode 100644 src/abacus.tween.js create mode 100644 test/unit/abacus.tween.js diff --git a/build.json b/build.json index 560b377..20a6d52 100644 --- a/build.json +++ b/build.json @@ -2,7 +2,9 @@ "files": { "dist/abacus": { "src": [ - "src/abacus.timer.js" + "src/abacus.js", + "src/abacus.timer.js", + "src/abacus.tween.js" ], "prehint": true, "posthint": false, @@ -18,4 +20,4 @@ } } } -} \ No newline at end of file +} diff --git a/src/abacus.js b/src/abacus.js new file mode 100644 index 0000000..cff0473 --- /dev/null +++ b/src/abacus.js @@ -0,0 +1,15 @@ +(function( window ) { + + var Abacus = window.Abacus || {}; + + Abacus.guid = function() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); + return v.toString(16); + }).toUpperCase(); + }; + + // Re-expose Abacus object + window.Abacus = Abacus; + +})( this ); diff --git a/src/abacus.timer.js b/src/abacus.timer.js index 21a6df5..704ae5f 100644 --- a/src/abacus.timer.js +++ b/src/abacus.timer.js @@ -12,9 +12,8 @@ }; })(); - var Abacus = window.Abacus || {}, // An array of callbacks to call in our rAF - callbackQueue = [], + var callbackQueue = [], // the function we call on each tick of the rAF timerLoop = function() { @@ -125,11 +124,8 @@ }; // Wrap new Timer() construction in Abacus.timer() API - Abacus.timer = function( options ) { + window.Abacus.timer = function( options ) { return new Timer( options ); }; - // Re-expose Abacus object - window.Abacus = Abacus; - })( this ); diff --git a/src/abacus.tween.js b/src/abacus.tween.js new file mode 100644 index 0000000..1d61462 --- /dev/null +++ b/src/abacus.tween.js @@ -0,0 +1,59 @@ +(function( window ) { + + var types = { + "linear": function( start, stop, index ) { + return ( stop - start ) * index + start; + } + }; + + function Tween( options ) { + + options = options || {}; + + var tweenFunction; + + this.index = options.index || 0; + this.start = options.start || 0; + this.stop = options.stop || 0; + + function chooseTweenFunction( tween ) { + if ( tween ) { + var tweenParamType = typeof( tween ); + if ( tweenParamType === "string" ) { + tweenFunction = types[ tween ] || function() {}; + } + else if ( tweenParamType === "function" ) { + tweenFunction = tween; + } + else { + tweenFunction = function(){}; + } + } + else { + tweenFunction = function(){}; + } + } //chooseTweenFunction + + this.get = function( preferredIndex ) { + this.index = preferredIndex || this.index; + return tweenFunction( this.start, this.stop, this.index ); + }; //step + + Object.defineProperty( this, "type", { + get: function() { + return tweenFunction; + }, + set: function( val ) { + chooseTweenFunction( val ); + } + }); + + chooseTweenFunction( options.type ); + + } //Tween + + window.Abacus.tween = function( options ) { + return new Tween( options ); + }; + +})( this ); diff --git a/test/index.html b/test/index.html index 6cf82a4..25b1c41 100644 --- a/test/index.html +++ b/test/index.html @@ -8,8 +8,11 @@ + + + diff --git a/test/unit/abacus.tween.js b/test/unit/abacus.tween.js new file mode 100644 index 0000000..81f7369 --- /dev/null +++ b/test/unit/abacus.tween.js @@ -0,0 +1,23 @@ +module('Tween Module'); +test('Test that the Abacus.tween exists', 1, function() { + ok( Abacus.tween, 'Abacus.tween exists' ); +}); + +test('Tween instances have step method', 1, function() { + var tween = Abacus.tween(); + equal( typeof tween.get, 'function', 'the tween instance has a get method' ); +}); + +test('Linear Tween is correct', 3, function() { + var tween = Abacus.tween({ + type: "linear", + start: 6, + stop: 8 + }); + + equal( tween.get( 0 ), 6, 'get(0) is correct' ); + equal( tween.get( 1 ), 8, 'get(1) is correct' ); + equal( tween.get( .5 ), 7, 'get(.5) is correct' ); +}); + +