@@ -8,23 +8,17 @@
88 // Optimistic Sean.
99
1010 // expected:
11- // crud for the canvas
1211 // expose 2d api plus some helpers
1312 // event based, allow for plugins to listen to events, like frame drawn
14- // plugin class
15- // an extendable prototype
1613 //
1714 // three.js friendlyness?
1815 //
19- // How do I write tests?
20- // for now maybe just a test class...
16+ // You should really split this into seperate files and use make to concat
2117
2218 var FlyingFunctions,
2319 Events,
24- Plugin,
2520
26- slice = Array.prototype.slice,
27- animFrame;
21+ slice = Array.prototype.slice;
2822
2923 // mixer: [s]ource into [d]estination
3024 // careful, it will overwrite without shame.
@@ -96,7 +90,6 @@
9690 this.events = this.events || {};
9791 if (this.events[ev] === undefined) { return this; }
9892 this.events[ev].splice(this.events[ev].indexOf(fn), 1);
99- // chainable
10093 return this;
10194 },
10295
@@ -106,7 +99,6 @@
10699 fn.apply(self, arguments);
107100 self.off(ev, fn);
108101 });
109- // chainable
110102 return this;
111103 },
112104
@@ -120,7 +112,6 @@
120112 len -= 1;
121113 handlers[len].apply(this, slice.call(arguments, 1));
122114 }
123- // chainable
124115 return this;
125116 },
126117 };
@@ -139,6 +130,19 @@
139130 // * c = the drawing context
140131 //
141132 // * 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.
142146
143147 FlyingFunctions = function (opts) {
144148 opts = opts || {};
@@ -174,20 +178,57 @@
174178 };
175179
176180 FlyingFunctions.prototype = mix(Events.prototype, {
177- Plugin: Plugin,
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+ },
178198
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/
179204 loop: function () {
180205 var self = this;
181206
182207 setTimeout(function () {
183- window.requestAnimationFrame(self.loop);
184-
185- // call out event
186- self.trigger('frame');
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+ }
187216
188217 }, 1000 / this.fps);
189218
190219 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');
191232 }
192233 });
193234