GWT2.6の場合
AnimationScheduler.get().requestAnimationFrame(AnimationCallback)
を使うといい
requestAnimationFrame()を使うメリットについては、Canvasの描画とか多分速くなる。
CanvasやWebGLを使ったJavaScriptを移植する際、Timer()より、こっちを使うほうが楽。
ただし、不具合(issue)があるかどうかは検証中
関連issue - GWT-7759
注意事項
GWT2.7とか追いきれていないので、最新のやり方は変わっているかもしれない。
コンソールに表示される警告
Chrome 41 windows7-64bit
'webkitRequestAnimationFrame' is vendor-specific. Please use the standard 'requestAnimationFrame' instead.
'webkitCancelRequestAnimationFrame' is vendor-specific. Please use the standard 'cancelAnimationFrame' instead.
Firefox 28 windows7-64bit
getUserData() と setUserData() の使用は推奨されません。代わりに WeakMap と element.dataset を使用してください。
IE11 windows7-64bit
何もなし
その他
ElementalのBrowser.getWindow().webkitRequestAnimationFrame(this);を使う方法もあるようだけど
機種依存解決できない。
コード
package com.akjava.gwt26.client;
import com.google.gwt.animation.client.AnimationScheduler;
import com.google.gwt.animation.client.AnimationScheduler.AnimationCallback;
import com.google.gwt.animation.client.AnimationScheduler.AnimationHandle;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Timer;
public class GWT2_6test implements EntryPoint ,AnimationCallback {
private AnimationHandle handler;
/**
* request animation test
*
* warning
*
* Chrome
*'webkitRequestAnimationFrame' is vendor-specific. Please use the standard 'requestAnimationFrame' instead.
*'webkitCancelRequestAnimationFrame' is vendor-specific. Please use the standard 'cancelAnimationFrame' instead.
*/
@Override
public void onModuleLoad() {
handler = AnimationScheduler.get().requestAnimationFrame(this);
//cancel
new Timer(){
@Override
public void run() {
handler.cancel();
}}.schedule(1000);
}
public static final native void log(String object)/*-{
if(console){
console.log(object);
}
}-*/;
@Override
public void execute(double timestamp) {
handler=AnimationScheduler.get().requestAnimationFrame(this);
log(""+timestamp);
}
}