Skip to content

Commit

Permalink
Renormalize timing on source resize (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesodland authored Nov 9, 2023
1 parent daef204 commit 5dc6e27
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions demo/view-timeline/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
overflow-y: scroll;
position: relative;
align-items: flex-start;
resize: both;
}

#container.rtl {
Expand Down
13 changes: 9 additions & 4 deletions src/proxy-animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ function playInternal(details, autoRewind) {

// Additional step for the polyfill.
addAnimation(details.timeline, details.animation,
tickAnimation.bind(details.proxy));
tickAnimation.bind(details.proxy), renormalizeTiming.bind(details.proxy));

// 7. If animation's hold time is resolved, let its start time be
// unresolved.
Expand Down Expand Up @@ -601,6 +601,12 @@ function tickAnimation(timelineTime) {
}
}

function renormalizeTiming() {
const details = proxyAnimations.get(this);
// Force renormalization.
details.specifiedTiming = null;
}

function notifyReady(details) {
if (details.pendingTask == 'pause') {
commitPendingPause(details);
Expand Down Expand Up @@ -747,8 +753,7 @@ function createProxyEffect(details) {
target.apply(effect, [details.specifiedTiming]);
}
target.apply(effect, argumentsList);
// Force renormalization.
details.specifiedTiming = null;
renormalizeTiming()
}
};
const proxy = new Proxy(effect, handler);
Expand Down Expand Up @@ -935,7 +940,7 @@ export class ProxyAnimation {
// Additional polyfill step needed to associate the animation with
// the scroll timeline.
addAnimation(details.timeline, details.animation,
tickAnimation.bind(this));
tickAnimation.bind(this), renormalizeTiming.bind(this));
break;

// If previous play state is paused:
Expand Down
26 changes: 20 additions & 6 deletions src/scroll-timeline-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ function updateSource(timeline, source) {
if (oldSource == source)
return;

if (oldSource && oldScrollListener) {
scrollEventSource(oldSource).removeEventListener("scroll",
oldScrollListener);
if (oldSource) {
details.resizeObserver.unobserve(oldSource);
if (oldScrollListener) {
scrollEventSource(oldSource).removeEventListener("scroll", oldScrollListener);
}
}
scrollTimelineOptions.get(timeline).source = source;
if (source) {
Expand All @@ -173,6 +175,7 @@ function updateSource(timeline, source) {
};
scrollEventSource(source).addEventListener("scroll", listener);
details.scrollListener = listener;
details.resizeObserver.observe(source)
}
}

Expand All @@ -196,8 +199,9 @@ export function removeAnimation(scrollTimeline, animation) {
* @param scrollTimeline {ScrollTimeline}
* @param animation {Animation}
* @param tickAnimation {function(number)}
* @param renormalizeTiming {function()}
*/
export function addAnimation(scrollTimeline, animation, tickAnimation) {
export function addAnimation(scrollTimeline, animation, tickAnimation, renormalizeTiming) {
let animations = scrollTimelineOptions.get(scrollTimeline).animations;
for (let i = 0; i < animations.length; i++) {
// @TODO: This early return causes issues when a page with the polyfill
Expand All @@ -211,11 +215,20 @@ export function addAnimation(scrollTimeline, animation, tickAnimation) {

animations.push({
animation: animation,
tickAnimation: tickAnimation
tickAnimation: tickAnimation,
renormalizeTiming: renormalizeTiming
});
updateInternal(scrollTimeline);
}

function renormalizeAnimationTimings(scrollTimeline) {
let animations = scrollTimelineOptions.get(scrollTimeline).animations;
for (const animation of animations) {
animation.renormalizeTiming()
updateInternal(scrollTimeline)
}
}

// TODO: this is a private function used for unit testing add function
export function _getStlOptions(scrollTimeline) {
return scrollTimelineOptions.get(scrollTimeline);
Expand All @@ -235,7 +248,8 @@ export class ScrollTimeline {

// Internal members
animations: [],
scrollListener: null
scrollListener: null,
resizeObserver: new ResizeObserver(() => renormalizeAnimationTimings(this))
});
const source =
options && options.source !== undefined ? options.source
Expand Down

0 comments on commit 5dc6e27

Please sign in to comment.