简体   繁体   English

在 requestAnimationFrame 内进行鼠标拖动的拖动是否有用?

[英]Is it useful to make the drag of mousedragging happening inside requestAnimationFrame?

I have a dragging library which moves the element on every mousemove.我有一个拖动库,可以在每次鼠标移动时移动元素。 As we all know, mousemove fires quite often and thus forces the browser into repaints.众所周知, mousemove 经常触发,从而迫使浏览器重新绘制。 This could be solved, by do the actual moving in requestAnimationFrame.这可以通过在 requestAnimationFrame 中进行实际移动来解决。

Is it a useful thing to do?这样做有用吗? Will it increase performance and decrease paint events?它会提高性能并减少绘画事件吗? Are there any issues I didnt think off?有没有我没想到的问题?

Depends on the browser and the use case.取决于浏览器和用例。

It is now asked by the specs that browsers do threshold themselves UI events (among which the mouse events).现在规范要求浏览器对自己的 UI 事件(其中鼠标事件)进行阈值处理。

Implementations are encouraged to determine the optimal frequency rate to balance responsiveness with performance.鼓励实现来确定最佳频率速率以平衡响应性和性能。

Chrome and Firefox do this, firing these events only at screen refresh rate, just before requestAniamtionFrame callbacks fire. Chrome 和 Firefox 会这样做,仅在requestAniamtionFrame回调触发之前以屏幕刷新率触发这些事件。
So in these browsers, you won't win anything by doing so, but you won't lose much either.因此,在这些浏览器中,这样做您不会赢得任何东西,但也不会损失太多。

Safari still fires as many such events as the device emits, so in this browser you'll win by maintaining your own threshold. Safari 仍然会触发与设备发出的一样多的此类事件,因此在此浏览器中,您将通过保持自己的阈值而获胜。

( Note that if you wish to unleash this threshold, you'll need to use pointerevents instead . ) 请注意,如果你想释放这个门槛,你就需要使用pointerevents代替。)


Now, this is useful to avoid uselessly calculating things that will get discarded by next calls before it can get painted.现在,这有助于避免无用地计算将在下一次调用之前被丢弃的东西,然后它才能被绘制。
The painting will always be throttled to the refresh rate of your screen (just after the requestAnimationFrame callbacks fire). 绘画将始终被限制为屏幕的刷新率(就在requestAnimationFrame回调触发之后)。
So it's up to you to determine if you wish to apply that threshold or not.因此,由您决定是否要应用该阈值。
For instance it can make sense to update a list of points as fast as possible, but to wait the requestAnimationFrame callback to actually make DOM changes or draw anything.例如,尽可能快地更新点列表是有意义的,但要等待requestAnimationFrame回调来实际进行 DOM 更改或绘制任何内容。

From the little you said about your case, it seems though that you may indeed win by waiting for requestAnimationFrame callbacks, only because you may be modifying the box model of your CSSOM.从你对你的案例所说的一点点来看,似乎你确实可能通过等待requestAnimationFrame回调而获胜,只是因为你可能正在修改 CSSOM 的盒子模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM