game_loop 0.9.4

  • Installing
  • Versions
  • 48




All games require a main loop to drive the game forward. This main loop is responsible for processing inputs from keyboards, mice, gamepads, and time. Also, the main loop manages browser state, e.g., pointer lock and fullscreen. Finally, the main loop calls the game specific update routine. The game_loop library provides a main loop designed for games along with a powerful input subsystem.

If you are familiar with GLUT, game_loop provides that and more.


  • Main loop optimized for games.
  • Keyboard input.
  • Mouse input.
  • Fullscreen management.
  • Pointer lock management.
  • Timers.

Why game_loop ?

1. Focus on your game not on the browser platform. A perfect main loop requires code which properly combines input, time, display, and state management. Writing this code is tedious, error prone, and ultimately, a waste of time.

Getting Started

1. Add the following to your project's pubspec.yaml and run pub install.


2. Add the correct import for your project.

import 'package:game_loop/game_loop_html.dart';


import 'package:game_loop/game_loop_isolate.dart';



Reference Manual


1. test.html


1. Initialize a game loop:

main() {
  // Construct a game loop.
  GameLoopHtml gameLoop = new GameLoopHtml(canvasElement);

2. Hook your game update function up and start the main loop:

main() {
  // Construct a game loop.
  GameLoopHtml gameLoop = new GameLoopHtml(canvasElement);
  gameLoop.onUpdate = ((gameLoop) {
    // Update game logic here.
    print('${gameLoop.frame}: ${gameLoop.gameTime} [dt = ${gameLoop.dt}].');

3. Hook your game render function up and start drawing:

main() {
  // Construct a game loop.
  GameLoopHtml gameLoop = new GameLoopHtml(canvasElement);
  gameLoop.onUpdate = ((gameLoop) {
    // Update game logic here.
    print('${gameLoop.frame}: ${gameLoop.gameTime} [dt = ${gameLoop.dt}].');
  gameLoop.onRender = ((gameLoop) {
    // Draw game into canvasElement using WebGL or CanvasRenderingContext here.
    // The interpolation factor can be used to draw correct inter-frame
  	print('Interpolation factor: ${gameLoop.renderInterpolationFactor}');

3. Add a timer to your game loop:

main() {
  // Construct a game loop.
  GameLoopHtml gameLoop = new GameLoopHtml(canvasElement);
  gameLoop.onUpdate = ((gameLoop) {
    print('${gameLoop.frame}: ${gameLoop.frameTime} [dt = ${gameLoop.dt}].');
  // Add a timer which fires in half a second.
  var timer = gameLoop.addTimer((timer) => print('timer fired.'), 0.5);


  • Mark Bennett contributed stackable GameState support.
  • Fox32 contributed fixes to input processing.


  • Add a new onKeyDown handler, good for preventing default.


  • Remove dart:json import.

1. Depend on it

Add this to your package's pubspec.yaml file:

  game_loop: "^0.9.4"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:game_loop/game_loop_common.dart';

import 'package:game_loop/game_loop_html.dart';

import 'package:game_loop/game_loop_isolate.dart';
Version Uploaded Documentation Archive
0.9.4 Jan 17, 2014 Go to the documentation of game_loop 0.9.4 Download game_loop 0.9.4 archive
0.9.3 Nov 8, 2013 Go to the documentation of game_loop 0.9.3 Download game_loop 0.9.3 archive
0.9.2 Oct 28, 2013 Go to the documentation of game_loop 0.9.2 Download game_loop 0.9.2 archive
0.9.1 Oct 10, 2013 failed Download game_loop 0.9.1 archive
0.9.0 May 26, 2013 failed Download game_loop 0.9.0 archive
0.8.0 Apr 26, 2013 failed Download game_loop 0.8.0 archive
0.7.1 Mar 9, 2013 failed Download game_loop 0.7.1 archive
0.7.0 Feb 25, 2013 failed Download game_loop 0.7.0 archive
0.6.0 Feb 20, 2013 failed Download game_loop 0.6.0 archive
0.5.0 Jan 29, 2013 failed Download game_loop 0.5.0 archive


We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6


Describes how popular the package is relative to other packages. [more]
39 / 100
Code health derived from static analysis. [more]
94 / 100
Reflects how tidy and up-to-date the package is. [more]
0 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: web

Platform components identified in package: html.


  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 error 21 hints.

    Strong-mode analysis of lib/src/game_loop_html/game_loop_html.dart failed with the following error:

    line: 398 col: 17
    Invalid override. The type of 'GameLoopHtml.state=' ('(GameLoopHtmlState) → void') isn't a subtype of 'GameLoop.state=' ('(GameLoopState) → void').

    Run dartfmt to format lib/game_loop_common.dart.

    Similar analysis of the following files failed:

    • lib/game_loop_html.dart (hint)
    • lib/src/game_loop_common/game_loop.dart (hint)
    • lib/src/game_loop_common/game_loop_analog_input.dart (hint)
    • lib/src/game_loop_common/game_loop_digital_input.dart (hint)
    • lib/src/game_loop_common/game_loop_position_input.dart (hint)
    • lib/src/game_loop_common/game_loop_state.dart (hint)
    • lib/src/game_loop_common/game_loop_timer.dart (hint)
    • lib/src/game_loop_common/simple_state.dart (hint)
    • lib/src/game_loop_html/game_loop_gamepad.dart (hint)
    • lib/src/game_loop_html/game_loop_html_state.dart (hint)
    • lib/src/game_loop_html/game_loop_keyboard.dart (hint)
    • lib/src/game_loop_html/game_loop_mouse.dart (hint)
    • lib/src/game_loop_html/game_loop_touch.dart (hint)
    • lib/src/game_loop_html/menu_state.dart (hint)
    • lib/src/game_loop_html/simple_html_state.dart (hint)
    • lib/src/game_loop_isolate/game_loop_isolate.dart (hint)
  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use game_loop.dart.


Package Constraint Resolved Available
Dev dependencies
browser any
unittest any