Permalink
Browse files

animations using deferred

  • Loading branch information...
1 parent 6cc0b3c commit 632ec530066891ee1546dc89bcfad7cfc270acc5 @OdeToCode committed Feb 14, 2012
Showing with 90 additions and 78 deletions.
  1. +24 −28 scripts/p5.js
  2. +35 −27 scripts/p5animations.js
  3. +31 −23 tests/tests.js
View
52 scripts/p5.js
@@ -92,55 +92,43 @@
var startAnimations = function (slide) {
$("[data-animation]", slide).add(slide).each(function () {
- var animation = extractAnimation($(this));
- if (animation) {
- animation.initialize($(this));
- currentAnimations.push(animation);
- }
+ addAnimationFor($(this));
});
};
+ var addAnimationFor = function (element) {
+ var animation = extractAnimation(element);
+ if (animation) {
+ currentAnimations.push(animation);
+ $.when(animation.done)
+ .then(function () { currentAnimations.remove(animation); });
+ }
+ };
+
var extractAnimation = function (element) {
var name = element.data("animation");
if (name) {
var factory = registeredAnimations[name];
if (factory) {
- return factory();
+ return factory(element);
}
}
return null;
};
var stepAnimation = function () {
- var stepped = false;
if (currentAnimations.length > 0) {
- stepped = runStep();
- }
- return stepped;
- };
-
- var runStep = function () {
- var animation = currentAnimations[0];
- if (animation) {
- var more = animation.step();
- if (!more) {
- if (animation.cancel) {
- animation.cancel();
- }
- currentAnimations.pop();
+ var animation = currentAnimations[0];
+ if (animation) {
+ animation.step();
}
- return more;
+ return true;
}
return false;
};
var killAnimations = function () {
- $(currentAnimations).each(function () {
- if (this.cancel) {
- this.cancel();
- }
- });
- currentAnimations = [];
+ $(window).trigger("killAnimations");
};
var slides = function () {
@@ -184,6 +172,14 @@
} ();
+(function () {
+
+ Array.prototype.remove = function(object) {
+ this.splice(this.indexOf(object),1);
+ };
+
+})();
+
$(function () {
p5.start();
});
View
62 scripts/p5animations.js
@@ -1,62 +1,70 @@
-var onebyone = function () {
+var completionSignal = function () {
- var target = null;
+ var deferred = $.Deferred();
- var initialize = function (element) {
- target = element;
- target.children().addClass("onebyone");
+ var complete = function () {
+ $(window).unbind("killAnimations", complete);
+ deferred.resolve();
};
+ $(window).bind("killAnimations", complete);
+
+ return {
+ complete: complete,
+ promise: deferred.promise()
+ };
+};
+
+var onebyone = function (element) {
+
+ var target = element;
+ var signal = completionSignal();
+
var step = function () {
var hidden = target.children(".onebyone");
hidden.first().removeClass("onebyone");
- return hidden.length > 0;
+ if (hidden.length <= 1) {
+ signal.complete();
+ }
};
- var cancel = function() {
-
- };
+ target.children().addClass("onebyone");
return {
- initialize: initialize,
step: step,
- cancel: cancel
+ done: signal.promise
};
-
};
-var timedAppear = function () {
+var timedAppear = function (element) {
- var target = null;
+ var target = element;
var timeoutHandle = null;
-
- var initialize = function (element) {
- target = element;
- target.hide();
-
- var delay = target.data("animation-delay");
- timeoutHandle = setTimeout(fadeIn, delay);
- };
+ var signal = completionSignal();
var step = function () {
- return timeoutHandle != null;
+
};
- var cancel = function () {
+ var cleanup = function () {
if (timeoutHandle) {
clearTimeout(timeoutHandle);
}
};
var fadeIn = function () {
target.fadeIn();
- timeoutHandle = null;
+ signal.complete();
};
+ var delay = target.data("animation-delay");
+ target.hide();
+ timeoutHandle = setTimeout(fadeIn, delay);
+ signal.promise.always(cleanup);
+
return {
- initialize: initialize,
step: step,
- cancel: cancel
+ done: signal.promise
};
};
View
54 tests/tests.js
@@ -69,11 +69,6 @@ var animTestEnvironment = {
module("Animation", animTestEnvironment);
-test("Initializes animation", function () {
- $(window).trigger(rightArrow());
- ok(lastAnimation.getState().isInitialized);
-});
-
test("Steps animation with right arrow", function () {
$(window).trigger(rightArrow());
$(window).trigger(rightArrow());
@@ -87,12 +82,12 @@ test("Cancels animation on left arrow", function () {
ok(lastAnimation.getState().isCancelled);
});
-test("Cancels when stepping stops", function () {
+test("Resolved when stepping stops", function () {
$(window).trigger(rightArrow());
$(window).trigger(rightArrow());
$(window).trigger(rightArrow());
$(window).trigger(rightArrow());
- ok(lastAnimation.getState().isCancelled);
+ ok(lastAnimation.getState().isResolved);
});
test("Moves to next slide when animation complete", function () {
@@ -104,6 +99,18 @@ test("Moves to next slide when animation complete", function () {
ok($("#slide3").hasClass("current"));
});
+module("array remove test");
+
+test("Can remove object reference from Array", function () {
+ var o1 = {};
+ var o2 = {};
+ var o3 = {};
+ var array = [o1, o2, o3];
+
+ array.remove(o2);
+ ok(array.length == 2);
+});
+
var rightArrow = function () {
var event = new $.Event("keydown");
event.keyCode = 39;
@@ -142,34 +149,35 @@ var lastAnimation = null;
var testAnimation = function () {
- var isInitialized = false;
var stepCount = 0;
var isCancelled = false;
+ var deferred = $.Deferred();
- lastAnimation = {
+ var complete = function () {
+ $(window).unbind("killAnimations", complete);
+ isCancelled = true;
+ deferred.resolve();
+ };
- initialize: function () {
- isInitialized = true;
- },
+ $(window).bind("killAnimations", complete);
+ lastAnimation = {
+ done: deferred.promise(),
step: function () {
stepCount++;
- return stepCount < 3;
- },
-
- cancel: function () {
- isCancelled = true;
- },
-
+ if (stepCount >= 3) {
+ deferred.resolve();
+ }
+ },
getState: function () {
- return {
- isInitialized: isInitialized,
+ return {
stepCount: stepCount,
- isCancelled: isCancelled
+ isCancelled: isCancelled,
+ isResolved: deferred.isResolved()
};
}
};
-
+
return lastAnimation;
};

0 comments on commit 632ec53

Please sign in to comment.