Already probably I have been doing a small game on HTML5 Canvas for a month and was disappointed with the result. The game itself works for 5 plus, everything works and is displayed as it should, but fps leaves much to be desired. I realized that I needed to optimize this business, began to read articles and everything else connected with it, did optimization, but the FPS did not stop subsiding, while there are no complicated things in the game itself, the most difficult is an array of 40 objects that is drawn on canvas, is it so much for javascript? How generally to learn in what place memory flows away? maybe I missed something somewhere.

I decided to check which fps will be on a plain empty canvas, why I can’t understand it so low. I hold about 40.

http://jsfiddle.net/1tkftzyg/4/

  • It would be nice if you give more specifics: give the code, or at least part of the code in which you expect a possible memory leak. It is very difficult to advise something without seeing the whole situation. If you want to find the problem yourself and are looking for a tool to find a memory leak in the browser, then you need to reformulate the question a little. - VisioN
  • I’m looking for both, I don’t have any assumptions, but I can just show the code that is executed every frame, but it will take time to analyze it, and people here don’t have it - arkadij_ok
  • the most common canvas, without any libraries, the render happens in a loop, going through an array of 40 elements, 40 diamonds are drawn in given coordinates and that's it - arkadij_ok
  • @shatal I'm not saying that everything is fine in the code, it's just that there is nothing like that, i.e. the usual procedures for waiting for the download of all images, the creation of just this very array, and the creation of a game "magnifier". Here I sit, profile, increases Used JS Heap, until I know what it is, but already went to google :) thanks for the tips - arkadij_ok
  • I decided to check which fps will be on a plain empty canvas, why I can’t understand it so low. I have about 40. jsfiddle.net/1tkftzyg/4 - arkadij_ok

1 answer 1

The first thing you want to advise after viewing your links, it will learn to use the profiler in the browser with the help of which you can find leaks, bottlenecks, as well as it shows fps. Personally, I do not like chrom's profiling tools, but I use, in my opinion the best, IE. It is also necessary to note that it is necessary to check separately in firefox, since it has problems with canvas and it cuts its capabilities almost twice.

And now about your problem. Horse racing fps is a completely normal and mundane story. But you are confused by the jerky animation. This ailment was always and it is treated only by smoothing the time delta. That is, you need to build all the animation in the game not on the timer “ticks”, but on the past tense. But the past tense does not help as long as it is not smoothed. What does this mean. Imagine that you are creating a motion animation of a square from left to right for a distance of 100px per second with the browser update set at 60fps per second. What are you doing for this? You take and divide the distance 100px by 60sec, which is 1.6. With good fps, a call to the render update function is called 60 times per second and multiplying the elapsed time by a predetermined value (1.6), the movement will look smooth. But if fps falls, then the intervals increase and thus the objects start jumping, since with ten updates they are removed by ten equal segments of 1.6, and with a jump of fps, this value also increases. Therefore, there is only one way out - to create a time smoothing function and build the whole animation for this time.

And if you don’t understand what I was trying to convey, search Google for articles, since this topic is not one big article.

  • Thanks, I understood you perfectly, but I tried to multiply the speed of all the objects by the delta, but I had to abandon this method because In the game you need to act immediately (the game is on the reaction), then with a large jerk of fps in one direction or another, there is a jerk and the game is just as sharp ... although now again I decided to try to smooth everything off with a delta, it doesn't look very smooth either :( - arkadij_ok
  • You must be a genius! Write a time smoothing function SO QUICKLY! And it cannot be uneasy, you smooth it ... - vas
  • so I did not understand you - arkadij_ok
  • So in Google! There are millions of articles, as I have already said. - vas