title author date CreateTime categories
win10 UWP 蜘蛛网效果
lindexi
2018-2-13 17:23:3 +0800
2018-2-13 17:23:3 +0800
Win10 UWP

我看见了知乎首页登录背景和普通的地球人写的博客,发现了个好看的效果。

那么我来告诉大家如何做这个效果。
第一步是在 Canvas 画点,第二步是让点移动,第三步是画线

在 Canvas 画一个点

我们画点可以使用 Ellipse 我们给他宽和高,Fill,就可以画出来。需要加在 Canvas ,可以使用canvas.Children.Add(ellipse)

Canvas 一开始的大小是 0 ,需要一开始给他一个足够大的值

  1. <Canvas x:Name="P" Width="1000" Height="1000"/>

于是给他一个比较大的值,超过外面的Grid就不显示。

添加 一个 Ellipse 就会显示,可以没有指定在哪显示,也就是显示的 X 和 Y。

指定添加到 Canvas 的 Element 的位置

我们可以通过几个方法改变控件的位置,在我之前写的拖动控件博客有说到。

现在使用 Canvas,可以使用 Canvas 有的一个方法。

假如有一个 X 和 Y 要设置在控件,那么可以使用

  1. Canvas.SetLeft(control, X);
  2. Canvas.SetTop(control, Y);

注意,Canvas 是类。

这个方法可以移动控件。

我就是用他移动点。

随机移动点

我首先写一个类,Staf。包含显示的 Point 和他的 X,Y,两个方向移动速度。还有移动多久,超过了可以移动的时间,就随机给新移动速度。

  1. public class Staf
  2. {
  3. public UIElement Point { set; get; }
  4.  
  5. public double X { set; get; }
  6.  
  7. public double Y { set; get; }
  8.  
  9. public double Vx { set; get; }
  10.  
  11. public double Vy { set; get; }
  12.  
  13. public void RandomStaf(Random ran)
  14. {
  15. var staf = this;
  16. _ran = ran;
  17. staf.Vx = (double)ran.Next(-1000, 1000) / 1000;
  18. staf.Vy = (double)ran.Next(-1000, 1000) / 1000;
  19. staf.Time = ran.Next(100);
  20. }
  21. private Random _ran;
  22. public int Time
  23. {
  24. set
  25. {
  26. _time = value;
  27. if (value == 0)
  28. {
  29. RandomStaf(_ran);
  30. }
  31. }
  32. get
  33. {
  34. return _time;
  35. }
  36. }
  37.  
  38. private int _time;
  39. }

画线

使用两重 foreach ,得到两个点之间距离,如果距离小于我给的一个值,那么就是可以连线

那么距离长的就把连线的宽度变短。

这个做法很简单,可以使用 StrokeThickness 设置线宽度。

  1. line.StrokeThickness=最大宽度 * (最大距离-距离)/最大距离

线需要多少个点可以确定?这个我就不说啦,确定了两个点是可以连线,于是使用就可以设置线的点。需要知道,点的X和Y是左上角,需要加上画的图形的值才是连在点,不然看起来不是连在点。

自动移动

可以使用 DispatcherTimer ,过 0.1 s就移动点和画线。

  1. public MainPage()
  2. {
  3. this.InitializeComponent();
  4. _time = new DispatcherTimer();
  5. _time.Interval = TimeSpan.FromTicks(500);
  6. _time.Tick += Time_Tick;
  7. _time.Start();
  8. }
  9.  
  10. private DispatcherTimer _time

Time_Tick就写移动点和线的代码

