Example usage for com.google.gwt.animation.client AnimationScheduler requestAnimationFrame

List of usage examples for com.google.gwt.animation.client AnimationScheduler requestAnimationFrame

Introduction

In this page you can find the example usage for com.google.gwt.animation.client AnimationScheduler requestAnimationFrame.

Prototype

public abstract AnimationHandle requestAnimationFrame(AnimationCallback callback, Element element);

Source Link

Document

Schedule an animation, letting the browser decide when to trigger the next step in the animation.

Usage

From source file:com.philbeaudoin.quebec.client.main.GameView.java

License:Apache License

@Inject
public GameView(AnimationScheduler animationScheduler, SpriteResources spriteResources) {
    this.animationScheduler = animationScheduler;
    this.spriteResources = spriteResources;
    widget = binder.createAndBindUi(this);
    canvas = fullCanvas.asCanvas();//from ww  w .j av  a 2 s .co m
    context = canvas.getContext2d();

    staticLayerCanvas = Canvas.createIfSupported();
    staticLayerContext = staticLayerCanvas.getContext2d();

    fullCanvas.addResizeHandler(new ResizeHandler() {
        @Override
        public void onResize(ResizeEvent event) {
            int width = canvas.getCoordinateSpaceWidth();
            int height = canvas.getCoordinateSpaceHeight();
            context.setFillStyle("#ddd");
            context.setStrokeStyle("#000");
            context.rect(0, 0, width, height);
            context.fill();
            context.stroke();
            forceRefresh = true;
        }
    });

    final AnimationCallback animationCallback = new AnimationCallback() {
        @Override
        public void execute(double timestamp) {
            GameView.this.animationScheduler.requestAnimationFrame(this, canvas.getElement());
            doUpdate(timestamp);
        }
    };
    animationScheduler.requestAnimationFrame(animationCallback, canvas.getElement());

    fullCanvas.addMouseMoveHandler(new MouseMoveHandler() {
        @Override
        public void onMouseMove(MouseMoveEvent event) {
            double height = canvas.getOffsetHeight();
            lastMouseX = event.getRelativeX(fullCanvas.getElement()) / height;
            lastMouseY = event.getRelativeY(fullCanvas.getElement()) / height;
            presenter.onMouseMove(lastMouseX, lastMouseY, time);
        }
    });

    fullCanvas.addMouseDownHandler(new MouseDownHandler() {
        @Override
        public void onMouseDown(MouseDownEvent event) {
            double height = canvas.getOffsetHeight();
            lastMouseX = event.getRelativeX(fullCanvas.getElement()) / height;
            lastMouseY = event.getRelativeY(fullCanvas.getElement()) / height;
            presenter.onMouseClick(lastMouseX, lastMouseY, time);
        }
    });
}