Skip to content

Commit

Permalink
Several improvements and changes to SKGeometry (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored Jun 19, 2022
1 parent e73ab52 commit ed1fc63
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 233 deletions.
10 changes: 7 additions & 3 deletions samples/Forms/SkiaSharpDemo/Demos/Extended/ShapesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
x:Class="SkiaSharpDemo.Demos.ShapesPage"
Title="Shapes"
Padding="12">
Title="Shapes">

<views:SKCanvasView PaintSurface="OnPaintSurface" />
<ScrollView>
<views:SKCanvasView x:Name="canvasView"
HeightRequest="720"
IgnorePixelScaling="True"
PaintSurface="OnPaintSurface" />
</ScrollView>

</ContentPage>
104 changes: 78 additions & 26 deletions samples/Forms/SkiaSharpDemo/Demos/Extended/ShapesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,37 @@ namespace SkiaSharpDemo.Demos
{
public partial class ShapesPage : ContentPage
{
private readonly Random random = new Random();

private List<float> sectors;
private static readonly Random random = new Random();
private readonly List<float> sectors = GeneratePieData();
private readonly SKColor[] colors =
{
0xff959edf,
0xff2de513,
0xffdbcc93,
0xffaa6b7f,
0xff3c5061,
0xffa31359,
0xff29ef1b,
0xff3d0fad,
0xffd365f1,
0xff3cd7d5,
};

public ShapesPage()
{
InitializeComponent();

GenerateData();

BindingContext = this;
}

protected override void OnAppearing()
{
base.OnAppearing();

GenerateData();
canvasView.InvalidateSurface();
}

private void GenerateData()
private static List<float> GeneratePieData()
{
// generate some random data
var count = random.Next(5, 10);
Expand All @@ -42,8 +52,7 @@ private void GenerateData()
var sum = data.Sum();

// get the percentages
sectors?.Clear();
sectors = data.Select(d => d / sum).ToList();
return data.Select(d => d / sum).ToList();
}

private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
Expand All @@ -54,32 +63,75 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
// clear the surface
canvas.Clear(SKColors.Transparent);

// get the radius of the screen
var radius = Math.Min(e.Info.Width / 2f, e.Info.Height / 2f);

// create the paint for a star
var starPaint = new SKPaint
// create a paint for text
var textPaint = new SKPaint
{
IsAntialias = true,
Color = SKColors.Gold
TextSize = 12,
};

// draw the star
canvas.DrawStar(e.Info.Width / 2f, e.Info.Height / 2f, radius * 0.4f, radius * 0.2f, 5, starPaint);

// create the paint for the pie
var piePaint = new SKPaint
// create the paint for the shapes
var shapePaint = new SKPaint
{
IsAntialias = true,
Color = SKColors.Green
Color = SKColors.CadetBlue,
};

// calculate the pie
var piePath = SKGeometry.CreatePiePath(sectors, radius * 0.9f, radius * 0.5f, 6f);
var bigSpace = 12f;
var smallSpace = 6f;
var shapeSize = 100f;

var offsetX = bigSpace;
var offsetY = bigSpace;

// Square
offsetY += textPaint.TextSize;
canvas.DrawText("Square (from center)", offsetX, offsetY, textPaint);
offsetY += smallSpace;
canvas.DrawSquare(offsetX + 50, offsetY + 50, 75, shapePaint);
offsetY += shapeSize;

// Triangle
offsetY += textPaint.TextSize;
canvas.DrawText("Triangle (using radius)", offsetX, offsetY, textPaint);
offsetY += smallSpace;
canvas.DrawTriangle(offsetX + 50, offsetY + 50, 40, shapePaint);
offsetY += shapeSize;

// Triangle
offsetY += textPaint.TextSize;
canvas.DrawText("Triangle (using radius)", offsetX, offsetY, textPaint);
offsetY += smallSpace;
canvas.DrawTriangle(offsetX + 75, offsetY + 50, 75, 40, shapePaint);
offsetY += shapeSize;

// Triangle
offsetY += textPaint.TextSize;
canvas.DrawText("Triangle (using rect)", offsetX, offsetY, textPaint);
offsetY += smallSpace;
canvas.DrawTriangle(SKRect.Create(offsetX, offsetY + 15, 150, 75), shapePaint);
offsetY += shapeSize;

// Regular Polygons
offsetY += textPaint.TextSize;
canvas.DrawText("Regular Polygons", offsetX, offsetY, textPaint);
offsetY += smallSpace;
for (var i = 5; i < 12; i++)
{
var x = (i - 5) * 60;
canvas.DrawRegularPolygon(offsetX + 50 + x, offsetY + 50, 30, i, shapePaint);
}
offsetY += shapeSize;

// draw the pie
canvas.Translate(e.Info.Width / 2f, e.Info.Height / 2f);
canvas.DrawPath(piePath, piePaint);
// Regular Stars
offsetY += textPaint.TextSize;
canvas.DrawText("Regular Stars", offsetX, offsetY, textPaint);
offsetY += smallSpace;
for (var i = 5; i < 12; i++)
{
var x = (i - 5) * 60;
canvas.DrawStar(offsetX + 50 + x, offsetY + 50, 30, 15, i, shapePaint);
}
}
}
}
14 changes: 7 additions & 7 deletions samples/Maui/SkiaSharpDemo/Demos/BlurHash/BlurHashPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public BlurHashPage()

