gwt 2.5 - JS functions in GWT -
how use simple js functions in gwt? (picture animation script example)
<script type="text/javascript"> var cspeed=4; var cwidth=64; var cheight=64; var ctotalframes=8; var cframewidth=64; var cimagesrc='images/sprites.gif'; var cimagetimeout=false; var cindex=0; var cxpos=0; var cpreloadertimeout=false; var seconds_between_frames=0; function startanimation(){ document.getelementbyid('loaderimage').style.backgroundimage='url('+cimagesrc+')'; document.getelementbyid('loaderimage').style.width=cwidth+'px'; document.getelementbyid('loaderimage').style.height=cheight+'px'; //fps = math.round(100/(maxspeed+2-speed)); fps = math.round(100/cspeed); seconds_between_frames = 1 / fps; cpreloadertimeout=settimeout('continueanimation()', seconds_between_frames/1000); } function continueanimation(){ cxpos += cframewidth; //increase index know frame of our animation on cindex += 1; //if our cindex higher our total number of frames, we're @ end , should restart if (cindex >= ctotalframes) { cxpos =0; cindex=0; } if(document.getelementbyid('loaderimage')) document.getelementbyid('loaderimage').style.backgroundposition=(-cxpos)+'px 0'; cpreloadertimeout=settimeout('continueanimation()', seconds_between_frames*1000); } function stopanimation(){//stops animation cleartimeout(cpreloadertimeout); cpreloadertimeout=false; } function imageloader(s, fun)//pre-loads sprites image { cleartimeout(cimagetimeout); cimagetimeout=0; genimage = new image(); genimage.onload=function (){cimagetimeout=settimeout(fun, 0)}; genimage.onerror=new function('alert(\'could not load image\')'); genimage.src=s; } //the following code starts animation new imageloader(cimagesrc, 'startanimation()');
tried wrapping
public static native void anim() /*-{ js here }-*/;
but uncaught referenceerror: startanimation not defined
i've seen http://www.gwtproject.org/doc/latest/devguidecodingbasicsjsni.html didn't find examples js functions
you can use jsni:
method 1: if call imageloader fun = 'startanimation()'
, mean, use imageloader way: imageloader(s, 'startanimation()');
class yourmethods { public static native void imageloader(string s) /*-{ imageloader(s, 'startanimation()'); }-*/; }
you can call method this:
yourmethods::imageloader(s);
method 2: if fun
parameter change (is not 'startanimation()'
), type string
class yourmethods { public static native void imageloader(string s, string fun) /*-{ imageloader(s, fun); }-*/; }
in case: define second parameter "javascript name of function", , call way:
yourmethods::imageloader(s, "startanimation()");
and gwt understand fun parameter name of javascript function, , type string
method 3: convert javascript code gwt
Comments
Post a Comment