`Viewfinder.visibleGameSize` is not reapplied when the viewport changes size
メンテナーはふだん 1 日以内に返信
評価
この issue はまだ評価されていません。
説明
What happened?
The zoom that visibleGameSize sets depends on the size of the viewport, but it's only recalculated in Viewfinder.onGameResize (viewfinder.dart#L221-L224), not when the size of the viewport itself changes. This breaks in three ways:
- The viewport is resized by the game. When a
FixedSizeViewportis resized at runtime, for example to expand a minimap from 256×160 to 576×352, the zoom stays at 0.44. The level then only fills the middle of the expanded minimap, with 1296×792 units visible instead of 576×352. - The viewport of a camera that the game added itself is replaced.
camera.viewport = ...adds the new viewport after the viewfinder (camera_component.dart#L112-L117), so on a game resize, the viewfinder recalculates the zoom from the old size of the viewport, and only then is the viewport resized. The zoom is one resize behind from then on: after maximizing the window it's still the zoom for the old size, and after restoring it, it's the zoom for the maximized window.FlameGame.cameraisn't affected, sinceFlameGame.onGameResizeresizes the viewport of that camera first (flame_game.dart#L219), but every other camera is, like the cameras added for split-screen or minimaps. visibleGameSizeis set before the viewport has a size. For exampleCameraComponent(world: world)..viewfinder.visibleGameSize = Vector2(100, 100)before the camera is added to the game sets the zoom to 0, which throwszoom level must be positive: 0.0.
https://github.com/user-attachments/assets/72c6672a-39cc-4102-9304-4f15c1b29479
https://github.com/user-attachments/assets/e6adbf48-3354-4ae7-89c0-bc7320124d1f
What do you expect?
The zoom is recalculated whenever the size of the viewport changes, so the region given to visibleGameSize always fits into it, including when the viewport only gets its size later.
How can we reproduce this?
- Add a second camera with a
FixedSizeViewport(256, 160), and set itsvisibleGameSizeto the size of the level, 576×352. The zoom is 0.44. - Set
minimap.viewport.size = Vector2(576, 352). - The zoom stays at 0.44 instead of 1.0.
What steps should take to fix this?
The viewport calls Viewfinder.onViewportResize whenever its size changes (viewport.dart#L88). That will recalculate the zoom, which covers all three cases and makes the onGameResize override unnecessary. _updateZoom will also skip the update while the viewport doesn't have a size yet, instead of setting the zoom to 0.
Do have an example of where the bug occurs?
import 'dart:ui';
import 'package:flame/camera.dart';
import 'package:flame/components.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 {
final levelSize = Vector2(576, 352);
world.add(
RectangleComponent(
size: levelSize,
paint: Paint()..color = Colors.brown,
),
);
final minimap = CameraComponent(
world: world,
viewport: FixedSizeViewport(256, 160)..position = Vector2.all(16),
);
minimap.viewfinder.position = levelSize / 2;
add(minimap);
minimap.viewfinder.visibleGameSize = levelSize;
// Expand the minimap after two seconds.
add(
TimerComponent(
period: 2,
removeOnFinish: true,
onTick: () => minimap.viewport.size = Vector2(576, 352),
),
);
}
}
Expected: after two seconds, the level fills the expanded minimap. Actual: the zoom stays at 0.44, and the level stays small in the middle of the minimap.
For the second case, add the camera to the game yourself, replace its viewport, and then resize the window:
class ReproGame extends FlameGame {
@override
Future<void> onLoad() async {
final arena = World()
..add(
RectangleComponent(
size: Vector2(330, 196),
anchor: Anchor.center,
paint: Paint()..color = Colors.brown,
),
);
final camera = CameraComponent(
world: arena,
viewport: FixedAspectRatioViewport(aspectRatio: 16 / 9),
);
addAll([arena, camera]);
camera.viewfinder.visibleGameSize = Vector2(330, 196);
// Switch to filling the window, like from a display mode setting.
add(
TimerComponent(
period: 2,
removeOnFinish: true,
onTick: () => camera.viewport = MaxViewport(),
),
);
}
}
Expected: after the switch, the arena fits the window at every window size. Actual: after maximizing the window the arena is too small, and after restoring it the arena is cut off.
Relevant log output
# Only for the third case, otherwise nothing is logged or thrown.
'package:flame/src/camera/viewfinder.dart': Failed assertion: line 61 pos 12: 'value > 0': zoom level must be positive: 0.0
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
Are you interested in working on a PR for this?
- I want to work on this
- 主要言語
- Dart
- スター
- 10.8k
- フォーク
- 1.1k
- 平均マージ
- 1日 21時間
- マージ済み PR(30日)
- 62
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
flame-engine/flame のほかの issue
-
`Viewfinder.visibleGameSize` zooms in too far with a `FixedResolutionViewport`対応中かも @ufrshubham が 1 日前に担当しました。 オープンbug
flame-engine/flame#4203 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Camera bounds with `considerViewport: true` are not updated when the viewport is resized対応中かも @ufrshubham が 1 日前に担当しました。 オープンbug
flame-engine/flame#4202 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
`CameraComponent.setBounds` with `considerViewport: true` only works for bounds that start at (0, 0), at zoom 1対応中かも @ufrshubham が 1 日前に担当しました。 オープンbug
flame-engine/flame#4201 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
`findPostProcessFromContext` returns `null` for components rendered with `CameraComponent.postProcess`対応中かも @ufrshubham が 1 日前に担当しました。 オープン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 が 2 日前に担当しました。 オープンbug
flame-engine/flame#4191 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
flame-engine/flame の issue をすべて見る
似ている issue
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
InlitX/GymMane#311 · リアクション 1 件 ·
メンテナーはふだん 4 日以内に返信
-
Remove Info buttonオープンenhancement user request
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
mill-road-winter-fair/mill_road_winter_fair_app#646 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
[CarouselView] Crash (RangeError / ArgumentError) when children or itemCount is empty with infinite: true or CarouselController.animateToItem対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンp: material_ui package platform-android platform-ios platform-linux platform-macos platform-web team-design
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
flutter/flutter#195878 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
good first issue help wanted parser-pack
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
pesaclarity/ujumbe-parsers#30 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
AimesSoft/NipaPlay-Reload#1092 ·
メンテナーはふだん 1 日以内に返信