简体   繁体   English

使用Tab在后台时使用d3.js绘制SVG

[英]Drawing a SVG with d3.js while tab is in background

Context : I am working on a webapp that has to display some fairly complicated and constantly updating (multiple times per second) SVG images. 上下文 :我正在使用一个Web应用程序,该应用程序必须显示一些相当复杂且不断更新(每秒多次)的SVG图像。 The updates stem from a seperate server and the SVG is updates as soon as an update is received by the web frontend. 该更新来自单独的服务器,并且只要Web前端收到更新,SVG便会更新。 The webapp is written in Scala.js and the image is created using the d3js library (see also: scala-js-d3 ). 该Webapp是用Scala.js编写的,并且该图像是使用d3js库创建的(另请参见: scala-js-d3 )。 We currently only support Google Chrome. 我们目前仅支持Google Chrome。

Problem : Once the webapp has been in a background tab for a while, the whole site gets unresponsive once navigated to again. 问题 :一旦webapp进入后台选项卡一段时间,一旦再次导航到整个站点,该站点将无响应。 Depending on how long the app was in the background, it sometimes takes up to 20 seconds for the application to be responsive again. 根据应用程序在后台运行的时间长短,有时可能需要20秒才能使应用程序再次响应。 Is there any way I can solve this? 有什么办法可以解决这个问题?

The solution that works best for me is a pretty simple one: 最适合我的解决方案是一个非常简单的解决方案:

Once the application is in a background tab or minimized (can be detected using the Page Visibility API ), I only update my model without drawing anything. 一旦应用程序位于背景选项卡中或最小化(可以使用Page Visibility API进行检测),我将仅更新模型而不绘制任何内容。 Then, once the application is put into the foreground again, I re-draw everything. 然后,一旦应用程序再次置于前台,我将重新绘制所有内容。 This fixed all of my performance problems. 这解决了我所有的性能问题。

I came across the same problem which I worked around by not doing transitions in background tabs. 我遇到了同样的问题,我不通过在后台标签中进行过渡来解决此问题。

mbostock shows a nice snippet for doing this at https://github.com/d3/d3-transition/issues/93#issuecomment-516452828 mbostockhttps://github.com/d3/d3-transition/issues/93#issuecomment-516452828上显示了一个不错的代码片段

Helper method… 辅助方法...

d3.selection.prototype.maybeTransition = function(duration) {
  return duration > 0 ? this.transition().duration(duration) : this;
};

…to be used the following way: …按以下方式使用:

selection
  .maybeTransition(document.visibilityState === "visible" ? my_duration : 0)
    .attr(…

Capturing the page visibility events one could then even adapt the transition time in such a manner that the transition time is reduced (thus the transition sped up) in such a way that it ends in about the same time had the page not been in a background tab. 捕获页面可见性事件然后甚至可以以这样的方式调整过渡时间,即减少过渡时间(从而加快过渡时间),使得过渡时间大约与页面不在背景中的时间相同标签。

If you are using setInterval() at the moment the browser might be 'punishing' you for running setInterval() in an inactive browser-tab. 如果您当前正在使用setInterval(),浏览器可能会“惩罚”您在非活动浏览器选项卡中运行setInterval()。

You can fix this by either using requestAnimationFrame() or by monitoring the tab visibility and only using setInterval() when the tab is active. 您可以使用requestAnimationFrame()或监视选项卡的可见性,并且仅在选项卡处于活动状态时使用setInterval()才能解决此问题。

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

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