Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: smooth sign-in typewriter motion and prevent sensor drag conflicts (#1145)

Gustavo Ye9414c64a6542
diff --git a/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.dom.test.tsx b/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.dom.test.tsx
index d9ab626ed..f66eab0d0 100644
--- a/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.dom.test.tsx
+++ b/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.dom.test.tsx
@@ -25,21 +25,25 @@ function mockMatchMedia({ mobile, reducedMotion = false }: { mobile: boolean; re
describe("SignInMobileTypewriter", () => {
let rafQueue: FrameRequestCallback[] = []
- function flushNextFrame(ts = 16) {
+ let frameTime = 0
+
+ function flushNextFrame() {
+ frameTime += 1000 / 60
const next = rafQueue.shift()
- next?.(ts)
+ next?.(frameTime)
}
function drainFrames() {
let guard = 0
while (rafQueue.length > 0 && guard < 120) {
- flushNextFrame(16 * (guard + 1))
+ flushNextFrame()
guard += 1
}
}
beforeEach(() => {
rafQueue = []
+ frameTime = 0
vi.stubGlobal("requestAnimationFrame", (cb: FrameRequestCallback) => {
rafQueue.push(cb)
return rafQueue.length
@@ -85,6 +89,7 @@ describe("SignInMobileTypewriter", () => {
window.dispatchEvent(tilted)
})
+ act(() => { drainFrames() })
expect(Number.parseFloat(stage.style.getPropertyValue("--tw-ry"))).toBeGreaterThan(-19)
expect(Number.parseFloat(stage.style.getPropertyValue("--tw-rx"))).toBeLessThan(11)
@@ -105,6 +110,105 @@ describe("SignInMobileTypewriter", () => {
expect(Number.parseFloat(stage.style.getPropertyValue("--tw-tx"))).toBeCloseTo(beforeTx, 1)
})
+ function orientation(beta: number, gamma: number) {
+ const event = new Event("deviceorientation")
+ Object.defineProperties(event, { beta: { value: beta }, gamma: { value: gamma } })
+ window.dispatchEvent(event)
+ }
+
+ function motion(x: number, y: number) {
+ const event = new Event("devicemotion")
+ Object.defineProperty(event, "acceleration", { value: { x, y, z: 0 } })
+ window.dispatchEvent(event)
+ }
+
+ function mobileStage() {
+ mockMatchMedia({ mobile: true })
+ const view = render(createElement(SignInMobileTypewriter, { collapsed: false }))
+ const stage = screen.getByTestId("sign-in-mobile-typewriter")
+ vi.spyOn(stage, "getBoundingClientRect").mockReturnValue({
+ x: 0, y: 0, left: 0, top: 0, right: 390, bottom: 240,
+ width: 390, height: 240, toJSON: () => ({}),
+ })
+ return { stage, view }
+ }
+
+ it("coalesces sensor events into one frame and converges without snapping or overshoot", () => {
+ const { stage } = mobileStage()
+ act(() => {
+ orientation(40, 0)
+ for (let i = 1; i <= 30; i += 1) orientation(40, i)
+ })
+ expect(rafQueue).toHaveLength(1)
+ expect(stage.style.getPropertyValue("--tw-ry")).toBe("")
+ act(() => { flushNextFrame() })
+ const first = Number.parseFloat(stage.style.getPropertyValue("--tw-ry"))
+ expect(first).toBeGreaterThan(-20)
+ expect(first).toBeLessThan(-17)
+ act(() => { drainFrames() })
+ expect(Number.parseFloat(stage.style.getPropertyValue("--tw-ry"))).toBe(-4)
+ expect(rafQueue).toHaveLength(0)
+ })
+
+ it("ignores small sensor noise and rejects invalid values", () => {
+ const { stage } = mobileStage()
+ act(() => { orientation(40, 0); drainFrames() })
+ const pose = stage.style.cssText
+ act(() => {
+ for (let i = 0; i < 30; i += 1) orientation(40 + (i % 2 ? 0.4 : -0.4), i % 2 ? 0.4 : -0.4)
+ orientation(Number.NaN, 10)
+ motion(Number.POSITIVE_INFINITY, 8)
+ })
+ expect(rafQueue).toHaveLength(0)
+ expect(stage.style.cssText).toBe(pose)
+ })
+
+ it("gives drag exclusive input and smoothly recalibrates orientation on release", () => {
+ const { stage } = mobileStage()
+ act(() => {
+ orientation(40, 0)
+ fireEvent.pointerDown(stage, { pointerId: 1, clientX: 320, clientY: 60 })
+ drainFrames()
+ })
+ const held = stage.style.cssText
+ act(() => {
+ for (let i = 0; i < 20; i += 1) {
+ orientation(65, -20)
+ motion(i % 2 ? 8 : -8, 5)
+ }
+ drainFrames()
+ })
+ expect(stage.style.cssText).toBe(held)
+ act(() => {
+ fireEvent.pointerUp(stage, { pointerId: 1 })
+ orientation(65, -20)
+ })
+ expect(stage.style.cssText).toBe(held)
+ act(() => { drainFrames() })
+ expect(Number.parseFloat(stage.style.getPropertyValue("--tw-ry"))).toBe(-20)
+ expect(Number.parseFloat(stage.style.getPropertyValue("--tw-rx"))).toBe(10)
+ act(() => { orientation(65, -20) })
+ expect(rafQueue).toHaveLength(0)
+ })
+
+ it("does not animate reduced motion and cancels a pending frame on unmount", () => {
+ mockMatchMedia({ mobile: true, reducedMotion: true })
+ const reduced = render(createElement(SignInMobileTypewriter, { collapsed: false }))
+ const stage = screen.getByTestId("sign-in-mobile-typewriter")
+ act(() => {
+ orientation(40, 0)
+ motion(8, 5)
+ fireEvent.pointerDown(stage, { pointerId: 1, clientX: 320, clientY: 60 })
+ })
+ expect(rafQueue).toHaveLength(0)
+ reduced.unmount()
+ const { view } = mobileStage()
+ act(() => { orientation(40, 0); orientation(55, 15) })
+ expect(rafQueue).toHaveLength(1)
+ view.unmount()
+ expect(cancelAnimationFrame).toHaveBeenCalled()
+ })
+
it("defers iOS sensor permission until releasing the typewriter stage and falls back to touch drag when denied", async () => {
mockMatchMedia({ mobile: true })
const requestPermission = vi.fn().mockResolvedValue("denied")
@@ -132,6 +236,7 @@ describe("SignInMobileTypewriter", () => {
await Promise.resolve()
})
expect(requestPermission).not.toHaveBeenCalled()
+ act(() => { drainFrames() })
expect(Number.parseFloat(stage.style.getPropertyValue("--tw-ry"))).toBeGreaterThan(-19)
await act(async () => {
@@ -140,6 +245,7 @@ describe("SignInMobileTypewriter", () => {
await Promise.resolve()
})
expect(requestPermission).toHaveBeenCalledOnce()
+ act(() => { drainFrames() })
expect(Number.parseFloat(stage.style.getPropertyValue("--tw-ry"))).toBeLessThan(-19)
await act(async () => {
fireEvent.pointerUp(stage, { pointerId: 2 })
diff --git a/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.module.css b/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.module.css
index 21e02bf3e..6f3a80602 100644
--- a/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.module.css
+++ b/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.module.css
@@ -14,7 +14,7 @@
height: 260px;
justify-content: center;
transform: translate3d(var(--tw-tx), var(--tw-ty), 0);
- transition: transform 90ms ease-out;
+ transition: none;
will-change: transform;
}
@@ -31,7 +31,7 @@
0
)
scale(calc(var(--tw-scale) * var(--blob-scale, 0.95)));
- transition: transform 120ms ease-out;
+ transition: none;
}
.typewriter :global(.typewriter-scene) {
@@ -40,7 +40,7 @@
scale3d(var(--tw-scale), var(--tw-scale), var(--tw-scale))
rotateY(var(--tw-ry))
rotateX(var(--tw-rx)) !important;
- transition: transform 90ms ease-out;
+ transition: none;
will-change: transform;
}
diff --git a/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.tsx b/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.tsx
index 90996342d..a18b319b0 100644
--- a/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.tsx
+++ b/src/web/src/app/(auth)/sign-in/sign-in-mobile-typewriter.tsx
@@ -34,6 +34,9 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
velX: 0,
velY: 0,
})
+ const targetRef = useRef({ tiltRx: REST_RX, tiltRy: REST_RY, tiltTx: 0, tiltTy: 0 })
+ const lastFrameRef = useRef<number | null>(null)
+ const resetSensorBaselineRef = useRef<(() => void) | null>(null)
const rafRef = useRef<number | null>(null)
const dragPointerRef = useRef<number | null>(null)
@@ -51,30 +54,41 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
el.style.setProperty("--tw-ty", `${ty.toFixed(2)}px`)
}, [])
- const stepSpring = useCallback(function tick() {
+ const stepSpring = useCallback(function tick(timestamp: number) {
const p = poseRef.current
- p.velX = (p.velX - p.shakeX * 0.22) * 0.82
- p.velY = (p.velY - p.shakeY * 0.22) * 0.82
- p.shakeX += p.velX
- p.shakeY += p.velY
- commitPose()
- if (
- Math.abs(p.shakeX) > 0.08
- || Math.abs(p.shakeY) > 0.08
- || Math.abs(p.velX) > 0.08
- || Math.abs(p.velY) > 0.08
- ) {
- rafRef.current = window.requestAnimationFrame(tick)
- } else {
+ const target = targetRef.current
+ const elapsed = lastFrameRef.current === null ? 1000 / 60 : clamp(timestamp - lastFrameRef.current, 0, 32)
+ lastFrameRef.current = timestamp
+ const blend = 1 - Math.exp(-elapsed / 100)
+ const frame = elapsed / (1000 / 60)
+ let moving = false
+ for (const key of ["tiltRx", "tiltRy", "tiltTx", "tiltTy"] as const) {
+ p[key] += (target[key] - p[key]) * blend
+ if (Math.abs(target[key] - p[key]) < 0.01) p[key] = target[key]
+ else moving = true
+ }
+ p.velX = (p.velX - p.shakeX * 0.22 * frame) * Math.pow(0.82, frame)
+ p.velY = (p.velY - p.shakeY * 0.22 * frame) * Math.pow(0.82, frame)
+ p.shakeX += p.velX * frame
+ p.shakeY += p.velY * frame
+ if (Math.max(Math.abs(p.shakeX), Math.abs(p.shakeY), Math.abs(p.velX), Math.abs(p.velY)) < 0.08) {
p.shakeX = 0
p.shakeY = 0
p.velX = 0
p.velY = 0
+ } else moving = true
+ commitPose()
+ if (moving) rafRef.current = window.requestAnimationFrame(tick)
+ else {
rafRef.current = null
- commitPose()
+ lastFrameRef.current = null
}
}, [commitPose])
+ const schedulePose = useCallback(() => {
+ if (rafRef.current === null) rafRef.current = window.requestAnimationFrame(stepSpring)
+ }, [stepSpring])
+
const attachSensors = useCallback(() => {
if (!sensorsActiveRef.current || sensorsAttachedRef.current || typeof window === "undefined") return
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return
@@ -84,6 +98,17 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
let baselineGamma: number | null = null
let prevRawX: number | null = null
let prevRawY: number | null = null
+ let acceptedBeta: number | null = null
+ let acceptedGamma: number | null = null
+ let lastImpulse = -Infinity
+ resetSensorBaselineRef.current = () => {
+ baselineBeta = null
+ baselineGamma = null
+ acceptedBeta = null
+ acceptedGamma = null
+ prevRawX = null
+ prevRawY = null
+ }
const onOrientation = (event: DeviceOrientationEvent) => {
if (typeof event.beta !== "number" || typeof event.gamma !== "number") return
@@ -93,16 +118,22 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
baselineBeta = event.beta
baselineGamma = event.gamma
}
+ if (acceptedBeta !== null && acceptedGamma !== null
+ && Math.abs(event.beta - acceptedBeta) < 0.5
+ && Math.abs(event.gamma - acceptedGamma) < 0.5) return
+ acceptedBeta = event.beta
+ acceptedGamma = event.gamma
const dGamma = clamp(event.gamma - baselineGamma, -30, 30)
const dBeta = clamp(event.beta - baselineBeta, -25, 25)
- poseRef.current.tiltRy = REST_RY + (dGamma / 30) * 16
- poseRef.current.tiltRx = REST_RX - (dBeta / 25) * 12
- poseRef.current.tiltTx = (dGamma / 30) * 16
- poseRef.current.tiltTy = (dBeta / 25) * 11
- commitPose()
+ targetRef.current.tiltRy = REST_RY + (dGamma / 30) * 16
+ targetRef.current.tiltRx = REST_RX - (dBeta / 25) * 12
+ targetRef.current.tiltTx = (dGamma / 30) * 16
+ targetRef.current.tiltTy = (dBeta / 25) * 11
+ schedulePose()
}
const onMotion = (event: DeviceMotionEvent) => {
+ if (dragPointerRef.current !== null) return
const linear = event.acceleration
const gravity = event.accelerationIncludingGravity
let dx = 0
@@ -123,12 +154,13 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
prevRawX = gravity.x
prevRawY = gravity.y
}
- if (Math.hypot(dx, dy) < 1.8) return
- poseRef.current.velX = clamp(poseRef.current.velX + dx * 0.9, -15, 15)
- poseRef.current.velY = clamp(poseRef.current.velY + dy * 0.7, -11, 11)
- if (rafRef.current === null) {
- rafRef.current = window.requestAnimationFrame(stepSpring)
- }
+ if (!Number.isFinite(dx) || !Number.isFinite(dy) || Math.hypot(dx, dy) < 1.8) return
+ const now = performance.now()
+ if (now - lastImpulse < 160) return
+ lastImpulse = now
+ poseRef.current.velX = clamp(poseRef.current.velX + dx * 0.25, -4, 4)
+ poseRef.current.velY = clamp(poseRef.current.velY + dy * 0.2, -3, 3)
+ schedulePose()
}
window.addEventListener("deviceorientation", onOrientation)
@@ -137,8 +169,9 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
window.removeEventListener("deviceorientation", onOrientation)
window.removeEventListener("devicemotion", onMotion)
sensorsAttachedRef.current = false
+ resetSensorBaselineRef.current = null
}
- }, [commitPose, stepSpring])
+ }, [schedulePose])
useEffect(() => {
if (!isMobile || collapsed) {
@@ -155,6 +188,8 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
window.cancelAnimationFrame(rafRef.current)
rafRef.current = null
}
+ lastFrameRef.current = null
+ dragPointerRef.current = null
}
}, [attachSensors, collapsed, isMobile])
@@ -207,20 +242,24 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
const updateTouchTilt = (clientX: number, clientY: number) => {
const el = stageRef.current
+ if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) return
if (!el || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return
const rect = el.getBoundingClientRect()
if (rect.width <= 0 || rect.height <= 0) return
const nx = clamp((clientX - (rect.left + rect.width / 2)) / (rect.width / 2), -1, 1)
const ny = clamp((clientY - (rect.top + rect.height / 2)) / (rect.height / 2), -1, 1)
- poseRef.current.tiltRy = REST_RY + nx * 16
- poseRef.current.tiltRx = REST_RX - ny * 12
- poseRef.current.tiltTx = nx * 14
- poseRef.current.tiltTy = ny * 10
- commitPose()
+ targetRef.current.tiltRy = REST_RY + nx * 16
+ targetRef.current.tiltRx = REST_RX - ny * 12
+ targetRef.current.tiltTx = nx * 14
+ targetRef.current.tiltTy = ny * 10
+ schedulePose()
}
const handlePointerDown = (event: PointerEvent<HTMLDivElement>) => {
+ if (dragPointerRef.current !== null || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return
dragPointerRef.current = event.pointerId
+ poseRef.current.velX = 0
+ poseRef.current.velY = 0
event.currentTarget.setPointerCapture?.(event.pointerId)
updateTouchTilt(event.clientX, event.clientY)
}
@@ -233,15 +272,16 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
const handlePointerEnd = (event: PointerEvent<HTMLDivElement>) => {
if (dragPointerRef.current !== event.pointerId) return
dragPointerRef.current = null
+ resetSensorBaselineRef.current?.()
if (event.type === "pointerup") unlockSensorsOnGesture()
if (event.currentTarget.hasPointerCapture?.(event.pointerId)) {
event.currentTarget.releasePointerCapture(event.pointerId)
}
- poseRef.current.tiltRy = REST_RY
- poseRef.current.tiltRx = REST_RX
- poseRef.current.tiltTx = 0
- poseRef.current.tiltTy = 0
- commitPose()
+ targetRef.current.tiltRy = REST_RY
+ targetRef.current.tiltRx = REST_RX
+ targetRef.current.tiltTx = 0
+ targetRef.current.tiltTy = 0
+ schedulePose()
}
return (
@@ -254,6 +294,7 @@ export function SignInMobileTypewriter({ collapsed }: { collapsed: boolean }) {
onPointerMove={handlePointerMove}
onPointerUp={handlePointerEnd}
onPointerCancel={handlePointerEnd}
+ onLostPointerCapture={handlePointerEnd}
className={cn(
styles.stage,
"relative flex w-full touch-none items-center justify-center transition-opacity duration-200 select-none sm:hidden",