Skip to main content

Camera2d

Struct Camera2d 

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

2D camera mapping world coordinates into logical screen pixels.

center and all input positions are in world units. zoom is logical screen pixels per world unit. rotation is in radians.

Implementations§

Source§

impl Camera2d

Source

pub const MIN_ZOOM: f32 = MIN_ZOOM

Smallest zoom accepted by camera constructors and setters.

Source§

impl Camera2d

Source

pub fn new(center: Vec2, zoom: f32) -> Result<Self, Camera2dError>

Builds a camera centered on a world-space point.

zoom is logical screen pixels per world unit and must be finite and positive. Invalid values return Camera2dError::InvalidZoom instead of being silently clamped.

Source

pub fn center(self) -> Vec2

Returns the world-space point displayed at the viewport center.

Source

pub fn set_center(&mut self, center: Vec2) -> Result<(), Camera2dError>

Replaces the world-space point displayed at the viewport center. Invalid coordinates leave the current center unchanged.

Source

pub fn zoom(self) -> f32

Returns camera scale in logical screen pixels per world unit.

Source

pub fn set_zoom(&mut self, zoom: f32) -> Result<(), Camera2dError>

Replaces camera scale in logical screen pixels per world unit.

Invalid values return Camera2dError::InvalidZoom and leave the current zoom unchanged.

Source

pub fn rotation(self) -> f32

Returns clockwise screen rotation in radians.

Source

pub fn set_rotation(&mut self, rotation: f32) -> Result<(), Camera2dError>

Replaces clockwise screen rotation in radians. Non-finite values leave the current rotation unchanged.

Source

pub fn projection(self) -> Projection2d

Returns the pseudo-depth projection applied to camera-relative coordinates.

Source

pub fn set_projection(&mut self, projection: Projection2d)

Replaces the pseudo-depth projection applied to camera-relative coordinates.

Source

pub fn pan_by(&mut self, delta: Vec2) -> Result<(), Camera2dError>

Pans the displayed world center by a finite world-space delta.

Source

pub fn zoom_about_screen( &mut self, factor: f32, anchor: LogicalScreenPosition, viewport: LogicalViewport, ) -> Result<(), Camera2dError>

Changes zoom while keeping the depth-zero world point under anchor fixed.

anchor is measured in logical pixels. The operation is atomic: invalid factors, singular projections, or overflows leave this camera unchanged.

Source

pub fn fit_to_bounds( &mut self, bounds: Rect, padding: f32, viewport: LogicalViewport, ) -> Result<(), Camera2dError>

Centers and zooms the camera so finite non-empty world bounds fit a viewport.

padding is measured in logical pixels on every edge. The extent calculation accounts for this camera’s rotation and depth-zero tilt.

Source

pub fn world_to_screen( self, world: Vec2, viewport: LogicalViewport, ) -> Result<LogicalScreenPosition, Camera2dError>

Converts a world-space point into logical screen pixel coordinates.

Returns an error if finite values overflow during the transform.

Source

pub fn projected_world_to_screen( self, world: Vec2, depth: f32, viewport: LogicalViewport, ) -> Result<LogicalScreenPosition, Camera2dError>

Converts a world-space point and pseudo-depth into logical screen pixels.

Source

pub fn screen_to_world( self, screen: LogicalScreenPosition, viewport: LogicalViewport, ) -> Result<Vec2, Camera2dError>

Converts logical screen pixel coordinates back into world coordinates.

This is equivalent to Camera2d::screen_to_world_at_depth with depth = 0.0.

Source

pub fn screen_to_world_at_depth( self, screen: LogicalScreenPosition, depth: f32, viewport: LogicalViewport, ) -> Result<Vec2, Camera2dError>

Converts logical screen pixel coordinates into world coordinates on a pseudo-depth plane.

depth must match the same caller-defined scalar depth passed to Camera2d::projected_world_to_screen. If projection tilt is close to 90 degrees, the inverse mapping is singular and returns Camera2dError::SingularProjection.

Source

pub fn tween(self) -> Result<Tween<Self>, TweenError>

Creates a tween initialized with this camera state.

Trait Implementations§

Source§

impl Clone for Camera2d

Source§

fn clone(&self) -> Camera2d

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Camera2d

Source§

impl Debug for Camera2d

Source§

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

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

impl Default for Camera2d

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Interpolate for Camera2d

Source§

fn interpolate(self, end: Self, amount: f32) -> Self

Interpolates between self and end.
Source§

fn is_valid_interpolation_value(self) -> bool

Returns whether this value may be stored by a Tween. Read more
Source§

impl PartialEq for Camera2d

Source§

fn eq(&self, other: &Camera2d) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Camera2d

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,