256
Fl
FlyingFunctions / files
FlyingFunctions / lib / flying-functions.js
7.3 KB
1/*jslint browser:true */ 2(function () { 3 'use strict'; 4 5 // remember: This is a study, hand build everything, no dependencies 6 // use all the latest and greatest that you can out of JS 7 // Good luck! :-) 8 // Optimistic Sean. 9 10 // expected: 11 // expose 2d api plus some helpers 12 // event based, allow for plugins to listen to events, like frame drawn 13 // 14 // three.js friendlyness? 15 // 16 // You should really split this into seperate files and use make to concat 17 18 var FlyingFunctions, 19 Events, 20 21 slice = Array.prototype.slice; 22 23 // mixer: [s]ource into [d]estination 24 // careful, it will overwrite without shame. 25 function mix(s, d) { 26 var keys = Object.keys(s), 27 key; 28 while (keys.length) { 29 key = keys.shift(); 30 d[key] = s[key]; 31 } 32 33 return d; 34 } 35 36 // Opera engineer Erik Möller wrote about rAF and developed a polyfill that 37 // better handles browsers without native support. You can read about it, but 38 // basically his code will choose a delay of between 4ms and 16ms in order to 39 // more closely match 60fps. Here it is, in case you’d like to use it. Note it 40 // uses the standard method name. I have also fixed the cancel* method’s name, 41 // as it has changed in WebKit. 42 // 43 // http://paulirish.com/2011/requestanimationframe-for-smart-animating/ 44 (function() { 45 var lastTime = 0, 46 vendors = ['webkit', 'moz'], 47 x; 48 49 for(x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { 50 window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; 51 window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || 52 window[vendors[x]+'CancelRequestAnimationFrame']; 53 } 54 55 if (!window.requestAnimationFrame) { 56 window.requestAnimationFrame = function(callback, element) { 57 var currTime = new Date().getTime(), 58 timeToCall = Math.max(0, 16 - (currTime - lastTime)), 59 id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); 60 lastTime = currTime + timeToCall; 61 return id; 62 }; 63 } 64 65 if (!window.cancelAnimationFrame) { 66 window.cancelAnimationFrame = function(id) { 67 clearTimeout(id); 68 }; 69 } 70 }()); 71 72 // https://github.com/jeromeetienne/microevent.js 73 // simple events 74 Events = function (){ 75 // keep this empty so when mixed the class we are mixing in doesn't have to 76 // worry about calling this contructor. 77 }; 78 79 Events.prototype = { 80 // attach the event 81 on: function (ev, fn) { 82 this.events = this.events || {}; 83 this.events[ev] = this.events[ev] || []; 84 this.events[ev].push(fn); 85 // chainable 86 return this; 87 }, 88 89 off: function (ev, fn) { 90 this.events = this.events || {}; 91 if (this.events[ev] === undefined) { return this; } 92 this.events[ev].splice(this.events[ev].indexOf(fn), 1); 93 return this; 94 }, 95 96 once: function (ev, fn) { 97 var self = this; 98 this.on(ev, function () { 99 fn.apply(self, arguments); 100 self.off(ev, fn); 101 }); 102 return this; 103 }, 104 105 // trigger an event and pass arguments to the handlers 106 trigger: function (ev) { 107 this.events = this.events || {}; 108 if (this.events && this.events[ev] === undefined) { return this; } 109 var handlers = this.events[ev], 110 len = handlers.length; 111 while (len) { 112 len -= 1; 113 handlers[len].apply(this, slice.call(arguments, 1)); 114 } 115 return this; 116 }, 117 }; 118 119 // Flying functions engine 120 // ----------------------- 121 // 122 // Usage: `new FlyingFunctions({ option: value, ... });` 123 // 124 // options: 125 // 126 // * fps = an integer between 1 and 60 127 // 128 // * canvas = the convas html element 129 // 130 // * c = the drawing context 131 // 132 // * container = the container to which append the canvas 133 // 134 // Events 135 // ------ 136 // 137 // frame: a new animation frame is ready 138 // 139 // last-frame: stop() was called, this is the last frame before the loop pauses. 140 // 141 // stop: stop() was called 142 // 143 // draw: a single frame is ready to be drawn 144 // 145 // clear: clear() was called, this event is handled after the canvas has been cleared. 146 147 FlyingFunctions = function (opts) { 148 opts = opts || {}; 149 150 // defaults, mark them on the opts obj that gets passed to the 151 // `init` event. 152 this.fps = opts.fps || 60; 153 this.container = opts.container || document.querySelector('body'); 154 155 // initialize the main canvas 156 this.canvas = opts.canvas; 157 if (!this.canvas) { 158 this.canvas = document.createElement('canvas'); 159 this.c = this.canvas.getContext('2d'); 160 } 161 162 this.c = opts.c || this.canvas.getContext('2d'); 163 164 // expose the canvas 165 this.container.appendChild(this.canvas); 166 this.canvas.width = opts.width || this.container.innerWidth; 167 this.canvas.height = opts.height || this.container.innerHeight; 168 169 // call any init function that might have been passed 170 if (opts.init) { 171 opts.init.call(this, opts); 172 } 173 174 // loop needs to be bound. 175 this.loop = this.loop.bind(this); 176 177 return this; 178 }; 179 180 FlyingFunctions.prototype = mix(Events.prototype, { 181 Events: Events, 182 183 // force a draw event, for screens outside the game loop 184 // schedule an animation frame for it 185 draw: function () { 186 var self = this; 187 window.requestAnimationFrame(function () { 188 self.trigger('draw'); 189 }); 190 }, 191 192 // clear, erase the canvas by *touching* the width 193 clear: function () { 194 var w = this.canvas.width; 195 this.canvas.width = w; 196 this.trigger('clear'); 197 }, 198 199 // the actual game loop, simple and to the point. 200 // just one trigger for any bound events. 201 // What other events should go here? 202 // 203 // http://creativejs.com/resources/requestanimationframe/ 204 loop: function () { 205 var self = this; 206 207 setTimeout(function () { 208 if (!self.stop_called) { 209 window.requestAnimationFrame(self.loop); 210 self.trigger('frame'); 211 } else { 212 self.trigger('last-frame'); 213 self.running = false; 214 delete self.stop_called; 215 } 216 217 }, 1000 / this.fps); 218 219 return this; 220 }, 221 222 start: function () { 223 this.running = true; 224 this.loop(); 225 this.trigger('start'); 226 }, 227 228 // set the flag for the loop to stop. 229 stop: function () { 230 this.stop_called = true; 231 this.trigger('stop'); 232 } 233 }); 234 235 window.FlyingFunctions = FlyingFunctions; 236}());