xonecas.io
256
Fl

FlyingFunctions / commit 597da28

tweaks leaned from trying to implement some sort of gravity. failed miserably

xonecas2013-03-28 597da28e17f0093aa122b3be39df897963e5838d parent 8caed0b

2 files changed

lib/flying-functions.js+57−16
@@ -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
test/gravity/index.html+82−0
@@ -0,0 +1,82 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+ <title>Flying Functions Gravity test</title>
5+</head>
6+<body>
7+
8+ <script src="../../lib/flying-functions.js"></script>
9+ <script>
10+ var ff = new FlyingFunctions({
11+ width: 460,
12+ height: 320
13+ }),
14+ g = 0.1;
15+
16+ var Ball = function (o) {
17+ this.x = o.x;
18+ this.y = o.y;
19+ this.size = o.size;
20+ this.dir = o.dir;
21+ this.speed = o.speed;
22+ };
23+
24+ Ball.prototype.update = function () {
25+ var max = ff.canvas.height,
26+ min = 0;
27+
28+ // bounce off the bottom, aka y >= 320
29+ if (this.y >= 320) { //ff.canvas.height
30+ this.y = 320;
31+ this.dir = -1;
32+ }
33+
34+ // bounce off the top? or should it never reach?
35+
36+ this.speed = this.dir === 1 ? this.speed + g : this.speed - g;
37+ this.y += this.speed;
38+ console.log(this.speed, this.y, this.dir);
39+
40+ };
41+
42+ Ball.prototype.draw = function () {
43+ var c = ff.c;
44+ c.save();
45+ c.fillStyle = "hsl(190, 100%, 50%)";
46+ c.beginPath();
47+ c.arc(this.x, this.y, this.size, 0, Math.PI * 2, true);
48+ c.fill();
49+ c.restore();
50+ }
51+
52+ var ball = new Ball({
53+ x: 20,
54+ y: 20,
55+ size: 10,
56+ dir: 1,
57+ speed: 1.1
58+ });
59+
60+ ff.on('draw', function () {
61+ ball.draw();
62+ });
63+
64+ ff.on('frame', function () {
65+ ball.update();
66+ this.clear();
67+ ball.draw();
68+ });
69+
70+ ff.canvas.addEventListener('click', function (ev) {
71+ if (ff.running) {
72+ ff.stop();
73+ } else {
74+ ff.start();
75+ }
76+ }, false);
77+
78+ ff.draw();
79+ </script>
80+
81+</body>
82+</html>