Skip to content

Commit

Permalink
Manually resolving startTime
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry authored and mergatron[bot] committed Sep 4, 2023
1 parent e6a41ee commit d0bd195
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
15 changes: 8 additions & 7 deletions dev/optimized-appear/resync.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@
*/
onAnimationStart: () => {
frame.postRender(() => {
const box = document.getElementById("box")
frame.postRender(() => {
const box = document.getElementById("box")

if (!box) return
if (!box) return

const { opacity } = window.getComputedStyle(box)

if (parseFloat(opacity) < 0.7) {
showError(box, "Resync failed")
}
const { opacity } = window.getComputedStyle(box)
if (parseFloat(opacity) < 0.65) {
showError(box, "Resync failed")
}
})
})
},
[optimizedAppearDataAttribute]: "a",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EasingDefinition } from "../../../easing/types"
import { frame, cancelFrame } from "../../../frameloop"
import { frame, cancelFrame, frameData } from "../../../frameloop"
import type { VisualElement } from "../../../render/VisualElement"
import type { MotionValue } from "../../../value"
import { AnimationPlaybackControls, ValueAnimationOptions } from "../../types"
Expand Down Expand Up @@ -136,6 +136,18 @@ export function createAcceleratedAnimation(
}
)

/**
* WAAPI animations don't resolve startTime synchronously. But a blocked
* thread could delay the startTime resolution by a noticeable amount.
* For synching handoff animations with the new Motion animation we want
* to ensure startTime is synchronously set.
*/
if (options.syncStart) {
animation.startTime = frameData.isProcessing
? frameData.timestamp
: performance.now()
}

const cancelAnimation = () => animation.cancel()

const safeCancel = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { frame } from "../../frameloop"
import { transformProps } from "../../render/html/utils/transform"
import type { AnimationTypeState } from "../../render/utils/animation-state"
import type { VisualElement } from "../../render/VisualElement"
import type { TargetAndTransition } from "../../types"
import type { TargetAndTransition, Transition } from "../../types"
import { optimizedAppearDataAttribute } from "../optimized-appear/data-id"
import type { VisualElementAnimationOptions } from "./types"
import { animateMotionValue } from "./motion-value"
Expand Down Expand Up @@ -62,7 +62,12 @@ export function animateTarget(
continue
}

const valueTransition = { delay, elapsed: 0, ...transition }
const valueTransition = {
delay,
syncStart: false,
elapsed: 0,
...transition,
}

/**
* If this is the first time a value is being animated, check
Expand All @@ -79,6 +84,7 @@ export function animateTarget(
value,
frame
)
valueTransition.syncStart = true
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/framer-motion/src/animation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface Transition
type?: "decay" | "spring" | "keyframes" | "tween" | "inertia"
duration?: number
autoplay?: boolean
syncStart?: boolean
}

export interface ValueAnimationTransition<V = any>
Expand Down

0 comments on commit d0bd195

Please sign in to comment.