-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapplication.js
More file actions
137 lines (114 loc) · 2.94 KB
/
application.js
File metadata and controls
137 lines (114 loc) · 2.94 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2014 Robert Scott Dionne. All rights reserved.
/**
* @fileoverview
* @author robertsdionne@gmail.com (Robert Scott Dionne)
*/
/**
* @param {Window} window The window.
* @param {discoball.Keys} keys
* @param {Object.<string, *>=} opt_options
* @constructor
*/
bouncingball.Application = function(window, keys, opt_options) {
/**
* @type {Window}
* @private
*/
this.window_ = window;
/**
* @type {discoball.Keys}
* @private
*/
this.keys_ = keys;
this.reset();
};
/**
* WebGL context identifier.
* @type {string}
*/
bouncingball.Application.WEBGL_CONTEXT = 'experimental-webgl';
/**
* Checks that the dimensions and, if so, dispatches onChange
* to the Renderer.
* @private
*/
bouncingball.Application.prototype.checkDimensions_ = function() {
var width = this.canvas_.width;
var height = this.canvas_.height;
if (this.width_ !== width ||
this.height_ !== height) {
this.width_ = width;
this.height_ = height;
this.renderer_.onChange(this.gl_, width, height);
}
};
/**
* Associates this Application with the given canvas.
* @param {HTMLCanvasElement} canvas
* @param {bouncingball.Renderer} renderer The renderer.
*/
bouncingball.Application.prototype.install = function(canvas, renderer) {
const devicePixelRatio = this.window_.devicePixelRatio || 1;
this.canvas_ = canvas;
this.canvas_.style = `width:${this.window_.innerWidth}px;height:${this.window_.innerHeight}px;`;
this.canvas_.width = this.window_.innerWidth * devicePixelRatio;
this.canvas_.height = this.window_.innerHeight * devicePixelRatio;
this.gl_ = /** @type {WebGLRenderingContext} */ (
this.canvas_.getContext(bouncingball.Application.WEBGL_CONTEXT));
this.renderer_ = renderer;
};
/**
* Starts the rendering loop.
*/
bouncingball.Application.prototype.start = function() {
this.renderer_.onCreate(this.gl_);
this.onFrame_();
};
/**
* Dispatches onChange and onDraw events to the Renderer.
* @private
*/
bouncingball.Application.prototype.onFrame_ = function() {
this.checkDimensions_();
this.renderer_.onDraw(this.gl_);
this.keys_.update();
this.handle_ = bouncingball.global.requestAnimationFrame(
bouncingball.bind(this.onFrame_, this));
};
/**
* Dissociates this Application with the previously associated canvas
* and stops the rendering loop.
*/
bouncingball.Application.prototype.uninstall = function() {
bouncingball.global.cancelAnimationFrame(this.handle_);
this.renderer_.onDestroy(this.gl_);
this.reset();
};
bouncingball.Application.prototype.reset = function() {
/**
* @type {HTMLCanvasElement}
* @private
*/
this.canvas_ = null;
/**
* @type {WebGLRenderingContext}
* @private
*/
this.gl_ = null;
/**
* @type {bouncingball.Renderer}
* @private
*/
this.renderer_ = null;
this.handle_ = null;
/**
* @type {?number}
* @private
*/
this.width_ = null;
/**
* @type {?number}
* @private
*/
this.height_ = null;
};