news 2026/9/3 10:55:39

鸿蒙HarmonyOS NEXT 球型水波纹动画

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
鸿蒙HarmonyOS NEXT 球型水波纹动画

找了很久水波纹动画(不是扩散的那种水波纹),没找到,都是看来没人搞,那我来搞一个吧,线上图看效果,注释写的很详细了,我就不过多阐述了,看代码吧

@Entry @Component struct WaterWaveBall { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); @State private phase: number = 0; // 波浪相位 private timer: number = 0; // 定时器ID private centerX: number = 100; // 球心X坐标(200/2=100) private centerY: number = 100; // 球心Y坐标(200/2=100) private radius: number = 100; // 球体半径(留出边框空间) private amplitude: number = 10; // 波浪振幅(按比例缩小) private waveLength: number = 180; // 波浪长度(按比例缩小) private waterLevel: number = 180; // 水位高度(按比例缩小) private waveSpeed: number = 2; // 波浪速度(按比例调整) aboutToAppear() { // 启动波浪动画 this.setprogress(0.5) this.startWaveAnimation(); } aboutToDisappear() { // 清除定时器 if (this.timer) { clearInterval(this.timer); this.timer = 0; } } setprogress(p: number) { this.waterLevel = 180 - 180 * p } // 启动波浪动画 startWaveAnimation() { if (!this.timer) { this.timer = setInterval(() => { // 更新波浪相位 - 确保波浪由左向右移动 this.phase = (this.phase + this.waveSpeed) % this.waveLength; }, 30); } } // 计算波浪Y坐标(确保波浪向右移动) calculateWaveY(x: number, phase: number): number { return this.waterLevel + this.amplitude * Math.sin(2 * Math.PI * (x - phase) / this.waveLength); } // 绘制波浪(尺寸优化版) drawWave(ctx: CanvasRenderingContext2D) { const canvasSize = 200; // 画布尺寸改为200 // 1. 清除画布 ctx.clearRect(0, 0, canvasSize, canvasSize); // 2. 绘制绿色边框 ctx.beginPath(); ctx.arc(this.centerX, this.centerY, this.radius, 0, Math.PI * 2); ctx.lineWidth = 20; ctx.strokeStyle = Color.Orange; ctx.stroke(); // 3. 设置圆形裁剪区域 ctx.beginPath(); ctx.arc(this.centerX, this.centerY, this.radius - 10, 0, Math.PI * 2); ctx.clip(); // 4. 绘制水面区域(粉色)- 波浪由左向右移动 ctx.beginPath(); // 起始点 const startY = this.calculateWaveY(0, this.phase); ctx.moveTo(0, startY); // 使用二次贝塞尔曲线绘制主波浪 const points = 30; // 减少点数以适应小尺寸 for (let i = 1; i <= points; i++) { const x = (i / points) * canvasSize; const y = this.calculateWaveY(x, this.phase); // 前一个点 const prevX = ((i - 1) / points) * canvasSize; const prevY = this.calculateWaveY(prevX, this.phase); // 计算控制点(取中点) const cpx = (prevX + x) / 2; const cpy = (prevY + y) / 2; // 使用二次贝塞尔曲线 ctx.quadraticCurveTo(cpx, cpy, x, y); } // 封闭路径 ctx.lineTo(canvasSize, canvasSize); ctx.lineTo(0, canvasSize); ctx.closePath(); // 5. 填充波浪区域(粉色渐变) const gradient = ctx.createLinearGradient(0, this.waterLevel - 25, 0, canvasSize); gradient.addColorStop(0, '#3de1ad'); // 浅绿色 gradient.addColorStop(0.7, '#0eb83a'); // 绿色 gradient.addColorStop(1, '#0c8918'); // 深绿色 ctx.fillStyle = gradient; ctx.fill(); // 6. 绘制球体内部的渐变效果 ctx.beginPath(); ctx.arc(this.centerX, this.centerY, this.radius - 2, 0, Math.PI * 2); const ballGradient = ctx.createRadialGradient( this.centerX, this.centerY - 15, 10, this.centerX, this.centerY, this.radius ); ballGradient.addColorStop(0, 'rgba(224, 247, 250, 0.9)'); ballGradient.addColorStop(0.7, 'rgba(224, 247, 250, 0.5)'); ballGradient.addColorStop(1, 'rgba(224, 247, 250, 0.1)'); ctx.fillStyle = ballGradient; ctx.fill(); } build() { Column() { // 标题 Text('小型球体水波浪动画') .fontSize(20) .fontWeight(FontWeight.Bold) .margin({ bottom: 15 }) .fontColor('#333') // 球形容器 Stack() { // 绘制波浪球 Canvas(this.context) .width(200) .height(200) .onReady(() => { // 初始绘制 this.drawWave(this.context); // 设置动画循环 setInterval(() => { this.drawWave(this.context); }, 30); }) } .width(200) .height(200) .borderRadius(100) .margin({ bottom: 15 }) .backgroundColor('#387ec3') .clip(true) // 确保内容被裁剪为圆形 } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) .alignItems(HorizontalAlign.Center) .backgroundColor('#F5F5F5') .padding(20) } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/3 10:51:39

2D 肉鸽幸存者游戏- python Pygame

本项目为前几天收费帮学妹做的一个项目&#xff0c;在工作环境中基本使用不到&#xff0c;但是很多学校把这个当作编程入门的项目来做&#xff0c;故分享出本项目供初学者参考。 一、项目描述 2D 俯视角「肉鸽幸存者」:纯走位 自动战斗,一局一构筑,局外天赋成长。由 Python …

作者头像 李华
网站建设 2026/9/3 10:50:46

离子引擎、核动力火箭与曲率驱动:星际推进技术深度解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/3 10:44:54

如何把微信聊天记录导出成 Word 和 CSV:WeChatMsg 留痕实用指南

如何把微信聊天记录导出成 Word 和 CSV&#xff1a;WeChatMsg 留痕实用指南 【免费下载链接】WeChatMsg 提取微信聊天记录&#xff0c;将其导出成HTML、Word、CSV文档永久保存&#xff0c;对聊天记录进行分析生成年度聊天报告 项目地址: https://gitcode.com/GitHub_Trending…

作者头像 李华
网站建设 2026/9/3 10:44:45

Python+MySQL数据分析实战:泡泡玛特盲盒销售可视化项目详解

做数据分析练手项目&#xff0c;最怕两件事&#xff1a;第一是数据太“假”&#xff0c;洗来洗去没有业务感觉&#xff1b;第二是流程太散&#xff0c;爬虫是爬虫、可视化是可视化&#xff0c;最后简历上只能写“用过 Python”。这次我们看的这个项目&#xff0c;用 Python My…

作者头像 李华