Skip to content

Commit

Permalink
add clamp helper
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Aug 2, 2019
1 parent 57cf88b commit dd2e478
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/js/control-bar/progress-control/progress-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import Component from '../../component.js';
import * as Dom from '../../utils/dom.js';
import clamp from '../../utils/clamp.js';
import {bind, throttle, UPDATE_REFRESH_INTERVAL} from '../../utils/fn.js';

import './seek-bar.js';
Expand Down Expand Up @@ -74,14 +75,14 @@ class ProgressControl extends Component {
// The default skin has a gap on either side of the `SeekBar`. This means
// that it's possible to trigger this behavior outside the boundaries of
// the `SeekBar`. This ensures we stay within it at all times.
seekBarPoint = Math.max(0, Math.min(1, seekBarPoint));
seekBarPoint = clamp(0, 1, seekBarPoint);

if (mouseTimeDisplay) {
mouseTimeDisplay.update(seekBarRect, seekBarPoint);
}

if (playProgressBar) {
playProgressBar.update(seekBarRect, seekBar.percent_);
playProgressBar.update(seekBarRect, seekBar.getProgress());
}

}
Expand Down
34 changes: 10 additions & 24 deletions src/js/control-bar/progress-control/seek-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,20 @@ class SeekBar extends Slider {
* This function updates the play progress bar and accessibility
* attributes to whatever is passed in.
*
* @param {number} currentTime
* The currentTime value that should be used for accessibility
* @param {EventTarget~Event} [event]
* The `timeupdate` or `ended` event that caused this to run.
*
* @param {number} percent
* The percentage as a decimal that the bar should be filled from 0-1.
* @listens Player#timeupdate
*
* @private
* @return {number}
* The current percent at a number from 0-1
*/
update_(currentTime, percent) {
update(event) {
const percent = super.update();

this.requestAnimationFrame(() => {
const currentTime = this.player_.ended() ?
this.player.duration() : this.getCurrentTime_();
const liveTracker = this.player_.liveTracker;
let duration = this.player_.duration();

Expand Down Expand Up @@ -163,25 +167,7 @@ class SeekBar extends Slider {
this.duration_ = duration;
}
});
}

/**
* Update the seek bar's UI.
*
* @param {EventTarget~Event} [event]
* The `timeupdate` or `ended` event that caused this to run.
*
* @listens Player#timeupdate
*
* @return {number}
* The current percent at a number from 0-1
*/
update(event) {
const percent = super.update();
const currentTime = this.player_.ended() ?
this.player.duration() : this.getCurrentTime_();

this.update_(currentTime, percent);
return percent;
}

Expand Down
4 changes: 0 additions & 4 deletions src/js/control-bar/progress-control/time-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Component from '../../component';
import * as Dom from '../../utils/dom.js';
import formatTime from '../../utils/format-time.js';
import * as Fn from '../../utils/fn.js';
import * as browser from '../../utils/browser.js';

/**
* Time tooltips display a time above the progress bar.
Expand All @@ -25,9 +24,6 @@ class TimeTooltip extends Component {
*/
constructor(player, options) {
super(player, options);
if (browser.IS_IOS || browser.IS_ANDROID) {
this.hide();
}
this.update = Fn.throttle(Fn.bind(this, this.update), Fn.UPDATE_REFRESH_INTERVAL);
}

Expand Down
26 changes: 19 additions & 7 deletions src/js/slider/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Component from '../component.js';
import * as Dom from '../utils/dom.js';
import {assign} from '../utils/obj';
import {IS_CHROME} from '../utils/browser.js';
import clamp from '../utils/clamp.js';
import keycode from 'keycode';

/**
Expand Down Expand Up @@ -230,7 +231,6 @@ class Slider extends Component {
* number from 0 to 1.
*/
update() {

// In VolumeBar init we have a setTimeout for update that pops and update
// to the end of the execution stack. The player is destroyed before then
// update will cause an error
Expand All @@ -241,24 +241,36 @@ class Slider extends Component {

// clamp progress between 0 and 1
// and only round to four decimal places, as we round to two below
const percent = Number(this.getPercent()) || 0;
const progress = Math.max(0, Math.min(1, percent)).toFixed(4);
const progress = this.getProgress();

if (progress === this.progress_) {
return;
}

this.progress_ = progress;

// Set the new bar width or height
const sizeKey = this.vertical() ? 'height' : 'width';
this.requestAnimationFrame(() => {
// Set the new bar width or height
const sizeKey = this.vertical() ? 'height' : 'width';

// Convert to a percentage for css value
this.bar.el().style[sizeKey] = (progress * 100).toFixed(2) + '%';
// Convert to a percentage for css value
this.bar.el().style[sizeKey] = (progress * 100).toFixed(2) + '%';
});

return progress;
}

/**
* Get the percentage of the bar that should be filled
* but clamped and rounded.
*
* @return {number}
* percentage filled that the slider is
*/
getProgress() {
return clamp(this.getPercent(), 0, 1).toFixed(4);
}

/**
* Calculate distance for slider
*
Expand Down
19 changes: 19 additions & 0 deletions src/js/utils/clamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Keep a number between a min and a max value
*
* @param {number} number
* The number to clamp
*
* @param {number} min
* The minimum value
* @param {number} max
* The maximum value
*
* @return {number}
* the clamped number
*/
const clamp = function(number, min, max) {
return Math.min(max, Math.max(min, Number(number)));
};

export default clamp;

0 comments on commit dd2e478

Please sign in to comment.