toRawSVG method

String? toRawSVG({
  1. int? width,
  2. int? height,
})

Export the current content to a raw SVG string. Will return null if there are no points.

Implementation

String? toRawSVG({int? width, int? height}) {
  if (isEmpty) {
    return null;
  }

  String colorToHex(Color c) => '#${c.value.toRadixString(16).padLeft(8, '0')}';

  String formatPoint(Point p) =>
      '${p.offset.dx.toStringAsFixed(2)},${p.offset.dy.toStringAsFixed(2)}';

  final String polylines = <String>[
    for (final List<Point> stroke in _latestActions)
      '<polyline '
          'fill="none" '
          'stroke="${colorToHex(penColor)}" '
          'points="${_translatePoints(stroke)!.map(formatPoint).join(' ')}" '
          '/>'
  ].join('\n');

  width ??= defaultWidth;
  height ??= defaultHeight;

  return '<svg '
      'viewBox="0 0 $width $height" '
      'xmlns="http://www.w3.org/2000/svg"'
      '>\n$polylines\n</svg>';
}