@@ -9,7 +9,7 @@ import videojs from 'video.js';
9
9
*/
10
10
11
11
/**
12
- * A class representing a marker.
12
+ * A class representing a marker that will be displayed in the progress bar .
13
13
*
14
14
* @class
15
15
* @extends Component
@@ -26,10 +26,12 @@ class MarkerDisplay extends videojs.getComponent('component') {
26
26
super ( player , options ) ;
27
27
const { gap } = options ;
28
28
29
- this . updateMarker = this . updateMarker . bind ( this ) ;
29
+ this . updateMarkerPlayed = this . updateMarkerPlayed . bind ( this ) ;
30
+ this . updateMarkerBuffered = this . updateMarkerBuffered . bind ( this ) ;
30
31
31
32
this . setMarkerWidth ( this . markerWidth ( ) , gap ) ;
32
- this . player ( ) . on ( 'timeupdate' , this . updateMarker ) ;
33
+ this . player ( ) . on ( 'timeupdate' , this . updateMarkerPlayed ) ;
34
+ this . player ( ) . on ( 'progress' , this . updateMarkerBuffered ) ;
33
35
}
34
36
35
37
/**
@@ -59,35 +61,50 @@ class MarkerDisplay extends videojs.getComponent('component') {
59
61
}
60
62
61
63
/**
62
- * Updates the marker background color, which uses the player's current time
63
- * to calculate the percentage of the marker that has been played.
64
+ * Updates the marker background color based on the provided time and the css
65
+ * variable name.
66
+ *
67
+ * @param {number } time - The time (in seconds).
68
+ * @param {string } cssVar - The CSS variable name.
64
69
*/
65
- updateMarker ( ) {
66
- const currentTime = this . player ( ) . currentTime ( ) ;
67
-
70
+ updateMarker ( time = 0 , cssVar ) {
68
71
const duration = this . player ( ) . duration ( ) ;
69
72
const markersElWidth = this . parentComponent_ . el ( ) . getClientRects ( ) [ 0 ] . width ;
70
73
const markerOffsetLeft = this . el ( ) . offsetLeft ;
71
74
const markerOffsetWidth = this . el ( ) . offsetWidth ;
72
75
const markerStart = ( duration * markerOffsetLeft ) / markersElWidth ;
73
76
const markerEnd =
74
77
( duration * ( markerOffsetLeft + markerOffsetWidth ) ) / markersElWidth ;
75
- const percent = ( currentTime - markerStart ) / ( markerStart - markerEnd ) ;
78
+ const percent = ( time - markerStart ) / ( markerStart - markerEnd ) ;
76
79
77
- if ( currentTime > markerEnd ) {
80
+ if ( time > markerEnd ) {
78
81
// Setting the value to 200% avoids losing pixel when resizing the player.
79
- this . el ( ) . style . setProperty ( '--_cst-marker-played' , `200%` ) ;
82
+ this . el ( ) . style . setProperty ( cssVar , `200%` ) ;
80
83
}
81
84
82
- if ( currentTime < markerStart ) {
83
- this . el ( ) . style . setProperty ( '--_cst-marker-played' , `0%` ) ;
85
+ if ( time < markerStart ) {
86
+ this . el ( ) . style . setProperty ( cssVar , `0%` ) ;
84
87
}
85
88
86
- if ( currentTime >= markerStart && currentTime <= markerEnd ) {
87
- this . el ( ) . style . setProperty ( '--_cst-marker-played' , `${ Math . abs ( percent ) * 100 } %` ) ;
89
+ if ( time >= markerStart && time <= markerEnd ) {
90
+ this . el ( ) . style . setProperty ( cssVar , `${ Math . abs ( percent ) * 100 } %` ) ;
88
91
}
89
92
}
90
93
94
+ /**
95
+ * Updates the buffered portion of the marker.
96
+ */
97
+ updateMarkerBuffered ( ) {
98
+ this . updateMarker ( this . player ( ) . bufferedEnd ( ) , '--_cst-marker-buffered' ) ;
99
+ }
100
+
101
+ /**
102
+ * Updates the played portion of the marker.
103
+ */
104
+ updateMarkerPlayed ( ) {
105
+ this . updateMarker ( this . player ( ) . currentTime ( ) , '--_cst-marker-played' ) ;
106
+ }
107
+
91
108
// ***************************************************************************
92
109
// Standard component functions **********************************************
93
110
// ***************************************************************************
@@ -116,7 +133,9 @@ class MarkerDisplay extends videojs.getComponent('component') {
116
133
* Disposes of the marker component.
117
134
*/
118
135
dispose ( ) {
119
- this . player ( ) . off ( 'timeupdate' , this . updateMarker ) ;
136
+ this . player ( ) . off ( 'timeupdate' , this . updateMarkerPlayed ) ;
137
+ this . player ( ) . off ( 'progress' , this . updateMarkerBuffered ) ;
138
+
120
139
super . dispose ( ) ;
121
140
}
122
141
}
0 commit comments