Created
March 2, 2011 07:56
-
-
Save micha149/850628 to your computer and use it in GitHub Desktop.
Get diagonal animation steps for a grid (qunit tested)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module("Diagonale"); | |
var getStep = function(index, cols) { | |
if (index <= cols) { | |
return index; | |
} | |
return index % cols === 0 | |
? index / cols + (cols - 1) | |
: Math.floor(index / cols) + index % cols; | |
}; | |
/** | |
* Tests results on a 3 column grid | |
* | |
* 1 2 3 | |
* 4 5 6 | |
* 7 8 9 | |
*/ | |
test("3 cols", function() { | |
var cols = 3; | |
equal(getStep(1, cols), 1, "Elemement 1 in step"); | |
equal(getStep(2, cols), 2, "Elemement 2 in step"); | |
equal(getStep(3, cols), 3, "Elemement 3 in step"); | |
equal(getStep(4, cols), 2, "Elemement 4 in step"); | |
equal(getStep(5, cols), 3, "Elemement 5 in step"); | |
equal(getStep(6, cols), 4, "Elemement 6 in step"); | |
equal(getStep(7, cols), 3, "Elemement 7 in step"); | |
equal(getStep(8, cols), 4, "Elemement 8 in step"); | |
equal(getStep(9, cols), 5, "Elemement 9 in step"); | |
}); | |
/** | |
* Tests results on a 4 column grid | |
* | |
* 1 2 3 4 | |
* 5 6 7 8 | |
* 9 10 11 12 | |
*/ | |
test("4 cols", function() { | |
cols = 4; | |
equal(getStep(4, cols), 4, "Elemement 4 in step"); | |
equal(getStep(5, cols), 2, "Elemement 5 in step"); | |
equal(getStep(7, cols), 4, "Elemement 7 in step"); | |
equal(getStep(8, cols), 5, "Elemement 8 in step"); | |
}); | |
/** | |
* Tests results on a 1 column grid | |
* | |
* 1 | |
* 2 | |
* 3 | |
*/ | |
test("1 col", function() { | |
cols = 1; | |
equal(getStep(1, cols), 1, "Elemement 1 in step"); | |
equal(getStep(2, cols), 2, "Elemement 2 in step"); | |
equal(getStep(3, cols), 3, "Elemement 3 in step"); | |
}); | |
/** | |
* Creating an array with animation sequence | |
*/ | |
test("Create a sequence", function() { | |
var total = 9, | |
columns = 3, | |
sequence = []; | |
for (var i = 1; i <= total; i++) { | |
var stepNum = getStep(i, columns) - 1, | |
step = sequence[stepNum] = sequence[stepNum] ? sequence[stepNum] : []; | |
step.push(i); | |
} | |
same(sequence[0], [1], "Step 1"); | |
same(sequence[1], [2,4], "Step 2"); | |
same(sequence[2], [3,5,7], "Step 3"); | |
same(sequence[3], [6,8], "Step 4"); | |
same(sequence[4], [9], "Step 5"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment