Camera bounds with `considerViewport: true` are not updated when the viewport is resized
维护者通常 1 天内回复
评估
这个 Issue 还没有评估数据。
描述
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?
- With the default
MaxViewportand an 800×600 window, callcamera.setBounds(Rectangle.fromLTWH(0, 0, 1500, 900), considerViewport: true). - 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.
- 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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
flame-engine/flame 的其他 Issue
-
`Viewfinder.visibleGameSize` is not reapplied when the viewport changes size可能已有人在做 @ufrshubham 于 3 天前认领。 未关闭bug
flame-engine/flame#4204 · 已指派 1 人 ·
维护者通常 1 天内回复
-
`Viewfinder.visibleGameSize` zooms in too far with a `FixedResolutionViewport`可能已有人在做 @ufrshubham 于 3 天前认领。 未关闭bug
flame-engine/flame#4203 · 已指派 1 人 ·
维护者通常 1 天内回复
-
`CameraComponent.setBounds` with `considerViewport: true` only works for bounds that start at (0, 0), at zoom 1可能已有人在做 @ufrshubham 于 3 天前认领。 未关闭bug
flame-engine/flame#4201 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
`findPostProcessFromContext` returns `null` for components rendered with `CameraComponent.postProcess`可能已有人在做 @ufrshubham 于 3 天前认领。 未关闭bug
flame-engine/flame#4200 · 已指派 1 人 ·
维护者通常 1 天内回复
-
`ComponentPool` returns components to the pool while they are still in use, and loses components removed before mounting可能已有人在做 @ufrshubham 于 4 天前认领。 未关闭bug
flame-engine/flame#4191 · 已指派 1 人 ·
维护者通常 1 天内回复
查看 flame-engine/flame 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
MaikuB/flutter_local_notifications#2866 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
madeofpendletonwool/PinePods#1078 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
carp-dk/cognition_package#40 ·
-
难度 2/5 1-3 小时 新手友好度 86/100
daniel-pimenta-cc/ledor#58 ·
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复