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

clipPath to use fillType param #38956

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/web_ui/lib/src/engine/html/path_to_svg_clip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ SVGSVGElement pathToSvgClipPath(ui.Path path,
clipPath.setAttribute('clipPathUnits', 'objectBoundingBox');
svgPath.setAttribute('transform', 'scale($scaleX, $scaleY)');
}

if (path.fillType == ui.PathFillType.evenOdd) {
svgPath.setAttribute('clip-rule', 'evenodd');
} else {
svgPath.setAttribute('clip-rule', 'nonzero');
}
svgPath.setAttribute('d', pathToSvg((path as SurfacePath).pathRef, offsetX: offsetX, offsetY: offsetY));
return root;
}
Expand Down
44 changes: 44 additions & 0 deletions lib/web_ui/test/html/canvas_clip_path_golden_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,50 @@ Future<void> testMain() async {
await canvasScreenshot(rc, 'image_clipped_by_oval_path',
region: const Rect.fromLTWH(0, 0, 600, 800));
});

test('Clips with fillType evenOdd', () async {
final engine.RecordingCanvas rc = engine.RecordingCanvas(const Rect.fromLTRB(0, 0, 500, 500));
rc.save();
const double testWidth = 400;
const double testHeight = 350;

// draw RGB test image
rc.drawImageRect(createTestImage(), const Rect.fromLTRB(0, 0, testWidth, testHeight),
const Rect.fromLTWH(0, 0, testWidth, testHeight), engine.SurfacePaint());

// draw a clipping path with:
// 1) an outside larger rectangle
// 2) a smaller inner rectangle specified by a path
final Path path = Path();
path.addRect(const Rect.fromLTWH(0, 0, testWidth, testHeight));
const double left = 25;
const double top = 30;
const double right = 300;
const double bottom = 250;
path
..moveTo(left, top)
..lineTo(right,top)
..lineTo(right,bottom)
..lineTo(left, bottom)
..close();
path.fillType = PathFillType.evenOdd;
rc.clipPath(path);

// draw an orange paint path of size testWidth and testHeight
final Path paintPath = Path();
paintPath.addRect(const Rect.fromLTWH(0, 0, testWidth, testHeight));
paintPath.close();
rc.drawPath(paintPath,
engine.SurfacePaint()
..color = const Color(0xFFFF9800)
..style = PaintingStyle.fill);
rc.restore();

// when fillType is set to evenOdd from the clipping path, expect the inner
// rectangle should clip some of the orange painted portion, revealing the RGB testImage
await canvasScreenshot(rc, 'clipPath_uses_fillType_evenOdd',
region: const Rect.fromLTWH(0, 0, 600, 800));
});
}

engine.HtmlImage createTestImage({int width = 200, int height = 150}) {
Expand Down