Merge pull request #536 from ueokande/make-smooth-scroll-smoother

Make smooth-scroll smoother
jh-changes
Shin'ya Ueoka 6 years ago committed by GitHub
commit 21788740c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/content/actions/operation.js
  2. 2
      src/content/components/common/keymapper.js
  3. 137
      src/content/scrolls.js
  4. 1
      src/shared/utils/keys.js

@ -10,7 +10,7 @@ import * as markActions from './mark';
import * as properties from 'shared/settings/properties'; import * as properties from 'shared/settings/properties';
// eslint-disable-next-line complexity, max-lines-per-function // eslint-disable-next-line complexity, max-lines-per-function
const exec = (operation, repeat, settings, addonEnabled) => { const exec = (operation, settings, addonEnabled) => {
let smoothscroll = settings.properties.smoothscroll || let smoothscroll = settings.properties.smoothscroll ||
properties.defaults.smoothscroll; properties.defaults.smoothscroll;
switch (operation.type) { switch (operation.type) {
@ -31,13 +31,13 @@ const exec = (operation, repeat, settings, addonEnabled) => {
}), '*'); }), '*');
break; break;
case operations.SCROLL_VERTICALLY: case operations.SCROLL_VERTICALLY:
scrolls.scrollVertically(operation.count, smoothscroll, repeat); scrolls.scrollVertically(operation.count, smoothscroll);
break; break;
case operations.SCROLL_HORIZONALLY: case operations.SCROLL_HORIZONALLY:
scrolls.scrollHorizonally(operation.count, smoothscroll, repeat); scrolls.scrollHorizonally(operation.count, smoothscroll);
break; break;
case operations.SCROLL_PAGES: case operations.SCROLL_PAGES:
scrolls.scrollPages(operation.count, smoothscroll, repeat); scrolls.scrollPages(operation.count, smoothscroll);
break; break;
case operations.SCROLL_TOP: case operations.SCROLL_TOP:
scrolls.scrollToTop(smoothscroll); scrolls.scrollToTop(smoothscroll);

@ -49,7 +49,7 @@ export default class KeymapperComponent {
} }
let operation = keymaps.get(matched[0]); let operation = keymaps.get(matched[0]);
let act = operationActions.exec( let act = operationActions.exec(
operation, key.repeat, state.setting, state.addon.enabled operation, state.setting, state.addon.enabled
); );
this.store.dispatch(act); this.store.dispatch(act);
this.store.dispatch(inputActions.clearKeys()); this.store.dispatch(inputActions.clearKeys());

