Scroll refactor (#1031)

* Use private members

* Reorganize
This commit is contained in:
toasted-nutbread 2020-11-14 17:24:30 -05:00 committed by GitHub
parent af16643f35
commit 34c6d4210a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,17 +15,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
class WindowScroll { class WindowScroll {
constructor() { constructor() {
this.animationRequestId = null; this._animationRequestId = null;
this.animationStartTime = 0; this._animationStartTime = 0;
this.animationStartX = 0; this._animationStartX = 0;
this.animationStartY = 0; this._animationStartY = 0;
this.animationEndTime = 0; this._animationEndTime = 0;
this.animationEndX = 0; this._animationEndX = 0;
this.animationEndY = 0; this._animationEndY = 0;
this.requestAnimationFrameCallback = this.onAnimationFrame.bind(this); this._requestAnimationFrameCallback = this._onAnimationFrame.bind(this);
}
get x() {
return window.scrollX || window.pageXOffset;
}
get y() {
return window.scrollY || window.pageYOffset;
} }
toY(y) { toY(y) {
@ -42,49 +49,43 @@ class WindowScroll {
} }
animate(x, y, time) { animate(x, y, time) {
this.animationStartX = this.x; this._animationStartX = this.x;
this.animationStartY = this.y; this._animationStartY = this.y;
this.animationStartTime = window.performance.now(); this._animationStartTime = window.performance.now();
this.animationEndX = x; this._animationEndX = x;
this.animationEndY = y; this._animationEndY = y;
this.animationEndTime = this.animationStartTime + time; this._animationEndTime = this._animationStartTime + time;
this.animationRequestId = window.requestAnimationFrame(this.requestAnimationFrameCallback); this._animationRequestId = window.requestAnimationFrame(this._requestAnimationFrameCallback);
} }
stop() { stop() {
if (this.animationRequestId === null) { if (this._animationRequestId === null) {
return; return;
} }
window.cancelAnimationFrame(this.animationRequestId); window.cancelAnimationFrame(this._animationRequestId);
this.animationRequestId = null; this._animationRequestId = null;
} }
onAnimationFrame(time) { // Private
if (time >= this.animationEndTime) {
window.scroll(this.animationEndX, this.animationEndY); _onAnimationFrame(time) {
this.animationRequestId = null; if (time >= this._animationEndTime) {
window.scroll(this._animationEndX, this._animationEndY);
this._animationRequestId = null;
return; return;
} }
const t = WindowScroll.easeInOutCubic((time - this.animationStartTime) / (this.animationEndTime - this.animationStartTime)); const t = this._easeInOutCubic((time - this._animationStartTime) / (this._animationEndTime - this._animationStartTime));
window.scroll( window.scroll(
WindowScroll.lerp(this.animationStartX, this.animationEndX, t), this._lerp(this._animationStartX, this._animationEndX, t),
WindowScroll.lerp(this.animationStartY, this.animationEndY, t) this._lerp(this._animationStartY, this._animationEndY, t)
); );
this.animationRequestId = window.requestAnimationFrame(this.requestAnimationFrameCallback); this._animationRequestId = window.requestAnimationFrame(this._requestAnimationFrameCallback);
} }
get x() { _easeInOutCubic(t) {
return window.scrollX || window.pageXOffset;
}
get y() {
return window.scrollY || window.pageYOffset;
}
static easeInOutCubic(t) {
if (t < 0.5) { if (t < 0.5) {
return (4.0 * t * t * t); return (4.0 * t * t * t);
} else { } else {
@ -93,7 +94,7 @@ class WindowScroll {
} }
} }
static lerp(start, end, percent) { _lerp(start, end, percent) {
return (end - start) * percent + start; return (end - start) * percent + start;
} }
} }