toRawSVG method

String? toRawSVG({
  1. int? width,
  2. int? height,
  3. double minDistanceBetweenPoints = 3,
})
inherited

Export the current content to a raw SVG string. Will return null if there are no points. width Canvas width to use height Canvas height to use minDistanceBetweenPoints Minimal distance between points to be included in svg. Used to reduce svg output size.

Implementation

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

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

  final StringBuffer svg = StringBuffer('<svg viewBox="0 0 $width $height" xmlns="http://www.w3.org/2000/svg">');
  final List<List<Point>> strokes = pointsToStrokes(minDistanceBetweenPoints);
  for (List<Point> stroke in strokes) {
    final List<Point> translatedStroke = _translatePoints(stroke);
    if (stroke.length > 1) {
      svg.writeln(
        '<polyline '
        'fill="none" '
        'stroke="${_colorToHex(exportPenColor ?? penColor)}" '
        'stroke-opacity="${_colorToOpacity(exportPenColor ?? penColor)}" '
        'points="${translatedStroke.map(formatPoint).join(' ')}" '
        'stroke-linecap="${strokeCap.name}" '
        'stroke-linejoin="${strokeJoin.name}" '
        'stroke-width="$penStrokeWidth" '
        '/>',
      );
    } else {
      svg.writeln(
        '<circle '
        'cx="${translatedStroke.first.offset.dx.toStringAsFixed(2)}" '
        'cy="${translatedStroke.first.offset.dy.toStringAsFixed(2)}" '
        'r="${(penStrokeWidth / 2).toStringAsFixed(2)}" '
        'fill="${_colorToHex(exportPenColor ?? penColor)}" '
        '/>',
      );
    }
  }
  svg.writeln('</svg>');
  return svg.toString();
}