Struct Canvas2D

Source
pub struct Canvas2D {
    pub size: Vec2,
    pub screen: Vec2,
    pub scale: f32,
    pub offset: Vec2,
}
Expand description

Fits a Y-down world into a logical-pixel screen, with aspect-preserving letterboxing. Uses camera zoom rather than a raw GPU viewport, so high-DPI screens behave identically. This transform does not scissor drawing outside the authored world bounds. Public fields remain available for existing struct literals; manually changed fields are the caller’s responsibility. Prefer the checked constructors.

Fields§

§size: Vec2

Positive world dimensions; fitting never changes the authored framing.

§screen: Vec2

Full host screen dimensions in logical pixels, including reserved HUD space.

§scale: f32

Logical screen pixels per world unit.

§offset: Vec2

Displayed world’s top-left in absolute logical screen pixels.

Implementations§

Source§

impl Canvas2D

Source

pub fn fit(size: Vec2, screen: Vec2) -> Canvas2D

Legacy full-screen fit, preserving its original arithmetic and signature.

§Panics

Panics for nonfinite or nonpositive world/screen dimensions. Use Self::try_fit for checked input and suspended/zero-sized screens.

Source

pub fn try_fit( size: Vec2, screen: Vec2, ) -> Result<Option<Canvas2D>, HudLayoutError>

Checked full-screen fit. World dimensions must be finite and positive; screen dimensions must be finite and nonnegative. Returns Ok(None) when either screen dimension is below one logical pixel. Invalid dimensions or an unrepresentable transform return HudLayoutError::InvalidLayout. Skip world rendering and input when there is no canvas; reconstruction after resize has no retained offsets or state.

use ferravex_render::{canvas::Canvas2D, vec2};
let world = vec2(1280., 800.);
let canvas = Canvas2D::try_fit(world, vec2(390., 844.))?.unwrap();
assert_eq!(canvas.size, world);
assert!(Canvas2D::try_fit(world, vec2(0., 0.))?.is_none());
assert!(Canvas2D::try_fit(vec2(0., 800.), vec2(390., 844.)).is_err());
Source

pub fn try_fit_in( size: Vec2, screen: Vec2, content: Rect, ) -> Result<Option<Canvas2D>, HudLayoutError>

Checked fit into an explicit content rectangle, in absolute host logical pixels. screen is the full logical screen, not content.size. Content must be finite, nonnegative and wholly inside that screen. Validate dimensions before returning Ok(None) for content below one logical pixel on either axis. Other failures, including f32 overflow, underflow or collapsed displayed edges, are InvalidLayout.

Fractional fits round scale inward when necessary to stay in content. HUD text is unaffected. Use Self::pointer_half_open to keep shared right/bottom edges owned by adjacent HUD rectangles.

use ferravex_play::ui::Rect;
use ferravex_render::{canvas::Canvas2D, vec2};
let content = Rect { min: [10., 80.], size: [760., 480.] };
let canvas = Canvas2D::try_fit_in(vec2(400., 200.), vec2(800., 600.), content)?
    .unwrap();
assert_eq!(canvas.offset, vec2(10., 130.));
assert_eq!(canvas.pointer_half_open(vec2(390., 320.)), Some(vec2(200., 100.)));
assert!(canvas.pointer_half_open(vec2(40., 40.)).is_none());
Source

pub fn from_fit(fit: CanvasFit) -> Canvas2D

Adapt task 271’s already-checked world fit without recomputing geometry. Self::camera honors its asymmetric offset automatically. This is a pure, allocation-free adapter; it does not change host state or input.

use ferravex_play::ui::{HudEdge, HudRegions};
use ferravex_render::{Camera, canvas::Canvas2D, vec2};
let mut hud = HudRegions::new([800., 600.], [10., 20., 30., 40.], [0.; 2])?;
hud.reserve("header", HudEdge::Top, 60.)?;
if let Some(fit) = hud.fit_canvas([400., 200.])? {
    let canvas = Canvas2D::from_fit(fit);
    let point = vec2(200., 100.);
    let logical = canvas.project(point);
    assert_eq!(canvas.pointer_half_open(logical), Some(point));
    let ndc = canvas.camera().matrix(800., 600.).project_point3(point.extend(0.));
    let dpr = 2.; // Synthetic host framebuffer conversion, not device evidence.
    let framebuffer = vec2((ndc.x + 1.) * 400., (1. - ndc.y) * 300.) * dpr;
    assert!((framebuffer / dpr).distance(logical) < 0.001);
}
Source

pub fn project(self, point: Vec2) -> Vec2

World units to absolute logical screen pixels, without clipping.

Source

pub fn unproject(self, point: Vec2) -> Vec2

Absolute logical screen pixels to world units, without clipping.

Source

pub fn pointer(self, point: Vec2) -> Option<Vec2>

Returns None for input in the letterbox margin, outside the playable canvas. Preserves inclusive right/bottom edges; use Self::pointer_half_open when the world shares those edges with reserved HUD controls.

Source

pub fn pointer_half_open(self, point: Vec2) -> Option<Vec2>

Maps mouse/touch positions in logical screen pixels to the displayed world. Left/top edges belong to the world; right/bottom edges do not. Membership uses displayed screen geometry before division, matching Viewport hits. Nonfinite input and nonpositive/nonfinite transform dimensions are inert.

use ferravex_render::{canvas::Canvas2D, vec2};
let canvas = Canvas2D::try_fit(vec2(400., 200.), vec2(800., 600.))?.unwrap();
assert_eq!(canvas.pointer_half_open(canvas.project(vec2(0., 0.))), Some(vec2(0., 0.)));
assert_eq!(canvas.pointer_half_open(canvas.project(canvas.size)), None);
assert_eq!(canvas.pointer(canvas.project(canvas.size)), Some(canvas.size));
Source

pub fn camera(self) -> Camera2D

Camera for the full screen, including translation into reserved content. Its offset is normalized (positive Y up); canvas offsets remain logical pixels (positive Y down). Legacy centered fits retain zero camera offset.

Trait Implementations§

Source§

impl Clone for Canvas2D

Source§

fn clone(&self) -> Canvas2D

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Canvas2D

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Copy for Canvas2D

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.