protected override void Render() { var root = Container.GetVisual(); var compositor = Window.Current.Compositor; var canvasPathBuilder = new CanvasPathBuilder(new CanvasDevice()); if (IsStrokeRounded) { canvasPathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin); } else { canvasPathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.None); } // Figure if (IsPie) { StartPointX = CenterPointX; StartPointY = CenterPointY; } else { StartPointX = Radius * Math.Cos(StartAngle * Degrees2Radians) + CenterPointX; StartPointY = Radius * Math.Sin(StartAngle * Degrees2Radians) + CenterPointY; } canvasPathBuilder.BeginFigure(new Vector2((float)StartPointX, (float)StartPointY)); canvasPathBuilder.AddArc( new Vector2((float)CenterPointX, (float)CenterPointY), (float)Radius, (float)Radius, (float)(StartAngle * Degrees2Radians), (float)(SweepAngle * Degrees2Radians)); canvasPathBuilder.EndFigure(IsClosed || IsPie ? CanvasFigureLoop.Closed : CanvasFigureLoop.Open); // Path var canvasGeometry = CanvasGeometry.CreatePath(canvasPathBuilder); var compositionPath = new CompositionPath(canvasGeometry); var pathGeometry = compositor.CreatePathGeometry(); pathGeometry.Path = compositionPath; var spriteShape = compositor.CreateSpriteShape(pathGeometry); spriteShape.FillBrush = compositor.CreateColorBrush(Fill); spriteShape.StrokeThickness = (float)StrokeThickness; spriteShape.StrokeBrush = compositor.CreateColorBrush(Stroke); spriteShape.StrokeStartCap = IsStrokeRounded ? CompositionStrokeCap.Round : CompositionStrokeCap.Flat; spriteShape.StrokeEndCap = IsStrokeRounded ? CompositionStrokeCap.Round : CompositionStrokeCap.Flat; // Visual var shapeVisual = compositor.CreateShapeVisual(); shapeVisual.Size = new Vector2((float)Container.ActualWidth, (float)Container.ActualHeight); shapeVisual.Shapes.Add(spriteShape); root.Children.RemoveAll(); root.Children.InsertAtTop(shapeVisual); }
private void SimplePathImperative_Click(object sender, RoutedEventArgs e) { // Same steps as for SimpleShapeImperative_Click to create, size and host a ShapeVisual compositor = Window.Current.Compositor; Windows.UI.Composition.ShapeVisual shape = compositor.CreateShapeVisual(); SetVisualOnElement(shape); // use Win2D's CanvasPathBuilder to create a simple path CanvasPathBuilder pathBuilder = new CanvasPathBuilder(CanvasDevice.GetSharedDevice()); pathBuilder.BeginFigure(1, 1); pathBuilder.AddLine(300, 300); pathBuilder.AddLine(1, 300); pathBuilder.EndFigure(CanvasFigureLoop.Closed); // create a Win2D CanvasGeomtryobject from the path CanvasGeometry triangleGeometry = CanvasGeometry.CreatePath(pathBuilder); // create a CompositionPath from the Win2D geometry CompositionPath trianglePath = new CompositionPath(triangleGeometry); // create a CompositionPathGeometry from the composition path CompositionPathGeometry compositionPathGeometry = compositor.CreatePathGeometry(trianglePath); // create a SpriteShape from the CompositionPathGeometry, give it a gradient fill and add to our ShapeVisual CompositionSpriteShape spriteShape = compositor.CreateSpriteShape(compositionPathGeometry); spriteShape.FillBrush = CreateGradientBrush(); shape.Shapes.Add(spriteShape); }
protected void Render(CanvasPathBuilder canvasPathBuilder) { // Path var canvasGeometry = CanvasGeometry.CreatePath(canvasPathBuilder); var compositionPath = new CompositionPath(canvasGeometry); var compositor = Window.Current.Compositor; var pathGeometry = compositor.CreatePathGeometry(); pathGeometry.Path = compositionPath; var spriteShape = compositor.CreateSpriteShape(pathGeometry); spriteShape.FillBrush = compositor.CreateColorBrush(Fill); spriteShape.StrokeThickness = (float)StrokeThickness; spriteShape.StrokeBrush = compositor.CreateColorBrush(Stroke); spriteShape.StrokeStartCap = IsStrokeRounded ? CompositionStrokeCap.Round : CompositionStrokeCap.Square; spriteShape.StrokeEndCap = IsStrokeRounded ? CompositionStrokeCap.Round : CompositionStrokeCap.Square; // Visual var shapeVisual = compositor.CreateShapeVisual(); shapeVisual.Size = new Vector2((float)Container.ActualWidth, (float)Container.ActualHeight); shapeVisual.Shapes.Add(spriteShape); shapeVisual.CenterPoint = new Vector3((float)RotationCenterX, (float)RotationCenterY, 0f); shapeVisual.RotationAxis = new Vector3(0f, 0f, 1f); shapeVisual.RotationAngle = (float)RotationAngle; var root = Container.GetVisual(); root.Children.RemoveAll(); root.Children.InsertAtTop(shapeVisual); _shapeVisual = shapeVisual; }
private void OnSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e) { var length = (float)Math.Min(e.NewSize.Width, e.NewSize.Height) * 0.95; var centerX = (float)e.NewSize.Width / 2; var centerY = (float)e.NewSize.Height / 2; var points = new List <Vector2>(); var r = length / 2; var r2 = r * 1.06; var r3 = r * 0.951; int index = 0; int segments = 100; for (int i = 0; i < segments; i += 2) { var x = r * Math.Cos(i * 2 * Math.PI / segments) + centerX; var y = r * Math.Sin(i * 2 * Math.PI / segments) + centerY; points.Add(new Vector2((float)x, (float)y)); var currentR = index++ % 2 == 0 ? r2 : r3; x = currentR * Math.Cos((i + 1) * 2 * Math.PI / segments) + centerX; y = currentR * Math.Sin((i + 1) * 2 * Math.PI / segments) + centerY; points.Add(new Vector2((float)x, (float)y)); } points.Add(points[0]); CanvasGeometry result; using (var builder = new CanvasPathBuilder(null)) { builder.BeginFigure(points[0]); for (int i = 0; i < points.Count - 2; i += 2) { var currentPoint = points[i]; var centerPoint = points[i + 1]; var nextPoint = points[i + 2]; builder.AddCubicBezier(currentPoint, centerPoint, nextPoint); } builder.EndFigure(CanvasFigureLoop.Open); result = CanvasGeometry.CreatePath(builder); } var compositor = Window.Current.Compositor; var path = new CompositionPath(result); var line3 = compositor.CreatePathGeometry(); line3.Path = path; var shape3 = compositor.CreateSpriteShape(line3); shape3.FillBrush = compositor.CreateColorBrush(Colors.Red); var visual = compositor.CreateShapeVisual(); visual.Shapes.Add(shape3); visual.Size = e.NewSize.ToVector2(); ElementCompositionPreview.SetElementChildVisual(this, visual); }
private protected void Render(Windows.UI.Composition.SkiaGeometrySource2D path, double?scaleX = null, double?scaleY = null, double?renderOriginX = null, double?renderOriginY = null) { var compositionPath = new CompositionPath(path); var pathGeometry = Visual.Compositor.CreatePathGeometry(); pathGeometry.Path = compositionPath; _pathSpriteShape = Visual.Compositor.CreateSpriteShape(pathGeometry); if (scaleX != null && scaleY != null) { _pathSpriteShape.Scale = new Vector2((float)scaleX.Value, (float)scaleY.Value); } else { _pathSpriteShape.Scale = new Vector2(1, 1); } _pathSpriteShape.Offset = LayoutRound(new Vector2((float)(renderOriginX ?? 0), (float)(renderOriginY ?? 0))); _rectangleVisual.Shapes.Clear(); _rectangleVisual.Shapes.Add(_pathSpriteShape); UpdateFill(); UpdateStroke(); UpdateStrokeThickness(); }
public static void UpdateClip(this FrameworkElement nativeView, IView view) { var clipGeometry = view.Clip; if (clipGeometry == null) { return; } if (view.Handler?.MauiContext?.Window is not Window window) { return; } var compositor = window.Compositor; var visual = ElementCompositionPreview.GetElementVisual(nativeView); var pathSize = new Rectangle(0, 0, view.Width, view.Height); var clipPath = clipGeometry.PathForBounds(pathSize); var device = CanvasDevice.GetSharedDevice(); var geometry = clipPath.AsPath(device); var path = new CompositionPath(geometry); var pathGeometry = compositor.CreatePathGeometry(path); var geometricClip = compositor.CreateGeometricClip(pathGeometry); visual.Clip = geometricClip; }
PathKeyFrameAnimation CreatePathKeyFrameAnimation(float initialProgress, CompositionPath initialValue, CompositionEasingFunction initialEasingFunction) { var result = _c.CreatePathKeyFrameAnimation(); result.Duration = TimeSpan.FromTicks(c_durationTicks); result.InsertKeyFrame(initialProgress, initialValue, initialEasingFunction); return(result); }
public static CompositionSpriteShape CreateSpriteShape(this Compositor compositor, string path) { var canvasGeometry = CanvasObject.CreateGeometry(new CanvasDevice(), path); var compositionPath = new CompositionPath(canvasGeometry); var pathGeometry = compositor.CreatePathGeometry(compositionPath); return(compositor.CreateSpriteShape(pathGeometry)); }
XElement FromCompositionPath(CompositionPath obj) { return(new XElement("CompositionPath", GetContents())); IEnumerable <XObject> GetContents() { yield return(FromIGeometrySource2D(obj.Source)); } }
CompositionPath Cache(CompositionPath key, CompositionPath obj) { var node = NodeFor(key); Debug.Assert(node.Copied == null, "Precondition"); Debug.Assert(!ReferenceEquals(key, obj), "Precondition"); node.Copied = obj; return(obj); }
/// <summary> /// Creates a CompositionGeometricClip from the given CanvasGeometry /// </summary> /// <param name="compositor">Compositor</param> /// <param name="geometry">CanvasGeometry</param> /// <returns>CompositionGeometricClip</returns> public static CompositionGeometricClip CreateGeometricClip(this Compositor compositor, CanvasGeometry geometry) { // Create the CompositionPath var path = new CompositionPath(geometry); // Create the CompositionPathGeometry var pathGeometry = compositor.CreatePathGeometry(path); // Create the CompositionGeometricClip return(compositor.CreateGeometricClip(pathGeometry)); }
CompositionPath Path() { if (_path != null) { return(_path); } var result = _path = new CompositionPath(Geometry_0()); return(result); }
CompositionPath GetCompositionPath(CompositionPath obj) { if (GetExisting(obj, out CompositionPath result)) { return(result); } result = Cache(obj, new CompositionPath(GetCanvasGeometry(obj.Source))); InitializeIDescribable(obj, result); return(result); }
private void CreateAnimatedChart1() { var shapeVisual = _compositor.CreateShapeVisual(); shapeVisual.RelativeSizeAdjustment = Vector2.One; var viewBox = _compositor.CreateViewBox(); viewBox.Size = new Vector2(500, 200); viewBox.Stretch = CompositionStretch.Fill; shapeVisual.ViewBox = viewBox; ElementCompositionPreview.SetElementChildVisual(ChartArea1, shapeVisual); var chart = _compositor.CreateSpriteShape(); chart.CenterPoint = viewBox.Size / 2; chart.FillBrush = _compositor.CreateColorBrush(Colors.LightBlue); var chartGeometry = _compositor.CreatePathGeometry(); var builder1 = new CanvasPathBuilder(null); builder1.BeginFigure(new Vector2(0, 200)); builder1.AddLine(new Vector2(0, 100)); builder1.AddCubicBezier(new Vector2(30, 100), new Vector2(60), new Vector2(100, 60)); builder1.AddCubicBezier(new Vector2(140, 60), new Vector2(200, 120), new Vector2(250, 120)); builder1.AddCubicBezier(new Vector2(350, 120), new Vector2(340, 40), new Vector2(380, 40)); builder1.AddCubicBezier(new Vector2(420, 40), new Vector2(400, 120), new Vector2(500, 120)); builder1.AddLine(new Vector2(500, 200)); builder1.EndFigure(CanvasFigureLoop.Closed); var curvedPath = new CompositionPath(CanvasGeometry.CreatePath(builder1)); var builder2 = new CanvasPathBuilder(null); builder2.BeginFigure(new Vector2(0, 200)); builder2.AddLine(new Vector2(0, 100)); builder2.AddCubicBezier(new Vector2(30, 100), new Vector2(60, 100), new Vector2(100, 100)); builder2.AddCubicBezier(new Vector2(140, 100), new Vector2(200, 100), new Vector2(250, 100)); builder2.AddCubicBezier(new Vector2(350, 100), new Vector2(340, 100), new Vector2(380, 100)); builder2.AddCubicBezier(new Vector2(420, 100), new Vector2(400, 100), new Vector2(500, 100)); builder2.AddLine(new Vector2(500, 200)); builder2.EndFigure(CanvasFigureLoop.Closed); var flatPath = new CompositionPath(CanvasGeometry.CreatePath(builder2)); chartGeometry.Path = flatPath; chart.Geometry = chartGeometry; shapeVisual.Shapes.Add(chart); var pathAnimation = _compositor.CreatePathKeyFrameAnimation(); pathAnimation.InsertKeyFrame(1, curvedPath); pathAnimation.Duration = TimeSpan.FromMilliseconds(800); pathAnimation.DelayTime = TimeSpan.FromMilliseconds(2000); chartGeometry.StartAnimation("Path", pathAnimation); }
protected override Size ArrangeOverride(Size finalSize) { var area = new Rect(0, 0, finalSize.Width, finalSize.Height); switch (Stretch) { default: case Stretch.None: break; case Stretch.Fill: area = new Rect(0, 0, finalSize.Width, finalSize.Height); break; case Stretch.Uniform: area = (area.Height > area.Width) ? (new Rect((float)area.X, (float)area.Y, (float)area.Width, (float)area.Width)) : (new Rect((float)area.X, (float)area.Y, (float)area.Height, (float)area.Height)); break; case Stretch.UniformToFill: area = (area.Height > area.Width) ? (new Rect((float)area.X, (float)area.Y, (float)area.Height, (float)area.Height)) : (new Rect((float)area.X, (float)area.Y, (float)area.Width, (float)area.Width)); break; } var shrinkValue = -ActualStrokeThickness / 2; if (area != Rect.Empty) { area.Inflate(shrinkValue, shrinkValue); } var geometrySource = GetGeometry(finalSize); var compositionPath = new CompositionPath(geometrySource); var pathGeometry = Visual.Compositor.CreatePathGeometry(); pathGeometry.Path = compositionPath; _pathSpriteShape = Visual.Compositor.CreateSpriteShape(pathGeometry); _rectangleVisual.Shapes.Clear(); _rectangleVisual.Shapes.Add(_pathSpriteShape); UpdateFill(); UpdateStroke(); UpdateStrokeThickness(); return(finalSize); // geometrySource.Geometry.Bounds.Size.ToSize(); }
private static void Render(DependencyObject d, DependencyPropertyChangedEventArgs args) { var arc = (EllipticalArc)d; if (arc.Container.ActualWidth == 0) { return; } var root = arc.Container.GetVisual(); var compositor = Window.Current.Compositor; var canvasPathBuilder = new CanvasPathBuilder(new CanvasDevice()); // Figure canvasPathBuilder.BeginFigure(new Vector2(arc.StartPointX, arc.StartPointY)); canvasPathBuilder.AddArc( new Vector2(arc.EndPointX, arc.EndPointY), arc.RadiusX, arc.RadiusY, (float)(arc.RotationAngle * Math.PI / 180), arc.IsClockwise ? CanvasSweepDirection.Clockwise : CanvasSweepDirection.CounterClockwise, arc.IsLargeArc ? CanvasArcSize.Large : CanvasArcSize.Small); canvasPathBuilder.EndFigure(arc.IsClosed ? CanvasFigureLoop.Closed : CanvasFigureLoop.Open); // Path var canvasGeometry = CanvasGeometry.CreatePath(canvasPathBuilder); var compositionPath = new CompositionPath(canvasGeometry); var pathGeometry = compositor.CreatePathGeometry(); pathGeometry.Path = compositionPath; var spriteShape = compositor.CreateSpriteShape(pathGeometry); spriteShape.FillBrush = compositor.CreateColorBrush(arc.Fill); spriteShape.StrokeThickness = (float)arc.StrokeThickness; spriteShape.StrokeBrush = compositor.CreateColorBrush(arc.Stroke); spriteShape.StrokeStartCap = arc.IsStrokeRounded ? CompositionStrokeCap.Round : CompositionStrokeCap.Flat; spriteShape.StrokeEndCap = arc.IsStrokeRounded ? CompositionStrokeCap.Round : CompositionStrokeCap.Flat; // Visual var shapeVisual = compositor.CreateShapeVisual(); shapeVisual.Size = new Vector2((float)arc.Container.ActualWidth, (float)arc.Container.ActualHeight); shapeVisual.Shapes.Add(spriteShape); root.Children.RemoveAll(); root.Children.InsertAtTop(shapeVisual); }
private void PathMorphImperative_Click(object sender, RoutedEventArgs e) { // Same steps as for SimpleShapeImperative_Click to create, size and host a ShapeVisual compositor = Window.Current.Compositor; Windows.UI.Composition.ShapeVisual shape = compositor.CreateShapeVisual(); SetVisualOnElement(shape); // Call helper functions that use Win2D to build square and circle path geometries and create CompositionPath's for them CanvasGeometry square = BuildSquareGeometry(); CompositionPath squarePath = new CompositionPath(square); CanvasGeometry circle = BuildCircleGeometry(); CompositionPath circlePath = new CompositionPath(circle); // Create a CompositionPathGeometry, CompositionSpriteShape and set offset and fill CompositionPathGeometry compositionPathGeometry = compositor.CreatePathGeometry(squarePath); CompositionSpriteShape spriteShape = compositor.CreateSpriteShape(compositionPathGeometry); spriteShape.Offset = new Vector2(150, 200); spriteShape.FillBrush = CreateGradientBrush(); // Create a PathKeyFrameAnimation to set up the path morph passing in the circle and square paths var playAnimation = compositor.CreatePathKeyFrameAnimation(); playAnimation.Duration = TimeSpan.FromSeconds(4); playAnimation.InsertKeyFrame(0, squarePath); playAnimation.InsertKeyFrame(0.3F, circlePath); playAnimation.InsertKeyFrame(0.6F, circlePath); playAnimation.InsertKeyFrame(1.0F, squarePath); // Make animation repeat forever and start it playAnimation.IterationBehavior = AnimationIterationBehavior.Forever; playAnimation.Direction = Windows.UI.Composition.AnimationDirection.Alternate; compositionPathGeometry.StartAnimation("Path", playAnimation); // Add the SpriteShape to our shape visual shape.Shapes.Add(spriteShape); }
void UpdateClip() { if (Child == null) { return; } var clipGeometry = Clip; if (clipGeometry == null) { return; } double width = Child.ActualWidth; double height = Child.ActualHeight; if (height <= 0 && width <= 0) { return; } var visual = ElementCompositionPreview.GetElementVisual(Child); var compositor = visual.Compositor; var pathSize = new Graphics.Rectangle(0, 0, width, height); var clipPath = clipGeometry.PathForBounds(pathSize); var device = CanvasDevice.GetSharedDevice(); var geometry = clipPath.AsPath(device); var path = new CompositionPath(geometry); var pathGeometry = compositor.CreatePathGeometry(path); var geometricClip = compositor.CreateGeometricClip(pathGeometry); visual.Clip = geometricClip; }
internal CompositionPathGeometry(CompositionPath path) { Path = path; }
internal CompositionPathGeometry CreatePathGeometry(CompositionPath path) => _compositor.CreatePathGeometry(path);
CompositionPath Path_2() { var result = _path_2 = new CompositionPath(Geometry_5()); return(result); }
public static CompositionAnimation ParseThumbnail(int width, int height, IList <ClosedVectorPath> contours, out ShapeVisual visual, bool animated = true) { CompositionPath path; if (contours?.Count > 0) { path = Parse(contours); } else { path = new CompositionPath(CanvasGeometry.CreateRoundedRectangle(null, 0, 0, width, height, 80, 80)); } var transparent = Color.FromArgb(0x00, 0x7A, 0x8A, 0x96); var foregroundColor = Color.FromArgb(0x33, 0x7A, 0x8A, 0x96); var backgroundColor = Color.FromArgb(0x33, 0x7A, 0x8A, 0x96); var gradient = Window.Current.Compositor.CreateLinearGradientBrush(); gradient.StartPoint = new Vector2(0, 0); gradient.EndPoint = new Vector2(1, 0); gradient.ColorStops.Add(Window.Current.Compositor.CreateColorGradientStop(0.0f, transparent)); gradient.ColorStops.Add(Window.Current.Compositor.CreateColorGradientStop(0.5f, foregroundColor)); gradient.ColorStops.Add(Window.Current.Compositor.CreateColorGradientStop(1.0f, transparent)); var background = Window.Current.Compositor.CreateRectangleGeometry(); background.Size = new Vector2(width, height); var backgroundShape = Window.Current.Compositor.CreateSpriteShape(background); backgroundShape.FillBrush = Window.Current.Compositor.CreateColorBrush(backgroundColor); var foreground = Window.Current.Compositor.CreateRectangleGeometry(); foreground.Size = new Vector2(width, height); var foregroundShape = Window.Current.Compositor.CreateSpriteShape(foreground); foregroundShape.FillBrush = gradient; var clip = Window.Current.Compositor.CreateGeometricClip(Window.Current.Compositor.CreatePathGeometry(path)); clip.ViewBox = Window.Current.Compositor.CreateViewBox(); clip.ViewBox.Size = new Vector2(width, height); clip.ViewBox.Stretch = CompositionStretch.UniformToFill; visual = Window.Current.Compositor.CreateShapeVisual(); visual.Clip = clip; visual.Shapes.Add(backgroundShape); visual.Shapes.Add(foregroundShape); visual.RelativeSizeAdjustment = Vector2.One; if (animated) { var animation = Window.Current.Compositor.CreateVector2KeyFrameAnimation(); animation.InsertKeyFrame(0, new Vector2(-width, 0)); animation.InsertKeyFrame(1, new Vector2(width, 0)); animation.IterationBehavior = AnimationIterationBehavior.Forever; animation.Duration = TimeSpan.FromSeconds(1); foregroundShape.StartAnimation("Offset", animation); return(animation); } return(null); }
bool GetExisting(CompositionPath key, out CompositionPath result) { result = (CompositionPath)NodeFor(key).Copied; return(result != null); }
CompositionPath Path() { var result = _path = new CompositionPath(Geometry_0()); return(result); }
internal CompositionPathGeometry(Compositor compositor, CompositionPath path = null) : base(compositor) { Path = path; }
ObjectData NodeFor(CompositionPath obj) { return(_graph[obj].Canonical); }
TNode NodeFor(CompositionPath obj) => _graph[obj].Canonical;
CompositionPath Path_1() { var result = _path_1 = new CompositionPath(Geometry_3()); return(result); }
TC CanonicalObject <TC>(CompositionPath obj) => (TC)NodeFor(obj).Object;
CompositionPath CompositionPath_6() { var result = _compositionPath_6 = new CompositionPath(Geometry_6()); return(result); }