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

未关闭
#4,202 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@ufrshubham 已经在做这个了。

开始于 2026年11月2日。

  • #4206 来自 @ufrshubham —— 未关闭

评估

这个 Issue 还没有评估数据。

描述

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
主要语言
Dart
星标
10.8k
派生
1.1k
平均合并
1 天 23 小时
30 天内合并 PR
59

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

flame-engine/flame 的其他 Issue

查看 flame-engine/flame 的全部 Issue

相似的 Issue

更多 Dart Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。