forked from madrobby/scripty2
/
heartbeat.js
99 lines (93 loc) · 2.81 KB
/
heartbeat.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
/** section: scripty2 fx
* class S2.FX.Heartbeat
*
* The heartbeat class provides for effects timing. An instance of this class
* is automatically created when the first effect on a page is instantiated.
*
* This class can be extended and replaced by your own implementation:
*
* // call before effects are created
* var myHeartbeat = Class.create(S2.FX.Heartbeat, { ... });
* S2.FX.initialize(new myHeartbeat());
*
* This can be used to implement customized debugging and more.
**/
S2.FX.Heartbeat = Class.create({
/**
* new S2.FX.Heartbeat([options])
* - options (Object): options hash
*
* The following options are available:
* * [[framerate]]: set (maximum) framerate for calls to [[S2.FX.beat]]/
**/
initialize: function(options) {
this.options = Object.extend({
framerate: Prototype.Browser.MobileSafari ? 20 : 60
}, options);
this.beat = this.beat.bind(this);
},
/**
* S2.FX.Heartbeat#start() -> undefined
*
* This function is called by [[S2.FX]] whenever there's a new active effect queued
* and there are no other effects running. This mechanism can be used to prevent
* unnecessary timeouts/intervals from being active, as [[S2.FX.Hearbeat.beat]] is only
* called when there are active effects that need to be rendered.
**/
start: function() {
if (this.heartbeatInterval) return;
this.heartbeatInterval =
setInterval(this.beat, 1000/this.options.framerate);
this.updateTimestamp();
},
/**
* S2.FX.Heartbeat#stop() -> undefined
*
* Called when the last active effect is dequeued.
**/
stop: function() {
if (!this.heartbeatInterval) return;
clearInterval(this.heartbeatInterval);
this.heartbeatInterval = null;
this.timestamp = null;
},
/**
* S2.FX.Heartbeat#beat() -> undefined
*
* This method fires an `effect:heartbeat` event which is in turn used by
* [[S2.FX]] to render all active effect queues.
*
* Fires: effect:heartbeat
**/
beat: function() {
this.updateTimestamp();
document.fire('effect:heartbeat');
},
/**
* S2.FX.Heartbeat#getTimestamp() -> Date
*
* Returns the current timestamp.
**/
getTimestamp: function() {
return this.timestamp || this.generateTimestamp();
},
/**
* S2.FX.Heartbeat#generateTimestamp() -> Date
*
* Returns the current date and time.
**/
generateTimestamp: function() {
return new Date().getTime();
},
/**
* S2.FX.Heartbeat#updateTimestamp() -> undefined
*
* Updates the current timestamp (sets it to the current date and time).
*
* If subclassed, this can be used to achieve special effects, for example all effects
* could be sped up or slowed down.
**/
updateTimestamp: function() {
this.timestamp = this.generateTimestamp();
}
});