API Reference /Screen

API Reference

Screen

The canvas size, its center, frame rate and frame count, plus the update loop and resize events.

Import
import * as screen from "codetoy/screen"
// Screen is a global static class; no using needed.
from codetoy.screen import width, height, centerX, centerY, fps, frameCount

The canvas fills its container (the editor preview, an embed, a Discord window) and can resize at any time. Read these values each frame instead of storing them once.

#Size

#width / height

The canvas size in pixels.

screen.width(): f64
screen.height(): f64
Screen.Width  // double field
Screen.Height // double field
width() -> float
height() -> float

#centerX / centerY

Half the width and height: the middle of the canvas.

screen.centerX(): f64
screen.centerY(): f64
Screen.CenterX // double field
Screen.CenterY // double field
centerX() -> float
centerY() -> float

#Frames

#fps

The current frames per second.

screen.fps(): f64
Screen.FPS // double field, updated once per second
fps() -> float

#frameCount

The number of frames drawn since the program started.

screen.frameCount(): i32
Not available in C# yet. Compare languages
frameCount() -> int

#The update loop

Codetoy calls your update function once per frame, before showing the canvas. See Program Structure.

export function update(): void {
  // runs every frame
}
Screen.OnUpdate(() => {
    // runs every frame
});
// Calling OnUpdate again replaces the previous callback.
def update():
    # runs every frame
    pass

#Resize event

Called whenever the canvas changes size, with the new width and height. Use it to recompute layout you cache, like a grid that fits the screen.

export function resize(width: f64, height: f64): void {
  cellSize = Math.min(width, height) / 10
}
Not available in C# yet. Compare languages
def resize(w, h):
    global cell_size
    cell_size = min(w, h) / 10

Note

C# has no resize callback yet. Compare Screen.Width and Screen.Height with last frame's values in Screen.OnUpdate if you need to react to resizes.

Esc