当前位置: 首页 > news >正文

C# Avalonia 16- Animation- SampleViewer - FollowExample

FollowExample.axaml代码

<UserControl xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"x:Class="AvaloniaUI.FollowExample"><Canvas x:Name="containerCanvas" Background="transparent" PointerMoved="OnPointerMoved"><Rectangle x:Name="followRectangle" Canvas.Left="0" Canvas.Top="0" Fill="red" Width="50" Height="50" /></Canvas>
</UserControl>

FollowExample.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Shares.Avalonia;
using System;namespace AvaloniaUI;public partial class FollowExample : UserControl
{private readonly AnimationPlayer animation = new AnimationPlayer();private Point lastMousePosition = new Point(300, 200);private Vector rectangleVelocity = new Vector(0, 0);public FollowExample(){InitializeComponent();// 初始位置Canvas.SetLeft(followRectangle, lastMousePosition.X);Canvas.SetTop(followRectangle, lastMousePosition.Y);// 配置 AnimationPlayeranimation.Duration = double.MaxValue; // 无限播放animation.Loop = true;animation.Fps = 60;// 每帧执行“跟随逻辑”animation.At(0).PlayGlobal(UpdateRectangle);animation.Start();}private void OnPointerMoved(object? sender, PointerEventArgs e){lastMousePosition = e.GetPosition(containerCanvas);}private void UpdateRectangle(double globalProgress){// 当前矩形位置var location = new Point(Canvas.GetLeft(followRectangle),Canvas.GetTop(followRectangle));// 指向鼠标的向量Vector toMouse = lastMousePosition - location;// 施加“跟随力”double followForce = 0.01;rectangleVelocity += toMouse * followForce;// 阻尼系数(防止发散)double drag = 0.8;rectangleVelocity *= drag;// 更新位置location += rectangleVelocity;Canvas.SetLeft(followRectangle, location.X);Canvas.SetTop(followRectangle, location.Y);}
}

运行效果

image

 

http://www.hskmm.com/?act=detail&tid=36238

相关文章:

  • 五节课掌握 Python 面向对象(以“人狗大战”为例)
  • 联邦学习与AI公平性研究新进展
  • ESP32 Arduino核心框架:全面支持多款ESP32芯片的开发平台
  • 图像分割 3D-Box-Segment-Anything(5)如何加速 - MKT
  • 静态方法(`@staticmethod`)和类方法(`@classmethod`)的应用场景及选择原则
  • 数据集 - MKT
  • 专门针对无人机分割的预训练模型​ - MKT
  • 为什么一般教材在讲解python的多态概念时,不用抽象基类及其相关内容讲解呢?
  • [Bash] bash scripting common pitfalls
  • [Bash]让人头晕的if条件
  • 鸭子类型(Duck Typing)中的“类型”,指的是什么的类型?为什么很多人认为“Python 没有真正实现多态”?多态的核心目的是什么?鸭子类型如何实现多态?
  • tryhackme-预安全-windows基础-windows 基础知识1-16
  • YOLO11深度学习的遥感视角地面房屋建筑检测分割与分析系统 - MKT
  • 鸭子类型(Duck Typing)中的“类型”,指的是什么的类型?为什么很多人认为“Python 没有真正实现多态”
  • OAK-D-SR近红外相机 - MKT
  • 图像分割 Segment Anything(1-2)第二代 - MKT
  • 对比c++中的多态和python的多态
  • 结对项目-自动生成小学四则运算题目命令行程序
  • tryhackme-预安全-linux 基础-Linux 基础知识(第二部分)-14
  • tryhackme-预安全-linux 基础-Linux 基础知识(第一部分)-13
  • 我测试了七个主流后端框架的性能-结果让我重新思考了技术选型
  • tryhackme-预安全-网络如何工作-总结-12
  • 目标检测 Grounding DINO 用语言指定要检测的目标 - MKT
  • 图像分割 Segment Anything(3)分割2D到3D点云分割 rgb-d相机 - MKT
  • 图像分割 3D-Box-Segment-Anything(3)分割2D到3D点云分割 rgb相机 - MKT
  • 图像分割 Segment Anything(3)分割2D到3D点云分割 rgb相机 - MKT
  • Python 包管理工具推荐:uv
  • 3D框预测 VoxelNeXt - MKT
  • 【神器】如何查看api域名内容
  • 高级程序语言第二次作业