Back

Animate

Hello World
Content:
- Comparison

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.

app.Animate( callback, fps )

when using cfg.NoDom; to cause a high performance you cannot use SetInterval but Animate.
Note: The NoDom option is currently not available due to GooglePlays 64bit requirement since August 2019.

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

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

cfg.No_Dom;

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      
number: frames per second
function( time, dtime )