-
Notifications
You must be signed in to change notification settings - Fork 4
/
enterframe.js
executable file
·89 lines (84 loc) · 1.66 KB
/
enterframe.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
/*!
* EnterFrame.js
* Copyright (c) Matsukaze. All Rights Reserved.
* @version 1.0
* @author mach3
* @require jQuery
*/
/**
* Let us to add event handler to timeline
* @class
* @param {object} option Configuration object
* @return {object} Enterframe object
*/
var EnterFrame = function( option ){
this.config( option );
};
EnterFrame.prototype = {
option:{
fps:30
},
active:true,
funcs:[],
tid:null,
/**
* Configure
* @param {object} option Configuration object
* @return {object} Enterframe object
*/
config:function( option ){
if ( isNaN(option) ){
this.option = $.extend( {}, this.option, option );
} else {
this.option.fps = option;
}
return this;
},
/**
* Start playing timeline
* @return {object} Enterframe object
*/
start:function(){
this.active = true;
this._enterFrame();
return this;
},
_enterFrame:function(){
if ( this.active ){
$.each( this.funcs, function( i, f ){ f(); } );
this.tid = setTimeout(
$.proxy( this._enterFrame, this ),
Math.floor( 1000 / this.option.fps )
);
}
},
/**
* Stop playing timeline
* @return {object} Enterframe object
*/
stop:function(){
this.active = false;
clearTimeout( this.tid );
return this;
},
/**
* Add function
* @param {function} func Function to add to the list
* @return {object} Enterframe object
*/
add:function( func ){
this.funcs.push( func );
return this;
},
/**
* Remove function
* @param {function} func Function to remove from the list
* @return {object} Enterframe object
*/
remove:function( func ){
this.funcs = $.grep( this.funcs, function( f, i ){
return ( func !== f );
} );
return this;
}
};