diff --git a/snake b/snake new file mode 100644 index 00000000..38a06a23 --- /dev/null +++ b/snake @@ -0,0 +1,185 @@ +/*================================================ + +Polyfill + +================================================*/ + +(function(){ 'use strict'; + + /*================================================ + + Request Animation Frame + + ================================================*/ + + var lastTime = 0; + var vendors = [ 'webkit', 'moz' ]; + for( var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x ) { + window.requestAnimationFrame = window[ vendors[ x ] + 'RequestAnimationFrame' ]; + window.cancelAnimationFrame = window[ vendors[ x ] + 'CancelAnimationFrame' ] || window[ vendors[ x ] + 'CancelRequestAnimationFrame' ]; + } + + if( !window.requestAnimationFrame ) { + window.requestAnimationFrame = function( callback, element ) { + var currTime = new Date().getTime(); + var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ); + var id = window.setTimeout( + function() { + callback( currTime + timeToCall ); + }, timeToCall ); + lastTime = currTime + timeToCall; + return id; + } + } + + if( !window.cancelAnimationFrame ) { + window.cancelAnimationFrame = function( id ) { + clearTimeout( id ); + } + } + +})(); + +/*================================================ + +DOM Manipulation + +================================================*/ + +(function(){ 'use strict'; + + function hasClass( elem, className ) { + return new RegExp( ' ' + className + ' ' ).test( ' ' + elem.className + ' ' ); + }; + + function addClass( elem, className ) { + if( !hasClass(elem, className ) ) { + elem.className += ' ' + className; + } + }; + + function removeClass( elem, className ) { + var newClass = ' ' + elem.className.replace( /[\t\r\n]/g, ' ' ) + ' '; + if( hasClass( elem, className ) ) { + while( newClass.indexOf(' ' + className + ' ' ) >= 0 ) { + newClass = newClass.replace( ' ' + className + ' ', ' ' ); + } + elem.className = newClass.replace( /^\s+|\s+$/g, '' ); + } + }; + + function toggleClass( elem, className ) { + var newClass = ' ' + elem.className.replace( /[\t\r\n]/g, ' ' ) + ' '; + if( hasClass(elem, className ) ) { + while( newClass.indexOf( ' ' + className + ' ' ) >= 0 ) { + newClass = newClass.replace( ' ' + className + ' ' , ' ' ); + } + elem.className = newClass.replace( /^\s+|\s+$/g, '' ); + } else { + elem.className += ' ' + className; + } + }; + +})(); + +/*================================================ + +Core + +================================================*/ + +g = {}; + +(function(){ 'use strict'; + + /*================================================ + + Math + + ================================================*/ + + g.m = Math; + g.mathProps = 'E LN10 LN2 LOG2E LOG10E PI SQRT1_2 SQRT2 abs acos asin atan ceil cos exp floor log round sin sqrt tan atan2 pow max min'.split( ' ' ); + for ( var i = 0; i < g.mathProps.length; i++ ) { + g[ g.mathProps[ i ] ] = g.m[ g.mathProps[ i ] ]; + } + g.m.TWO_PI = g.m.PI * 2; + + /*================================================ + + Miscellaneous + + ================================================*/ + + g.isset = function( prop ) { + return typeof prop != 'undefined'; + }; + + g.log = function() { + if( g.isset( g.config ) && g.config.debug && window.console ){ + console.log( Array.prototype.slice.call( arguments ) ); + } + }; + +})(); + +/*================================================ + +Group + +================================================*/ + +(function(){ 'use strict'; + + g.Group = function() { + this.collection = []; + this.length = 0; + }; + + g.Group.prototype.add = function( item ) { + this.collection.push( item ); + this.length++; + }; + + g.Group.prototype.remove = function( index ) { + if( index < this.length ) { + this.collection.splice( index, 1 ); + this.length--; + } + }; + + g.Group.prototype.empty = function() { + this.collection.length = 0; + this.length = 0; + }; + + g.Group.prototype.each = function( action, asc ) { + var asc = asc || 0, + i; + if( asc ) { + for( i = 0; i < this.length; i++ ) { + this.collection[ i ][ action ]( i ); + } + } else { + i = this.length; + while( i-- ) { + this.collection[ i ][ action ]( i ); + } + } + }; + +})(); + +/*================================================ + +Utilities + +================================================*/ + +(function(){ 'use strict'; + + g.util = {}; + + /*================================================ + + Random