I am having serious image and performance issues with the script below. The biggest problem is that the animation of the object becomes really jerky, almost crippling, so in IE9, but more and more annoying in Firefox.
Until recently, it was pretty fast, but I'm worried that complexity is slowing down. Oddly enough, the Sunspider Test runs faster on my IE9 instance than on Firefox.
script (which is a fragment of a large collection ***):
- Checks the HTML5 session storage log for users through the game.
- Depending on the stage, an object is animated between the two using crSpline .
- Ensures that the browser window follows the object through a large canvas, through scrollLeft, etc.
- Finally, it loads the popup through the colorbox .
- When this window is closed, the user's progression log is increased accordingly, and the object moves again.
Are there any obvious speed improvements that I could make to my code? There is a fair battle, how can I reduce this? Are there any infinite loops that I skip? Is there any software that I can use to profile JS slow points?
*** (I cannot provide other JS files or HTML, but I have identified this script as a problem)
UPDATE: After more honest testing, it seems that the step animation function that follows the object in the window through scrollLeft causes jerky animation. Removing it greatly improves the situation.
However, this is not a viable long-term solution. A quick solution is to call the follow function after completion, but it is a much less pleasant experience for the end user, especially when the object moves long distances.
So, how could I change the step function to run "slower" / more efficiently? . I assume that jerky is caused by him, using all available resources to track the object every millisecond.
(function ($) { sessionStorage.gameMainStage = 0 moveShip = function() { switch (sessionStorage.gameMainStage) { case '1': $("#object").animate( { crSpline: $.crSpline.buildSequence([[715, 425], [582, 524], [556, 646], [722, 688], [963, 629], [1143, 467]]) },{ duration: 10000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"dialog-1.html", width:"737px", height:"474px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; case '2': $("#object").animate( { crSpline: $.crSpline.buildSequence([[1143, 467], [1343, 667], [1443, 367], [1243, 167], [1499, 285]]) }, { duration: 5000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"dialog-2", width:"737px", height:"547px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; case '3': $("#object").animate( { crSpline: $.crSpline.buildSequence([[1499, 285], [1922, 423]]) }, { duration: 5000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"dialog-3.html", width:"737px", height:"547px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; case '4': $("#object").animate( { crSpline: $.crSpline.buildSequence([[1922, 423], [2216, 578]]) },{ duration: 5000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"game-1.html", width:"737px", height:"547px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; case '5': $("#object").animate( { crSpline: $.crSpline.buildSequence([[2216, 578], [2769, 904]]) },{ duration: 5000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"dialog-4.html", width:"737px", height:"547px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; case '6': $("#object").animate( { crSpline: $.crSpline.buildSequence([[2769, 904], [3263, 903]]) },{ duration: 5000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"dialog-5.html", width:"737px", height:"547px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; case '7': $.colorbox({href:"game-2.html", width:"500px", height:"600px", iframe: true, overlayClose: false, escKey: false, close: ""}); break; case '8': $.colorbox({href:"dialog-6.html", width:"737px", height:"567px", iframe: true, overlayClose: false, escKey: false, close: ""}); break; case '9': $("#object").animate( { crSpline: $.crSpline.buildSequence([[3263, 903], [4141, 820]]) },{ duration: 5000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"dialog-7.html", width:"737px", height:"547px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; case '10': $("#object").animate( { crSpline: $.crSpline.buildSequence([[4141, 820], [4568, 949], [4447, 1175]]) },{ duration: 5000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"dialog-8.html", width:"737px", height:"434px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; case '11': $.colorbox({href:"dialog-9.html", width:"737px", height:"567px", iframe: true, overlayClose: false, escKey: false, close: ""}); break; case '12': $("#object").animate( { crSpline: $.crSpline.buildSequence([[4447, 1175], [4701, 1124], [4816, 822]]) },{ duration: 5000, step: function() { var mover = $('#object'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); }, complete: function() { $.colorbox({href:"dialog-10.html", width:"900px", height:"687px", iframe: true, overlayClose: false, escKey: false, close: ""}); } } ); break; } }; })(jQuery);