praeclarum / NGraphics

NGraphics is a cross platform library for rendering vector graphics on .NET. It provides a unified API for both immediate and retained mode graphics using high quality native renderers.
MIT License
705 stars 133 forks source link

ICanvas.ClipRectangle #87

Open justinyu15 opened 7 years ago

justinyu15 commented 7 years ago

please Add 【ICanvas.ClipRectangle】

public interface ICanvas { void ClipRectangle(Rect frame, Size corner); }

public class GraphicCanvas : ICanvas { public void ClipRectangle(Rect frame, Size corner) { Graphic.Children.Add(new Rectangle(frame, corner) { Transform = CurrentTransform, }); } }

public class FilterCanvas : ICanvas { public void ClipRectangle(Rect frame, Size corner) { NextCanvas.ClipRectangle(frame, corner); } }

public class AndroidPlatform : IPlatform { public void ClipRectangle(Rect frame, Size corner) { var left = (float)(frame.X); var top = (float)(frame.Y); var right = (float)(frame.X + frame.Width); var bottom = (float)(frame.Y + frame.Height); var rx = (float)corner.Width; var ry = (float)corner.Height; if (rx > 0 || ry > 0) { var path = new APath(); path.AddRoundRect(new RectF(left, top, right, bottom), rx, ry, APath.Direction.Ccw); graphics.ClipPath(path); } else { var path = new APath(); path.AddRect(new RectF(left, top, right, bottom), APath.Direction.Ccw); graphics.ClipPath(path); } } }

public class ApplePlatform : IPlatform { public void ClipRectangle(Rect frame, Size corner) { DrawElement(() => { context.SaveState(); if (corner.Width > 0 || corner.Height > 0) { var path = AddRoundedRect(Conversions.GetCGRect(frame), Conversions.GetCGSize(corner)); context.AddPath(path); context.Clip(); } else { context.AddRect(Conversions.GetCGRect(frame)); context.Clip(); } return frame; }); } }

aloisdeniel commented 6 years ago

Would be even better with Path clipping support (don't know how difficult it is to implement though)!