@@ -17,13 +17,17 @@ Future<void> testMain() async {
17
17
const Rect region = Rect .fromLTWH (0 , 0 , 300 , 300 );
18
18
19
19
late BitmapCanvas canvas;
20
+ late BitmapCanvas domCanvas;
20
21
21
22
setUp (() {
22
23
canvas = BitmapCanvas (region, RenderStrategy ());
24
+ // setting isInsideSvgFilterTree true forces use of DOM canvas
25
+ domCanvas = BitmapCanvas (region, RenderStrategy ()..isInsideSvgFilterTree = true );
23
26
});
24
27
25
28
tearDown (() {
26
29
canvas.rootElement.remove ();
30
+ domCanvas.rootElement.remove ();
27
31
});
28
32
29
33
test ('draws lines with varying strokeWidth' , () async {
@@ -33,6 +37,75 @@ Future<void> testMain() async {
33
37
domDocument.body! .append (canvas.rootElement);
34
38
await matchGoldenFile ('canvas_lines_thickness.png' , region: region);
35
39
});
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
+ });
36
109
}
37
110
38
111
void paintLines (BitmapCanvas canvas) {
@@ -70,3 +143,10 @@ void paintLines(BitmapCanvas canvas) {
70
143
paint3.color = 0xFFFF9800 ; // Colors.orange;
71
144
canvas.drawLine (const Offset (50 , 70 ), const Offset (150 , 70 ), paint3);
72
145
}
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