Skip to content

draw.texture().area() documentation doesn't match what the function actually does #1025

@alexkammin

Description

@alexkammin

The documentation for the draw.texture().area() function mentions that:

Texture coordinates range from (0.0, 0.0) in the bottom left of the texture, to (1.0, 1.0) in the top right of the texture.

However, in the actual code (0.0, 0.0) seems to refer to the top left of the texture
The following code has the intended effect of displaying the top left quarter of the image, but it doesn't match the documentation

use nannou::{prelude::*, wgpu::Texture};

const IMAGE_NAME: &str = "image.jpg";
const IMAGE_WIDTH: u32 = 1024;

pub struct Model {
    texture: Texture,
}

impl Model {
    pub fn new(app: &App) -> Self {
        app.new_window()
            .size(IMAGE_WIDTH, IMAGE_WIDTH)
            .size_pixels(IMAGE_WIDTH, IMAGE_WIDTH)
            .view(view)
            .fullscreen()
            .build()
            .unwrap();

        let assets = app.assets_path().unwrap();
        let img_path = assets.join(IMAGE_NAME);
        let texture = wgpu::Texture::from_path(app, img_path).unwrap();

        Model { texture }
    }
}

fn view(app: &App, model: &Model, frame: Frame) {
    let draw = app.draw();

    // Top-left quarter
    let q1: Rect<f32> = Rect::from_corner_points([0., 0.],  [0.5, 0.5]);

    let half_width = IMAGE_WIDTH as f32 / 2.;
    let quarter_width = half_width / 2.;

    draw.texture(&model.texture)
        .area(q1)
        .w_h(half_width, half_width)
        .x_y(-quarter_width, quarter_width);

    draw.to_frame(app, &frame).unwrap();
}

Therefore, I'm pretty sure the docs should instead say:

Texture coordinates range from (0.0, 0.0) in the top left of the texture, to (1.0, 1.0) in the bottom right of the texture.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions