xonecas.io
256
Fl

FlyingFunctions / files

FlyingFunctions / test / gravity / index.html

1.9 KB
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>