Skip to content
Open
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
35 changes: 33 additions & 2 deletions dist/js/splide-extension-auto-scroll.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @splidejs/splide-extension-auto-scroll
* Version : 0.5.3
* License : MIT
* Copyright: 2022 Naotoshi Fujita
* Copyright: 2023 Naotoshi Fujita
*/
'use strict';

Expand Down Expand Up @@ -380,6 +380,7 @@ function AutoScroll(Splide2, Components2, options) {
const { setIndex, getIndex } = Components2.Controller;
const { orient } = Components2.Direction;
const { toggle } = Components2.Elements;
const { listSize } = Components2.Layout;
const { Live } = Components2;
const { root } = Splide2;
const throttledUpdateArrows = Throttle(Components2.Arrows.update, 500);
Expand All @@ -390,9 +391,14 @@ function AutoScroll(Splide2, Components2, options) {
let focused;
let busy;
let currPosition;
let baseTime;
let basePosition;
let lastRenderTime;
let msPerFrame;
function setup() {
const { autoScroll } = options;
autoScrollOptions = assign({}, DEFAULTS, isObject(autoScroll) ? autoScroll : {});
msPerFrame = autoScrollOptions.fpsLock ? 1e3 / autoScrollOptions.fpsLock : void 0;
}
function mount() {
if (!Splide2.is(FADE)) {
Expand Down Expand Up @@ -468,6 +474,9 @@ function AutoScroll(Splide2, Components2, options) {
Live.disable(true);
focused = hovered = stopped = false;
updateButton();
baseTime = Date.now();
basePosition = getPosition();
lastRenderTime = void 0;
}
}
function pause(stop = true) {
Expand All @@ -488,6 +497,8 @@ function AutoScroll(Splide2, Components2, options) {
function move() {
const position = getPosition();
const destination = computeDestination(position);
if (destination === null)
return;
if (position !== destination) {
translate(destination);
updateIndex(currPosition = getPosition());
Expand All @@ -500,8 +511,28 @@ function AutoScroll(Splide2, Components2, options) {
throttledUpdateArrows();
}
function computeDestination(position) {
const currentTimestamp = Date.now();
if (autoScrollOptions.fpsLock && lastRenderTime && currentTimestamp - lastRenderTime < msPerFrame / 2) {
return null;
}
const speed = autoScrollOptions.speed || 1;
position += orient(speed);
const virtualViewportSize = autoScrollOptions.virtualViewportSize || 1e3;
const realViewportSize = listSize();
const virtualToRealScale = realViewportSize / virtualViewportSize;
const speedScale = autoScrollOptions.virtualSpeed ? virtualToRealScale : 1;
if (autoScrollOptions.fpsLock) {
const timePassed = currentTimestamp - baseTime;
const framesPassed = timePassed * autoScrollOptions.fpsLock / 1e3;
const expectedPositionAtPassedFrames = orient(framesPassed * speed * speedScale) + basePosition;
if (expectedPositionAtPassedFrames !== position) {
position = expectedPositionAtPassedFrames;
lastRenderTime = currentTimestamp;
} else {
return null;
}
} else {
position += orient(speed * speedScale);
}
if (Splide2.is(SLIDE)) {
position = clamp(position, getLimit(false), getLimit(true));
}
Expand Down
35 changes: 33 additions & 2 deletions dist/js/splide-extension-auto-scroll.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @splidejs/splide-extension-auto-scroll
* Version : 0.5.3
* License : MIT
* Copyright: 2022 Naotoshi Fujita
* Copyright: 2023 Naotoshi Fujita
*/
function empty(array) {
array.length = 0;
Expand Down Expand Up @@ -376,6 +376,7 @@ function AutoScroll(Splide2, Components2, options) {
const { setIndex, getIndex } = Components2.Controller;
const { orient } = Components2.Direction;
const { toggle } = Components2.Elements;
const { listSize } = Components2.Layout;
const { Live } = Components2;
const { root } = Splide2;
const throttledUpdateArrows = Throttle(Components2.Arrows.update, 500);
Expand All @@ -386,9 +387,14 @@ function AutoScroll(Splide2, Components2, options) {
let focused;
let busy;
let currPosition;
let baseTime;
let basePosition;
let lastRenderTime;
let msPerFrame;
function setup() {
const { autoScroll } = options;
autoScrollOptions = assign({}, DEFAULTS, isObject(autoScroll) ? autoScroll : {});
msPerFrame = autoScrollOptions.fpsLock ? 1e3 / autoScrollOptions.fpsLock : void 0;
}
function mount() {
if (!Splide2.is(FADE)) {
Expand Down Expand Up @@ -464,6 +470,9 @@ function AutoScroll(Splide2, Components2, options) {
Live.disable(true);
focused = hovered = stopped = false;
updateButton();
baseTime = Date.now();
basePosition = getPosition();
lastRenderTime = void 0;
}
}
function pause(stop = true) {
Expand All @@ -484,6 +493,8 @@ function AutoScroll(Splide2, Components2, options) {
function move() {
const position = getPosition();
const destination = computeDestination(position);
if (destination === null)
return;
if (position !== destination) {
translate(destination);
updateIndex(currPosition = getPosition());
Expand All @@ -496,8 +507,28 @@ function AutoScroll(Splide2, Components2, options) {
throttledUpdateArrows();
}
function computeDestination(position) {
const currentTimestamp = Date.now();
if (autoScrollOptions.fpsLock && lastRenderTime && currentTimestamp - lastRenderTime < msPerFrame / 2) {
return null;
}
const speed = autoScrollOptions.speed || 1;
position += orient(speed);
const virtualViewportSize = autoScrollOptions.virtualViewportSize || 1e3;
const realViewportSize = listSize();
const virtualToRealScale = realViewportSize / virtualViewportSize;
const speedScale = autoScrollOptions.virtualSpeed ? virtualToRealScale : 1;
if (autoScrollOptions.fpsLock) {
const timePassed = currentTimestamp - baseTime;
const framesPassed = timePassed * autoScrollOptions.fpsLock / 1e3;
const expectedPositionAtPassedFrames = orient(framesPassed * speed * speedScale) + basePosition;
if (expectedPositionAtPassedFrames !== position) {
position = expectedPositionAtPassedFrames;
lastRenderTime = currentTimestamp;
} else {
return null;
}
} else {
position += orient(speed * speedScale);
}
if (Splide2.is(SLIDE)) {
position = clamp(position, getLimit(false), getLimit(true));
}
Expand Down
40 changes: 37 additions & 3 deletions dist/js/splide-extension-auto-scroll.js

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

2 changes: 1 addition & 1 deletion dist/js/splide-extension-auto-scroll.js.map

Large diffs are not rendered by default.

Loading