renderToContext2D method

void renderToContext2D(
  1. dynamic context2D
)

Exports the signature to html canvas.

Implementation

void renderToContext2D(dynamic context2D) {
  final String strokePenColor =
      '${strokeColor.red},${strokeColor.green},${strokeColor.blue},${strokeColor.opacity.toStringAsFixed(2)}';

  final String backgroundFillColor =
      '${backgroundColor.red},${backgroundColor.green},${backgroundColor.blue},${backgroundColor.opacity.toStringAsFixed(2)}';

  //Drawing the background of the SignaturePad
  context2D.fillStyle = 'rgba($backgroundFillColor)';
  context2D.fillRect(0, 0, size.width, size.height);
  context2D.fill();

  context2D.beginPath();

  if (!_restrictBezierPathCalculation) {
    for (int i = 0; i < _dotPoints.length; i++) {
      final Offset point = _dotPoints[i];
      context2D.moveTo(point.dx, point.dy);
      context2D.arc(point.dx, point.dy,
          (_minimumStrokeWidth + _maximumStrokeWidth) / 2, 0, pi * 2, true);
    }

    for (int i = 0; i < _bezierPoints.length; i++) {
      context2D.moveTo(_bezierPoints[i].x, _bezierPoints[i].y);
      context2D.arc(_bezierPoints[i].x, _bezierPoints[i].y,
          _bezierPoints[i].width / 2, 0, 2 * pi, false);
    }

    context2D.fillStyle = 'rgba($strokePenColor)';
    context2D.fill();
  } else {
    for (int i = 0; i < _data.length; i++) {
      if (_data[i].length == 1) {
        final _TouchPoint point = _data[i][0];
        context2D.moveTo(point.x, point.y);
        context2D.arc(point.x, point.y,
            (_minimumStrokeWidth + _maximumStrokeWidth) / 2, 0, pi * 2, true);
        context2D.fillStyle = 'rgba($strokePenColor)';
        context2D.fill();
      } else {
        final List<_TouchPoint> drawPath = _data[i];
        for (int i = 0; i < drawPath.length; i++) {
          if (i < drawPath.length - 1) {
            context2D.moveTo(drawPath[i].x, drawPath[i].y);
            context2D.lineTo(drawPath[i + 1].x, drawPath[i + 1].y);
          }
        }

        context2D.lineWidth = _maximumStrokeWidth;
        context2D.strokeStyle = 'rgba($strokePenColor)';
        context2D.lineCap = 'round';
        context2D.stroke();
      }
    }
  }
}