Skip to content

Instantly share code, notes, and snippets.

@xrd
Forked from anonymous/animate.js
Last active August 29, 2015 14:00
Show Gist options
  • Save xrd/3884fe2e2b53e1859850 to your computer and use it in GitHub Desktop.
Save xrd/3884fe2e2b53e1859850 to your computer and use it in GitHub Desktop.
mod = angular.module 'AnimateAlgorithms', []
mod.service 'Algorithm', [ '$timeout', ($timeout) ->
reset = () ->
world.states = []
world.the_state = {}
save = (arr) ->
# console.log "Array: %o", arr
world.states.push {
marking: arr?.marking,
primes: angular.copy( arr?.primes ),
looking : arr?.looking,
found: arr?.found,
p : arr?.p,
numbers: angular.copy( arr?.numbers )
}
animate = (stepInt) ->
$timeout ( () ->
console.log "Starting animation..."
reallyAnimate( stepInt ) ), 2000
printCount = 0
addArrayItemToWorldState = (x,i,obj) ->
if obj instanceof Object
console.log "Found hash obj"
for k in Object.keys( obj )
if printCount > 1000 && printCount < 1030
console.log "Old/New", world.the_state[x][i][k], obj[k]
world.the_state[x][i] = {} unless world.the_state[x][i]
world.the_state[x][i][k] = obj[k]
else
world.the_state[x][i] = obj
printCount += 1
reallyAnimate = (stepInt) ->
if world.states.length > 1
new_state = world.states.shift()
for x of new_state
type = Object.prototype.toString.call( new_state[x] )
# Update existing objects rather than resetting to get animations
if type == "[object Array]"
# iterate over all of them
world.the_state[x] = [] unless world.the_state[x]
addArrayItemToWorldState( x, i, new_state[x][i] ) for i of new_state[x]
else
world.the_state[x] = new_state[x]
$timeout( ( () ->
reallyAnimate( stepInt ) ), stepInt )
world = {
states: []
the_state: {}
animate: animate
save: save
reset: reset
}
world
]
(function() {
var mod;
mod = angular.module('AnimateAlgorithms', []);
mod.service('Algorithm', [
'$timeout', function($timeout) {
var addArrayItemToWorldState, animate, printCount, reallyAnimate, reset, save, world;
reset = function() {
world.states = [];
return world.the_state = {};
};
save = function(arr) {
return world.states.push({
marking: arr != null ? arr.marking : void 0,
primes: angular.copy(arr != null ? arr.primes : void 0),
looking: arr != null ? arr.looking : void 0,
found: arr != null ? arr.found : void 0,
p: arr != null ? arr.p : void 0,
numbers: angular.copy(arr != null ? arr.numbers : void 0)
});
};
animate = function(stepInt) {
return $timeout((function() {
console.log("Starting animation...");
return reallyAnimate(stepInt);
}), 2000);
};
printCount = 0;
addArrayItemToWorldState = function(x, i, obj) {
var k, _i, _len, _ref;
if (obj instanceof Object) {
console.log("Found hash obj");
_ref = Object.keys(obj);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
k = _ref[_i];
if (printCount > 1000 && printCount < 1030) {
console.log("Old/New", world.the_state[x][i][k], obj[k]);
}
if (!world.the_state[x][i]) {
world.the_state[x][i] = {};
}
world.the_state[x][i][k] = obj[k];
}
} else {
world.the_state[x][i] = obj;
}
return printCount += 1;
};
reallyAnimate = function(stepInt) {
var i, new_state, type, x;
if (world.states.length > 1) {
new_state = world.states.shift();
for (x in new_state) {
type = Object.prototype.toString.call(new_state[x]);
if (type === "[object Array]") {
if (!world.the_state[x]) {
world.the_state[x] = [];
}
for (i in new_state[x]) {
addArrayItemToWorldState(x, i, new_state[x][i]);
}
} else {
world.the_state[x] = new_state[x];
}
}
return $timeout((function() {
return reallyAnimate(stepInt);
}), stepInt);
}
};
world = {
states: [],
the_state: {},
animate: animate,
save: save,
reset: reset
};
return world;
}
]);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment