GWT: Cyclically loading an Image -
i try implement kind of videostream relays on simple jpeg files. on server, jpeg being created cyclically external camera. , want include picture in gwt application.
my first idea reload picture timer simple not good: client opens connection each reload-cycle, , picture flickers (at least in firefox).
how solve these problems? thinking "web-sockets", don't know how do. want avoid single connection each reload. idea have open connection provides new picture client asks for.
and how avoid flickering when swapping picture?
any ideas welcome!
regards, vandahlen
a solution avoid flickering have 2 images absolutely positioned in same location. timer load 1 or other alternatively in each frame. set load
handler each image, changes z-index when image loaded , restarts timer.
adding parameter image url, makes browser ask server each time bypass cache.
if time between frames small, browser re-use same connection if keep-alive correctly configured in server. enabled typical value of 5-15 seconds increase, if .jpg images updated periodicity, don't have worry , better solution.
i propose ui solution based on these ideas. work if use websocket/comet mechanism giving last .jpg file in base64 format (just change url value returned).
gwt code:
public void onmoduleload() { final image i1 = new image(); i1.setwidth("400px"); final image i2 = new image(); i2.setwidth("400px"); absolutepanel panel = new absolutepanel(); panel.add(i1, 0, 0); panel.add(i2, 0, 0); panel.setsize("600px", "400px"); rootpanel.get().add(panel); // change base64 data if use comet/websockets string url = "my_image_url.jpg?"; final timer loadnext = new timer() { boolean b; int c; public void run() { // counter parameter forces load next frame instead of using cache if (b = !b) { i1.seturl(url + c++); } else { i2.seturl(url + c++); } } }; i1.addloadhandler(new loadhandler() { public void onload(loadevent event) { i1.getelement().getstyle().setzindex(1); i2.getelement().getstyle().setzindex(0); loadnext.schedule(1000); } }); i2.addloadhandler(new loadhandler() { public void onload(loadevent event) { i1.getelement().getstyle().setzindex(0); i2.getelement().getstyle().setzindex(1); loadnext.schedule(1000); } }); loadnext.schedule(1000); }
if want use gwtquery, code smaller:
// change base64 data if use comet/websockets final string url = "my_image_url.jpg?"; final gquery images = $("<img/><img/>").appendto(document); images.css($$("position: fixed, top: 10px, left: 600px, width: 400px")); final timer timer = new timer() { int c; public void run() { images.eq(c%2).attr("src", url + c++); } }; images.bind("load", new function(){ public void f() { $(this).css($$("z-index: 1")).siblings("img").css($$("z-index: 0")); timer.schedule(1000); } }); timer.schedule(1000);
Comments
Post a Comment