Skip to content

Commit

Permalink
1. Initial tween library offering
Browse files Browse the repository at this point in the history
2. Simple tests for tweening
3. build.json changes for tween lib
4. Abacus core file
  • Loading branch information
secretrobotron committed Oct 16, 2011
1 parent 811872d commit 8eef005
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 8 deletions.
6 changes: 4 additions & 2 deletions build.json
Expand Up @@ -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,
Expand All @@ -18,4 +20,4 @@
}
}
}
}
}
15 changes: 15 additions & 0 deletions 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 );
8 changes: 2 additions & 6 deletions src/abacus.timer.js
Expand Up @@ -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() {

Expand Down Expand Up @@ -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 );
59 changes: 59 additions & 0 deletions 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 );
3 changes: 3 additions & 0 deletions test/index.html
Expand Up @@ -8,8 +8,11 @@
<link rel="Stylesheet" media="screen" href="qunit/qunit/qunit.css" />

<!-- Abacus -->
<script src="../src/abacus.js"></script>
<script src="../src/abacus.tween.js"></script>
<script src="../src/abacus.timer.js"></script>
<!-- tests -->
<script src="unit/abacus.tween.js"></script>
<script src="unit/abacus.timer.js"></script>
</head>
<body>
Expand Down
23 changes: 23 additions & 0 deletions 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' );
});


0 comments on commit 8eef005

Please sign in to comment.