Untitled

mail@pastecode.io avatar
unknown
dart
5 months ago
1.7 kB
2
Indexable
class ImageGenerator {

  final void Function(Uint8List, String, String) onImageGenerated;
  StrokePath strokePath;
  PenTypes activePen;
  String drawingId;
  String segmentId;

  ImageGenerator({required this.onImageGenerated, required this.strokePath, required this.activePen, required this.drawingId, required this.segmentId});

  void genImage() async {
    // ignore: deprecated_member_use
    double devicePixelRatio = window.devicePixelRatio;
    double scaleFactor = devicePixelRatio * 5;

    final width = ((strokePath.getWidth() + strokePath.png_padding) * scaleFactor);
    final height = ((strokePath.getHeight() + strokePath.png_padding) * scaleFactor);

    final recorder = PictureRecorder();
    final canvas = Canvas(
      recorder,
      Rect.fromPoints(
        Offset.zero,
        Offset(strokePath.getWidth() + strokePath.png_padding, strokePath.getHeight() + strokePath.png_padding) * scaleFactor
      ),
    );

    Path? linesPath = strokePath.linesPath;
    Path? circlesPath = strokePath.circlesPath;

    final paint = activePen.paint;

    final Matrix4 matrix4 = Matrix4.identity()..scale(scaleFactor, scaleFactor);
    Path sLinepath = linesPath.transform(matrix4.storage);
    Path sCirclepath = circlesPath.transform(matrix4.storage);

    canvas.drawPath(sLinepath, paint);
    canvas.drawPath(sCirclepath, paint);

    final picture = recorder.endRecording();

    var img = await picture.toImage(width.ceil(), height.ceil());

    final byteData = await img.toByteData(format: ImageByteFormat.png);

    print("IMAGE READY!!!!");
    onImageGenerated(byteData!.buffer.asUint8List(), drawingId, segmentId);
  }
}
Leave a Comment