|
0 |
<!DOCTYPE html>
|
|
1 |
<head>
|
|
2 |
<meta charset="utf-8">
|
|
3 |
<title>yoob.setUpQuantumAnimationFrame Demo</title>
|
|
4 |
<style>
|
|
5 |
#canvas { border: 1px solid blue; }
|
|
6 |
</style>
|
|
7 |
</head>
|
|
8 |
<body>
|
|
9 |
|
|
10 |
<h1>yoob.setUpQuantumAnimationFrame Demo</h1>
|
|
11 |
|
|
12 |
<div>
|
|
13 |
<canvas id="canvas" width="200" height="200">
|
|
14 |
Your browser doesn't support displaying an HTML5 canvas.
|
|
15 |
</canvas>
|
|
16 |
</div>
|
|
17 |
|
|
18 |
<div>
|
|
19 |
<button id="start" onclick="controller.start();">Start</button>
|
|
20 |
<button id="stop" onclick="controller.stop();">Stop</button>
|
|
21 |
</div>
|
|
22 |
|
|
23 |
</body>
|
|
24 |
<script src="../src/yoob/animation-frame.js"></script>
|
|
25 |
<script type="text/javascript">
|
|
26 |
var c = document.getElementById('canvas');
|
|
27 |
|
|
28 |
var Controller = function() {
|
|
29 |
var ctx;
|
|
30 |
var animCfg = {};
|
|
31 |
|
|
32 |
this.init = function(canvas) {
|
|
33 |
this.canvas = canvas;
|
|
34 |
ctx = canvas.getContext("2d");
|
|
35 |
this.w = 20;
|
|
36 |
this.h = 20;
|
|
37 |
this.x = 0;
|
|
38 |
this.y = canvas.height / 2 - this.h / 2;
|
|
39 |
this.dx = 1;
|
|
40 |
this.draw();
|
|
41 |
};
|
|
42 |
|
|
43 |
this.draw = function() {
|
|
44 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
45 |
ctx.lineWidth = "1";
|
|
46 |
ctx.strokeStyle = "black";
|
|
47 |
ctx.shadowColor = "transparent";
|
|
48 |
ctx.strokeRect(this.x, this.y, this.w, this.h);
|
|
49 |
};
|
|
50 |
|
|
51 |
this.update = function() {
|
|
52 |
if (this.x + this.w > this.canvas.width || this.x < 0) {
|
|
53 |
this.dx *= -1;
|
|
54 |
}
|
|
55 |
this.x += this.dx;
|
|
56 |
};
|
|
57 |
|
|
58 |
this.start = function() {
|
|
59 |
if (animCfg.request)
|
|
60 |
return;
|
|
61 |
yoob.setUpQuantumAnimationFrame(this, animCfg);
|
|
62 |
};
|
|
63 |
|
|
64 |
this.stop = function() {
|
|
65 |
if (!animCfg.request)
|
|
66 |
return;
|
|
67 |
cancelRequestAnimationFrame(animCfg.request);
|
|
68 |
animCfg.request = undefined;
|
|
69 |
// if you don't do the following, you will get all those ticks
|
|
70 |
// that have "occurred" since the stop, when you start again
|
|
71 |
animCfg.lastTime = null;
|
|
72 |
};
|
|
73 |
};
|
|
74 |
|
|
75 |
var controller = new Controller();
|
|
76 |
controller.init(document.getElementById('canvas'));
|
|
77 |
</script>
|