So on my last post I shared a recap of my tech talk on SkiaSharp with Xamarin.Forms, check it out if you missed it: So I gave a Tech Talk on SkiaSharp with Xamarin.Forms…
There I talked about some of the most important parts of the whole 1 hour plus presentation-hands-on-labs session, in which I didn’t share all the details of the whole session. I did a pretty comprehensive demo session there, specially about the 2D drawing basics of SkiaSharp, which I didn’t highlight in that post.
Basic 2D Shapes with SkiaSharp…
So today I thought of sharing the demos I did there, about basic 2D shapes drawing with SkiaSharp more extensively… 🙂 Since there seem to be a lack of tutorials explaining this topic of, “draw basic Shapes with SkiaSharp”, which I think should be more important for beginners!
So buckle up fellas, let’s see how we could draw some of the most commonly used 2D shapes with SkiaSharp with ease… 😉
There’s many out of the box support for drawing basic 2D Shapes from SkiaSharp, such as DrawCircle(), DrawRectangle(), DrawLine(), DrawOval() and so on many more. You could stright away use those methods or you could even go around it and use Paths and Lines drawing methods of SkiaSharp in order to draw them, which is completely up to you.
But SkiaSharp doesn’t have methods for drawing for every single kind of Geometrical shape there is out there. So if you want to draw some kind of complex shape, then you could basically use a combination of Paths and Lines drawing methods in SkiaSharp, which has many kinds of methods you could come up with. 😉 that’s the beauty of SkiaSharp! Anyways the choice of drawing methods are totally up to you!
Now if you want to get ahead of yourself, you may grab the live hands on demo code I did at the presentation which includes all of the below code, right from my github repo: https://github.com/UdaraAlwis/XFSkiaSharpDemo
Just on a note, here I will not be discussing basics of SkiaSharp or the setting up of SkiaSharp library or the Canvas properties and behaviours, I’ll directly get into the programming of the shapes drawing, but if you want to get a head start, head off to Xamarin SkiaSharp Documentation or my previous post, So I gave a Tech Talk on SkiaSharp with Xamarin.Forms…
1. Simple Stroke Line…
We use the DrawLine() and pass in the Line’s starting point’s XY position and and ending point’s XY position, while passing in the paint configuration, SKPaint as we wish.
Since SkiaSharp support pure Xamarin.Forms you can straight away run all your native projects without any hassle of handling native code.
2. Drawing a Circle (Filled)
We shall be using the DrawCircle() whilst passing in the Circle’s center XY position and desired radius for it. To define whether its a Filled or Non-Filled circle we’ll be using Style property in our SKPaint configuration.
Next let’s draw a Circle with just the stroke (with filling the inner of the circle).
3. Drawing a Circle (Un-filled)
We do this by setting the Style property to Stroke! and everything else is the same 🙂
Look how simple eh 😉
4. A Square Rectangle!
How about a standard Rectangle? We shall use the SKRect object to configure our Rectangle as we wish and draw it up!
See it in action? 😉
The square root of 69 is 8 something, right? – Drake 😉 lol
5. Let’s draw an Ellipse…
There’s many ways to draw an Eclipse, but most common way is to use DrawOval(), as well as other kinds of complex drawings.
So here we’re configuring a Rectangle with SKRect, which an Ellipse could be mathematically consist of.
6. How about an Arc shape?
Well it’s basically the same concept as of an Ellipse, but since we need an “Arc”, we’re going to use some basic mathematical angles to configure the starting angle, startAngle and sweep angle, sweepAngle of the Arc we’re going to draw with a Path object.
So there we’re configuring our Path object to start off from -90 degrees and ends up at 230 degrees from the start point, drawing the Arc shape. Notice the comment I’ve added there, showcasing how you could also calculate the Arc’s drawing angle as a percentage value. 😀
Pretty cool eh! 😉
7. Did we forget Text?
Did you know you could even draw text on a SkiaSharp canvas right away by using DrawText() method.
SkPaint object holds several properties for drawing Text on the canvas, such as TextAlright, TextSize and many more you could play around with..
Hello World, indeed! 😉
8. Let’ draw a simple Triangle?
Well SkiaSharp doesn’t have a out of the box method call for drawing a Triangle, this is where simple Drawing path and points comes into play.
So basically what we do is, we’ll draw three lines that’s interconnects at the ending points, using DrawPoints() method and pass in the list of Points that’ll draw the Lines…
See it first may be?
So now if you think about it, you could actually draw any kind of a Shape with interconnecting Points and Paths using the above method. 😀
9. Draw any Shape?
It’s true earlier step, in Triangle drawing I said you could use the DrawPoints() and a bunch of Points to draw any kind of shape in SkiaSharp. This is actually a painful, but there’s actually a better way… 😉 yaay!
So basically if you needed to draw any kind of shape, all you need is a Path and a bunch of Points that interconnects. A much easier way to do this is by using a SKPath configuration object, using this you could pass define the Starting Point of the drawing path, move around the drawing path with interconnecting Points by using MoveTo() and LineTo() calls. For this you use the mighty DrawPath() method, which you could use to draw anything on the canvas. 😀
There you go…
So with the use of SKPath, you could draw any kind of 2D shape as you wish… 😀
10. Final shape?
Oh sorry! there ain’t none! 😛 just put up a 10th point for the fun of it! 😉
Well you could grab all of the above code up in my Github repo: https://github.com/UdaraAlwis/XFSkiaSharpDemo That right there is actually the live hands on demo code I did at my original presentation…
So now get out of here and start drawing 2D with SkiaSharp! 😀
or may be check out my talk on SkiaSharp…
Shape the love fellas! 😀