@ -1,11 +1,11 @@
import * as doms from 'shared/utils/dom'; import * as doms from 'shared/utils/dom';
const SCROLL_DELTA_X = 48; const SCROLL_DELTA_X = 64;
const SCROLL_DELTA_Y = 48; const SCROLL_DELTA_Y = 64;
const SMOOTH_SCROLL_DURATION = 150;
// dirty way to store scrolling state on globally // dirty way to store scrolling state on globally
let scrolling = [false]; let scrolling = false;
let lastTimeoutId = null;
const isScrollableStyle = (element) => { const isScrollableStyle = (element) => {
let { overflowX, overflowY } = window.getComputedStyle(element); let { overflowX, overflowY } = window.getComputedStyle(element);
@ -51,65 +51,49 @@ const scrollTarget = () => {
return window.document.documentElement; return window.document.documentElement;
}; };
class SmoothScroller { const resetScrolling = () => {
constructor(element, repeat) { scrolling = false;
};
class Scroller {
constructor(element, smooth) {
this.element = element; this.element = element;
this.repeat = repeat; this.smooth = smooth;
this.scrolling = scrolling;
if (repeat) {
this.easing = SmoothScroller.linearEasing;
} else {
this.easing = SmoothScroller.inOutQuadEasing;
}
} }
scroll(x, y) { scrollTo(x, y) {
if (this.scrolling[0]) { let behavior = this.smooth ? 'smooth' : 'auto';
window.scrollTo({
left: x,
top: y,
behavior: behavior,
});
if (!this.smooth) {
return; return;
} }
scrolling[0] = true; this.prepareReset();
this.startX = this.element.scrollLeft;
this.startY = this.element.scrollTop;
this.targetX = x;
this.targetY = y;
this.distanceX = x - this.startX;
this.distanceY = y - this.startY;
this.timeStart = 0;
window.requestAnimationFrame(this.loop.bind(this));
}
loop(time) {
if (!this.timeStart) {
this.timeStart = time;
} }
let elapsed = time - this.timeStart; scrollBy(x, y) {
let v = this.easing(elapsed / SMOOTH_SCROLL_DURATION); let behavior = this.smooth ? 'smooth' : 'auto';
let nextX = this.startX + this.distanceX * v; window.scrollBy({
let nextY = this.startY + this.distanceY * v; left: x,
top: y,
window.scrollTo(nextX, nextY); behavior: behavior,
});
if (elapsed < SMOOTH_SCROLL_DURATION) { if (!this.smooth) {
window.requestAnimationFrame(this.loop.bind(this)); return;
} else {
scrolling[0] = false;
this.element.scrollTo(this.targetX, this.targetY);
} }
this.prepareReset();
} }
static inOutQuadEasing(t) { prepareReset() {
if (t < 1) { scrolling = true;
return t * t; if (lastTimeoutId) {
} clearTimeout(lastTimeoutId);
return -(t - 1) * (t - 1) + 1; lastTimeoutId = null;
} }
lastTimeoutId = setTimeout(resetScrolling, 100);
static linearEasing(t) {
return t;
} }
} }
@ -123,77 +107,70 @@ class RoughtScroller {
} }
} }
const scroller = (element, smooth, repeat) => {
if (smooth) {
return new SmoothScroller(element, repeat);
}
return new RoughtScroller(element);
};
const getScroll = () => { const getScroll = () => {
let target = scrollTarget(); let target = scrollTarget();
return { x: target.scrollLeft, y: target.scrollTop }; return { x: target.scrollLeft, y: target.scrollTop };
}; };
const scrollVertically = (count, smooth, repeat) => { const scrollVertically = (count, smooth) => {
let target = scrollTarget(); let target = scrollTarget();
let x = target.scrollLeft; let delta = SCROLL_DELTA_Y * count;
let y = target.scrollTop + SCROLL_DELTA_Y * count; if (scrolling) {
if (repeat && smooth) { delta = SCROLL_DELTA_Y * count * 4;
y = target.scrollTop + SCROLL_DELTA_Y * count * 4;
} }
scroller(target, smooth, repeat).scroll(x, y); new Scroller(target, smooth).scrollBy(0, delta);
}; };
const scrollHorizonally = (count, smooth, repeat) => { const scrollHorizonally = (count, smooth) => {
let target = scrollTarget(); let target = scrollTarget();
let x = target.scrollLeft + SCROLL_DELTA_X * count; let delta = SCROLL_DELTA_X * count;
let y = target.scrollTop; if (scrolling) {
if (repeat && smooth) { delta = SCROLL_DELTA_X * count * 4;
y = target.scrollTop + SCROLL_DELTA_Y * count * 4;
} }
scroller(target, smooth, repeat).scroll(x, y); new Scroller(target, smooth).scrollBy(delta, 0);
}; };
const scrollPages = (count, smooth, repeat) => { const scrollPages = (count, smooth) => {
let target = scrollTarget(); let target = scrollTarget();
let height = target.clientHeight; let height = target.clientHeight;
let x = target.scrollLeft; let delta = height * count;
let y = target.scrollTop + height * count; if (scrolling) {
scroller(target, smooth, repeat).scroll(x, y); delta = height * count;
}
new Scroller(target, smooth).scrollBy(0, delta);
}; };
const scrollTo = (x, y, smooth) => { const scrollTo = (x, y, smooth) => {
let target = scrollTarget(); let target = scrollTarget();
scroller(target, smooth, false).scroll(x, y); new Scroller(target, smooth).scrollTo(x, y);
}; };
const scrollToTop = (smooth) => { const scrollToTop = (smooth) => {
let target = scrollTarget(); let target = scrollTarget();
let x = target.scrollLeft; let x = target.scrollLeft;
let y = 0; let y = 0;
scroller(target, smooth, false).scroll(x, y); new Scroller(target, smooth).scrollTo(x, y);
}; };
const scrollToBottom = (smooth) => { const scrollToBottom = (smooth) => {
let target = scrollTarget(); let target = scrollTarget();
let x = target.scrollLeft; let x = target.scrollLeft;
let y = target.scrollHeight; let y = target.scrollHeight;
scroller(target, smooth, false).scroll(x, y); new Scroller(target, smooth).scrollTo(x, y);
}; };
const scrollToHome = (smooth) => { const scrollToHome = (smooth) => {
let target = scrollTarget(); let target = scrollTarget();
let x = 0; let x = 0;
let y = target.scrollTop; let y = target.scrollTop;
scroller(target, smooth, false).scroll(x, y); new Scroller(target, smooth).scrollTo(x, y);
}; };
const scrollToEnd = (smooth) => { const scrollToEnd = (smooth) => {
let target = scrollTarget(); let target = scrollTarget();
let x = target.scrollWidth; let x = target.scrollWidth;
let y = target.scrollTop; let y = target.scrollTop;
scroller(target, smooth, false).scroll(x, y); new Scroller(target, smooth).scrollTo(x, y);
}; };
export { export {

@ -18,7 +18,6 @@ const fromKeyboardEvent = (e) => {
return { return {
key: modifiedKeyName(e.key), key: modifiedKeyName(e.key),
repeat: e.repeat,
shiftKey: shift, shiftKey: shift,
ctrlKey: e.ctrlKey, ctrlKey: e.ctrlKey,
altKey: e.altKey, altKey: e.altKey,