@@ -17,13 +17,17 @@ Future<void> testMain() async {
1717 const Rect region = Rect .fromLTWH (0 , 0 , 300 , 300 );
1818
1919 late BitmapCanvas canvas;
20+ late BitmapCanvas domCanvas;
2021
2122 setUp (() {
2223 canvas = BitmapCanvas (region, RenderStrategy ());
24+ // setting isInsideSvgFilterTree true forces use of DOM canvas
25+ domCanvas = BitmapCanvas (region, RenderStrategy ()..isInsideSvgFilterTree = true );
2326 });
2427
2528 tearDown (() {
2629 canvas.rootElement.remove ();
30+ domCanvas.rootElement.remove ();
2731 });
2832
2933 test ('draws lines with varying strokeWidth' , () async {
@@ -33,6 +37,75 @@ Future<void> testMain() async {
3337 domDocument.body! .append (canvas.rootElement);
3438 await matchGoldenFile ('canvas_lines_thickness.png' , region: region);
3539 });
40+ test ('draws lines with varying strokeWidth with dom canvas' , () async {
41+
42+ paintLines (domCanvas);
43+
44+ domDocument.body! .append (domCanvas.rootElement);
45+ await matchGoldenFile ('canvas_lines_thickness_dom_canvas.png' , region: region);
46+ });
47+ test ('draws lines with negative Offset values with dom canvas' , () async {
48+ // test rendering lines correctly with negative offset when using DOM
49+ final SurfacePaintData paintWithStyle = SurfacePaintData ()
50+ ..color = 0xFFE91E63 // Colors.pink
51+ ..style = PaintingStyle .stroke
52+ ..strokeWidth = 16
53+ ..strokeCap = StrokeCap .round;
54+
55+ // canvas.drawLine ignores paint.style (defaults to fill) according to api docs.
56+ // expect lines are rendered the same regardless of the set paint.style
57+ final SurfacePaintData paintWithoutStyle = SurfacePaintData ()
58+ ..color = 0xFF4CAF50 // Colors.green
59+ ..strokeWidth = 16
60+ ..strokeCap = StrokeCap .round;
61+
62+ // test vertical, horizontal, and diagonal lines
63+ final List <Offset > points = < Offset > [
64+ const Offset (- 25 , 50 ), const Offset (45 , 50 ),
65+ const Offset (100 , - 25 ), const Offset (100 , 200 ),
66+ const Offset (- 150 , - 145 ), const Offset (100 , 200 ),
67+ ];
68+ final List <Offset > shiftedPoints = points.map ((Offset point) => point.translate (20 , 20 )).toList ();
69+
70+ paintLinesFromPoints (domCanvas, paintWithStyle, points);
71+ paintLinesFromPoints (domCanvas, paintWithoutStyle, shiftedPoints);
72+
73+ domDocument.body! .append (domCanvas.rootElement);
74+ await matchGoldenFile ('canvas_lines_with_negative_offset.png' , region: region);
75+ });
76+
77+ test ('drawLines method respects strokeCap with dom canvas' , () async {
78+ final SurfacePaintData paintStrokeCapRound = SurfacePaintData ()
79+ ..color = 0xFFE91E63 // Colors.pink
80+ ..strokeWidth = 16
81+ ..strokeCap = StrokeCap .round;
82+
83+ final SurfacePaintData paintStrokeCapSquare = SurfacePaintData ()
84+ ..color = 0xFF4CAF50 // Colors.green
85+ ..strokeWidth = 16
86+ ..strokeCap = StrokeCap .square;
87+
88+ final SurfacePaintData paintStrokeCapButt = SurfacePaintData ()
89+ ..color = 0xFFFF9800 // Colors.orange
90+ ..strokeWidth = 16
91+ ..strokeCap = StrokeCap .butt;
92+
93+ // test vertical, horizontal, and diagonal lines
94+ final List <Offset > points = < Offset > [
95+ const Offset (5 , 50 ), const Offset (45 , 50 ),
96+ const Offset (100 , 5 ), const Offset (100 , 200 ),
97+ const Offset (5 , 10 ), const Offset (100 , 200 ),
98+ ];
99+ final List <Offset > shiftedPoints = points.map ((Offset point) => point.translate (50 , 50 )).toList ();
100+ final List <Offset > twiceShiftedPoints = shiftedPoints.map ((Offset point) => point.translate (50 , 50 )).toList ();
101+
102+ paintLinesFromPoints (domCanvas, paintStrokeCapRound, points);
103+ paintLinesFromPoints (domCanvas, paintStrokeCapSquare, shiftedPoints);
104+ paintLinesFromPoints (domCanvas, paintStrokeCapButt, twiceShiftedPoints);
105+
106+ domDocument.body! .append (domCanvas.rootElement);
107+ await matchGoldenFile ('canvas_lines_with_strokeCap.png' , region: region);
108+ });
36109}
37110
38111void paintLines (BitmapCanvas canvas) {
@@ -70,3 +143,10 @@ void paintLines(BitmapCanvas canvas) {
70143 paint3.color = 0xFFFF9800 ; // Colors.orange;
71144 canvas.drawLine (const Offset (50 , 70 ), const Offset (150 , 70 ), paint3);
72145}
146+
147+ void paintLinesFromPoints (BitmapCanvas canvas, SurfacePaintData paint, List <Offset > points) {
148+ // points list contains pairs of Offset points, so for loop step is 2
149+ for (int i = 0 ; i < points.length - 1 ; i += 2 ) {
150+ canvas.drawLine (points[i], points[i + 1 ], paint);
151+ }
152+ }
0 commit comments