在iOS上移动html元素位置缓慢

时间:2012-07-25 13:45:45

标签: javascript jquery css ios html5

当我在CSS中修改多个html元素的位置时,我在iOS html5 webapp上遇到了巨大的性能问题。我还想手动移动我的元素。我不想使用CSS转换,因为无法停止动画(我们正在制作一个高度响应的游戏)。

我的示例在Android上的桌面浏览器(chrome,firefox等)上运行良好。但在iPad 2和iPhone 4S(都运行iOS 5.1)上速度非常慢。 在Phonegap应用程序中运行html5代码比直接在浏览器中运行更好但仍然很慢。

你有什么建议改进的东西?
editable example
full screen example

2 个答案:

答案 0 :(得分:1)

首先,如果你想要一些不慢的东西,可以避免所有的jQuery调用。

以下是我将如何重写(非常快速)您的代码:

// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
          window.webkitRequestAnimationFrame || 
          window.mozRequestAnimationFrame    || 
          window.oRequestAnimationFrame      || 
          window.msRequestAnimationFrame     || 
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();

var canvas = document.getElementById('canvas-test');
    canvas.height = 500;
    canvas.width = 500;
    var context = canvas.getContext('2d');

    // in this example, the fillstyle is always the same. no need to change it at every loop
    context.fillStyle = "#FF0000";


var balls = [];
var ballcanvas = [];
var ballctx = [];

// create 30 balls in canvases
var eDivBody = document.getElementById('divbody');
for (var i = 0; i < 30; i++){
    balls[i] = {
        x : 250,
        y : 100 + i * 2,
        dx : 3, // direction
    };

    // create the canvas
    var eBall = document.createElement('canvas');
    eBall.id = 'ballcanvas' + i;
    eBall.width = 75;
    eBall.height = 75;
    eDivBody.appendChild(eBall);

    // some css
    // no need for jQuery
    eBall.style.position = "absolute";
    eBall.style.left = balls[i].x + "px";
    eBall.style.top = balls[i].y + "px";
    eBall.style.backgroundColor = "#000000";

    // associate the element to the ball, no need to go threw the DOM after
balls[i].element = eBall;
}


var ball_test = {
    x : 250,
    y : 300,
    dx : 3 // direction
};


function loop(ball_test, balls, canvas, context, ballcanvas, ballctx){
    //change of direction on the sides
    if (ball_test.x > 400 || ball_test.x < 100)
        ball_test.dx *= -1;
    // movement
    ball_test.x += ball_test.dx;

    // the same for balls in canvases
    // never use array.legth in a loop condition. put it in a variable then compare. 
    for (var i = 0, j = balls.length; i < j; i++){
        // balls are following the test ball, no need to re-check the bounds
        // we take the test ball direction
        balls[i].dx =  ball_test.dx;

        //movement   
        balls[i].x += balls[i].dx;
        // change left style - No need for jQuery
        balls[i].element.style.left = balls[i].x + "px";
    }

    // display ball_test
    displayBallTest(ball_test, canvas, context);

    // Prefer the use of requestAnimationFrame
    requestAnimFrame(function(){
        loop(ball_test, balls, canvas, context, ballcanvas, ballctx);
    });
};

// no need to recalculate Math.PI * 2 for every loop.
// do it just the first time then use the value
var pi2 = Math.PI * 2;

function displayBallTest(ball, canvas, context){
    // clear canvas    
    // you don't need to clear all the canvas, just the zone where you now the ball is.
    // must need some calculation to be the most efficient possible
    context.clearRect(ball.x - 50 , ball.y  - 50, 100, 100);

    context.beginPath();
    context.arc(ball.x, ball.y, 40, 0, pi2 );
    context.fill();
};

// start main loop
loop(ball_test, balls, canvas, context, ballcanvas, ballctx);

我对代码进行了评论,但这是我所做的:

  • 完全避免使用jQuery。没有必要,除非你选择不将你的脚本放在内容的末尾,否则就可以了。
  • 尽可能使用requestAnimationFrame
  • 避免在全局值时重新计算或重置值 (Math.PI * 2,context.fillStyle ......)
  • 避免使用.length if for loop condition

但我认为你的问题来自于你想要移动30个画布元素而不是将它们内容绘制到主画布中。 当您使用Canvas Drawing时,iOS会很快。 对我来说,如果您选择在主画布上绘制而不是移动DOM元素,则会解决您的性能问题。

答案 1 :(得分:0)

您可以做的一件事是缓存您的选择器,而不是每次都执行它:

   // some css            
    $('#ballcanvas' + i).css("position", "absolute");
    $('#ballcanvas' + i).css("left", balls[i].x + "px");
    $('#ballcanvas' + i).css("top", balls[i].y + "px");
    $('#ballcanvas' + i).css("background-color", "#000000");

应该是这样的:

var thisBall = $('#ballcanvas' + i)
thisBall.css("position", "absolute");
... rest of your code ....

除此之外:当你已经拥有Jquery document.getElementById时,为什么还要使用$