Skip to content

Commit 8e2e91c

Browse files
committed
feat(markers-display): displays the marker buffer
This feature modifies the `MarkerDisplay` class to add support for displaying and updating the marker buffer at every progress event. - modifies the `updateMarker` function to take a `time` and a `css variable` as parameters - adds the `updateMarkerBuffered` function, which uses `updateMarker` to update the marker buffer - adds the `updateMarkerPlayed` function, which uses `updateMarker` to update the played marker - renames the `css variable` used to update the marker's buffering percentage - redefines the default color of `--cst-marker-buffered-background-color`
1 parent 5c40056 commit 8e2e91c

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cassettator.js",
3-
"version": "0.420.0",
3+
"version": "0.504.0",
44
"description": "A collection of video.js components and plugins",
55
"author": "amtins <[email protected]>",
66
"license": "MIT",

src/markers/css/cassettator-markers.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* private variables */
44
--_cst-marker-played: 0%;
55
/* TODO implement buffered */
6-
--cst-marker-buffered: 0%;
6+
--_cst-marker-buffered: 0%;
77
}
88

99
.cassettator-markers .vjs-time-tooltip {
@@ -36,7 +36,7 @@
3636
height: 100%;
3737
background: linear-gradient(to right,
3838
var(--cst-marker-played-background-color, #fff) var(--_cst-marker-played),
39-
var(--cst-marker-buffered-background-color, transparent) 0 var(--cst-marker-buffered),
39+
var(--cst-marker-buffered-background-color, rgba(115, 133, 159, 0.7)) 0 var(--_cst-marker-buffered),
4040
var(--cst-marker-background-color, var(--cst-default-background-color)) 0%);
4141
border-radius: var(--cst-marker-border-radius, 0.05em);
4242
}

src/markers/src/marker-display.js

+35-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import videojs from 'video.js';
99
*/
1010

1111
/**
12-
* A class representing a marker.
12+
* A class representing a marker that will be displayed in the progress bar.
1313
*
1414
* @class
1515
* @extends Component
@@ -26,10 +26,12 @@ class MarkerDisplay extends videojs.getComponent('component') {
2626
super(player, options);
2727
const { gap } = options;
2828

29-
this.updateMarker = this.updateMarker.bind(this);
29+
this.updateMarkerPlayed = this.updateMarkerPlayed.bind(this);
30+
this.updateMarkerBuffered = this.updateMarkerBuffered.bind(this);
3031

3132
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);
3335
}
3436

3537
/**
@@ -59,35 +61,50 @@ class MarkerDisplay extends videojs.getComponent('component') {
5961
}
6062

6163
/**
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.
6469
*/
65-
updateMarker() {
66-
const currentTime = this.player().currentTime();
67-
70+
updateMarker(time = 0, cssVar) {
6871
const duration = this.player().duration();
6972
const markersElWidth = this.parentComponent_.el().getClientRects()[0].width;
7073
const markerOffsetLeft = this.el().offsetLeft;
7174
const markerOffsetWidth = this.el().offsetWidth;
7275
const markerStart = (duration * markerOffsetLeft) / markersElWidth;
7376
const markerEnd =
7477
(duration * (markerOffsetLeft + markerOffsetWidth)) / markersElWidth;
75-
const percent = (currentTime - markerStart) / (markerStart - markerEnd);
78+
const percent = (time - markerStart) / (markerStart - markerEnd);
7679

77-
if (currentTime > markerEnd) {
80+
if (time > markerEnd) {
7881
// 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%`);
8083
}
8184

82-
if (currentTime < markerStart) {
83-
this.el().style.setProperty('--_cst-marker-played', `0%`);
85+
if (time < markerStart) {
86+
this.el().style.setProperty(cssVar, `0%`);
8487
}
8588

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}%`);
8891
}
8992
}
9093

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+
91108
// ***************************************************************************
92109
// Standard component functions **********************************************
93110
// ***************************************************************************
@@ -116,7 +133,9 @@ class MarkerDisplay extends videojs.getComponent('component') {
116133
* Disposes of the marker component.
117134
*/
118135
dispose() {
119-
this.player().off('timeupdate', this.updateMarker);
136+
this.player().off('timeupdate', this.updateMarkerPlayed);
137+
this.player().off('progress', this.updateMarkerBuffered);
138+
120139
super.dispose();
121140
}
122141
}

0 commit comments

Comments
 (0)