全部代码


  1. <Page
  2. x:Class="Bsgame.MainPage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Bsgame"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  10. <Canvas x:Name="P" Width="1000" Height="1000">
  11. </Canvas>
  12. <Canvas x:Name="Pw" Width="1000" Height="1000"></Canvas>
  13. </Grid>
  14. </Page>
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Runtime.InteropServices.WindowsRuntime;
  20. using Windows.Foundation;
  21. using Windows.Foundation.Collections;
  22. using Windows.UI;
  23. using Windows.UI.Xaml;
  24. using Windows.UI.Xaml.Controls;
  25. using Windows.UI.Xaml.Controls.Primitives;
  26. using Windows.UI.Xaml.Data;
  27. using Windows.UI.Xaml.Input;
  28. using Windows.UI.Xaml.Media;
  29. using Windows.UI.Xaml.Media.Animation;
  30. using Windows.UI.Xaml.Navigation;
  31. using Windows.UI.Xaml.Shapes;
  32. namespace Bsgame
  33. {
  34. /// <summary>
  35. /// 可用于自身或导航至 Frame 内部的空白页。
  36. /// </summary>
  37. public sealed partial class MainPage : Page
  38. {
  39. public MainPage()
  40. {
  41. this.InitializeComponent();
  42. _time = new DispatcherTimer();
  43. _time.Interval = TimeSpan.FromTicks(500);
  44. _time.Tick += Time_Tick;
  45. RandomStaf();
  46. _time.Start();
  47. _width = Window.Current.Bounds.Width;
  48. _height = Window.Current.Bounds.Height;//lindexi
  49. }
  50. private void RandomStaf()
  51. {
  52. const int count = 20;
  53. for (int i = 0; i < count; i++)
  54. {
  55. Staf staf = new Staf();
  56. staf.X = ran.Next((int)_width);
  57. staf.Y = ran.Next((int)_height);
  58. staf.Point = new Ellipse()
  59. {
  60. Height = 10,
  61. Width = 10,
  62. Fill = new SolidColorBrush(Colors.Gray),
  63. };
  64. staf.RandomStaf(ran);
  65. // CSDN
  66. _staf.Add(staf);
  67. }
  68. foreach (var temp in _staf)
  69. {
  70. P.Children.Add(temp.Point);
  71. //lindexi
  72. }
  73. }
  74. private List<Staf> _staf = new List<Staf>();
  75. private double _width;
  76. private double _height;
  77. private void Time_Tick(object sender, object e)
  78. {
  79. foreach (var temp in _staf)
  80. {
  81. if (temp.X > _width || temp.Y > _height
  82. || temp.X < 0 || temp.Y < 0)
  83. {
  84. temp.X = ran.Next((int)_width);
  85. temp.Y = ran.Next((int)_height);
  86. }//lindexi.oschina.io
  87. temp.X -= temp.Vx;
  88. temp.Y -= temp.Vy;
  89. Canvas.SetLeft(temp.Point, temp.X);
  90. Canvas.SetTop(temp.Point, temp.Y);
  91. temp.Time--;
  92. }
  93. const double distan = 200;
  94. Pw.Children.Clear();
  95. Line line = new Line();
  96. foreach (var temp in _staf)
  97. {
  98. foreach (var p in _staf)
  99. {
  100. line.X1 = temp.X + 5;
  101. line.Y1 = temp.Y + 5;
  102. line.X2 = p.X + 5;
  103. line.Y2 = p.Y + 5;
  104. double sqrt = Math.Sqrt(Math.Pow((line.X1 - line.X2), 2) +
  105. Math.Pow((line.Y1 - line.Y2), 2));
  106. if (sqrt < distan)
  107. {
  108. line.Stroke = new SolidColorBrush(Colors.Gray);
  109. line.StrokeThickness = 5* (distan- sqrt) /distan;
  110. Pw.Children.Add(line);
  111. line = new Line();
  112. }
  113. }
  114. }
  115. }
  116. private Random ran = new Random();
  117. private DispatcherTimer _time;
  118. }
  119. public class Staf
  120. {
  121. public UIElement Point { set; get; }
  122. public double X { set; get; }
  123. public double Y { set; get; }
  124. public double Vx { set; get; }
  125. public double Vy { set; get; }
  126. public void RandomStaf(Random ran)
  127. {
  128. var staf = this;
  129. _ran = ran;
  130. staf.Vx = (double)ran.Next(-1000, 1000) / 1000;
  131. staf.Vy = (double)ran.Next(-1000, 1000) / 1000;
  132. staf.Time = ran.Next(100);
  133. }
  134. private Random _ran;
  135. public int Time
  136. {
  137. set
  138. {
  139. _time = value;
  140. if (value == 0)
  141. {
  142. RandomStaf(_ran);
  143. }
  144. }
  145. get
  146. {
  147. return _time;
  148. }
  149. }
  150. private int _time;
  151. }
  152. }

