pub struct WebpAnimation { /* private fields */ }Expand description
Represents a WebP animation, handling frame data, playback control, and rendering.
Implementations§
Source§impl WebpAnimation
impl WebpAnimation
Sourcepub fn from_file<P: AsRef<Path>>(path: P, app: &App) -> Result<Self, String>
pub fn from_file<P: AsRef<Path>>(path: P, app: &App) -> Result<Self, String>
Creates a new WebpAnimation instance by loading frames from a WebP file.
This function decodes the WebP file at the given path and prepares the animation for playback.
§Parameters
path: Path to the WebP file containing the animation.app: Reference to the NannouAppinstance, used for creating textures.
§Returns
A Result containing:
Ok(WebpAnimation): The animation instance if successful.Err(String): An error message if loading fails.
§Errors
Returns an error if the file cannot be decoded or contains no frames.
Examples found in repository?
8fn model(app: &App) -> Model {
9 // Create a new window
10 app.new_window().view(view).build().unwrap();
11
12 // Load the WEBP animation
13 let assets = app.assets_path().expect("Failed to find assets directory");
14 let webp_path = assets.join("animation.webp"); // Place 'animation.webp' in the 'assets' directory
15
16 // Initialize the animation
17 let animation =
18 WebpAnimation::from_file(&webp_path, app).expect("Failed to load WEBP animation");
19
20 Model { animation }
21}Sourcepub fn update(&mut self)
pub fn update(&mut self)
Updates the animation’s current frame based on elapsed time.
This function should be called in each frame of the main loop to keep the animation in sync with its intended frame durations.
Sourcepub fn texture(&self) -> &Texture
pub fn texture(&self) -> &Texture
Returns a reference to the current texture.
§Returns
A reference to the Texture of the current frame.
Examples found in repository?
28fn view(app: &App, model: &Model, frame: Frame) {
29 // Clear the frame
30 frame.clear(BLACK);
31
32 let win = app.window_rect();
33
34 // Define the rectangle where the animation will be drawn
35 let r = Rect::from_w_h(
36 model.animation.width() as f32,
37 model.animation.height() as f32,
38 )
39 .top_left_of(win);
40
41 let draw = app.draw();
42 draw.texture(model.animation.texture())
43 .xy(r.xy())
44 .wh(r.wh());
45
46 draw.to_frame(app, &frame).unwrap();
47}Sourcepub fn width(&self) -> u32
pub fn width(&self) -> u32
Returns the width of the current frame.
§Returns
The width (in pixels) of the current frame’s texture.
Examples found in repository?
28fn view(app: &App, model: &Model, frame: Frame) {
29 // Clear the frame
30 frame.clear(BLACK);
31
32 let win = app.window_rect();
33
34 // Define the rectangle where the animation will be drawn
35 let r = Rect::from_w_h(
36 model.animation.width() as f32,
37 model.animation.height() as f32,
38 )
39 .top_left_of(win);
40
41 let draw = app.draw();
42 draw.texture(model.animation.texture())
43 .xy(r.xy())
44 .wh(r.wh());
45
46 draw.to_frame(app, &frame).unwrap();
47}Sourcepub fn height(&self) -> u32
pub fn height(&self) -> u32
Returns the height of the current frame.
§Returns
The height (in pixels) of the current frame’s texture.
Examples found in repository?
28fn view(app: &App, model: &Model, frame: Frame) {
29 // Clear the frame
30 frame.clear(BLACK);
31
32 let win = app.window_rect();
33
34 // Define the rectangle where the animation will be drawn
35 let r = Rect::from_w_h(
36 model.animation.width() as f32,
37 model.animation.height() as f32,
38 )
39 .top_left_of(win);
40
41 let draw = app.draw();
42 draw.texture(model.animation.texture())
43 .xy(r.xy())
44 .wh(r.wh());
45
46 draw.to_frame(app, &frame).unwrap();
47}Sourcepub fn set_looping(&mut self, looping: bool)
pub fn set_looping(&mut self, looping: bool)
Sets whether the animation should loop after reaching the final frame.
§Parameters
looping: Iftrue, the animation will loop indefinitely. Iffalse, it will stop at the last frame.
Auto Trait Implementations§
impl Freeze for WebpAnimation
impl !RefUnwindSafe for WebpAnimation
impl Send for WebpAnimation
impl Sync for WebpAnimation
impl Unpin for WebpAnimation
impl !UnwindSafe for WebpAnimation
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<Swp, Dwp, T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<Swp, Dwp, T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T, U> ConvertInto<U> for Twhere
U: ConvertFrom<T>,
impl<T, U> ConvertInto<U> for Twhere
U: ConvertFrom<T>,
Source§fn convert_into(self) -> U
fn convert_into(self) -> U
Source§fn convert_unclamped_into(self) -> U
fn convert_unclamped_into(self) -> U
Source§fn try_convert_into(self) -> Result<U, OutOfBounds<U>>
fn try_convert_into(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains the unclamped color. Read moreSource§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more