/** @license Animator.js 1.1.9 This library is released under the BSD license: Copyright (c) 2006, Bernard Sumption. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name BernieCode nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // http://www.berniecode.com/writing/animator.html // Applies a sequence of numbers between 0 and 1 to a number of subjects // construct - see setOptions for parameters function Animator(options) { this.setOptions(options); var _this = this; this.timerDelegate = function(){_this.onTimerEvent()}; this.subjects = []; this.subjectScopes = []; this.target = 0; this.state = 0; this.lastTime = null; }; Animator.prototype = { // apply defaults setOptions: function(options) { this.options = Animator.applyDefaults({ interval: 20, // time between animation frames duration: 400, // length of animation onComplete: function(){}, onStep: function(){}, transition: Animator.tx.easeInOut }, options); }, // animate from the current state to provided value seekTo: function(to) { this.seekFromTo(this.state, to); }, // animate from the current state to provided value seekFromTo: function(from, to) { this.target = Math.max(0, Math.min(1, to)); this.state = Math.max(0, Math.min(1, from)); this.lastTime = new Date().getTime(); if (!this.intervalId) { this.intervalId = window.setInterval(this.timerDelegate, this.options.interval); } }, // animate from the current state to provided value jumpTo: function(to) { this.target = this.state = Math.max(0, Math.min(1, to)); this.propagate(); }, // seek to the opposite of the current target toggle: function() { this.seekTo(1 - this.target); }, // add a function or an object with a method setState(state) that will be called with a number // between 0 and 1 on each frame of the animation addSubject: function(subject,scope) { this.subjects[this.subjects.length] = subject; this.subjectScopes[this.subjectScopes.length] = scope; return this; }, // remove all subjects clearSubjects: function() { this.subjects = []; this.subjectScopes = []; }, // forward the current state to the animation subjects propagate: function() { var value = this.options.transition(this.state); for (var i=0; i= Math.abs(this.state - this.target)) { this.state = this.target; } else { this.state += movement; } try { this.propagate(); } finally { this.options.onStep.call(this); if (this.target == this.state) { window.clearInterval(this.intervalId); this.intervalId = null; this.options.onComplete.call(this); } } }, // shortcuts play: function() {this.seekFromTo(0, 1)}, reverse: function() {this.seekFromTo(1, 0)}, // return a string describing this Animator, for debugging inspect: function() { var str = "# 20) return; } }, getStyle: function(state) { state = this.from + ((this.to - this.from) * state); if (this.property == 'filter') return "alpha(opacity=" + Math.round(state*100) + ")"; if (this.property == 'opacity') return state; return Math.round(state) + this.units; }, inspect: function() { return "\t" + this.property + "(" + this.from + this.units + " to " + this.to + this.units + ")\n"; } } // animates a colour based style property between two hex values function ColorStyleSubject(els, property, from, to) { this.els = Animator.makeArray(els); this.property = Animator.camelize(property); this.to = this.expandColor(to); this.from = this.expandColor(from); this.origFrom = from; this.origTo = to; } ColorStyleSubject.prototype = { // parse "#FFFF00" to [256, 256, 0] expandColor: function(color) { var hexColor, red, green, blue; hexColor = ColorStyleSubject.parseColor(color); if (hexColor) { red = parseInt(hexColor.slice(1, 3), 16); green = parseInt(hexColor.slice(3, 5), 16); blue = parseInt(hexColor.slice(5, 7), 16); return [red,green,blue] } if (window.DEBUG) { alert("Invalid colour: '" + color + "'"); } }, getValueForState: function(color, state) { return Math.round(this.from[color] + ((this.to[color] - this.from[color]) * state)); }, setState: function(state) { var color = '#' + ColorStyleSubject.toColorPart(this.getValueForState(0, state)) + ColorStyleSubject.toColorPart(this.getValueForState(1, state)) + ColorStyleSubject.toColorPart(this.getValueForState(2, state)); for (var i=0; i 255) number = 255; var digits = number.toString(16); if (number < 16) return '0' + digits; return digits; } ColorStyleSubject.parseColor.rgbRe = /^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i; ColorStyleSubject.parseColor.hexRe = /^\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/; // Animates discrete styles, i.e. ones that do not scale but have discrete values // that can't be interpolated function DiscreteStyleSubject(els, property, from, to, threshold) { this.els = Animator.makeArray(els); this.property = Animator.camelize(property); this.from = from; this.to = to; this.threshold = threshold || 0.5; } DiscreteStyleSubject.prototype = { setState: function(state) { var j=0; for (var i=0; i section ? 1 : 0); } if (this.options.rememberance) { document.location.hash = this.rememberanceTexts[section]; } } }