跳到主内容
@wquguru
精选88Hacker News Best(web_list)技巧与观点

绕过iOS Safari 60Hz限制实现120Hz原生级交互

Bypassing iOS Safari's 60 FPS Limit to Build Web Applications That Feel Native

原文
发到 X
推荐理由

前端工程师必看,这篇详细拆解了如何在iOS Web端榨取120Hz刷新率,从底层IPC机制到具体代码实现都有完整复盘,直接解决移动端Web交互粘滞痛点。

Bypassing iOS Safari’s 60 FPS Limit to Build Web Applications That Feel Native

绕过 iOS Safari 的 60 FPS 限制,构建具有原生体验的 Web 应用

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(原 Twitter)上发布了一段实验视频,展示了使用 DOM 和 CSS 实现的令人信服的类原生移动端交互。这重新点燃了我长久以来的痒处,并促使我做出回应。然而,这是我从未认为值得去构建的东西。任何使用这种方法构建的内容都会被锁定在 60 FPS。

Why the iOS Safari Experience Sucks

为什么 iOS Safari 的体验如此糟糕

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”标志,否则 WebKit 会将 requestAnimationFrame 限制为 120 Hz 屏幕上的 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 的那篇文章。它感觉如此舒适的原因是滚动由合成器负责。它在自己的线程中运行。它无需经过 JavaScript 往返即可进行移动、裁剪和淡入淡出。

Finger to digitizer, digitizer to compositor, compositor to GPU. This will become the basis of our interaction.

手指到数字化仪,数字化仪到合成器,合成器到 GPU。这将成为我们交互的基础。

High Refresh Rate Interactions

高刷新率交互

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 属性,而右边的那个只是将一个方块放置在可滚动区域中。

Try dragging this

尝试拖动这个

Try dragging this

尝试拖动这个

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.

为了利用滚动区域的力量,我们需要找到一种在渲染器之外读取滚动位置的方法。如果我们能做到这一点,我们就可以将其视为触控板,或者用它来“提取”原生的回弹、物理效果、动量等,并直接从合成器应用到我们的动画中。

  • Want a 1:1 trackpad experience? We just measure only when the finger is pressed.
  • Want a true trackball with native momentum? We just make it an infinitely scrollable region.
  • Want native bounce? We configure over-scroll padding and set the bounds of the container.
  • Want a dial with notches? Snap points!
  • 想要 1:1 的触控板体验吗?我们仅在手指按下时进行测量。
  • 想要一个具有原生动量效果的真正轨迹球?我们只需将其设为一个无限滚动的区域。
  • 想要原生的回弹效果?我们配置过滚动填充(over-scroll padding)并设置容器的边界。
  • 想要带有刻度的旋钮?使用吸附点(Snap points)!

The point is, it’d be crazy flexible if we could just find a way to pull the values out of it frequently enough.

关键在于,如果我们能找到一种方法足够频繁地从它中提取数值,那将会非常灵活。

Creating “The Controller”

创建“控制器”

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.

如果你构建的交互不需要动量、弹性回弹或吸附效果,你可以完全放弃这些功能,只需在控制器上使用一个被动(passive)的 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 频率从控制器接收更新!拖动下方的网格查看效果。

Position

位置

0, 0

Updates

更新频率

0/s

Unique values

唯一值数量

0/s

Speed

速度

0 pt/s

Try dragging this

尝试拖动此处

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 支持亚点级(sub-point)精度的滚动,但 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-selection indicators. All trade-offs, but by no means required.

如果你想直接使用视口(viewport)本身作为控制器,你也可以选择以相同的方式侵入性地设置 initial-scale,但这会带来一些注意事项。你必须按 DPR 缩放单位、禁用文本自动调整大小,并手动缩放安全区域。此外,你还会看到极小的文本选择指示器。这些都是权衡取舍,但绝非必需。

Position

位置

0.00, 0.00

Updates

更新频率

0/s

Unique values

唯一值数量

0/s

Speed

速度

0 pt/s

Try dragging this

尝试拖动此处

From here on out, I’ll refer to these scrolling grids as “controllers”, because they act exactly like one. All we have to do is make one invisible and lay it over where we want to read high-frequency input.

从此处开始,我将把这些滚动网格称为“控制器”,因为它们的作用与控制器完全一致。我们要做的只是创建一个不可见的网格,并将其覆盖在我们希望读取高频输入的位置上。

Note that the page itself can also be a controller, but Safari will throttle reported scroll positions to 10 Hz during the URL bar resize that happens when you change scroll direction. WebKit skips this if we give the page a scroll-driven animation that animates a non-GPU-accelerated attribute. I went with transitioning between two transparent colours for this page.

请注意,页面本身也可以充当控制器,但在你改变滚动方向时,Safari 会在 URL 栏调整大小期间将报告的滚动位置节流至 10 Hz。如果我们为页面提供一个对非 GPU 加速属性进行动画处理的滚动驱动动画,WebKit 会跳过此限制。为此,我选择了在该页面上在两种透明颜色之间进行过渡。

Breaking the 60 FPS Render Barrier

突破 60 FPS 渲染瓶颈

Now we get high-frequency input, but the DOM still only updates 60 times a second, so if we tried to leverage it as it stands, we’d just be updating a value twice for every frame that’s drawn. The user would get no benefit.

现在我们获得了高频输入,但 DOM 每秒仍只更新 60 次,因此如果我们直接利用它,对于每一帧的绘制,我们实际上只是在更新两次值。用户将无法从中受益。

The vast majority of dragging interactions are already covered by our controllers, and predefined animations are covered by CSS animations. But what about more complicated interactions? What about computation-derived layouts? What about shaders?

绝大多数拖拽交互已经由我们的控制器覆盖,预定义动画则由 CSS 动画覆盖。但对于更复杂的交互呢?基于计算得出的布局呢?着色器呢?

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件