可以看到性能很差,于是把连线去掉,显示点不显示连接

  1. private void RandomStaf(object sender, object e)
  2. {
  3. Storyboard board = new Storyboard();
  4. board.Duration = new Duration(TimeSpan.FromSeconds(1));
  5. board.Completed += RandomStaf;
  6. DoubleAnimationUsingKeyFrames animation;
  7. foreach (var temp in _staf)
  8. {
  9. double f = temp.X;
  10.  
  11. temp.X += temp.Vx * 10;
  12. if (temp.X > _width - 100)
  13. {
  14. temp.X = _width - 100;
  15. }
  16. else if (temp.X < 0)
  17. {
  18. temp.X = 0;
  19. }
  20.  
  21. animation = EllPoile(f, temp.X);
  22. Storyboard.SetTarget(animation, temp.Point);
  23. Storyboard.SetTargetProperty(animation, "(Canvas.Left)");
  24. board.Children.Add(animation);
  25.  
  26. f = temp.Y;
  27. temp.Y += temp.Vy * 10;
  28.  
  29. if (temp.Y > _height - 100)
  30. {
  31. temp.Y = _height - 100;
  32. }
  33. else if (temp.Y < 0)
  34. {
  35. temp.Y = 0;
  36. }
  37.  
  38. animation = EllPoile(f, temp.Y);
  39. Storyboard.SetTarget(animation, temp.Point);
  40. Storyboard.SetTargetProperty(animation, "(Canvas.Top)");
  41.  
  42. if (temp.X >= _width - 100 || temp.Y >= _height - 100
  43. || temp.X <= 0 || temp.Y <= 0)
  44. {
  45. temp.X = ran.Next((int)_width);
  46. temp.Y = ran.Next((int)_height);
  47. }
  48. board.Children.Add(animation);
  49. temp.Time -= 10;
  50.  
  51. animation = EllPoile(10, 15);
  52. Storyboard.SetTarget(animation, temp.Point);
  53. Storyboard.SetTargetProperty(animation, "Height");
  54. board.Children.Add(animation);
  55.  
  56. animation = EllPoile(10, 15);
  57. Storyboard.SetTarget(animation, temp.Point);
  58. Storyboard.SetTargetProperty(animation, "Width");
  59. board.Children.Add(animation);
  60.  
  61. animation = new DoubleAnimationUsingKeyFrames();
  62. EasingDoubleKeyFrame frame = new EasingDoubleKeyFrame();
  63. frame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
  64. frame.Value = 0;
  65. animation.KeyFrames.Add(frame);
  66.  
  67. frame = new EasingDoubleKeyFrame();
  68. frame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5));
  69. frame.Value = 180;
  70. animation.KeyFrames.Add(frame);
  71.  
  72. frame = new EasingDoubleKeyFrame();
  73. frame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1));
  74. frame.Value = 0;
  75. animation.KeyFrames.Add(frame);
  76. Storyboard.SetTarget(animation, temp.Point.RenderTransform);
  77. Storyboard.SetTargetProperty(animation, "(CompositeTransform.Rotation)");
  78. board.Children.Add(animation);
  79.  
  80. }
  81. board.Begin();
  82.  
  83. }

