[web] Implement basic radial gradient (TileMode.clamp, no transform) (flutter/engine#12811)
* Add GradientRadial paintStyle implementation
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
repository: https://github.com/flutter/goldens.git
|
||||
revision: 8859d44913f7e63ff61f1a2662ec5eaa8708fe79
|
||||
revision: 7efcec3e8b0bbb6748a992b23a0a89300aa323c7
|
||||
@@ -162,7 +162,8 @@ class GradientLinear extends EngineGradient {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(flutter_web): Add screenshot tests when infra is ready.
|
||||
// TODO(flutter_web): For transforms and tile modes implement as webgl
|
||||
// shader instead. See https://github.com/flutter/flutter/issues/32819
|
||||
class GradientRadial extends EngineGradient {
|
||||
GradientRadial(this.center, this.radius, this.colors, this.colorStops,
|
||||
this.tileMode, this.matrix4)
|
||||
@@ -177,20 +178,25 @@ class GradientRadial extends EngineGradient {
|
||||
|
||||
@override
|
||||
Object createPaintStyle(html.CanvasRenderingContext2D ctx) {
|
||||
// TODO(flutter_web): see https://github.com/flutter/flutter/issues/32819
|
||||
if (matrix4 != null && !Matrix4.fromFloat64List(matrix4).isIdentity()) {
|
||||
throw UnimplementedError('matrix4 not supported in GradientRadial shader');
|
||||
throw UnimplementedError(
|
||||
'matrix4 not supported in GradientRadial shader');
|
||||
}
|
||||
final html.CanvasGradient gradient =
|
||||
ctx.createRadialGradient(center.dx, center.dy, 0, center.dx, center.dy, radius);
|
||||
if (tileMode != ui.TileMode.clamp) {
|
||||
throw UnimplementedError(
|
||||
'TileMode not supported in GradientRadial shader');
|
||||
}
|
||||
final html.CanvasGradient gradient = ctx.createRadialGradient(
|
||||
center.dx, center.dy, 0, center.dx, center.dy, radius);
|
||||
if (colorStops == null) {
|
||||
assert(colors.length == 2);
|
||||
gradient.addColorStop(0, colors[0].toCssString());
|
||||
gradient.addColorStop(1, colors[1].toCssString());
|
||||
return gradient;
|
||||
}
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
gradient.addColorStop(colorStops[i], colors[i].toCssString());
|
||||
} else {
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
gradient.addColorStop(colorStops[i], colors[i].toCssString());
|
||||
}
|
||||
}
|
||||
return gradient;
|
||||
}
|
||||
|
||||
@@ -888,7 +888,8 @@ class Canvas {
|
||||
}
|
||||
|
||||
void drawVertices(Vertices vertices, BlendMode blendMode, Paint paint) {
|
||||
assert(vertices != null); // vertices is checked on the engine side
|
||||
if (vertices == null) return;
|
||||
//assert(vertices != null); // vertices is checked on the engine side
|
||||
assert(paint != null);
|
||||
assert(blendMode != null);
|
||||
_canvas.drawVertices(vertices, blendMode, paint);
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
// 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/ui.dart' hide TextStyle;
|
||||
import 'package:ui/src/engine.dart';
|
||||
import 'package:test/test.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);
|
||||
|
||||
// Commit a recording canvas to a bitmap, and compare with the expected
|
||||
Future<void> _checkScreenshot(RecordingCanvas rc, String fileName,
|
||||
{Rect region = const Rect.fromLTWH(0, 0, 500, 500),
|
||||
bool write = false}) async {
|
||||
final EngineCanvas engineCanvas = BitmapCanvas(screenRect);
|
||||
rc.apply(engineCanvas);
|
||||
|
||||
// Wrap in <flt-scene> so that our CSS selectors kick in.
|
||||
final html.Element sceneElement = html.Element.tag('flt-scene');
|
||||
try {
|
||||
sceneElement.append(engineCanvas.rootElement);
|
||||
html.document.body.append(sceneElement);
|
||||
await matchGoldenFile('$fileName.png', region: region, write: write);
|
||||
} finally {
|
||||
// The page is reused across tests, so remove the element after taking the
|
||||
// golden screenshot.
|
||||
sceneElement.remove();
|
||||
}
|
||||
}
|
||||
|
||||
setUp(() async {
|
||||
debugEmulateFlutterTesterEnvironment = true;
|
||||
await webOnlyInitializePlatform();
|
||||
webOnlyFontCollection.debugRegisterTestFonts();
|
||||
await webOnlyFontCollection.ensureFontsLoaded();
|
||||
});
|
||||
|
||||
Future<void> _testGradient(String fileName, Shader shader,
|
||||
{Rect paintRect = const Rect.fromLTRB(50, 50, 300, 300),
|
||||
Rect shaderRect = const Rect.fromLTRB(50, 50, 300, 300)}) async {
|
||||
final RecordingCanvas rc =
|
||||
RecordingCanvas(const Rect.fromLTRB(0, 0, 500, 500));
|
||||
final Paint paint = Paint()..shader = shader;
|
||||
final Path path = Path();
|
||||
path.addRect(paintRect);
|
||||
rc.drawPath(path, paint);
|
||||
await _checkScreenshot(rc, fileName);
|
||||
}
|
||||
|
||||
test('Should draw centered radial gradient.', () async {
|
||||
Rect shaderRect = const Rect.fromLTRB(50, 50, 300, 300);
|
||||
await _testGradient(
|
||||
'radial_gradient_centered',
|
||||
Gradient.radial(
|
||||
Offset((shaderRect.left + shaderRect.right) / 2,
|
||||
(shaderRect.top + shaderRect.bottom) / 2),
|
||||
shaderRect.width / 2,
|
||||
[
|
||||
const Color.fromARGB(255, 0, 0, 0),
|
||||
const Color.fromARGB(255, 0, 0, 255)
|
||||
]),
|
||||
shaderRect: shaderRect);
|
||||
});
|
||||
|
||||
test('Should draw right bottom centered radial gradient.', () async {
|
||||
Rect shaderRect = const Rect.fromLTRB(50, 50, 300, 300);
|
||||
await _testGradient(
|
||||
'radial_gradient_right_bottom',
|
||||
Gradient.radial(
|
||||
Offset(shaderRect.right, shaderRect.bottom), shaderRect.width / 2, [
|
||||
const Color.fromARGB(255, 0, 0, 0),
|
||||
const Color.fromARGB(255, 0, 0, 255)
|
||||
]),
|
||||
shaderRect: shaderRect);
|
||||
});
|
||||
|
||||
test('Should draw with radial gradient with TileMode.clamp.', () async {
|
||||
Rect shaderRect = const Rect.fromLTRB(50, 50, 100, 100);
|
||||
await _testGradient(
|
||||
'radial_gradient_tilemode_clamp',
|
||||
Gradient.radial(
|
||||
Offset((shaderRect.left + shaderRect.right) / 2,
|
||||
(shaderRect.top + shaderRect.bottom) / 2),
|
||||
shaderRect.width / 2,
|
||||
[
|
||||
const Color.fromARGB(255, 0, 0, 0),
|
||||
const Color.fromARGB(255, 0, 0, 255)
|
||||
],
|
||||
<double>[0.0, 1.0],
|
||||
TileMode.clamp),
|
||||
shaderRect: shaderRect);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user