Skip to content

Canvas.drawVertices does not use anti-aliasing #77485

@NWalker1208

Description

@NWalker1208

The drawVertices method does not use any anti-aliasing, even when it is enabled in the Paint settings.

Steps to Reproduce

  1. Draw a set of vertices with drawVertices.
  2. Draw the same set using drawPath with Path.addPolygon.
  3. Use the same Paint for both methods.

Expected results:
Both drawings should be anti-aliased.

Actual results:
The drawing of drawVertices is not anti-aliased.

Logs flutter analyze
Analyzing vertices_test...
No issues found! (ran in 2.1s)

flutter doctor -v

[√] Flutter (Channel dev, 2.1.0-10.0.pre, on Microsoft Windows [Version 10.0.19042.844], locale en-US)
    • Flutter version 2.1.0-10.0.pre at d:\Developer\flutter
    • Framework revision cc9b78fc5c (8 days ago), 2021-02-25 13:26:03 -0800
    • Engine revision a252ec09b7
    • Dart version 2.13.0 (build 2.13.0-77.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at D:\Developer\Android\SDK
    • Platform android-30, build-tools 30.0.1
    • ANDROID_SDK_ROOT = D:\Developer\Android\SDK
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.8.5)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.8.31005.135
    • Windows 10 SDK version 10.0.18362.0

[√] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] VS Code, 64-bit edition (version 1.52.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.19.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19042.844]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 89.0.4389.72
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 88.0.705.81

• No issues found!
Sample Code
import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

enum PaintMethod {
  /// Path uses Path.addPolygon to draw a path.
  path,

  /// Vertices uses the drawVertices method. No anti-aliasing is applied.
  vertices,
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Vertex Test',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(
        shape: [
          /// Creates a 15 sided polygon
          for (int i = 0; i < 16; i++)
            Offset(50, 50) + Offset.fromDirection(2 * pi * (i / 15), 30),
        ],
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, this.shape = const []}) : super(key: key);

  /// The shape to be painted.
  final List<Offset> shape;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  /// The method to use when painting.
  PaintMethod method = PaintMethod.path;

  void setMethod(PaintMethod value) => setState(() => method = value);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Vertex Test'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: CustomPaint(
                painter: VertexPainter(widget.shape, method),
                child: Container(),
              ),
            ),
            Row(
              children: [
                Radio<PaintMethod>(
                  value: PaintMethod.path,
                  groupValue: method,
                  onChanged: (v) => setMethod(v!),
                ),
                Expanded(child: Text('Path')),
                Radio<PaintMethod>(
                  value: PaintMethod.vertices,
                  groupValue: method,
                  onChanged: (v) => setMethod(v!),
                ),
                Expanded(child: Text('Vertices')),
              ],
            )
          ],
        ),
      ),
    );
  }
}

class VertexPainter extends CustomPainter {
  const VertexPainter(this.positions, this.method);

  /// Positions should be between (0,0) and (100,100)
  final List<Offset> positions;

  /// The method to use when painting
  final PaintMethod method;

  @override
  void paint(Canvas canvas, Size size) {
    canvas.save();
    canvas.scale(size.width / 100, size.height / 100);
    final paint = Paint()
      ..color = Colors.red
      ..style = PaintingStyle.fill
      ..isAntiAlias = true;

    switch (method) {
      case PaintMethod.path:
        canvas.drawPath(Path()..addPolygon(positions, false), paint);
        break;
      case PaintMethod.vertices:
        canvas.drawVertices(
          Vertices(VertexMode.triangleFan, positions),
          BlendMode.srcOver,
          paint,
        );
    }
    canvas.restore();
  }

  @override
  bool shouldRepaint(VertexPainter old) =>
      old.positions != positions || old.method != method;
}
Screenshots These screenshots are from the sample code above.

Full Drawing

drawPath drawVertices
antialiasing-path antialiasing-vertices

Cropped

drawPath drawVertices
antialiasing-path-crop antialiasing-vertices-crop

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: renderingUI glitches reported at the engine/skia or impeller rendering levele: web_canvaskitCanvasKit (a.k.a. Skia-on-WebGL) rendering backend for Webengineflutter/engine related. See also e: labels.found in release: 2.0Found to occur in 2.0has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-engineOwned by Engine teamtriaged-engineTriaged by Engine team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions