Skip to content

Commit

Permalink
Refactor DOM-related plugin elements.
Browse files Browse the repository at this point in the history
This drops some confusing, unneeded DOM-specific code for a more generic approach.
  • Loading branch information
swashcap committed Apr 15, 2015
1 parent 69070f7 commit 594261e
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions jquery.canvas-animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
var $window = $(window);

var NAMESPACE = 'ca';

var setupCanvas = function ($canvas, options) {
var defaults = {
/**
Expand Down Expand Up @@ -88,7 +90,7 @@
*/
var context = $canvas.get(0).getContext('2d');

var $scroller, canvasWidth, canvasHeight, animationTriggerPoint,
var canvasWidth, canvasHeight, animationTriggerPoint,
requestAnimationId, now, then, elapsed;

options = $.extend(defaults, options);
Expand Down Expand Up @@ -201,7 +203,7 @@
* @return {undefined}
*/
var maybeAnimate = function () {
var scrollTop = $scroller.scrollTop();
var scrollTop = $window.scrollTop();
var t;

if (
Expand All @@ -211,7 +213,7 @@
) {
then = Date.now();
hasAnimated = true;
$scroller.unbind('scroll.automagically');
$window.unbind('scroll.' + NAMESPACE);

/**
* Delay animation by 500ms on non-mobile viewports.
Expand All @@ -236,31 +238,12 @@
return function init() {
var t;

// Set the `$scroller` depending on the design
if ($window.width() <= 1024) {
$scroller = $('body > .wrapper');
} else {
$scroller = $window;
}

/**
* Set up the dimensions.
*
* These must be on a timer because the page's content is initially
* hidden. A jQuery `fadeIn()` with no time value is used to fade in
* the content.
*
* @{@link /skin/frontend/pro/ccipair/js/ccair.js}
*/
t = setTimeout(function () {
setCanvasDimensions($canvas);
animationTriggerPoint = getAnimationTriggerPoint();

// Retry the initial paint
paintImage(images[0]);
// Set up the dimensions.
setCanvasDimensions($canvas);
animationTriggerPoint = getAnimationTriggerPoint();

clearTimeout(t);
}, 1000);
// Retry the initial paint
paintImage(images[0]);

// Load the actual images
for (var i = 0; i < options.images.length; i++) {
Expand All @@ -279,7 +262,7 @@
paintImage(images[0]);
}
}, 60));
$scroller.bind('scroll.automagically', $.throttle(maybeAnimate, 60));
$window.bind('scroll.' + NAMESPACE, $.throttle(maybeAnimate, 60));
};
};

Expand Down

0 comments on commit 594261e

Please sign in to comment.