-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRecorder.js
More file actions
95 lines (71 loc) · 1.65 KB
/
Recorder.js
File metadata and controls
95 lines (71 loc) · 1.65 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
/*
* @author zz85
* Recorder class for recording events. Used in "It came upon" http://jabtunes.com/itcameupon/
*
* Usage:
* // start recording
* recorder = new Recorder();
* recorder.start();
*
* // record events
* recorder.record(event);
*
* // stop recording
* recorder.stop();
*
* // save
* save = recorder.toJSON();
*
* // playback (using director)
* recorder._recordings = (myrecording);
* playbackDirector = recorder.getDirector(playbackHandler);
*/
var Recorder = function() {
this.reset = function() {
this._started = false;
this._recordings = [];
}
this.start = function() {
this._started = true;
this._startTime = Date.now();
};
// records event
this.record = function(event) {
if (!this._started) return;
var time = Date.now();
var runningTime = time - this._startTime;
this._recordings.push({
time: runningTime,
event: event
// todo: copies argument too?
});
};
this.getDirector = function(callback) {
var recordings = this._recordings;
var director = new THREE.Director();
function callBackEvent(event) {
return function() {
callback(event)
};
}
for (var i=0,il=recordings.length;i<il;i++) {
var event = recordings[i].event;
var closure = callBackEvent(event);
director.addAction(recordings[i].time / 1000, closure);
}
return director;
}
this.stop = function() {
this._started = false;
};
this.hasStarted = function() {
return this._started;
};
this.toJSON = function() {
return JSON.stringify(this._recordings);
}
this.fromJSON = function(json) {
this._recordings = JSON.parse(json);
}
this.reset();
};