剪辑SpriteComponent的某些部分

问题描述 投票:0回答:1

我使用 SpriteComponent 加载了全屏图像,并尝试使用 canvas.clipPath 删除该图像的某些部分。

预期:图像将被渲染,除了所有被剪切的部分 问题:图像仅在剪切部分的一侧渲染

如果您知道如何归档预期内容,请告诉我。 非常感谢!

flutter canvas sprite flame flutter-clippath
1个回答
0
投票

尝试使用 CustomPainter

示例:

class MyCustomClipper extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var path = Path();

    // Define your path here
    path.moveTo(size.width / 2, size.height / 2);
    path.lineTo(size.width / 2, size.height);
    path.lineTo(size.width, size.height);
    path.close();

    canvas.clipPath(path);

    // After clipping, you can draw on the canvas
    canvas.drawRect(
      Rect.fromLTWH(0, 0, size.width, size.height),
      Paint()..color = Colors.red,
    );
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

您可以使用您的小部件而不是容器

// Usage
CustomPaint(
  painter: MyCustomClipper(),
  child: Container(
    width: 200,
    height: 200,
  ),
)
© www.soinside.com 2019 - 2024. All rights reserved.