Back

Animate

Animate calls a function repeatedly like setInterval() but with the current time (Date.getTime()) and the difference to the last call in milliseconds as parameter
when using _AddOptions( "NoDom" ); to cause a high performance you cannot use SetInterval but Animate.

comparison:

normal:
  setInterval: about 242 calls per second
  app.animate: about 217 calls per second

with "NoDom" option:
  setInterval: error
  app.Animate: up to 1000 calls per second

  app.Animate( callback, fps )

Example - Digital Clock



function OnStart()
{
    app.SetOrientation( "Portrait" );
    app.SetDebugEnabled( false );

    lay = app.CreateLayout( "Linear", "FillXY,VCenter" );

    txt = app.CreateText( "", -1, -1, "multiline" );
    txt.SetTextSize( 30 );
    lay.AddChild( txt );

    app.AddLayout( lay );

    app.Animate( OnAnimate, 30 );
}

function OnAnimate( time, dtime )
{
    txt.SetText( new Date().toLocaleString() + "\n" + time );
}
    Copy     Copy All       Run      

Example - SpeedTest



_AddOptions( "NoDom" );

var ltime = Date.now(), c = 0;

function OnStart()
{
    lay = app.CreateLayout( "Linear", "FillXY,VCenter" );

    txt = app.CreateText( "", .5, .1, "left" );
    lay.AddChild( txt );

    app.AddLayout( lay );

    app.Animate(OnAnimate, 1000);
}

function OnAnimate( time, dtime )
{
    c++;
    if( time - ltime >= 1000 ) {
        txt.SetText( c + " cps" );
        ltime = time;
        c = 0;
    }
}
    Copy     Copy All       Run      

function(time, dtime)
number: integer