-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.js
72 lines (49 loc) · 1.48 KB
/
Scene.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(function () {
'use strict';
window.NRD = window.NRD || {};
function Scene (canvas) {
this.canvas = canvas;
this.context = canvas.getContext('2d');
this.canvasObjects = [];
this.requestId = -1;
this.play = this.play.bind(this);
this.init();
}
Scene.prototype.init = function () {
this.updateAspectRatio();
return this;
};
Scene.prototype.play = function () {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.update();
this.render();
this.requestId = window.requestAnimationFrame(this.play);
return this;
};
Scene.prototype.add = function (canvasObject) {
this.canvasObjects.push(canvasObject);
return this;
};
Scene.prototype.updateAspectRatio = function () {
this.canvas.width = this.canvas.scrollWidth;
this.canvas.height = this.canvas.scrollHeight;
return this;
};
Scene.prototype.update = function () {
var i = 0;
var canvasObject;
while ((canvasObject = this.canvasObjects[i++]) !== undefined) {
canvasObject.update(this);
}
return this;
};
Scene.prototype.render = function () {
var i = 0;
var canvasObject;
while ((canvasObject = this.canvasObjects[i++]) !== undefined) {
canvasObject.render(this);
}
return this;
};
NRD['Scene'] = Scene;
}());