Bypassing iOS Safari's 60 FPS Limit to Build Web Applications That Feel Native
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Bypassing iOS Safari’s 60 FPS Limit to Build Web Applications That Feel Native
这篇文章是关于 iOS 的体验。
You can still read it here, but it works best in Safari on an iPhone. Scan this code to open it there.
你仍然可以在这里阅读,但在 iPhone 的 Safari 浏览器中效果最佳。扫描此代码即可在那里打开。
Rida F’kih · November 20, 2026 · 10 min read
Rida F’kih · 2026年11月20日 · 10分钟阅读
Devon Govett of React Aria posted “Scrolling is All You Need: Building a Swipeable Sheet” back in October, and in it he talked about his team developing a sheet component that cleverly leverages the physics and smooth interactions provided by native browsers to provide the illusion of a native interaction.
React Aria 的 Devon Govett 在十月份发布了《Scrolling is All You Need: Building a Swipeable Sheet》,文中他谈到其团队开发了一个面板组件,巧妙地利用了原生浏览器提供的物理效果和流畅交互,从而营造出原生交互的错觉。
More recently, Jordan Walke, creator of React, posted a video of an experiment on X showing a convincing native-like mobile interaction using DOM & CSS. It re-established a long-lived itch that I’ve had, and prompted this response from me. It’s something I could never justify building, however. Anything I built using this method would be pegged to 60 FPS.
最近,React 的创建者 Jordan Walke 在 X 上发布了一个实验视频,展示了使用 DOM 和 CSS 实现的令人信服的原生移动端交互。这重新勾起了我长久以来的心结,并促使我写下了这篇回应。然而,这是我无法证明其合理性的东西。我用这种方法构建的任何东西都会被限制在 60 FPS。
What the video Jordan Walke posted doesn’t show is that, unless you flip the “Prefer Page Rendering Updates near 60fps” flag buried deep within the Safari developer settings on iOS, WebKit limits requestAnimationFrame to just 60 Hz on a 120 Hz screen, and on a regular page, touch and pointer events are held to that same rate. This is what makes even the best mobile websites feel irritating to use.
Jordan Walke 发布的视频中没有展示的是:除非你开启 iOS Safari 开发者设置深处隐藏的“Prefer Page Rendering Updates near 60fps”(优先在 60fps 附近进行页面渲染更新)标志,否则 WebKit 会将 120 Hz 屏幕上的 requestAnimationFrame 限制在 60 Hz,而在普通页面上,触摸和指针事件也会被限制在相同的频率。这就是为什么即使是最好的移动网站用起来也让人感到烦躁。
How can I make this experience suck less without resorting to flags? Let’s call back to the React Aria piece. The reason it feels so nice is that scrolling is the responsibility of the compositor. It runs in its own thread. It moves, clips and fades without a JavaScript round-trip.
如何在不依赖标志的情况下让这种体验不那么糟糕?让我们回顾一下 React Aria 的文章。它感觉如此舒适的原因在于滚动是由合成器(compositor)负责的。它在自己的线程中运行。它在移动、裁剪和淡入淡出时无需 JavaScript 的往返处理。
Finger to digitizer, digitizer to compositor, compositor to GPU. This will become the basis of our interaction.
手指到数字化仪,数字化仪到合成器,合成器到 GPU。这将成为我们交互的基础。
Behold, two grids. They both move a square when you drag. The one on the left uses JavaScript events and sets the transform attribute in CSS, while the one on the right simply places a square in a scrollable area.
看,这里有两个网格。当你拖动时,它们都会移动一个方块。左边的使用 JavaScript 事件并设置 CSS 中的 transform 属性,而右边的只是将一个方块放置在可滚动区域内。
If you’re on iOS Safari, you’ll immediately be able to tell which is which because one refreshes at most 60 times a second in a renderer pass, while the other uses the compositor.
如果你在 iOS Safari 上,你一眼就能看出哪个是哪个,因为其中一个在渲染器通道中每秒最多刷新 60 次,而另一个则使用了合成器。
In order to leverage the power of the scroll area, we need to find a way to read the scroll position outside of the renderer. If we can do that, we can treat it like a trackpad, or use it to “extract” native bounce, physics, momentum, etc., and apply them directly from the compositor to our animations.
为了利用滚动区域的强大功能,我们需要找到一种在渲染器之外读取滚动位置的方法。如果我们能做到这一点,我们就可以把它当作触控板来处理,或者利用它来“提取”原生的回弹、物理效果、惯性等,并将它们直接从合成器应用到我们的动画中。
The point is, it’d be crazy flexible if we could just find a way to pull the values out of it frequently enough.
重点是,如果我们能找到一种方法足够频繁地从中提取数值,那将会有极大的灵活性。
I measured scroll events updating 5 to 26 times per second during momentum scroll on an iPhone Duo. The best case scenario is during slow, deliberate scrolls where it reaches almost 60 updates a second. Useless.
我在 iPhone Duo 上测量了惯性滚动期间滚动事件的更新频率,为每秒 5 到 26 次。最好的情况是在缓慢、刻意的滚动过程中,每秒能达到近 60 次更新。毫无用处。
Instead, we’ll be aiming to read scrollTop and scrollLeft synchronously.
相反,我们的目标是同步读取 scrollTop 和 scrollLeft。
First, I tried having a MessageChannel call itself recursively, which worked: but it produced 50,000 reads a second with just 80 distinct values. That’s a lot of waste and heat since it pins a CPU core.
首先,我尝试让 MessageChannel 递归调用自身,这奏效了:但它每秒产生 50,000 次读取,却只有 80 个不同的值。这造成了大量的浪费和发热,因为它占用了 CPU 的一个核心。
Next failure was trying to use Atomics to wake at the screen’s refresh rate, which worked and similarly extracted 80 distinct positions every second, but the drawbacks were too great. Since it needs a SharedArrayBuffer, we’re forced to use COOP/COEP headers which break cross-origin assets on our website.
接下来的失败尝试是使用 Atomics 在屏幕刷新率下唤醒,这虽然奏效且同样每秒提取出 80 个不同的位置,但缺点太大。因为它需要 SharedArrayBuffer,我们被迫使用 COOP/COEP 标头,这会破坏我们网站上的跨域资源。
What finally worked was using a MessageChannel like before, throttling the next measurement to the next tick using a setTimeout according to the refresh rate of the device, phase-locked to vsync using requestAnimationFrame timestamps. I was nervous about this approach, but as you’ll see later, it panned out well. The only remaining drawback specific to this solution was easy to work around. iOS rounds performance.now() to the millisecond, so I derived the minimum time between draws from the refresh interval.
最终奏效的方法是像之前一样使用 MessageChannel,根据设备的刷新率使用 setTimeout 将下一次测量限制在下一个时钟周期,并使用 requestAnimationFrame 时间戳与垂直同步(vsync)进行锁相。我对这种方法感到紧张,但正如你稍后将看到的,它效果很好。该方案唯一剩下的缺点很容易解决。iOS 将 performance.now() 四舍五入到毫秒,所以我从刷新间隔中推导出了绘制之间的最小时间。
Each solution got us 80 position reads per second. Two-thirds towards our goal of 120 Hz input.
每个解决方案都让我们每秒获得 80 次位置读取。距离我们 120 Hz 输入的目标还差三分之二。
Safari’s UI process sends the web process one scroll position at a time, and waits for an acknowledgement from the web process before sending another. Anything that arrives in the meantime is overwritten. This was costing us dropped positions. Digging through WebKit yielded what we needed to get around this. It turns out that on every tick where the page has a rendering update queued, Safari flushes the position without waiting. An empty requestAnimationFrame loop is all we need to do that.
Safari 的 UI 进程一次向 Web 进程发送一个滚动位置,并等待 Web 进程的确认后再发送下一个。在此期间到达的任何内容都会被覆盖。这导致我们丢失了位置信息。深入研究 WebKit 后,我们找到了绕过此问题的方法。事实证明,在页面排队等待渲染更新的每一个时钟周期,Safari 都会在不等待的情况下刷新位置。我们只需要一个空的 requestAnimationFrame 循环就能做到这一点。
This bought us ~12.5% more reads, and brought us to 90 unique position reads per second.
这为我们增加了约 12.5% 的读取次数,使我们达到了每秒 90 次唯一位置读取。
I sort of assumed I had hit the limit of the aforementioned IPC, but it turns out that ProMotion was just deciding that there’s no need to render at a refresh rate higher than 90 Hz.
我原以为我已经达到了上述 IPC 的极限,但事实证明,ProMotion 只是认为没有必要以高于 90 Hz 的刷新率进行渲染。
Squeezing out the last 30 Hz was trickier. A GPU-accelerated CSS animation lets the screen go to 120 Hz, but only while something is scrolling. So a tiny invisible scroller smooth-scrolls in the background, ready to boost us to the full ProMotion refresh rate whenever we need it.
挤出最后 30 Hz 更棘手。GPU 加速的 CSS 动画可以让屏幕达到 120 Hz,但仅在滚动时有效。因此,一个微小的不可见滚动器在后台平滑滚动,随时准备在我们需要时将我们提升到完整的 ProMotion 刷新率。
Of course, we don’t want the display pegging to 120 Hz the entire time, so we reserve all the tricks we used to get there for times we expect animations to occur: touch events, scroll-derived animations, etc.
当然,我们不希望显示器一直保持在 120 Hz,所以我们将所有实现这一目标的技巧保留在预期动画发生的时候:触摸事件、滚动派生动画等。
If the interaction you’re building doesn’t need momentum, rubber banding, or snapping, you can forego them entirely and just use a passive touchmove listener on the controller which fires at the full 120 Hz.
如果你构建的交互不需要惯性、回弹或吸附效果,你可以完全放弃它们,只需在控制器上使用一个被动的 touchmove 监听器,它会以完整的 120 Hz 触发。
Just like that, we’re receiving updates from the controller at a full 120 Hz! Drag the grid below to see.
就这样,我们以完整的 120 Hz 从控制器接收更新!拖动下方的网格即可查看。
One issue remains. scrollTop and scrollLeft are rounded to the nearest integer even though iOS scrolls with sub-point precision. Easy fix, thankfully. We just scale the scrollable surface up by the device pixel ratio, then its container back down by its reciprocal.
还有一个问题。尽管 iOS 的滚动具有亚像素精度,但 scrollTop 和 scrollLeft 还是被四舍五入为整数。谢天谢地,这很容易修复。我们只需将可滚动表面按设备像素比(DPR)放大,然后将其容器按其倒数缩小即可。
If you want to use the viewport itself as a controller, you can choose to invasively set the initial-scale in the same way, but this comes with some caveats. You’ll have to scale units by the DPR, disable text autosizing, and manually scale safe areas. You’ll also get tiny text-s
如果你想将视口本身用作控制器,你可以选择以同样的方式侵入式地设置 initial-scale,但这有一些注意事项。你必须按 DPR 缩放单位,禁用文本自动调整大小,并手动缩放安全区域。你还会得到微小的文本-s