2

我正在开发一个画布绘图模块,我需要擦除用户绘图

基本面是

  1. 在画布中添加图像
  2. 用户可以使用铅笔在图像上绘图(即黑色)现在用户可以在擦除功能上擦除此黑色(即清除用户绘图)

我的尝试是:

case PaintMode.eraser:
              print('eraser**** _image Painter!!!*'); // print(_painter.color)
              //  canvas.saveLayer(Offset.zero & size, Paint());1
              var _painterTemp = _painter!
                // ..color = Colors.transparent.withOpacity(0.8)//also tried it
                ..strokeCap = StrokeCap.round
                ..blendMode = BlendMode.clear;
    
              for (var i = 0; i < _offset!.length - 1; i++) {
                if (_offset[i] != null && _offset[i + 1] != null) {
                  final _path = Path()
                    ..moveTo(_offset[i]!.dx, _offset[i]!.dy)
                    ..lineTo(_offset[i + 1]!.dx, _offset[i + 1]!.dy);
                
                } else if (_offset[i] != null && _offset[i + 1] == null) {
                 
                  canvas.drawPoints(PointMode.points, [_offset[i]!], _painterTemp);
                }
              }
              //   canvas.restore();//1
    
              break

但我只得到黑线

参考 https://github.com/yellowQ-software/yellowQ-Flutter-Image-Painter

编辑-----我编辑过

case PaintMode.eraser:
          print('eraser**** _image Painter!!!*');

       
          var _painterTemp = _painter!
            ..color = Colors.transparent
            ..blendMode = BlendMode.clear; //srcOver

          for (var i = 0; i < _offset!.length - 1; i++) {
            if (_offset[i] != null && _offset[i + 1] != null) {
              final _path = Path()
                ..moveTo(_offset[i]!.dx, _offset[i]!.dy)
                ..lineTo(_offset[i + 1]!.dx, _offset[i + 1]!.dy);

              canvas.drawPath(_path, _painter);

              canvas.drawPath(_path, _painterTemp);
              canvas.saveLayer(Offset.zero & size, Paint());
            } else if (_offset[i] != null && _offset[i + 1] == null) {
              canvas.drawPoints(PointMode.points, [_offset[i]!], _painter);
              canvas.drawPoints(PointMode.points, [_offset[i]!], _painterTemp);
            }
            //  canvas.saveLayer(Offset.zero & size, Paint()); //1
          }
          //   canvas.restore();//1

          break;

但不工作但得到custom painter called canvas.save() or canvas.saveLayer() at least 773 more times than it called canvas.restore().

有什么帮助吗?

4

0 回答 0