Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

[Question] What are x_bounds and y_bounds in Canvas? #286

Closed
rahulwa opened this issue May 9, 2020 · 3 comments
Closed

[Question] What are x_bounds and y_bounds in Canvas? #286

rahulwa opened this issue May 9, 2020 · 3 comments

Comments

@rahulwa
Copy link

rahulwa commented May 9, 2020

First time, I am working on any UI app so I don't know much about it. I tried to go through examples but unable to understand what are x or y bounds.
Also hacking on my first Rust project :)
Thanks in advance.

@fdehau
Copy link
Owner

fdehau commented May 10, 2020

Those are used to define the viewport of the canvas. Only the points whose coordinates are within the viewport are displayed. When you render the canvas using Frame::render_widget, you give an area to draw the widget to (a Rect) and the crate translates the floating point coordinates to those used by our internal terminal representation.

For example, when we draw the entire world map, the bounds are the ranges for latitude and longitude as floats:

            let canvas = Canvas::default()
                .block(Block::default().borders(Borders::ALL).title("World"))
                .paint(|ctx| {
                    ctx.draw(&Map {
                        color: Color::White,
                        resolution: MapResolution::High,
                    });
                    ctx.print(app.x, -app.y, "You are here", Color::Yellow);
                })
                .x_bounds([-180.0, 180.0])
                .y_bounds([-90.0, 90.0]);
            f.render_widget(canvas, chunks[0]);

If you were to "zoom" to a certain part of the world you may want to choose different bounds.

@fdehau fdehau closed this as completed May 10, 2020
@gmale
Copy link

gmale commented Feb 28, 2021

Great explanation. Is it possible to add that explanation to the docs for x_bounds and y_bounds?

@rishadbaniya
Copy link

Great explanation. Is it possible to add that explanation to the docs for x_bounds and y_bounds?

was thinking the same

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants