Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions third_party/packages/flutter_svg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,43 @@ import 'dart:ui' as ui;
pictureInfo.picture.dispose();
```

or if you want to scale it, you can do something like:

<?code-excerpt "example/lib/readme_excerpts.dart (OutputScaling)"?>
```dart
import 'dart:ui' as ui;

// ···
const rawSvg = '''<svg ...>...</svg>''';
final PictureInfo pictureInfo = await vg.loadPicture(
const SvgStringLoader(rawSvg),
null,
);

// You can convert the picture to a scaled image:
const double targetWidth = 512;
const double targetHeight = 512;
final pictureRecorder = ui.PictureRecorder();
final canvas = Canvas(
pictureRecorder,
Rect.fromPoints(
Offset.zero,
const Offset(targetWidth, targetHeight),
),
);
canvas.scale(
targetWidth / pictureInfo.size.width,
targetHeight / pictureInfo.size.height,
);
canvas.drawPicture(pictureInfo.picture);
final ui.Image scaledImage = await pictureRecorder.endRecording().toImage(
targetWidth.ceil(),
targetHeight.ceil(),
);

pictureInfo.picture.dispose();
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is adding a lot of code length to the README, and a lot of it is duplicated from above, or is about PictureRecorder rather than flutter_svg. Do we actually need to document anything other than the need to call scale to make it clear how to do this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep the example code footprint smaller, I could keep the current example and just add two lines like so:

  // You can scale the canvas to achieve lossless vector image stretching before drawing it
  canvas.scale(1.2, 1.2);
  // You can draw the picture to a canvas:
  canvas.drawPicture(pictureInfo.picture);

Would that be better maybe?


The `SvgPicture` helps to automate this logic, and it provides some convenience
wrappers for getting assets from multiple sources.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
// intended for use as an actual example application.

// #docregion OutputConversion
// #docregion OutputScaling
import 'dart:ui' as ui;

// #enddocregion OutputConversion
// #enddocregion OutputScaling

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
Expand Down Expand Up @@ -101,6 +103,41 @@ Future<ui.Image> convertSvgOutput() async {
return image;
}

/// Demonstrates scaling SVG to a larger image
Future<ui.Image> scaleSvgOutput() async {
// #docregion OutputScaling
const rawSvg = '''<svg ...>...</svg>''';
final PictureInfo pictureInfo = await vg.loadPicture(
const SvgStringLoader(rawSvg),
null,
);

// You can convert the picture to a scaled image:
const double targetWidth = 512;
const double targetHeight = 512;
final pictureRecorder = ui.PictureRecorder();
final canvas = Canvas(
pictureRecorder,
Rect.fromPoints(
Offset.zero,
const Offset(targetWidth, targetHeight),
),
);
canvas.scale(
targetWidth / pictureInfo.size.width,
targetHeight / pictureInfo.size.height,
);
canvas.drawPicture(pictureInfo.picture);
final ui.Image scaledImage = await pictureRecorder.endRecording().toImage(
targetWidth.ceil(),
targetHeight.ceil(),
);

pictureInfo.picture.dispose();
// #enddocregion OutputScaling
return scaledImage;
}

// #docregion ColorMapper
class _MyColorMapper extends ColorMapper {
const _MyColorMapper();
Expand Down