forked from mozilla/popcorn-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popcorn.code.js
executable file
·175 lines (153 loc) · 4.2 KB
/
popcorn.code.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
// PLUGIN: Code
(function ( Popcorn ) {
/**
* Code Popcorn Plug-in
*
* Adds the ability to run arbitrary code (JavaScript functions) according to video timing.
*
* @param {Object} options
*
* Required parameters: start, end, template, data, and target.
* Optional parameter: static.
*
* start: the time in seconds when the mustache template should be rendered
* in the target div.
*
* end: the time in seconds when the rendered mustache template should be
* removed from the target div.
*
* onStart: the function to be run when the start time is reached.
*
* onFrame: [optional] a function to be run on each paint call
* (e.g., called ~60 times per second) between the start and end times.
*
* onEnd: [optional] a function to be run when the end time is reached.
*
* Example:
var p = Popcorn('#video')
// onStart function only
.code({
start: 1,
end: 4,
onStart: function( options ) {
// called on start
}
})
// onStart + onEnd only
.code({
start: 6,
end: 8,
onStart: function( options ) {
// called on start
},
onEnd: function ( options ) {
// called on end
}
})
// onStart, onEnd, onFrame
.code({
start: 10,
end: 14,
onStart: function( options ) {
// called on start
},
onFrame: function ( options ) {
// called on every paint frame between start and end.
// uses mozRequestAnimationFrame, webkitRequestAnimationFrame,
// or setTimeout with 16ms window.
},
onEnd: function ( options ) {
// called on end
}
});
*
*/
Popcorn.plugin( "code" , function( options ) {
var running = false,
instance = this;
// Setup a proper frame interval function (60fps), favouring paint events.
var step = (function() {
var buildFrameRunner = function( runner ) {
return function( f, options ) {
var _f = function() {
running && f.call( instance, options );
running && runner( _f );
};
_f();
};
};
// Figure out which level of browser support we have for this
if ( window.webkitRequestAnimationFrame ) {
return buildFrameRunner( window.webkitRequestAnimationFrame );
} else if ( window.mozRequestAnimationFrame ) {
return buildFrameRunner( window.mozRequestAnimationFrame );
} else {
return buildFrameRunner( function( f ) {
window.setTimeout( f, 16 );
});
}
})();
if ( !options.onStart || typeof options.onStart !== "function" ) {
options.onStart = Popcorn.nop;
}
if ( options.onEnd && typeof options.onEnd !== "function" ) {
options.onEnd = undefined;
}
if ( options.onFrame && typeof options.onFrame !== "function" ) {
options.onFrame = undefined;
}
return {
start: function( event, options ) {
options.onStart.call( instance, options );
if ( options.onFrame ) {
running = true;
step( options.onFrame, options );
}
},
end: function( event, options ) {
if ( options.onFrame ) {
running = false;
}
if ( options.onEnd ) {
options.onEnd.call( instance, options );
}
}
};
},
{
about: {
name: "Popcorn Code Plugin",
version: "0.1",
author: "David Humphrey (@humphd)",
website: "http://vocamus.net/dave"
},
options: {
start: {
elem: "input",
type: "number",
label: "Start"
},
end: {
elem: "input",
type: "number",
label: "End"
},
onStart: {
elem: "input",
type: "function",
label: "onStart"
},
onFrame: {
elem: "input",
type: "function",
label: "onFrame",
optional: true
},
onEnd: {
elem: "input",
type: "function",
label: "onEnd"
}
}
});
})( Popcorn );