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
impl Renderer
Sourcepub fn create(canvas_id: &str) -> Result<Self, Error>
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 rendererErr(Error)- Failed to find canvas, create WebGL context, or initialize renderer
§Errors
Error::UnableToRetrieveCanvas- Canvas element not foundError::FailedToRetrieveWebGl2RenderingContext- WebGL2 not supported or failed to initialize
Sourcepub fn canvas_padding_color(self, color: u32) -> Self
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.
Sourcepub fn create_with_canvas(canvas: HtmlCanvasElement) -> Result<Self, Error>
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 rendererErr(Error)- Failed to create WebGL context or initialize renderer
Sourcepub fn resize(&mut self, width: i32, height: i32)
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 pixelsheight- New canvas height in pixels
Sourcepub fn clear(&mut self, r: f32, g: f32, b: f32)
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)
Sourcepub fn begin_frame(&mut self)
pub fn begin_frame(&mut self)
Begins a new rendering frame.
Sourcepub fn render(&mut self, drawable: &impl Drawable)
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 theDrawabletrait
Sourcepub fn end_frame(&mut self)
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.
Sourcepub fn gl(&self) -> &WebGl2RenderingContext
pub fn gl(&self) -> &WebGl2RenderingContext
Returns a reference to the WebGL2 rendering context.
Sourcepub fn canvas(&self) -> &HtmlCanvasElement
pub fn canvas(&self) -> &HtmlCanvasElement
Returns a mutable reference to the WebGL2 rendering context.
Sourcepub fn canvas_size(&self) -> (i32, i32)
pub fn canvas_size(&self) -> (i32, i32)
Returns the current canvas dimensions as a tuple.
§Returns
Tuple containing (width, height) in pixels
Sourcepub fn is_context_lost(&self) -> bool
pub fn is_context_lost(&self) -> bool
Checks if the WebGL context has been lost.
Returns true if the context is lost and needs to be restored.
Use [restore_context] to recover from context loss.
Sourcepub fn restore_context(&mut self) -> Result<(), Error>
pub fn restore_context(&mut self) -> Result<(), Error>
Restores the WebGL context after a context loss event.
This method obtains a fresh WebGL2 context from the canvas and reinitializes the renderer state. After calling this, you must also recreate GPU resources in other components (textures, buffers, etc.).
§Returns
Ok(())- Context successfully restoredErr(Error)- Failed to obtain new WebGL context
§Note
This only restores the renderer’s context. You must separately call
recreation methods on TerminalGrid and FontAtlas to fully recover.