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: Vec2Positive world dimensions; fitting never changes the authored framing.
screen: Vec2Full host screen dimensions in logical pixels, including reserved HUD space.
scale: f32Logical screen pixels per world unit.
offset: Vec2Displayed world’s top-left in absolute logical screen pixels.
Implementations§
Source§impl Canvas2D
impl Canvas2D
Sourcepub fn fit(size: Vec2, screen: Vec2) -> Canvas2D
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.
Sourcepub fn try_fit(
size: Vec2,
screen: Vec2,
) -> Result<Option<Canvas2D>, HudLayoutError>
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());Sourcepub fn try_fit_in(
size: Vec2,
screen: Vec2,
content: Rect,
) -> Result<Option<Canvas2D>, HudLayoutError>
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());Sourcepub fn from_fit(fit: CanvasFit) -> Canvas2D
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);
}Sourcepub fn project(self, point: Vec2) -> Vec2
pub fn project(self, point: Vec2) -> Vec2
World units to absolute logical screen pixels, without clipping.
Sourcepub fn unproject(self, point: Vec2) -> Vec2
pub fn unproject(self, point: Vec2) -> Vec2
Absolute logical screen pixels to world units, without clipping.
Sourcepub fn pointer(self, point: Vec2) -> Option<Vec2>
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.
Sourcepub fn pointer_half_open(self, point: Vec2) -> Option<Vec2>
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));