Struct Renderer

Source
pub struct Renderer { /* private fields */ }
Expand description

High-level WebGL2 renderer for terminal-style applications.

The Renderer manages the WebGL2 rendering context, canvas, and provides a simplified interface for rendering drawable objects. It handles frame management, viewport setup, and coordinate system transformations.

Implementations§

Source§

impl Renderer

Source

pub fn create(canvas_id: &str) -> Result<Self, Error>

Creates a new renderer by querying for a canvas element with the given ID.

This method searches the DOM for a canvas element with the specified ID, initializes a WebGL2 context, and sets up the renderer with orthographic projection matching the canvas dimensions.

§Parameters
  • canvas_id - CSS selector for the canvas element (e.g., “canvas” or “#my-canvas”)
§Returns
  • Ok(Renderer) - Successfully created renderer
  • Err(Error) - Failed to find canvas, create WebGL context, or initialize renderer
§Errors
  • Error::UnableToRetrieveCanvas - Canvas element not found
  • Error::FailedToRetrieveWebGl2RenderingContext - WebGL2 not supported or failed to initialize
Source

pub fn canvas_padding_color(self, color: u32) -> Self

Sets the background color for the canvas area outside the terminal grid.

When the canvas dimensions don’t align perfectly with the terminal cell grid, there may be unused pixels around the edges. This color fills those padding areas to maintain a consistent appearance.

Source

pub fn create_with_canvas(canvas: HtmlCanvasElement) -> Result<Self, Error>

Creates a new renderer from an existing HTML canvas element.

This method takes ownership of an existing canvas element and initializes the WebGL2 context and renderer state. Useful when you already have a reference to the canvas element.

§Parameters
  • canvas - HTML canvas element to use for rendering
§Returns
  • Ok(Renderer) - Successfully created renderer
  • Err(Error) - Failed to create WebGL context or initialize renderer
Source

pub fn resize(&mut self, width: i32, height: i32)

Resizes the canvas and updates the viewport.

This method changes the canvas resolution and adjusts the WebGL viewport to match. The projection matrix is automatically updated to maintain proper coordinate mapping.

§Parameters
  • width - New canvas width in pixels
  • height - New canvas height in pixels
Source

pub fn clear(&mut self, r: f32, g: f32, b: f32)

Clears the framebuffer with the specified color.

Sets the clear color and clears both the color and depth buffers. Color components should be in the range [0.0, 1.0].

§Parameters
  • r - Red component (0.0 to 1.0)
  • g - Green component (0.0 to 1.0)
  • b - Blue component (0.0 to 1.0)
Source

pub fn begin_frame(&mut self)

Begins a new rendering frame.

Source

pub fn render(&mut self, drawable: &impl Drawable)

Renders a drawable object.

This method calls the drawable’s prepare, draw, and cleanup methods in sequence, providing it with a render context containing.

§Parameters
  • drawable - Object implementing the Drawable trait
Source

pub fn end_frame(&mut self)

Ends the current rendering frame.

This method finalizes the frame rendering. In future versions, this may handle buffer swapping or other post-rendering operations.

Source

pub fn gl(&self) -> &WebGl2RenderingContext

Returns a reference to the WebGL2 rendering context.

Source

pub fn canvas(&self) -> &HtmlCanvasElement

Returns a mutable reference to the WebGL2 rendering context.

Source

pub fn canvas_size(&self) -> (i32, i32)

Returns the current canvas dimensions as a tuple.

§Returns

Tuple containing (width, height) in pixels

Trait Implementations§

Source§

impl Debug for Renderer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.