snapshot method

Future snapshot()

Implementation

Future snapshot() async {
  try {
    try {
      if (stream == null) return;
      Log().debug("Taking Snapshot");

      int left = 0;
      int top = 0;
      int width = canvas.width!;
      int height = canvas.height!;
      if ((width + left) > canvas.width!) width = canvas.width! - left;
      if ((height + top) > canvas.height!) height = canvas.height! - top;

      // Get Image RGBA Bitmap
      ImageData image =
          canvas.context2D.getImageData(left, top, width, height);

      var rgba = image.data.toList();
      var bytes = Uint8List.fromList(rgba);
      for (int i = 0; i < bytes.length; i++) {
        image.data[i] = bytes[i];
      }

      canvas2.width = width;
      canvas2.height = height;
      canvas2.context2D.putImageData(image, 0, 0);

      if (widget.model.debug == true) {
        Blob blob = await canvas2.toBlob('image/png', 1.0);
        await Platform.fileSaveAsFromBlob(blob, "${newId()}-.png");
      }

      ImageData image2 = canvas2.context2D.getImageData(0, 0, width, height);
      var rgba2 = image2.data.toList();

      // save snapshot
      String uri = canvas2.toDataUrl('image/png', 1.0);
      await onSnapshot(rgba2, width, height, UriData.fromString(uri));
    } catch (e) {
      Log().debug('$e');
    }
  } catch (e) {
    Log().debug('$e');
  }
}