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 AnimationHandle requestAnimationFrame(AnimationCallback callback) 

Source Link

Document

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

Usage

From source file:com.vaadin.tests.widgetset.client.ResizeTerrorizerControlConnector.java

License:Apache License

private void terrorize(final double startWidth, final double endWidth, final double startHeight,
        final double endHeight, final double duration) {
    final AbstractComponentConnector target = getTarget();

    final AnimationScheduler scheduler = AnimationScheduler.get();
    AnimationCallback callback = new AnimationCallback() {
        double startTime = -1;
        int frameCount = 0;

        @Override//from  www .  j av a  2  s  .co m
        public void execute(double timestamp) {
            frameCount++;

            boolean done = false;
            if (startTime == -1) {
                startTime = timestamp;
            }

            double time = timestamp - startTime;
            if (time > duration) {
                time = duration;
                done = true;
            }

            double progress = time / duration;

            double widthToSet = startWidth + (endWidth - startWidth) * progress;

            double heightToSet = startHeight + (endHeight - startHeight) * progress;

            if (widthToSet != startWidth) {
                target.getWidget().setWidth(widthToSet + "px");
            }
            if (heightToSet != startHeight) {
                target.getWidget().setHeight(heightToSet + "px");
            }

            // TODO Optionally inform LayoutManager as well
            if (target.getWidget() instanceof RequiresResize) {
                ((RequiresResize) target.getWidget()).onResize();
            }

            if (!done) {
                scheduler.requestAnimationFrame(this);
            } else {
                double fps = Math.round(frameCount / (duration / 1000));
                String results = frameCount + " frames, " + fps + " fps";

                getWidget().showResults(results);
            }
        }
    };
    scheduler.requestAnimationFrame(callback);
}