Camera bounds with `considerViewport: true` are not updated when the viewport is resized

Đang mở
#4,202 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@ufrshubham đang làm issue này rồi.

Từ ngày 2/11/2026.

  • #4206 của @ufrshubham — đang mở

Đánh giá

Issue này chưa được đánh giá.

Mô tả

bug
What happened?

With camera.setBounds(bounds, considerViewport: true), the region that the camera has to stay in depends on the size of the viewport. ViewportAwareBoundsBehavior only recalculates it when the scale of the viewfinder or of the viewport's transform changes (viewport_aware_bounds_behavior.dart#L26-L30). MaxViewport and FixedAspectRatioViewport change their size, not their scale, when the window is resized, so the bounds keep using the size that the viewport had when setBounds was called. After the window is made bigger, the camera shows the empty space past the edges of the bounds. After it is made smaller, the edges can't be reached anymore.

Since the behavior listens to the viewport's transform directly, it also keeps listening to the old viewport after camera.viewport is replaced, and onRemove then removes the listener from the new viewport instead.

https://github.com/user-attachments/assets/1d7a89c2-d8de-4430-8cb6-42ca099c0e79

What do you expect?

The bounds are recalculated whenever the size of the viewport changes, so the camera never shows past the bounds, at any window size.

How can we reproduce this?
  1. With the default MaxViewport and an 800×600 window, call camera.setBounds(Rectangle.fromLTWH(0, 0, 1500, 900), considerViewport: true).
  2. Move the camera to the top-left corner. It stops at (400, 300), with the corner of the bounds at the corner of the screen.
  3. Resize the window to 1280×720. The camera stays at (400, 300) instead of (640, 360), and the left 240 and top 60 units of the screen show the empty space outside of the bounds.
What steps should take to fix this?

The viewport already notifies the viewfinder when its size changes, through Viewfinder.onViewportResize (viewport.dart#L88). The viewfinder will pass that on to its ViewportAwareBoundsBehavior, which recalculates the bounds whenever the size of the visible world region has changed. For zoom and angle changes, the behavior will listen to the viewfinder's transform instead of the viewport's transform, which also gets rid of the stale listener when the viewport is replaced.

Do have an example of where the bug occurs?
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart' show Colors;
import 'package:flutter/widgets.dart';

void main() => runApp(GameWidget(game: ReproGame()));

class ReproGame extends FlameGame {
  @override
  Future<void> onLoad() async {
    world.add(
      RectangleComponent(
        size: Vector2(1500, 900),
        paint: Paint()..color = Colors.brown,
      ),
    );
    camera.setBounds(
      Rectangle.fromLTWH(0, 0, 1500, 900),
      considerViewport: true,
    );
    // Look at the top-left corner of the level, then make the window bigger.
    camera.moveTo(Vector2(-10000, -10000));
  }
}

Run it on desktop or web and make the window bigger. Expected: the corner of the level stays at the corner of the screen. Actual: empty space shows up on the left and at the top.

Relevant log output
Nothing is logged or thrown.
Execute in a terminal and put output into the code block below
[√] Flutter (Channel stable, 3.47.9, on Microsoft Windows [Version 10.0.22631.6199], locale en-US)
    • Flutter version 3.47.9 on channel stable
    • Framework revision c197ca5199 (4 days ago), 2026-10-29 10:10:20 -0400
    • Engine revision 89c39edfdf
    • Dart version 3.13.7
    • DevTools version 2.60.0

[√] Windows Version (11 Pro 64-bit, 23H2, 2009)
[√] Android toolchain - develop for Android devices (Android SDK version 36.1.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2026 18.11.2)
[√] Connected device (3 available)
[√] Network resources

• No issues found!
Affected platforms

All

Other information

The fix builds on #4201, which changes how the bounds are calculated.

Are you interested in working on a PR for this?
  • I want to work on this
Ngôn ngữ chính
Dart
Star
10.8k
Fork
1.1k
Merge trung bình
1 ngày 22 giờ
Pull request đã merge (30 ngày)
54

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của flame-engine/flame

Tất cả issue của flame-engine/flame

Issue tương tự

Thêm issue về Dart

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.