Javafx animation canvas render loop
Skill JohannesRabauer/javafx-skills/skills/javafx-animation-canvas-render-loop
Animate JavaFX UIs with Timeline, Transition, Canvas, and AnimationTimer-based render loops.From its SKILL.md
npx -y skills add JohannesRabauer/javafx-skills --skill javafx-animation-canvas-render-loopAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 4 stars4 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
2.4 KB, as published. Nobody here has run it
JavaFX Animation, Canvas, and Render Loops
Use this skill when the UI needs property animation, custom per-frame drawing, or deterministic render/update loops built on top of JavaFX primitives.
Example
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
public class RenderLoopView extends StackPane {
public RenderLoopView() {
var canvas = new Canvas(640, 360);
var gc = canvas.getGraphicsContext2D();
AnimationTimer timer = new AnimationTimer() {
private long lastUpdate;
private double x;
@Override
public void handle(long now) {
if (lastUpdate == 0L) {
lastUpdate = now;
return;
}
double deltaSeconds = (now - lastUpdate) / 1_000_000_000.0;
lastUpdate = now;
x = (x + 120 * deltaSeconds) % canvas.getWidth();
render(gc, canvas.getWidth(), canvas.getHeight(), x);
}
};
timer.start();
getChildren().add(canvas);
}
private void render(GraphicsContext gc, double width, double height, double x) {
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, width, height);
gc.setFill(Color.CORNFLOWERBLUE);
gc.fillOval(x, height / 2 - 20, 40, 40);
}
}
Setup notes
- Use
TimelineorTransitionwhen a standard property animation is enough. - Use
AnimationTimeronly for explicit per-frame control. - Keep simulation, file IO, and networking off the FX thread even when the render loop runs on it.
Gotchas
Canvasis immediate-mode rendering, so redraw responsibility is entirely yours.- Delta-time logic must guard against the first frame and timing spikes.
- If all you need is a fade, move, or scale animation, a render loop is unnecessary complexity.