Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(markers-display): displays the marker buffer #9

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cassettator.js",
"version": "0.420.0",
"version": "0.504.0",
"description": "A collection of video.js components and plugins",
"author": "amtins <[email protected]>",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/markers/css/cassettator-markers.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* private variables */
--_cst-marker-played: 0%;
/* TODO implement buffered */
--cst-marker-buffered: 0%;
--_cst-marker-buffered: 0%;
}

.cassettator-markers .vjs-time-tooltip {
Expand Down Expand Up @@ -36,7 +36,7 @@
height: 100%;
background: linear-gradient(to right,
var(--cst-marker-played-background-color, #fff) var(--_cst-marker-played),
var(--cst-marker-buffered-background-color, transparent) 0 var(--cst-marker-buffered),
var(--cst-marker-buffered-background-color, rgba(115, 133, 159, 0.7)) 0 var(--_cst-marker-buffered),
var(--cst-marker-background-color, var(--cst-default-background-color)) 0%);
border-radius: var(--cst-marker-border-radius, 0.05em);
}
Expand Down
51 changes: 35 additions & 16 deletions src/markers/src/marker-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import videojs from 'video.js';
*/

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

this.updateMarker = this.updateMarker.bind(this);
this.updateMarkerPlayed = this.updateMarkerPlayed.bind(this);
this.updateMarkerBuffered = this.updateMarkerBuffered.bind(this);

this.setMarkerWidth(this.markerWidth(), gap);
this.player().on('timeupdate', this.updateMarker);
this.player().on('timeupdate', this.updateMarkerPlayed);
this.player().on('progress', this.updateMarkerBuffered);
}

/**
Expand Down Expand Up @@ -59,35 +61,50 @@ class MarkerDisplay extends videojs.getComponent('component') {
}

/**
* Updates the marker background color, which uses the player's current time
* to calculate the percentage of the marker that has been played.
* Updates the marker background color based on the provided time and the css
* variable name.
*
* @param {number} time - The time (in seconds).
* @param {string} cssVar - The CSS variable name.
*/
updateMarker() {
const currentTime = this.player().currentTime();

updateMarker(time = 0, cssVar) {
const duration = this.player().duration();
const markersElWidth = this.parentComponent_.el().getClientRects()[0].width;
const markerOffsetLeft = this.el().offsetLeft;
const markerOffsetWidth = this.el().offsetWidth;
const markerStart = (duration * markerOffsetLeft) / markersElWidth;
const markerEnd =
(duration * (markerOffsetLeft + markerOffsetWidth)) / markersElWidth;
const percent = (currentTime - markerStart) / (markerStart - markerEnd);
const percent = (time - markerStart) / (markerStart - markerEnd);

if (currentTime > markerEnd) {
if (time > markerEnd) {
// Setting the value to 200% avoids losing pixel when resizing the player.
this.el().style.setProperty('--_cst-marker-played', `200%`);
this.el().style.setProperty(cssVar, `200%`);
}

if (currentTime < markerStart) {
this.el().style.setProperty('--_cst-marker-played', `0%`);
if (time < markerStart) {
this.el().style.setProperty(cssVar, `0%`);
}

if (currentTime >= markerStart && currentTime <= markerEnd) {
this.el().style.setProperty('--_cst-marker-played', `${Math.abs(percent) * 100}%`);
if (time >= markerStart && time <= markerEnd) {
this.el().style.setProperty(cssVar, `${Math.abs(percent) * 100}%`);
}
}

/**
* Updates the buffered portion of the marker.
*/
updateMarkerBuffered() {
this.updateMarker(this.player().bufferedEnd(), '--_cst-marker-buffered');
}

/**
* Updates the played portion of the marker.
*/
updateMarkerPlayed() {
this.updateMarker(this.player().currentTime(), '--_cst-marker-played');
}

// ***************************************************************************
// Standard component functions **********************************************
// ***************************************************************************
Expand Down Expand Up @@ -116,7 +133,9 @@ class MarkerDisplay extends videojs.getComponent('component') {
* Disposes of the marker component.
*/
dispose() {
this.player().off('timeupdate', this.updateMarker);
this.player().off('timeupdate', this.updateMarkerPlayed);
this.player().off('progress', this.updateMarkerBuffered);

super.dispose();
}
}
Expand Down