Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): a channel rebuilt after a late first frame carries on from the screen (#9637)

Christopher Chedeau240e8c54a54d
diff --git a/.changeset/animator-carry-on.md b/.changeset/animator-carry-on.md
new file mode 100644
index 0000000000..100a0ececf
--- /dev/null
+++ b/.changeset/animator-carry-on.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[chore] Internal: the touch presentations' animator carries a channel on from the screen when a rebase, a set or a retarget that starts now rebuilds it after its call's first frame came late and left the start unread. The channel leaves that call with a start of its own, dated from that frame, instead of starting its curve again; the call's other channels keep waiting for the browser's start. Not exported from the package entry point.
+
+@vjeux
diff --git a/packages/core/src/motion/animator.test.ts b/packages/core/src/motion/animator.test.ts
index 23cd42add7..f65cb41dd3 100644
--- a/packages/core/src/motion/animator.test.ts
+++ b/packages/core/src/motion/animator.test.ts
@@ -942,67 +942,85 @@ describe('a call from rest starts at the commit, as Core Animation does', () =>
expect(animator.get('x')).toBeCloseTo(curve.value(0.11), 9);
});
- it('a rebase after a late first frame reads what the rebuilt animation shows, from the first value it starts at the frame that commits it', async () => {
+ it('a rebase after a late first frame carries the channel on from that frame, with a start of its own', async () => {
const {element, animator} = setup();
animator.animate({x: 300}, {spring: springs.settle});
clock = 1070;
await commitFrame(1070, {startUnread: true});
clock = 1170;
+ const shown = animator.get('x');
+ expect(shown).toBeCloseTo(curve.value(0.1), 1);
animator.rebase({x: -16});
const rebuilt = live('translate').at(-1) as Recorded;
- expect(rebuilt.startTime).toBeNull();
- // Until the frame that commits it, the screen shows its first keyframe.
- // The inline value and the hit-test origin show it too.
+ // The compositor has x moving since that frame, so the rebuilt animation
+ // starts now, from the value on screen in the new coordinates.
+ expect(rebuilt.startTime).toBe(1170);
const first = parse(rebuilt.keyframes[0].translate)[0];
+ expect(first).toBeCloseTo(shown - 16, 1);
expect(animator.get('x')).toBeCloseTo(first, 1);
expect(inlineX(element)).toBeCloseTo(first, 1);
expect(presentedOffsetOf(element).x).toBeCloseTo(0, 1);
- // It starts at that frame, which bounds its start again.
clock = 1186;
await commitFrame(1186, {startUnread: true});
clock = 1202;
- const shown = sampleAt(rebuilt, 'translate', rebuilt.createdAt + 16)[0];
- expect(animator.get('x')).toBeCloseTo(shown, 1);
+ const later = sampleAt(rebuilt, 'translate', 1202)[0];
+ expect(animator.get('x')).toBeCloseTo(later, 1);
animator.animate({x: 400}, {spring: springs.settle});
const retarget = live('translate').at(-1) as Recorded;
- expect(sampleAt(retarget, 'translate', 1202)[0]).toBeCloseTo(shown, 1);
+ expect(sampleAt(retarget, 'translate', 1202)[0]).toBeCloseTo(later, 1);
});
- it('a set of the other axis after a late first frame reads what the rebuilt animation shows, from the first value it starts at the frame that commits it', async () => {
+ it('a set of the other axis after a late first frame carries x on from that frame', async () => {
const {element, animator} = setup();
animator.animate({x: 300}, {spring: springs.settle});
clock = 1070;
await commitFrame(1070, {startUnread: true});
clock = 1170;
+ const shown = animator.get('x');
+ const speed = animator.getVelocity('x');
animator.set({y: 10});
const rebuilt = live('translate').at(-1) as Recorded;
- expect(rebuilt.startTime).toBeNull();
- const first = parse(rebuilt.keyframes[0].translate)[0];
- expect(animator.get('x')).toBeCloseTo(first, 1);
- expect(inlineX(element)).toBeCloseTo(first, 1);
+ expect(rebuilt.startTime).toBe(1170);
+ const [x, y] = parse(rebuilt.keyframes[0].translate);
+ expect(x).toBeCloseTo(shown, 1);
+ expect(y).toBeCloseTo(10, 1);
+ expect(animator.get('x')).toBeCloseTo(shown, 1);
+ expect(animator.getVelocity('x')).toBeCloseTo(speed, 0);
+ expect(inlineX(element)).toBeCloseTo(shown, 1);
expect(presentedOffsetOf(element).x).toBeCloseTo(0, 1);
clock = 1186;
await commitFrame(1186, {startUnread: true});
clock = 1202;
- const shown = sampleAt(rebuilt, 'translate', rebuilt.createdAt + 16)[0];
- expect(animator.get('x')).toBeCloseTo(shown, 1);
+ expect(animator.get('x')).toBeCloseTo(
+ sampleAt(rebuilt, 'translate', 1202)[0],
+ 1,
+ );
});
- it('a retarget of the other axis without a clock after a late first frame reads the first value the adopted curve starts from', async () => {
+ it('a retarget of the other axis without a clock after a late first frame carries x on from that frame', async () => {
const {element, animator} = setup();
animator.animate({x: 300}, {spring: springs.settle});
clock = 1070;
await commitFrame(1070, {startUnread: true});
clock = 1170;
+ const shown = animator.get('x');
animator.animate({y: 50}, {spring: springs.settle});
const rebuilt = live('translate').at(-1) as Recorded;
- expect(rebuilt.startTime).toBeNull();
- // The new call takes the x curve and starts it again from its first
- // keyframe. The reads, the inline value and the hit-test origin show it.
- const first = parse(rebuilt.keyframes[0].translate)[0];
- expect(animator.get('x')).toBeCloseTo(first, 1);
- expect(inlineX(element)).toBeCloseTo(first, 1);
+ // The element is moving on screen, so the call starts now: x carries
+ // on from the screen and y starts from rest.
+ expect(rebuilt.startTime).toBe(1170);
+ const [x, y] = parse(rebuilt.keyframes[0].translate);
+ expect(x).toBeCloseTo(shown, 1);
+ expect(y).toBeCloseTo(0, 1);
+ expect(animator.get('x')).toBeCloseTo(shown, 1);
+ expect(inlineX(element)).toBeCloseTo(shown, 1);
expect(presentedOffsetOf(element).x).toBeCloseTo(0, 1);
+ clock = 1186;
+ await commitFrame(1186, {startUnread: true});
+ clock = 1202;
+ const [laterX, laterY] = sampleAt(rebuilt, 'translate', 1202);
+ expect(animator.get('x')).toBeCloseTo(laterX, 1);
+ expect(animator.get('y')).toBeCloseTo(laterY, 1);
});
it('a retarget that joins a call by its clock after a late first frame reads the first value the rebuilt animation starts from', async () => {
@@ -1044,6 +1062,54 @@ describe('a call from rest starts at the commit, as Core Animation does', () =>
expect(animator.get('y')).toBeCloseTo(shown[1], 1);
});
+ it('a call whose only channel carries on after a late first frame reports that frame as its start', async () => {
+ const {animator} = setup();
+ const handle = animator.animate({x: 300}, {spring: springs.settle});
+ clock = 1070;
+ await commitFrame(1070, {startUnread: true});
+ clock = 1170;
+ animator.rebase({x: -16});
+ await expect(handle.started).resolves.toBe(1070);
+ });
+
+ it('a rebase of a coast after a late first frame keeps its course from that frame', async () => {
+ const {animator} = setup();
+ animator.decay(coast(animator, {x: 1000}));
+ clock = 1070;
+ await commitFrame(1070, {startUnread: true});
+ clock = 1170;
+ const shown = animator.get('x');
+ animator.rebase({x: -16});
+ const rebuilt = live('translate').at(-1) as Recorded;
+ expect(rebuilt.startTime).toBe(1170);
+ expect(parse(rebuilt.keyframes[0].translate)[0]).toBeCloseTo(
+ shown - 16,
+ 1,
+ );
+ clock = 1202;
+ expect(animator.get('x')).toBeCloseTo(
+ sampleAt(rebuilt, 'translate', 1202)[0],
+ 1,
+ );
+ });
+
+ it('a channel whose call waits past its undated end after a late first frame still carries on from the screen', async () => {
+ const {animator} = setup();
+ animator.animate({x: 300}, {spring: springs.settle});
+ clock = 1070;
+ await commitFrame(1070, {startUnread: true});
+ // Past the curve's end counted from the call's own time, and short of
+ // its end counted from that frame.
+ const duration =
+ Number(live('translate')[0].options.duration) - END_HOLD_MS;
+ clock = 1000 + duration + 30;
+ const shown = animator.get('x');
+ expect(shown).toBeLessThan(300);
+ animator.set({y: 10});
+ const rebuilt = live('translate').at(-1) as Recorded;
+ expect(parse(rebuilt.keyframes[0].translate)[0]).toBeCloseTo(shown, 1);
+ });
+
it('a late first frame that leaves the start unread: a read of a later time takes the curve from that frame, as the velocity does', async () => {
const {animator} = setup();
animator.animate({x: 300}, {spring: springs.settle});
diff --git a/packages/core/src/motion/animator.ts b/packages/core/src/motion/animator.ts
index 3f5585890a..8d39ccc5d7 100644
--- a/packages/core/src/motion/animator.ts
+++ b/packages/core/src/motion/animator.ts
@@ -1052,7 +1052,7 @@ export function createAnimator(
if (segment == null) {
return null;
}
- if (time >= segment.start + segment.duration) {
+ if (time >= endOf(property, segment)) {
return null;
}
return segment;
@@ -1089,6 +1089,53 @@ export function createAnimator(
? null
: {latest, start: segment.start + latest - segment.group.provisional};
+ /**
+ * When a waiting curve's call had its first frame, which put the curve on
+ * screen. Undefined for a curve that does not wait, or whose call has had
+ * no first frame.
+ */
+ const shownSince = (
+ property: MotionProperty,
+ segment: Segment,
+ ): number | undefined =>
+ segment.group?.resolved === false
+ ? segment.group.latest?.get(CHANNEL_OF[property])
+ : undefined;
+
+ /** A curve's end, dated from its call's first frame while it waits. */
+ const endOf = (property: MotionProperty, segment: Segment): number =>
+ (datedFromLatest(segment, shownSince(property, segment))?.start ??
+ segment.start) + segment.duration;
+
+ /**
+ * Takes a curve on screen out of the call that still waits for the
+ * browser's start. The curve keeps its course, dated from the call's first
+ * frame. A call that has no channel left starts at that frame.
+ */
+ const leaveWaitingCall = (
+ segment: Segment,
+ channel: Channel,
+ latest: number,
+ ) => {
+ const group = segment.group;
+ if (group == null) {
+ return;
+ }
+ const index = group.segments.indexOf(segment);
+ if (index >= 0) {
+ group.segments.splice(index, 1);
+ }
+ segment.start += latest - group.provisional;
+ segment.group = undefined;
+ if (!CHANNELS[channel].some(p => segments.get(p)?.group === group)) {
+ group.latest?.delete(channel);
+ group.channels.delete(channel);
+ }
+ if (group.channels.size === 0 && group.segments.length === 0) {
+ resolveGroup(group, latest);
+ }
+ };
+
/**
* The value on screen at `time`. Until the browser reports its start, a
* held curve shows its first value. A frame that ran without that start
@@ -1316,12 +1363,25 @@ export function createAnimator(
// Settled curves become resting values.
for (const property of properties) {
const segment = segments.get(property);
- if (segment != null && time >= segment.start + segment.duration) {
+ if (segment != null && time >= endOf(property, segment)) {
resting[property] = segment.target;
segments.delete(property);
}
}
const moving = properties.filter(p => segments.has(p));
+ if (callGroup == null) {
+ // A call that starts now rebuilds a channel the compositor has moving
+ // since a first frame without a known start. The channel carries on
+ // from that frame, with a start of its own, instead of starting its
+ // curve again.
+ for (const property of moving) {
+ const segment = segments.get(property) as Segment;
+ const latest = shownSince(property, segment);
+ if (latest != null) {
+ leaveWaitingCall(segment, channel, latest);
+ }
+ }
+ }
// An explicit call can rebuild a channel while one of its curves still
// waits for the browser's start. The animation then joins the current
// call, or else that waiting call, with no start of its own.
@@ -2017,10 +2077,14 @@ export function createAnimator(
for (const channel of start === 'commit' ? [] : channels) {
for (const property of CHANNELS[channel]) {
const segment = segments.get(property);
+ // A call's first frame puts its channels on screen, even before the
+ // browser reports the start. So such a channel is not at rest either.
if (
segment != null &&
- time < segment.start + segment.duration &&
- (segment.group == null || segment.group.resolved)
+ time < endOf(property, segment) &&
+ (segment.group == null ||
+ segment.group.resolved ||
+ shownSince(property, segment) != null)
) {
return null;
}
@@ -3739,9 +3803,11 @@ export function createAnimator(
}
owners.add(segment.owner);
// A curve still waiting for the browser's start stays in its call:
- // it starts with that call's commit.
+ // it starts with that call's commit. A curve whose call had its first
+ // frame is on screen already, so it leaves the call (leaveWaitingCall).
+ const latest = shownSince(property, segment);
const group =
- segment.group != null && !segment.group.resolved
+ latest == null && segment.group != null && !segment.group.resolved
? segment.group
: undefined;
const replace = (next: Segment) => {
@@ -3758,6 +3824,9 @@ export function createAnimator(
if (segment.spring != null) {
const from = valueAt(property, time) + delta;
const velocity = velocityAt(property, time);
+ if (latest != null) {
+ leaveWaitingCall(segment, CHANNEL_OF[property], latest);
+ }
const curve = solveSpring(toSpring(segment.spring), {
from,
to: segment.target,
@@ -3778,6 +3847,9 @@ export function createAnimator(
spring: segment.spring,
});
} else {
+ if (latest != null) {
+ leaveWaitingCall(segment, CHANNEL_OF[property], latest);
+ }
replace({
...segment,
target: segment.target + delta,