Implement basic Picture.toImage via BitmapCanvas (flutter/engine#13391)
Implement basic Picture.toImage via BitmapCanvas and update the dartdocs.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
repository: https://github.com/flutter/goldens.git
|
||||
revision: 686dd320f6cce6da9a7a43e3ec9c0147f39eb19d
|
||||
revision: 7935a97f89a6af5ae5182b2b5e59debda0189984
|
||||
@@ -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<Image> toImage(int width, int height) => null;
|
||||
Future<Image> 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.
|
||||
|
||||
@@ -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<void> _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)));
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user