2018-2-13-win10-UWP--蜘蛛网效果的更多相关文章

  1. win10 UWP 蜘蛛网效果

    我看见了知乎首页登录背景和普通的地球人写的博客,发现了个好看的效果. 那么我来告诉大家如何做这个效果. 第一步是在 Canvas 画点,第二步是让点移动,第三步是画线 在 Canvas 画一个点 我们 ...

  2. win10 uwp 萤火虫效果

    原文:win10 uwp 萤火虫效果 本文在Nukepayload2指导下,使用他的思想用C#写出来. 本文告诉大家,如何使用 win2d 做出萤火虫效果. 安装 win2d 安装win2d的方法请使 ...

  3. win10 uwp 视差效果

    本文翻译:http://jamescroft.co.uk/blog/uwp/playing-with-scrolling-parallax-effects-on-ui-elements-in-wind ...

  4. win10 uwp win2d CanvasVirtualControl 与 CanvasAnimatedControl

    本文来告诉大家 CanvasVirtualControl ,在什么时候使用这个控件. 在之前的入门教程win10 uwp win2d 入门 看这一篇就够了我直接用的是CanvasControl,实际上 ...

  5. win10 uwp 使用 Microsoft.Graph 发送邮件

    在 2018 年 10 月 13 号参加了 张队长 的 Office 365 训练营 学习如何开发 Office 365 插件和 OAuth 2.0 开发,于是我就使用 UWP 尝试使用 Micros ...

  6. Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

    安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...

  7. Win10 UWP开发系列:实现Master/Detail布局

    在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...

  8. 【Win10 UWP】后台任务与动态磁贴

    动态磁贴(Live Tile)是WP系统的大亮点之一,一直以来受到广大用户的喜爱.这一讲主要研究如何在UWP应用里通过后台任务添加和使用动态磁贴功能. 从WP7到Win8,再到Win10 UWP,磁贴 ...

  9. win10 uwp 列表模板选择器

    本文主要讲ListView等列表可以根据内容不同,使用不同模板的列表模板选择器,DataTemplateSelector. 如果在 UWP 需要定义某些列的显示和其他列不同,或者某些行的显示和其他行不 ...

  10. win10 uwp 毛玻璃

    毛玻璃在UWP很简单,不会和WPF那样伤性能. 本文告诉大家,如何在 UWP 使用 win2d 做毛玻璃. 毛玻璃可以使用 win2D 方法,也可以使用 Compositor . 使用 win2d 得 ...

随机推荐

  1. Failed to delete access_log

    重复build 关闭已经开启的tomcat    terminal 再次开启即可

  2. oralce MTS

    MTS的组件包括: processes on the system. communication software. the shared global section (SGA).          ...

  3. oracle怎么捕获表上的DML语句(不包括select)语句)

    可以采用dml触发器,如 CREATE OR REPLACE TRIGGER tr_capt_sql BEFORE DELETE OR INSERT OR UPDATE ON manager.test ...

  4. Nuxt.js打造旅游网站第2篇_首页开发

    页面效果: 1.初始化默认布局 nuxtjs提供了一个公共布局组件layouts/default.vue,该布局组件默认作用于所有页面,所以我们可以在这里加上一些公共样式,在下一小结中还会导入公共组件 ...

  5. LightOJ 1370 Bi-shoe and Phi-shoe【欧拉函数 && 质数】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题意: 给定值,求满足欧拉值大于等于这个 ...

  6. GMTC2019|闲鱼-基于Flutter的架构演进与创新

    2012年应届毕业加入阿里巴巴,主导了闲鱼基于Flutter的新混合架构,同时推进了Flutter在闲鱼各业务线的落地.未来将持续关注终端技术的演变及趋势 Flutter的优势与挑战 Flutter是 ...

  7. @NOIP2018 - D2T3@ 保卫王国

    目录 @题目描述@ @题解@ @代码@ @题目描述@ Z 国有n座城市,n−1 条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻 ...

  8. SprinfJdbcTemplate+SpringMVC 代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件)

    代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件) 原文地址: http://jilongliang.iteye.com/blog/2262070 p ...

  9. oracle函数 nls_charset_name(n1)

    [功能]返回字符集名称参应id值 [参数]n1,数值型 [返回]字符型 sql> select nls_charset_name(852) from dual; nls_char ------- ...

  10. 高二小假期集训—D5

    刚调完了一个非常恶心的题(可能是我写的太恶心了),心累……先写会博客吧. 今天上午该完了考试的三道题,感觉第二道真的是个好题(学长说是经常会遇到的一类题……完了完了),看了一个小时std才看懂,写了篇 ...