简体   繁体   English

如何不断移动SVG路径而不损失性能

[英]How to constantly move SVG paths without losing performance

I am trying to do a mobile game where I draw 6 SVG paths and I move then through the screen (from top to bottom) constantly. 我正在尝试做一个手机游戏,绘制6条SVG路径,然后不断在屏幕上(从上到下)移动。 I am manipulating the paths with a simple javascript that updates some variables value and use them to set the attribute "d" of the paths. 我使用一个简单的JavaScript操作路径,该JavaScript更新了一些变量值并使用它们来设置路径的属性“ d”。 Like the example below: 像下面的例子:

setInterval(scrollPaths, 17); // ~ 60 fps

function scrollPaths() {

  // leftYPoints is an array of points defined earlier and scrollSpeed is an integer value (e.g. 2)
  for (var i = 0; i < leftYPoints.length; i++) leftYPoints[i] += scrollSpeed;

  // then I change the paths attribute
  var pathAttribute = "M"+ leftXPoints[0] + leftYPoints[0]
      + " L" + leftXPoints[1] + leftYPoints[1];

  document.getElementById("leftpath").setAttribute("d", pathAttribute);
  document.getElementById("righpath").setAttribute("d", pathAttribute);
  ... // continue to do that with the other paths, changing some variables only

}

The javascript itself runs very fast (scrollPaths takes about 5ms every time) and runs perfectly on the browser. JavaScript本身运行非常快(scrollPaths每次大约需要5毫秒),并且可以在浏览器上完美运行。 But when I test the script in the mobile browser it seems that theres is a lot of lag on the paths. 但是,当我在移动浏览器中测试脚本时,路径上似乎有很多滞后。 You can see that the paths are not scrolling smoothly. 您会看到路径滚动不流畅。 So I tried to decrease the value of scrollSpeed to a very small value but that did not solve it. 所以我试图将scrollSpeed的值减小到一个很小的值,但这并没有解决。 So I thought the problem was related to the rendering method of the mobile browser or something like that. 因此,我认为问题与移动浏览器的渲染方法或类似问题有关。 I tried to find some answers but nothing solved my problem. 我试图找到一些答案,但没有任何办法解决我的问题。 Then I found AmeliaBR`s answer here How do you move an SVG around a webpage without triggering slow redraws? 然后,我在这里找到AmeliaBR的答案。 如何在不触发缓慢重绘的情况下在网页上移动SVG? where she says that it is better to use the transform attribute because the browser will understand it as it should just move some content that was already rendered instead of re-calculating the whole layout. 她说最好使用transform属性,因为浏览器会理解它,因为它应该只移动一些已经渲染的内容,而不是重新计算整个布局。 So I tried to do that like this: 所以我尝试这样做:

var newYPos = 1;

setInterval(scrollPaths, 17); // ~ 60 fps

function scrollPaths() {

  // increased the position of the paths
  newYPos += 1;

  // then I did the transform of the paths with a group <g>
  document.getElementById("pathsgroup").setAttribute("transform", "translate(0," + value + ")");

}

But unfortunately the result was not very effective. 但是不幸的是结果并不十分有效。 The javascript ran a little faster but the lag effect of the paths is still happening. javascript运行得更快一些,但是路径的滞后效应仍在发生。 So I am here, asking: 所以我在这里,问:

  1. Does anyone knows what is happening? 有人知道发生了什么吗?
  2. Is there a better way to do that? 有更好的方法吗?
  3. Or the problem is that mobile browsers are not ready for that yet (they still lack performance)? 还是问题在于移动浏览器尚未为此做好准备(它们仍然缺乏性能)?

Not sure if it helps but I tested it on a Nexus 5 with Chrome (supposed to have a very good performance). 不确定它是否有帮助,但是我在装有Chrome的Nexus 5上进行了测试(应该有很好的性能)。

Thanks. 谢谢。

Maybe try storing the "pathsgroup" element as a variable so you don't have to keep using getElement every iteration? 也许尝试将“ pathsgroup”元素存储为变量,这样就不必每次迭代都继续使用getElement了吗?

var newYPos = 1;
var pathsGroup = document.getElementById("pathsgroup");

setInterval(scrollPaths, 17); // ~ 60 fps

function scrollPaths() {

  // increased the position of the paths
  newYPos += 1;

  // then I did the transform of the paths with a group <g>
  pathsGroup.setAttribute("transform", "translate(0," + value + ")");

}

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

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