Drawing performance issue in flutter

674 views
Skip to first unread message

Manusb

unread,
Jul 26, 2018, 8:02:04 AM7/26/18
to Flutter Dev
Hi all,
here is the code the problem is after drawing continuously the drawing speed become very slow compare to previous.
Thanks for any help.
Widget build(BuildContext context) {
_currentPainter = new DrawPainting(_points);

return new Container(
child: new ConstrainedBox(
constraints: const BoxConstraints.expand(),
child: new GestureDetector(
onPanUpdate: (DragUpdateDetails details) {
setState(() {
RenderBox referenceBox = context.findRenderObject();

Offset localPosition =
referenceBox.globalToLocal(details.globalPosition);
_points = new List.from(_points)..add(localPosition);
});
},
onPanEnd: (DragEndDetails details) =>_points.add(null),
child: new CustomPaint(
painter: _currentPainter,

),
),
),
);
}

class DrawPainting extends CustomPainter {
List<Offset> points = [];
Canvas _lastCanvas;
Size _lastSize;
DrawPainting(points){

this.points = points;
}

void paint(Canvas canvas, Size size) {
print({"the main paint is called .... ": {"size" : size}});
_lastCanvas = canvas;
_lastSize = size;


Paint paint = new Paint()
..color = Colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 8.0;


for (int i = 0; i < points.length - 1; i++) {
if (points[i] != null &&
points[i + 1] != null &&
(points[i].dx >= 0 &&
points[i].dy >= 0 &&
points[i].dx < size.width &&
points[i].dy < size.height) &&
(points[i + 1].dx >= 0 &&
points[i + 1].dy >= 0 &&
points[i + 1].dx < size.width &&
points[i + 1].dy < size.height)){
canvas.drawLine(points[i], points[i + 1], paint);
}

}
}

bool shouldRepaint(DrawPainting other) => other.points != points;
}

Eric Seidel

unread,
Jul 26, 2018, 11:41:05 AM7/26/18
to Manusb, Flutter Dev
From a quick glance at the code it makes sense that drawing speed would slow down over time since every intput event adds more points to draw.  You might want to pursue a strategy of rasterizing some of your earlier inputs into a bitmap instead of just always adding to an ever-growing list of points. :)

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages