Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2a9fa79

Browse files
authored
Revert "fix canvas drawLine bugs (#38753)" (#38815)
This reverts commit 24eb954.
1 parent bb2d5e9 commit 2a9fa79

File tree

4 files changed

+27
-90
lines changed

4 files changed

+27
-90
lines changed

lib/web_ui/lib/src/engine/html/bitmap_canvas.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,20 @@ class BitmapCanvas extends EngineCanvas {
538538
if (_useDomForRenderingFill(paint)) {
539539
final Matrix4 transform = _canvasPool.currentTransform;
540540
final SurfacePath surfacePath = path as SurfacePath;
541+
final ui.Rect? pathAsLine = surfacePath.toStraightLine();
542+
if (pathAsLine != null) {
543+
ui.Rect rect = (pathAsLine.top == pathAsLine.bottom)
544+
? ui.Rect.fromLTWH(
545+
pathAsLine.left, pathAsLine.top, pathAsLine.width, 1)
546+
: ui.Rect.fromLTWH(
547+
pathAsLine.left, pathAsLine.top, 1, pathAsLine.height);
548+
549+
rect = adjustRectForDom(rect, paint);
550+
final DomHTMLElement element = buildDrawRectElement(
551+
rect, paint, 'draw-rect', _canvasPool.currentTransform);
552+
_drawElement(element, rect.topLeft, paint);
553+
return;
554+
}
541555

542556
final ui.Rect? pathAsRect = surfacePath.toRect();
543557
if (pathAsRect != null) {
@@ -549,15 +563,16 @@ class BitmapCanvas extends EngineCanvas {
549563
drawRRect(pathAsRRect, paint);
550564
return;
551565
}
552-
final DomElement svgElm = pathToSvgElement(surfacePath, paint);
566+
final ui.Rect pathBounds = surfacePath.getBounds();
567+
final DomElement svgElm = pathToSvgElement(
568+
surfacePath, paint, '${pathBounds.right}', '${pathBounds.bottom}');
553569
if (!_canvasPool.isClipped) {
554570
final DomCSSStyleDeclaration style = svgElm.style;
555571
style.position = 'absolute';
556572
if (!transform.isIdentity()) {
557573
style
558574
..transform = matrix4ToCssTransform(transform)
559-
..transformOrigin = '0 0 0'
560-
..overflow = 'visible';
575+
..transformOrigin = '0 0 0';
561576
}
562577
}
563578
_applyFilter(svgElm, paint);

lib/web_ui/lib/src/engine/html/clip.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ class PersistedPhysicalShape extends PersistedContainerSurface
392392
path,
393393
SurfacePaintData()
394394
..style = ui.PaintingStyle.fill
395-
..color = color.value);
395+
..color = color.value,
396+
'${pathBounds2.right}',
397+
'${pathBounds2.bottom}');
396398

397399
/// Render element behind the clipped content.
398400
rootElement!.insertBefore(_svgElement!, childContainer);

lib/web_ui/lib/src/engine/html/dom_canvas.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import '../svg.dart';
1414
import '../text/canvas_paragraph.dart';
1515
import '../util.dart';
1616
import '../vector_math.dart';
17-
import 'bitmap_canvas.dart';
1817
import 'painting.dart';
1918
import 'path/path.dart';
2019
import 'path/path_to_svg.dart';
@@ -334,10 +333,14 @@ String _borderStrokeToCssUnit(double value) {
334333
return '${value.toStringAsFixed(3)}px';
335334
}
336335

337-
SVGSVGElement pathToSvgElement(SurfacePath path, SurfacePaintData paint) {
336+
SVGSVGElement pathToSvgElement(
337+
SurfacePath path, SurfacePaintData paint, String width, String height) {
338338
// In Firefox some SVG typed attributes are returned as null without a
339339
// setter. So we use strings here.
340-
final SVGSVGElement root = createSVGSVGElement();
340+
final SVGSVGElement root = createSVGSVGElement()
341+
..setAttribute('width', '${width}px')
342+
..setAttribute('height', '${height}px')
343+
..setAttribute('viewBox', '0 0 $width $height');
341344

342345
final SVGPathElement svgPath = createSVGPathElement();
343346
root.append(svgPath);
@@ -347,9 +350,6 @@ SVGSVGElement pathToSvgElement(SurfacePath path, SurfacePaintData paint) {
347350
paint.strokeWidth != null)) {
348351
svgPath.setAttribute('stroke', colorValueToCssString(paint.color)!);
349352
svgPath.setAttribute('stroke-width', '${paint.strokeWidth ?? 1.0}');
350-
if (paint.strokeCap != null) {
351-
svgPath.setAttribute('stroke-linecap', '${stringForStrokeCap(paint.strokeCap)}');
352-
}
353353
svgPath.setAttribute('fill', 'none');
354354
} else if (paint.color != null) {
355355
svgPath.setAttribute('fill', colorValueToCssString(paint.color)!);

lib/web_ui/test/html/drawing/canvas_lines_golden_test.dart

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@ Future<void> testMain() async {
1717
const Rect region = Rect.fromLTWH(0, 0, 300, 300);
1818

1919
late BitmapCanvas canvas;
20-
late BitmapCanvas domCanvas;
2120

2221
setUp(() {
2322
canvas = BitmapCanvas(region, RenderStrategy());
24-
// setting isInsideSvgFilterTree true forces use of DOM canvas
25-
domCanvas = BitmapCanvas(region, RenderStrategy()..isInsideSvgFilterTree = true);
2623
});
2724

2825
tearDown(() {
2926
canvas.rootElement.remove();
30-
domCanvas.rootElement.remove();
3127
});
3228

3329
test('draws lines with varying strokeWidth', () async {
@@ -37,75 +33,6 @@ Future<void> testMain() async {
3733
domDocument.body!.append(canvas.rootElement);
3834
await matchGoldenFile('canvas_lines_thickness.png', region: region);
3935
});
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-
});
10936
}
11037

11138
void paintLines(BitmapCanvas canvas) {
@@ -143,10 +70,3 @@ void paintLines(BitmapCanvas canvas) {
14370
paint3.color = 0xFFFF9800; // Colors.orange;
14471
canvas.drawLine(const Offset(50, 70), const Offset(150, 70), paint3);
14572
}
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

Comments
 (0)