pub struct SpriteAtlas { /* private fields */ }Expand description
A named collection of sprite regions packed into one shared image.
Packing many sprites into a single texture keeps the number of canvas
draw calls (and image decode stalls) low. Each entry stores a source
rectangle in pixels; SpriteAtlas::uv converts it to normalized
coordinates for GPU samplers, and SpriteAtlas::draw blits it
directly through the 2D canvas API.
Implementations§
Source§impl SpriteAtlas
Implements image-backed drawing, UV conversion, and recording for SpriteAtlas.
impl SpriteAtlas
Implements image-backed drawing, UV conversion, and recording for SpriteAtlas.
Sourcepub fn create(image: HtmlImageElement) -> SpriteAtlas
pub fn create(image: HtmlImageElement) -> SpriteAtlas
Sourcepub fn insert(&mut self, name: &str, region: Rect)
pub fn insert(&mut self, name: &str, region: Rect)
Inserts or replaces the source rectangle stored under a name.
§Arguments
&str- The sprite name.Rect- The source rectangle in atlas pixels.
Sourcepub fn uv(&self, region: Rect) -> UvRect
pub fn uv(&self, region: Rect) -> UvRect
Returns the normalized texture coordinates for one atlas region.
Normalizes against the image’s intrinsic (naturalWidth /
naturalHeight) size, which is the atlas’s true texture size
regardless of any CSS size applied to the element. A zero-sized
atlas — not yet loaded, or decoded with no intrinsic size — yields
an all-zero UvRect rather than dividing by zero, so the result is
never NaN or infinite.
§Arguments
Rect- The source rectangle in atlas pixels.
§Returns
UvRect- The normalized(u0, v0, u1, v1)coordinates.
Sourcepub fn normalize_uv(region: Rect, width: f64, height: f64) -> UvRect
pub fn normalize_uv(region: Rect, width: f64, height: f64) -> UvRect
Normalizes a pixel rectangle against an explicit atlas size.
The pure half of SpriteAtlas::uv, separated so the arithmetic
can be exercised without an image element. A non-positive width
or height yields an all-zero UvRect instead of dividing by zero.
§Arguments
Rect- The source rectangle in atlas pixels.f64- The atlas width in pixels.f64- The atlas height in pixels.
§Returns
UvRect- The normalized(u0, v0, u1, v1)coordinates.
Sourcepub fn draw(&self, context: &CanvasRenderingContext2d, name: &str, dest: Rect)
pub fn draw(&self, context: &CanvasRenderingContext2d, name: &str, dest: Rect)
Blits one named atlas region into a destination rectangle.
Unknown names are a no-op. Uses the canvas transform already in effect, so the caller controls world positioning.
§Arguments
&CanvasRenderingContext2d- The canvas context.&str- The sprite name to blit.Rect- The destination rectangle in current canvas space.
Sourcepub fn record(
&self,
list: &mut DrawList,
name: &str,
dest_position: Vector2D,
dest_width: f64,
dest_height: f64,
)
pub fn record( &self, list: &mut DrawList, name: &str, dest_position: Vector2D, dest_width: f64, dest_height: f64, )
Records one named atlas region into a deferred draw list.
Unknown names are a no-op, so callers do not need to probe with
SpriteAtlas::get first.
§Arguments
&mut DrawList- The draw list to record into.&str- The sprite name to blit.Vector2D- The destination top-left position in world space.f64- The destination width in pixels.f64- The destination height in pixels.