diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 02032f0e65..0eacb1bca4 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -3893,8 +3893,9 @@ class Picture extends NativeFieldWrapperClass2 { /// Creates an image from this picture. /// - /// The picture is rasterized using the number of pixels specified by the - /// given width and height. + /// The returned image will be `width` pixels wide and `height` pixels high. + /// The picture is rasterized within the 0 (left), 0 (top), `width` (right), + /// `height` (bottom) bounds. Content outside these bounds is clipped. /// /// Although the image is returned synchronously, the picture is actually /// rasterized the first time the image is drawn and then cached. diff --git a/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml b/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml index fa2c3c0f72..e82417f928 100644 --- a/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml +++ b/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml @@ -1,2 +1,2 @@ repository: https://github.com/flutter/goldens.git -revision: 686dd320f6cce6da9a7a43e3ec9c0147f39eb19d \ No newline at end of file +revision: 7935a97f89a6af5ae5182b2b5e59debda0189984 \ No newline at end of file diff --git a/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart index 792abc4d64..7c861fd0f1 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart @@ -1063,12 +1063,26 @@ class Picture { /// Creates an image from this picture. /// - /// The picture is rasterized using the number of pixels specified by the - /// given width and height. + /// The returned image will be `width` pixels wide and `height` pixels high. + /// The picture is rasterized within the 0 (left), 0 (top), `width` (right), + /// `height` (bottom) bounds. Content outside these bounds is clipped. /// /// Although the image is returned synchronously, the picture is actually /// rasterized the first time the image is drawn and then cached. - Future toImage(int width, int height) => null; + Future toImage(int width, int height) async { + final engine.BitmapCanvas canvas = engine.BitmapCanvas(Rect.fromLTRB(0, 0, width.toDouble(), height.toDouble())); + recordingCanvas.apply(canvas); + final String imageDataUrl = canvas.canvas.toDataUrl(); + final html.ImageElement imageElement = html.ImageElement() + ..src = imageDataUrl + ..width = width + ..height = height; + return engine.HtmlImage( + imageElement, + width, + height, + ); + } /// Release the resources used by this object. The object is no longer usable /// after this method is called. diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/path_transform_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/path_transform_test.dart index 6b87c0af7f..f5221ea4c4 100644 --- a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/path_transform_test.dart +++ b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/path_transform_test.dart @@ -4,20 +4,17 @@ import 'dart:html' as html; import 'dart:math' as math; -import 'dart:typed_data'; import 'package:ui/ui.dart' hide TextStyle; import 'package:ui/src/engine.dart'; import 'package:test/test.dart'; -import '../../matchers.dart'; import 'package:web_engine_tester/golden_tester.dart'; void main() async { const double screenWidth = 600.0; const double screenHeight = 800.0; const Rect screenRect = Rect.fromLTWH(0, 0, screenWidth, screenHeight); - final Paint testPaint = Paint()..color = const Color(0xFFFF0000); // Commit a recording canvas to a bitmap, and compare with the expected Future _checkScreenshot(RecordingCanvas rc, String fileName, @@ -163,7 +160,6 @@ void main() async { test('Should draw transformed arc.', () async { final RecordingCanvas rc = RecordingCanvas(const Rect.fromLTRB(0, 0, 500, 500)); - const double yStart = 20; final Path path = Path(); path.moveTo(350, 280); @@ -198,7 +194,6 @@ void main() async { test('Should draw transformed rrect.', () async { final RecordingCanvas rc = RecordingCanvas(const Rect.fromLTRB(0, 0, 500, 500)); - const double yStart = 20; final Path path = Path(); path.addRRect(RRect.fromLTRBR(50, 50, 300, 200, Radius.elliptical(4, 8))); diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/picture_golden_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/picture_golden_test.dart new file mode 100644 index 0000000000..8fc1f01adb --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/picture_golden_test.dart @@ -0,0 +1,40 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:html' as html; + +import 'package:ui/src/engine.dart'; +import 'package:ui/ui.dart' as ui; + +import 'package:test/test.dart'; +import 'package:web_engine_tester/golden_tester.dart'; + +void main() { + group('Picture', () { + test('toImage produces an image', () async { + final ui.PictureRecorder recorder = ui.PictureRecorder(); + final RecordingCanvas canvas = recorder.beginRecording(ui.Rect.fromLTRB(0, 0, 200, 100)); + canvas.drawCircle( + const ui.Offset(100, 50), + 40, + ui.Paint() + ..color = ui.Color.fromARGB(255, 255, 100, 100), + ); + final ui.Picture picture = recorder.endRecording(); + final HtmlImage image = await picture.toImage(200, 100); + expect(image, isNotNull); + html.document.body + ..style.margin = '0' + ..append(image.imgElement); + try { + await matchGoldenFile( + 'picture_to_image.png', + region: ui.Rect.fromLTRB(0, 0, 200, 100), + ); + } finally { + image.imgElement.remove(); + } + }); + }); +}