paint method

void paint(
  1. Canvas canvas
)

Paints the line to the canvas.

Implementation

void paint(Canvas canvas) {
  // Check input values
  if (_points.length < 2) return;

  assert(_points.length == colors.length);
  assert(_points.length == widths.length);

  _cachedPaint.blendMode = transferMode;

  // Calculate normals
  List<Vector2> vectors = <Vector2>[];
  for (Offset pt in _points) {
    vectors.add(Vector2(pt.dx, pt.dy));
  }
  List<Vector2> miters = _computeMiterList(vectors, false);

  List<Offset> vertices = <Offset>[];
  List<int> indices = <int>[];
  List<Color> verticeColors = <Color>[];
  List<Offset>? textureCoordinates;
  late double textureTop;
  late double textureBottom;
  List<double>? stops;

  // Add first point
  Offset lastPoint = _points[0];
  Vector2 lastMiter = miters[0];

  // Add vertices and colors
  _addVerticesForPoint(vertices, lastPoint, lastMiter, widths[0]);
  verticeColors.add(colors[0]);
  verticeColors.add(colors[0]);

  assert(texture.rotated == false);

  // Setup for calculating texture coordinates
  textureTop = texture.frame.top;
  textureBottom = texture.frame.bottom;
  textureCoordinates = <Offset>[];

  // Use correct stops
  if (textureStops != null) {
    assert(_points.length == textureStops!.length);
    stops = textureStops;
  } else {
    if (_calculatedTextureStops == null) _calculateTextureStops();
    stops = _calculatedTextureStops;
  }

  // Texture coordinate points
  double xPos = _xPosForStop(stops![0]);
  textureCoordinates.add(Offset(xPos, textureTop));
  textureCoordinates.add(Offset(xPos, textureBottom));

  // Add the rest of the points
  for (int i = 1; i < _points.length; i++) {
    // Add vertices
    Offset currentPoint = _points[i];
    Vector2 currentMiter = miters[i];
    _addVerticesForPoint(vertices, currentPoint, currentMiter, widths[i]);

    // Add references to the triangles
    int lastIndex0 = (i - 1) * 2;
    int lastIndex1 = (i - 1) * 2 + 1;
    int currentIndex0 = i * 2;
    int currentIndex1 = i * 2 + 1;
    indices.addAll(<int>[lastIndex0, lastIndex1, currentIndex0]);
    indices.addAll(<int>[lastIndex1, currentIndex1, currentIndex0]);

    // Add colors
    verticeColors.add(colors[i]);
    verticeColors.add(colors[i]);

    // Texture coordinate points
    double xPos = _xPosForStop(stops[i]);
    textureCoordinates.add(Offset(xPos, textureTop));
    textureCoordinates.add(Offset(xPos, textureBottom));

    // Update last values
    lastPoint = currentPoint;
    lastMiter = currentMiter;
  }

  var vertexObject = ui.Vertices(
    VertexMode.triangleStrip,
    vertices,
    textureCoordinates: textureCoordinates,
    colors: verticeColors,
  );
  canvas.drawVertices(vertexObject, BlendMode.modulate, _cachedPaint);
}