Chapter 07 · Java + TeaVM

A frame callback and a moving target.

TeaVM schedules requestAnimationFrame callbacks. Pointer movement updates Java fields, and the pause button demonstrates ordinary browser event handling.

This chapter requires Canvas 2D support.

Starting the TeaVM application…

How it works

Animation state stays in Java. Each frame clears and redraws the Canvas; pointer coordinates are converted from CSS pixels to the logical scene.

Window.requestAnimationFrame(timestamp -> {
    elapsed += deltaSeconds;
    drawScene();
    Window.requestAnimationFrame(this::animate);
});
canvas.onEvent("pointermove", event -> {
    MouseEvent mouse = (MouseEvent) event;
    pointerX = mouse.getOffsetX();
});