forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
media.js
224 lines (192 loc) · 5.76 KB
/
media.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"use strict";
define([], function() {
function AudioCollection( params ) {
var loaded = false;
var collection = {};
var loader = new params.loader( params.context, params.files );
loader.onProgress = function( item, current, total) {
collection[item.id] = item.buffer;
var percent = Math.ceil( current/total *100 );
if( progressCallback ) {
progressCallback( percent );
}
if( percent >= 100 ) {
loaded = true;
if( loadCallback ) {
loadCallback();
}
}
};
loader.load();
var loadCallback;
function onLoaded( callback ) {
loadCallback = callback;
if( loaded ) {
loadCallback();
}
}
var progressCallback;
function onProgress( callback ) {
progressCallback = callback;
}
function isLoaded() {
return loaded;
}
function get( name ) {
return collection[name];
}
return {
get: get,
onLoaded: onLoaded,
isLoaded: isLoaded,
onProgress: onProgress,
};
}
function BitmapCollection( params ) {
var loaded = false;
var collection = {};
var loader = new THREE.LoadingManager();
loader.onProgress = function( item, current, total) {
var percent = Math.ceil( current/total *100 );
if( progressCallback ) {
progressCallback( percent );
}
if( percent >= 100 ) {
loaded = true;
if( loadCallback ) {
loadCallback();
}
}
};
function load() {
var imageLoader = new params.loader( loader );
params.files.forEach( function(entry) {
imageLoader.load( 'assets/'+entry.file, function(obj) {
collection[entry.id] = obj;
});
});
}
load();
var loadCallback;
function onLoaded( callback ) {
loadCallback = callback;
if( loaded ) {
loadCallback();
}
}
var progressCallback;
function onProgress( callback ) {
progressCallback = callback;
}
function isLoaded() {
return loaded;
}
function get( name ) {
return collection[name];
}
return {
get: get,
onLoaded: onLoaded,
isLoaded: isLoaded,
onProgress: onProgress,
};
}
function Video( params ) {
var duration, buffered;
var loaded = false;
var video = document.createElement('video');
video.width = params.width;
video.height = params.height;
video.autoplay = true;
video.loop = true;
video.setAttribute("src",params.src);
video.addEventListener('loadedmetadata', function() {
duration = this.duration;
if( this.buffered.length > 0 ) {
buffered = this.buffered.end(0);
}
notifyLoadPercentage();
}, false);
video.addEventListener('progress', function() {
if( this.buffered.length > 0 ) {
buffered = this.buffered.end(0);
}
notifyLoadPercentage();
}, false);
function load() {
duration = 0;
buffered = 0;
video.load();
}
load();
function notifyLoadPercentage() {
if( duration > 0 && buffered > 0 && !loaded ) {
var percent = Math.ceil( buffered / duration * 100 );
if( progressCallback ) {
progressCallback( percent );
}
if( percent >= 100 ) {
loaded = true;
video.pause();
video.currentTime = 0;
if( loadCallback ) {
loadCallback();
}
}
}
}
function getCurrentFrame() {
return Math.floor(
video.currentTime.toFixed(5) * params.frameRate
);
}
function restart() {
video.currentTime = 0;
}
function frameToTime( frame ) {
return frame / params.frameRate + 0.00001;
}
function seek( delta ) {
var time = frameToTime( getCurrentFrame() + delta );
video.currentTime = time;
return video.currentTime >= video.duration;
}
var getDimensions = function() {
return {
width:video.width,
height:video.height
};
};
var copyToContext = function(context) {
context.drawImage(video, 0, 0);
};
var loadCallback;
function onLoaded( callback ) {
loadCallback = callback;
if( loaded ) {
loadCallback();
}
}
var progressCallback;
function onProgress( callback ) {
progressCallback = callback;
}
function isLoaded() {
return loaded;
}
return {
seek:seek,
onLoaded: onLoaded,
isLoaded: isLoaded,
restart: restart,
onProgress: onProgress,
copyToContext:copyToContext,
getDimensions:getDimensions,
};
}
return {
Video:Video,
BitmapCollection:BitmapCollection,
AudioCollection:AudioCollection,
};
}());