From b207a48175f2677d7a9ce24045ca0bd50a817df4 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Sat, 17 Aug 2024 11:43:43 +0800 Subject: [PATCH] feat!: resize canvas on web --- .../lib/thermion/widgets/thermion_widget.dart | 6 ++++-- .../lib/thermion_flutter_web.dart | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/thermion_flutter/thermion_flutter/lib/thermion/widgets/thermion_widget.dart b/thermion_flutter/thermion_flutter/lib/thermion/widgets/thermion_widget.dart index 4cd26d4c..e69d2eb1 100644 --- a/thermion_flutter/thermion_flutter/lib/thermion/widgets/thermion_widget.dart +++ b/thermion_flutter/thermion_flutter/lib/thermion/widgets/thermion_widget.dart @@ -83,9 +83,11 @@ class _ThermionWidgetState extends State { @override Widget build(BuildContext context) { if (_texture?.usesBackingWindow == true) { - return Stack(children: [ + return ResizeObserver( + onResized: _resizeTexture, + child: Stack(children: [ Positioned.fill(child: CustomPaint(painter: TransparencyPainter())) - ]); + ])); } if (_texture == null || _resizing) { diff --git a/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart b/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart index 4a097bc0..765c5dc2 100644 --- a/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart +++ b/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart @@ -4,8 +4,12 @@ import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_in import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; import 'package:web/web.dart'; +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; class ThermionFlutterWebPlugin extends ThermionFlutterPlatform { + ThermionViewerWasm? _viewer; + static void registerWith(Registrar registrar) { ThermionFlutterPlatform.instance = ThermionFlutterWebPlugin(); } @@ -24,6 +28,13 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform { @override Future resizeTexture(ThermionFlutterTexture texture, int width, int height, int offsetLeft, int offsetRight) async { + final canvas = document.getElementById("canvas") as HTMLCanvasElement; + canvas.width = width; + canvas.height = height; + + _viewer!.updateViewportAndCameraProjection(width, height, 1.0); + + print("Resized canvas to ${canvas.width}x${canvas.height}"); return ThermionFlutterTexture(null, null, 0, 0, null); } @@ -35,8 +46,8 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform { var width = window.innerWidth; var height = window.innerHeight; - var viewer = ThermionViewerWasm(assetPathPrefix: "/assets/"); - await viewer.initialize(width, height, uberArchivePath: uberArchivePath); - return viewer; + _viewer = ThermionViewerWasm(assetPathPrefix: "/assets/"); + await _viewer!.initialize(width, height, uberArchivePath: uberArchivePath); + return _viewer!; } }