flo_draw icon indicating copy to clipboard operation
flo_draw copied to clipboard

`center_region` does not scale canvas horizontally to fit window (only vertical scaling works)

Open rcook opened this issue 6 months ago • 1 comments

When using center_region to fit a logical canvas region to the window, the canvas only scales vertically to fit the window. If the window is made wider than the canvas aspect ratio, the canvas is clipped horizontally instead of being fully visible (letterboxed). There appears to be no way to ensure the entire logical region is always visible, scaled to fit both axes, with possible letterboxing or pillarboxing.

Steps to reproduce the behaviour

  1. Run the following minimal example.
  2. Resize the window to be much wider than the canvas aspect ratio.
  3. Observe that the red border (logical region) is clipped horizontally and not fully visible.
use flo_canvas::{Color, GraphicsContext, LayerId};
use flo_draw::{create_drawing_window_with_events, with_2d_graphics};

const WIDTH: f32 = 1280.0;
const HEIGHT: f32 = 1024.0;

fn main() {
    with_2d_graphics(|| {
        let (canvas, _events) = create_drawing_window_with_events("Test");

        canvas.draw(|gc| {
            gc.layer(LayerId(0));
            gc.clear_canvas(Color::Rgba(0.0, 0.0, 0.0, 1.0));
            gc.canvas_height(HEIGHT);
            gc.center_region(0.0, 0.0, WIDTH, HEIGHT);

            // Draw a border to show the logical region
            gc.stroke_color(Color::Rgba(1.0, 0.0, 0.0, 1.0));
            gc.line_width(10.0);
            gc.move_to(0.0, 0.0);
            gc.line_to(WIDTH, 0.0);
            gc.line_to(WIDTH, HEIGHT);
            gc.line_to(0.0, HEIGHT);
            gc.line_to(0.0, 0.0);
            gc.stroke();
        });
    });
}

Expected behaviour

The entire logical region (0,0)-(1280,1024) should always be visible in the window, scaled to fit both axes, with letterboxing or pillarboxing as needed.

Actual behaviour

The logical region is only scaled vertically to fit the window. If the window is wider than the canvas aspect ratio, the logical region is clipped horizontally.

Additional context

  • There is no viewport or similar method in the public API.
  • Redrawing all content and calling center_region on resize does not fix the issue.
  • This makes it impossible to guarantee the entire canvas is always visible in the window.

rcook avatar Jul 10 '25 18:07 rcook