Canvas
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.
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.
Canvas::Canvas();Factories preferred
Creates an instance of Canvas.
static std::unique_ptr<Canvas> Canvas::Create();Properties
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.
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.
void Canvas::Advance(float delta);| Parameter | Description |
|---|---|
| delta | Elapsed time in seconds since the last frame. |
GetNodeType() Node2D::Type virtual
Identifies this node as Node2D::Type::Canvas.
Type vglx::Canvas::GetNodeType() const override;Returns the canvas size in window coordinates.
Returns zero until Resize is called.
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.
void Canvas::HandleEvent(Event* event);| Parameter | Description |
|---|---|
| event | Keyboard, 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.
void Canvas::Resize(int width, int height);| Parameter | Description |
|---|---|
| width | Canvas width in window coordinates. |
| height | Canvas height in window coordinates. |