当前位置:网站首页>Skiasharp's WPF self drawn bouncing ball (case version)
Skiasharp's WPF self drawn bouncing ball (case version)
2022-07-26 23:57:00 【Lanchuang elite team】
Familiar with SkiaSharp Basic operation of , Make a bouncing ball effect this time , After implementation , Found that the effect is really good .
The general effect is as follows :

Principle analysis
First, the grid effect is realized , This is simpler , Direct horizontal line , A vertical bar , Just OK 了 .
The other is a random circle , I have a hundred randomly , The initial position is roughly the same , however , The angle in each direction is random , The color is random , The movement speed is random .
then , They move , It will automatically rebound back when it meets the wall , Formed a good visual effect .
Wpf and SkiaSharp
Create a new one WPF project , then ,Nuget Bag can
You want to add Nuget package
Install-Package SkiaSharp.Views.WPF -Version 2.88.0
The core logic is this part , It will be set by me 60FPS To refresh the current palette .
skContainer.PaintSurface += SkContainer_PaintSurface;
_ = Task.Run(() =>
{
while (true)
{
try
{
Dispatcher.Invoke(() =>
{
skContainer.InvalidateVisual();
});
_ = SpinWait.SpinUntil(() => false, 1000 / 60);// Per second 60 frame
}
catch
{
break;
}
}
});
Realize the circular logic of the code
/// <summary>
/// circle
/// </summary>
internal class Circles
{
private Random r = new Random();
public Circles()
{
VelocityX = GetRandom(0, 3);
VelocityY = GetRandom(0, 3);
Radius = GetRandom(0, 50);
Color = new SKColor((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255));
}
public float X {
get; set; } = 100;
public float Y {
get; set; } = 100;
public float VelocityX {
get; set; }
public float VelocityY {
get; set; }
public float Radius {
get; set; }
public SKColor Color {
get; set; }
public float GetRandom(int min, int max)
{
var result = r.Next(min * 100, max * 100);
return (float)(result / 100.0);
}
}
Circular movement logic
/// <summary>
/// Adjusting position
/// </summary>
public void AdjustPosition(SKCanvas canvas, SKTypeface Font, int Width, int Height)
{
foreach (var circle in circles)
{
using var paint = new SKPaint
{
Color = circle.Color,
Style = SKPaintStyle.Fill,
IsAntialias = true,
StrokeWidth = 1
};
canvas.DrawCircle(circle.X, circle.Y, circle.Radius, paint);
if (circle.X + circle.VelocityX + circle.Radius > Width || circle.X + circle.VelocityX - circle.Radius < 0)
{
circle.VelocityX = -circle.VelocityX;
}
if (circle.Y + circle.VelocityY + circle.Radius > Height || circle.Y + circle.VelocityY - circle.Radius < 0)
{
circle.VelocityY = -circle.VelocityY;
}
circle.X += circle.VelocityX;
circle.Y += circle.VelocityY;
}
}
Implement the logic of grid
/// <summary>
/// Draw a grid
/// </summary>
public void DrawGrid(SKCanvas canvas, SKColor sKColor, int Width, int Height, int StepX, int StepY)
{
using var paint = new SKPaint
{
Color = sKColor,
Style = SKPaintStyle.Stroke,
StrokeWidth = 0.5f,
IsStroke = true,
IsAntialias = true
};
for (var i = 0.5; i < Width; i += StepX)
{
var path = new SKPath();
path.MoveTo((float)i, 0);
path.LineTo((float)i, Height);
path.Close();
canvas.DrawPath(path, paint);
}
for (var i = 0.5; i < Height; i += StepY)
{
var path = new SKPath();
path.MoveTo(0, (float)i);
path.LineTo(Width, (float)i);
path.Close();
canvas.DrawPath(path, paint);
}
}
}
effect

The effect is really good .
summary
This case is solved , The next time , Think of a good case .
Code address
https://github.com/kesshei/BouncingBallsDemo.git
https://gitee.com/kesshei/BouncingBallsDemo.git
reading
One button three times !, Thank you for your support , Your support is my motivation !
边栏推荐
- Question 141 of Li Kou: circular linked list
- 动态sql
- 12_ Binding style
- Chapter 1 requirements analysis and SSM environment preparation
- Chapter 1 Introduction and use skills of interceptors
- [Luogu] p2341 popular cattle
- [literature reading] an investigation on hardware aware vision transformer scaling
- 带你熟悉云网络的“电话簿”:DNS
- Pytorch data pipeline standardized code template
- The basic operation of data tables in MySQL is very difficult. This experiment will take you through it from the beginning
猜你喜欢

C语言数组

MVC three-tier architecture

第二部分—C语言提高篇_7. 结构体

上千Tile的倾斜模型浏览提速,告别一块一块往外蹦的尴尬

Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching

第3章 跨域问题

08 design of intelligent agricultural environmental monitoring system based on ZigBee

Paging plug-in -- PageHelper

Part II - C language improvement_ 8. File operation
![[Gorm] model relationship -hasone](/img/90/3069059ddd09dc538c10f76d659b08.png)
[Gorm] model relationship -hasone
随机推荐
Galaxy securities online account opening commission, is online account opening safe for customer managers
Silicon Valley class lesson 5 - Tencent cloud object storage and course classification management
文件上传到服务器
Part II - C language improvement_ 9. Linked list
np. transpose & np.expand_ dims
Tencent cloud lightweight application server purchase method steps!
13_ conditional rendering
Thousands of tiles' tilt model browsing speeds up, saying goodbye to the embarrassment of jumping out one by one
What scenarios are Tencent cloud lightweight application servers suitable for?
[step by step, even thousands of miles] key words in the specified time period of the statistical log
MVC three-tier architecture
[interview: concurrent Article 27: multithreading: hesitation mode]
Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching
11_ Weather case - monitoring properties
[Luogu] p1395 meeting
[2016] [paper notes] differential frequency tunable THz technology——
MySQL syntax uses detailed code version
Paging plug-in -- PageHelper
Pytorch学习记录(二):张量
Silicon Valley class lesson 6 - Tencent cloud on demand management module (I)