Friday, June 28, 2013

Changing the cursor from emscripten

You may have an app which needs to change the cursor when you interact with elements in the canvas. In the app I'm working on, I needed the cursor to change when you hover over drawn bars that allow for sizing. To do this I wrote a little javascript code to do the cursor change and then had the c++ call it.

The javascript code:

function changeMouseCursor( type ) {

    if ( type == 0 ) {  
        $('canvas').css( 'cursor', 'default' );  
    }
    else if ( type == 1 ) {
        $('canvas').css('cursor', 'n-resize' );
    }
    else if ( type == 2 ) {
        $('canvas').css('cursor', 's-resize' );
    }
    else if ( type == 4 ) {
        $('canvas').css('cursor', 'e-resize' );
    }
    else if ( type == 5 ) {
        $('canvas').css('cursor', 'se-resize' );
    }
    else if ( type == 6 ) {
        $('canvas').css('cursor', 'ne-resize' );
    }
    else if ( type == 8 ) {
        $('canvas').css('cursor', 'w-resize' );
    }
    else if ( type == 9 ) {
        $('canvas').css('cursor', 'sw-resize' );
    }
    else if ( type == 10 ) {
        $('canvas').css('cursor', 'nw-resize' );
    }
}


Code in emscripten:

int sizeType = IsOverDraggableBorder( m_mouseX, m_mouseY );
                       
 if ( sizeType != m_sizingType )
{
        //change the cursor
        char call[200];
        sprintf(call, "changeMouseCursor(%d)", sizeType );
        emscripten_run_script(call);
                           
         m_sizingType = sizeType;
 }


Here is what it looks like in my app :)



Friday, June 7, 2013

Emscripten speed optimizations

One more important thing I have left off from previous posts is that without these optimization options fed to the compiler it will not be fast! Make sure that you have "-O2 -s ASM_JS=1" as part of your build config. As far as I can tell the "-s ASM_JS=1" doesn't seem to do much, but the first part "-O2" makes a significant difference!

Your build command might look as follows:
~/emscripten/emscripten/emcc -O2 -s ASM_JS=1 main.cpp -o /var/www/html/test.html

Another thing to note is that you most likely should get the beta firefox version (currently 22) to see how your code performs with the latest asm.js optimizations.