Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance during resize #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
47 changes: 32 additions & 15 deletions src/scroll-timeline-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,36 +211,55 @@ export function measureSubject(source, subject) {

/**
* Update measurements of source, and update timelines
* @param {HTMLElement} source
* @param {HTMLElement} source Source element
* @param {boolean} resize True if called in a ResizeObserver callback
*/
function updateMeasurements(source) {
function updateMeasurements(source, resize) {
if (!source) {
return;
}

let details = sourceDetails.get(source);
if (resize) {
// Multiple timelines can invoke `updateMeasurements()` for one source during the resize observation broadcast.
// Once invoked there is no need to update measurements for this source until after the next animation frame.
if (details.resizeMeasured) {
return;
}

details.resizeMeasured = true;
requestAnimationFrame(() => {
details.resizeMeasured = false;
});
}

details.sourceMeasurements = measureSource(source);

// Update measurements of the subject of connected view timelines
for (const ref of details.timelineRefs) {
const timeline = ref.deref();
if ((timeline instanceof ViewTimeline)) {
const timelineDetails = scrollTimelineOptions.get(timeline)
timelineDetails.subjectMeasurements = measureSubject(source, timeline.subject)
const timelineDetails = scrollTimelineOptions.get(timeline);
timelineDetails.subjectMeasurements = measureSubject(source, timeline.subject);
}
}

if (details.updateScheduled)
if (details.updateScheduled) {
// A task has already been scheduled to update timelines and animations
return;
}
details.updateScheduled = true;

// Schedule a task to update timelines after all measurements are completed
setTimeout(() => {
// Schedule a task to update timelines after all measurements are completed
details.updateScheduled = false;
for (const ref of details.timelineRefs) {
const timeline = ref.deref();
if (timeline) {
updateInternal(timeline);
}
}

details.updateScheduled = false;
});
details.updateScheduled = true;
}

function updateSource(timeline, source) {
Expand Down Expand Up @@ -283,9 +302,7 @@ function updateSource(timeline, source) {

// Use resize observer to detect changes to source size
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
updateMeasurements(timelineDetails.source)
}
updateMeasurements(timelineDetails.source, true)
});
resizeObserver.observe(source);
for (const child of source.children) {
Expand All @@ -295,7 +312,7 @@ function updateSource(timeline, source) {
// Use mutation observer to detect updated style attributes on source element
const mutationObserver = new MutationObserver((records) => {
for (const record of records) {
updateMeasurements(record.target);
updateMeasurements(record.target, false);
}
});
mutationObserver.observe(source, {attributes: true, attributeFilter: ['style', 'class']});
Expand Down Expand Up @@ -836,12 +853,12 @@ export class ViewTimeline extends ScrollTimeline {
}
if (details.subject) {
const resizeObserver = new ResizeObserver(() => {
updateMeasurements(details.source)
updateMeasurements(details.source, true)
})
resizeObserver.observe(details.subject)

const mutationObserver = new MutationObserver(() => {
updateMeasurements(details.source);
updateMeasurements(details.source, false);
});
mutationObserver.observe(details.subject, {attributes: true, attributeFilter: ['class', 'style']});
}
Expand Down
Loading