forked from mozilla/popcorn-js
/
popcorn.wordriver.js
executable file
·144 lines (110 loc) · 5.04 KB
/
popcorn.wordriver.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
// PLUGIN: Wordriver
(function (Popcorn) {
var container = {},
spanLocation = 0,
setupContainer = function( target ) {
container[ target ] = document.createElement( "div" );
var t = document.getElementById( target );
t && t.appendChild( container[ target ] );
container[ target ].style.height = "100%";
container[ target ].style.position = "relative";
return container[ target ];
},
// creates an object of supported, cross platform css transitions
span = document.createElement( "span" ),
prefixes = [ "webkit", "Moz", "ms", "O", "" ],
specProp = [ "Transform", "TransitionDuration", "TransitionTimingFunction" ],
supports = {},
prop;
document.head.appendChild( span );
for ( var sIdx = 0, sLen = specProp.length; sIdx < sLen; sIdx++ ) {
for ( var pIdx = 0, pLen = prefixes.length; pIdx < pLen; pIdx++ ) {
prop = prefixes[ pIdx ] + specProp[ sIdx ];
if ( prop in span.style ) {
supports[ specProp[ sIdx ].toLowerCase() ] = prop;
break;
}
}
}
// Garbage collect support test span
document.head.removeChild( span );
/**
* Word River popcorn plug-in
* Displays a string of text, fading it in and out
* while transitioning across the height of the parent container
* for the duration of the instance (duration = end - start)
*
* @param {Object} options
*
* Example:
var p = Popcorn( '#video' )
.wordriver({
start: 5, // When to begin the Word River animation
end: 15, // When to finish the Word River animation
text: 'Hello World', // The text you want to be displayed by Word River
target: 'wordRiverDiv', // The target div to append the text to
color: "blue" // The color of the text. (can be Hex value i.e. #FFFFFF )
} )
*
*/
Popcorn.plugin( "wordriver" , {
manifest: {
about:{
name: "Popcorn WordRiver Plugin"
},
options:{
start : {elem:'input', type:'text', label:'In'},
end : {elem:'input', type:'text', label:'Out'},
target : 'wordriver-container',
text : {elem:'input', type:'text', label:'Text'},
color : {elem:'input', type:'text', label:'Color'}
}
},
_setup: function( options ) {
options._duration = options.end - options.start;
options._container = container[ options.target ] || setupContainer( options.target );
options.word = document.createElement( "span" );
options.word.style.position = "absolute";
options.word.style.whiteSpace = "nowrap";
options.word.style.opacity = 0;
options.word.style.MozTransitionProperty = "opacity, -moz-transform";
options.word.style.webkitTransitionProperty = "opacity, -webkit-transform";
options.word.style.OTransitionProperty = "opacity, -o-transform";
options.word.style.transitionProperty = "opacity, transform";
options.word.style[ supports.transitionduration ] = 1 + "s, " + options._duration + "s";
options.word.style[ supports.transitiontimingfunction ] = "linear";
options.word.innerHTML = options.text;
options.word.style.color = options.color || "black";
},
start: function( event, options ){
options._container.appendChild( options.word );
// Resets the transform when changing to a new currentTime before the end event occurred.
options.word.style[ supports.transform ] = "";
options.word.style.fontSize = ~~( 30 + 20 * Math.random() ) + "px";
spanLocation = spanLocation % ( options._container.offsetWidth - options.word.offsetWidth );
options.word.style.left = spanLocation + "px";
spanLocation += options.word.offsetWidth + 10;
options.word.style[ supports.transform ] = "translateY(" +
( document.getElementById( options.target ).offsetHeight - options.word.offsetHeight ) + "px)";
options.word.style.opacity = 1;
// automatically clears the word based on time
setTimeout( function() {
options.word.style.opacity = 0;
// ensures at least one second exists, because the fade animation is 1 second
}, ( ( (options.end - options.start) - 1 ) || 1 ) * 1000 );
},
end: function( event, options ){
// manually clears the word based on user interaction
options.word.style.opacity = 0;
},
_teardown: function( options ) {
// removes word span from generated container
options.word.parentNode && options._container.removeChild( options.word );
// if no more word spans exist in container, remove container
container[ options.target ] &&
!container[ options.target ].childElementCount &&
document.getElementById( options.target ).removeChild( container[ options.target ] ) &&
delete container[ options.target ];
}
});
})( Popcorn );