Skip to content

Canvas ​

Root node of a 2D canvas hierarchy.

Canvas is the top-level container for all 2D nodes that participate in rendering and updates. It owns the canvas hierarchy, defines the canvas size in window coordinates, and maps that space to clip space through projection_matrix. Canvas coordinates use a top-left origin with Y increasing downward, matching the coordinate space of mouse events.

Call Resize whenever the window size changes, typically alongside Camera::Resize, and call Advance once per frame to drive updates.

cpp
auto canvas = vglx::Canvas::Create();

window.OnResize([&](const vglx::ResizeParameters& p) {
  camera->Resize(p.window_width, p.window_height);
  canvas->Resize(p.window_width, p.window_height);
});

// Inside the main loop:
canvas->Advance(delta);

INFO

Derives from Node2D and inherits all public properties and methods.

Construction ​

Constructors ​


Canvas() ​

Constructs a canvas.

cpp
Canvas::Canvas();

Factories preferred ​


Canvas::Create() std::unique_ptr<Canvas> ​

Creates an instance of Canvas.

cpp
static std::unique_ptr<Canvas> Canvas::Create();

Properties ​

projection_matrix Matrix3 ​

Projection matrix that maps canvas coordinates to clip space.

Rebuilt by Resize. Remains the identity until the first call with a positive width and height.

cpp
Matrix3 projection_matrix {1.0f};

Functions ​

Advance() void ​

Advances the canvas by one frame.

Propagates per-frame updates through the canvas hierarchy, calling OnUpdate on the canvas and all attached nodes in depth-first order.

cpp
void Canvas::Advance(float delta);
ParameterDescription
deltaElapsed time in seconds since the last frame.

GetNodeType() Node2D::Type virtual ​

Identifies this node as Node2D::Type::Canvas.

cpp
Type vglx::Canvas::GetNodeType() const override;

GetSize() Vector2 ​

Returns the canvas size in window coordinates.

Returns zero until Resize is called.

cpp
Vector2 Canvas::GetSize() const;

HandleEvent() void ​

Propagates an input event through the canvas hierarchy.

Events travel bottom-up: children receive the event before their parents, and the canvas itself receives it last. Propagation stops as soon as a node marks the event as handled.

cpp
void Canvas::HandleEvent(Event* event);
ParameterDescription
eventKeyboard, mouse, or gamepad event to propagate.

Resize() void ​

Updates the canvas to reflect the current window size.

Stores the size and rebuilds projection_matrix so that canvas coordinates from (0, 0) at the top-left to (width, height) at the bottom-right fill the viewport. A non-positive dimension is stored but leaves the projection unchanged.

cpp
void Canvas::Resize(int width, int height);
ParameterDescription
widthCanvas width in window coordinates.
heightCanvas height in window coordinates.

Released under the MIT License.