Skip to content

Commit

Permalink
fast forward animations by swap time
Browse files Browse the repository at this point in the history
  • Loading branch information
martrapp committed Sep 7, 2024
1 parent 1f87c9b commit b919aa5
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ElementSpec, Spec } from './types';

const DEBUG = true;
const DEBUG = false;

const crossing = top!.__vtbag?.elementCrossing;
init();
Expand Down Expand Up @@ -29,16 +29,13 @@ function pageSwap() {
}
}

function pageReveal(event: Event) {
function pageReveal() {
let values;
let storage;
if (storage = self.crossingStorage) {
values = storage.getItem('@vtbag/element-crossing') ?? [];
if (self.crossingStorage) {
values = self.crossingStorage.getItem('@vtbag/element-crossing') ?? [];
} else {
storage = top!.sessionStorage;
values = JSON.parse(storage.getItem('@vtbag/element-crossing') ?? '[]');
values = JSON.parse(top!.sessionStorage.getItem('@vtbag/element-crossing') ?? '[]');
}
!("viewTransition" in event && event.viewTransition) && storage.removeItem('@vtbag/element-crossing');
restore(values);
}

Expand Down Expand Up @@ -165,20 +162,20 @@ function elementSpec(element: HTMLElement) {
}
});
if (!id) console.error('[crossing]', 'missing id in', element);
else return { id, timestamp: new Date().getTime(), specs };
else return { id, specs };
}

function restore(values: ElementSpec[]) {
DEBUG && console.log('[crossing]', 'restore', values);
values.forEach((elementSpec: ElementSpec) => {
let element = document.querySelector<HTMLElement>(
'#' +
elementSpec.id +
",[data-vtbag-x*='#" +
elementSpec.id +
"'],[data-vtbag-x*='id:" +
elementSpec.id +
"']"
elementSpec.id +
",[data-vtbag-x*='#" +
elementSpec.id +
"'],[data-vtbag-x*='id:" +
elementSpec.id +
"']"
);
if (element) {
elementSpec.specs.forEach((s) => {
Expand Down Expand Up @@ -219,11 +216,8 @@ function restore(values: ElementSpec[]) {
element
);
}
animations.forEach(
(a) =>
(a.currentTime =
~~(s.value ?? '0') + (new Date().getTime() - elementSpec.timestamp))
);
animations.forEach((a) => (a.currentTime = ~~(s.value ?? '0')));
element!.setAttribute(s.key, s.value ?? '');
break;
case 'elem':
const crossing = top?.__vtbag?.elementCrossing;
Expand Down

0 comments on commit b919aa5

Please sign in to comment.