Sources = new ObservableCollection<StreamImageSource?>
{
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("img1.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("img2.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("img3.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("img4.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("img5.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("img6.png") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("img7.png") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("BlurHash/img1.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("BlurHash/img2.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("BlurHash/img3.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("BlurHash/img4.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("BlurHash/img5.jpg") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("BlurHash/img6.png") },
new StreamImageSource { Stream = token => FileSystem.OpenAppPackageFileAsync("BlurHash/img7.png") },
null,
};

Expand Down
10 changes: 7 additions & 3 deletions samples/Maui/SkiaSharpDemo/Demos/Extended/ShapesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:SkiaSharp.Views.Maui.Controls;assembly=SkiaSharp.Views.Maui.Controls"
x:Class="SkiaSharpDemo.Demos.ShapesPage"
Title="Shapes"
Padding="12">
Title="Shapes">

<views:SKCanvasView PaintSurface="OnPaintSurface" />
<ScrollView>
<views:SKCanvasView x:Name="canvasView"
HeightRequest="720"
IgnorePixelScaling="True"
PaintSurface="OnPaintSurface" />
</ScrollView>

</ContentPage>
110 changes: 80 additions & 30 deletions samples/Maui/SkiaSharpDemo/Demos/Extended/ShapesPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
using SkiaSharp;
using Microsoft.Maui.Dispatching;
using SkiaSharp;
using SkiaSharp.Extended;
using SkiaSharp.Views.Maui;

namespace SkiaSharpDemo.Demos;

public partial class ShapesPage : ContentPage
{
private readonly Random random = new Random();

private List<float>? sectors;
private static readonly Random random = new Random();
private readonly List<float> sectors = GeneratePieData();
private readonly SKColor[] colors =
{
0xff959edf,
0xff2de513,
0xffdbcc93,
0xffaa6b7f,
0xff3c5061,
0xffa31359,
0xff29ef1b,
0xff3d0fad,
0xffd365f1,
0xff3cd7d5,
};

public ShapesPage()
{
InitializeComponent();

GenerateData();

BindingContext = this;
}

protected override void OnAppearing()
{
base.OnAppearing();

GenerateData();
canvasView.InvalidateSurface();
}

private void GenerateData()
private static List<float> GeneratePieData()
{
// generate some random data
var count = random.Next(5, 10);
Expand All @@ -38,46 +49,85 @@ private void GenerateData()
var sum = data.Sum();

// get the percentages
sectors?.Clear();
sectors = data.Select(d => d / sum).ToList();
return data.Select(d => d / sum).ToList();
}

private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
if (sectors == null)
return;

var surface = e.Surface;
var canvas = surface.Canvas;

// clear the surface
canvas.Clear(SKColors.Transparent);

// get the radius of the screen
var radius = Math.Min(e.Info.Width / 2f, e.Info.Height / 2f);

// create the paint for a star
var starPaint = new SKPaint
// create a paint for text
var textPaint = new SKPaint
{
IsAntialias = true,
Color = SKColors.Gold
TextSize = 12,
};

// draw the star
canvas.DrawStar(e.Info.Width / 2f, e.Info.Height / 2f, radius * 0.4f, radius * 0.2f, 5, starPaint);

// create the paint for the pie
var piePaint = new SKPaint
// create the paint for the shapes
var shapePaint = new SKPaint
{
IsAntialias = true,
Color = SKColors.Green
Color = SKColors.CadetBlue,
};

// calculate the pie
var piePath = SKGeometry.CreatePiePath(sectors, radius * 0.9f, radius * 0.5f, 6f);
var bigSpace = 12f;
var smallSpace = 6f;
var shapeSize = 100f;

var offsetX = bigSpace;
var offsetY = bigSpace;

// Square
offsetY += textPaint.TextSize;
canvas.DrawText("Square (from center)", offsetX, offsetY, textPaint);
offsetY += smallSpace;
canvas.DrawSquare(offsetX + 50, offsetY + 50, 75, shapePaint);
offsetY += shapeSize;

// Triangle
offsetY += textPaint.TextSize;
canvas.DrawText("Triangle (using radius)", offsetX, offsetY, textPaint);
offsetY += smallSpace;
canvas.DrawTriangle(offsetX + 50, offsetY + 50, 40, shapePaint);
offsetY += shapeSize;

// Triangle
offsetY += textPaint.TextSize;
canvas.DrawText("Triangle (using radius)", offsetX, offsetY, textPaint);
offsetY += smallSpace;
canvas.DrawTriangle(offsetX + 75, offsetY + 50, 75, 40, shapePaint);
offsetY += shapeSize;

// Triangle
offsetY += textPaint.TextSize;
canvas.DrawText("Triangle (using rect)", offsetX, offsetY, textPaint);
offsetY += smallSpace;
canvas.DrawTriangle(SKRect.Create(offsetX, offsetY + 15, 150, 75), shapePaint);
offsetY += shapeSize;

// Regular Polygons
offsetY += textPaint.TextSize;
canvas.DrawText("Regular Polygons", offsetX, offsetY, textPaint);
offsetY += smallSpace;
for (var i = 5; i < 12; i++)
{
var x = (i - 5) * 60;
canvas.DrawRegularPolygon(offsetX + 50 + x, offsetY + 50, 30, i, shapePaint);
}
offsetY += shapeSize;

// draw the pie
canvas.Translate(e.Info.Width / 2f, e.Info.Height / 2f);
canvas.DrawPath(piePath, piePaint);
// Regular Stars
offsetY += textPaint.TextSize;
canvas.DrawText("Regular Stars", offsetX, offsetY, textPaint);
offsetY += smallSpace;
for (var i = 5; i < 12; i++)
{
var x = (i - 5) * 60;
canvas.DrawStar(offsetX + 50 + x, offsetY + 50, 30, 15, i, shapePaint);
}
}
}
1 change: 1 addition & 0 deletions samples/Maui/SkiaSharpDemo/Demos/PlaygroundPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:SkiaSharp.Extended.UI.Controls;assembly=SkiaSharp.Extended.UI"
xmlns:views="clr-namespace:SkiaSharpDemo.Views"
x:Class="SkiaSharpDemo.Demos.PlaygroundPage"
Title="Play">
Expand Down
Loading

0 comments on commit ed1fc63

Please sign in to comment.