Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 2.13 KB

scale.md

File metadata and controls

61 lines (47 loc) · 2.13 KB

Scale

Previously, we used custom_size of Sprite to specify the size of an image. The component is only useful when we know the exact size we are going to set. For the sizes that are relative to the original size, we have to resize the image by Transform.

We use the function Transform::from_scale to resize an object based on its original size.

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn(SpriteBundle {
        texture: asset_server.load("bevy_bird_dark.png"),
        transform: Transform::from_scale((2.0, 0.5, 1.0).into()),
        ..default()
    });
}

The function Transform::from_scale takes a struct Vec3 as a parameter, which specifies the scale factor for x, y and z-axis.

The full code is as follows:

use bevy::{
    app::{App, Startup},
    asset::AssetServer,
    core_pipeline::core_2d::Camera2dBundle,
    ecs::system::{Commands, Res},
    sprite::SpriteBundle,
    transform::components::Transform,
    utils::default,
    DefaultPlugins,
};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn(SpriteBundle {
        texture: asset_server.load("bevy_bird_dark.png"),
        transform: Transform::from_scale((2.0, 0.5, 1.0).into()),
        ..default()
    });
}

Result:

Scale

➡️ Next: Combining Multiple Transformation

📘 Back: Table of contents