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

【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(七) -> JS动画(二)

目录

1 -> 动画动效

1.1 -> 创建动画对象

1.2 -> 添加动画事件和调用接口

2 -> 动画帧

2.1 -> 请求动画帧

2.2 -> 取消动画帧


1 -> 动画动效

通过设置插值器来实现动画效果。

说明

从API Version 6 开始支持。

1.1 -> 创建动画对象

通过createAnimator创建一个动画对象,通过设置参数options来设置动画的属性。

<!-- test.hml -->
<div class="container"><div style="width: 300px;height: 300px;margin-top: 100px;background: linear-gradient(pink, purple);transform: translate({{translateVal}});"></div><div class="row"><button type="capsule" value="play" onclick="playAnimation"></button></div>
</div>
/* test.css */
.container {width:100%;height:100%;flex-direction: column;align-items: center;justify-content: center;
}
button{width: 200px;
}
.row{width: 65%;height: 100px;align-items: center;justify-content: space-between;margin-top: 50px;margin-left: 260px;
}
/* test.js */
import animator from '@ohos.animator';
export default {data: {translateVal: 0,animation: null},onInit() {},onShow(){var options = {duration: 3000,easing:"friction",delay:"1000",fill: 'forwards',direction:'alternate',iterations: 2,begin: 0,end: 180};//设置参数this.animation = animator.createAnimator(options)//创建动画},playAnimation() {var _this = this;this.animation.onframe = function(value) {_this.translateVal= value};this.animation.play();}
}

说明

  • 使用createAnimator创建动画对象时必须传入options参数。

  • begin插值起点,不设置时默认为0。

  • end插值终点,不设置时默认为1。

1.2 -> 添加动画事件和调用接口

animator支持事件和接口,可以通过添加frame、cancel、repeat、finish事件和调用update、play、pause、cancel、reverse、finish接口自定义动画效果。animator支持的事件和接口具体见动画中的createAnimator。

<!-- test.hml -->
<div style="flex-direction: column;align-items: center;width: 100%;height: 100%;"><div style="width:200px;height: 200px;margin-top: 100px;background: linear-gradient(#b30d29, #dcac1b);transform: scale({{scaleVal}});"></div><div style="width: {{DivWidth}};height: {{DivHeight}};margin-top: 200px;background: linear-gradient(pink, purple);margin-top: 200px;transform:translateY({{translateVal}});"></div><div class="row"><button type="capsule" value="play" onclick="playAnimation"></button><button type="capsule" value="update" onclick="updateAnimation"></button></div><div class="row1"><button type="capsule" value="pause" onclick="pauseAnimation"></button><button type="capsule" value="finish" onclick="finishAnimation"></button></div><div class="row2"><button type="capsule" value="cancel" onclick="cancelAnimation"></button><button type="capsule" value="reverse" onclick="reverseAnimation"></button></div>
</div>
/* test.css */
button{width: 200px;
}
.row{width: 65%;height: 100px;align-items: center;justify-content: space-between;margin-top: 150px;position: fixed;top: 52%;left: 120px;
}
.row1{width: 65%;height: 100px;align-items: center;justify-content: space-between;margin-top: 120px;position: fixed;top: 65%;left: 120px;
}
.row2{width: 65%;height: 100px;align-items: center;justify-content: space-between;margin-top: 100px;position: fixed;top: 75%;left: 120px;
}
/* test.js */
import animator from '@ohos.animator';
import prompt from '@system.prompt';
export default {data: {scaleVal:1,DivWidth:200,DivHeight:200,translateVal:0,animation: null},onInit() {var options = {duration: 3000,fill: 'forwards',begin: 200,end: 270};this.animation = animator.createAnimator(options);},onShow() {var _this= this;//添加动画重放事件this.animation.onrepeat = function() {prompt.showToast({message: 'repeat'});var repeatoptions = {duration: 2000,iterations: 1,direction: 'alternate',begin: 180,end: 240};_this.animation.update(repeatoptions);_this.animation.play();};},playAnimation() {var _this= this;//添加动画逐帧插值回调事件this.animation.onframe = function(value) {_this. scaleVal= value/150,_this.DivWidth = value,_this.DivHeight = value,_this.translateVal = value-180};this.animation.play();},updateAnimation() {var newoptions = {duration: 5000,iterations: 2,begin: 120,end: 180};this.animation.update(newoptions);this.animation.play();//调用动画播放接口},pauseAnimation() {this.animation.pause();//调用动画暂停接口},finishAnimation() {var _this= this;//添加动画完成事件this.animation.onfinish = function() {prompt.showToast({message: 'finish'})};this.animation.finish(); //调用动画完成接口},cancelAnimation() {this.animation.cancel(); //调用动画取消接口},reverseAnimation() {this.animation.reverse(); //调用动画倒放接口}
}

说明

在调用update接口的过程中可以使用这个接口更新动画参数,入参与createAnimator一致。

2 -> 动画帧

2.1 -> 请求动画帧

请求动画帧时通过requestAnimationFrame函数逐帧回调,在调用该函数时传入一个回调函数。

runframe在调用requestAnimationFrame时传入带有timestamp参数的回调函数step,将step中的timestamp赋予起始的startTime。当timestamp与startTime的差值小于规定的时间时将再次调用requestAnimationFrame,最终动画将会停止。

<!-- test.hml -->
<div class="container"><tabs onchange="changecontent"><tab-content><div class="container"><stack style="width: 300px;height: 300px;margin-top: 100px;margin-bottom: 100px;"><canvas id="mycanvas" style="width: 100%;height: 100%;background-color: coral;"></canvas><div style="width: 50px;height: 50px;border-radius: 25px;background-color: indigo;position: absolute;left: {{left}};top: {{top}};"></div></stack><button type="capsule" value="play" onclick="runframe"></button></div></tab-content></tabs>
</div>
/* test.css */
.container {flex-direction: column;justify-content: center;align-items: center;width: 100%;height: 100%;
}
button{width: 300px;
}
/* test.js */
export default {data: {timer: null,left: 0,top: 0,flag: true,animation: null,startTime: 0,},onShow() {var test = this.$element("mycanvas");var ctx = test.getContext("2d");ctx.beginPath();ctx.moveTo(0, 0);ctx.lineTo(300, 300);ctx.lineWidth = 5;ctx.strokeStyle = "red";ctx.stroke();},runframe() {this.left = 0;this.top = 0;this.flag = true;this.animation = requestAnimationFrame(this.step);},step(timestamp) {if (this.flag) {this.left += 5;this.top += 5;if (this.startTime == 0) {this.startTime = timestamp;}var elapsed = timestamp - this.startTime;if (elapsed < 500) {console.log('callback step timestamp: ' + timestamp);this.animation = requestAnimationFrame(this.step);}} else {this.left -= 5;this.top -= 5;this.animation = requestAnimationFrame(this.step);}if (this.left == 250 || this.left == 0) {this.flag = !this.flag}},onDestroy() {cancelAnimationFrame(this.animation);}
}

说明

requestAnimationFrame函数在调用回调函数时在第一个参数位置传入timestamp时间戳,表示requestAnimationFrame开始去执行回调函数的时刻。

2.2 -> 取消动画帧

通过cancelAnimationFrame函数取消逐帧回调,在调用cancelAnimationFrame函数时取消requestAnimationFrame函数的请求。

<!-- test.hml -->
<div class="container"><tabs onchange="changecontent"><tab-content><div class="container"><stack style="width: 300px;height: 300px;margin-top: 100px;margin-bottom: 100px;"><canvas id="mycanvas" style="width: 100%;height: 100%;background-color: coral;"></canvas><div style="width: 50px;height: 50px;border-radius: 25px;background-color: indigo;position: absolute;left: {{left}};top: {{top}};"></div></stack><button type="capsule" value="play" onclick="runframe"></button></div></tab-content></tabs>
</div>
/* test.css */
.container {flex-direction: column;justify-content: center;align-items: center;width: 100%;height: 100%;
}
button{width: 300px;
}
/* test.js */
export default {data: {timer: null,left: 0,top: 0,flag: true,animation: null},onShow() {var test = this.$element("mycanvas");var ctx = test.getContext("2d");ctx.beginPath();ctx.moveTo(0, 0);ctx.lineTo(300, 300);ctx.lineWidth = 5;ctx.strokeStyle = "red";ctx.stroke();},runframe() {this.left = 0;this.top = 0;this.flag = true;this.animation = requestAnimationFrame(this.step);},step(timestamp) {if (this.flag) {this.left += 5;this.top += 5;this.animation = requestAnimationFrame(this.step);} else {this.left -= 5;this.top -= 5;this.animation = requestAnimationFrame(this.step);}if (this.left == 250 || this.left == 0) {this.flag = !this.flag}},onDestroy() {cancelAnimationFrame(this.animation);}
}

说明

在调用该函数时需传入一个具有标识id的参数。


感谢各位大佬支持!!!

互三啦!!!

相关文章:

【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(七) -> JS动画(二)

目录 1 -> 动画动效 1.1 -> 创建动画对象 1.2 -> 添加动画事件和调用接口 2 -> 动画帧 2.1 -> 请求动画帧 2.2 -> 取消动画帧 1 -> 动画动效 通过设置插值器来实现动画效果。 说明 从API Version 6 开始支持。 1.1 -> 创建动画对象 通过cre…...

SpaCy处理NLP的详细工作原理及工作原理框图

spaCy处理NLP的详细工作原理及工作原理框图 spaCy处理NLP的详细工作原理 spaCy是一个基于Python的开源自然语言处理&#xff08;NLP&#xff09;库&#xff0c;它提供了一系列高效且易用的工具&#xff0c;用于执行各种NLP任务&#xff0c;如文本预处理、文本解析、命名实体识…...

Mysql中的常用函数

1、datediff(date1,date2) date1减去date2&#xff0c;返回两个日期之间的天数。 SELECT DATEDIFF(2008-11-30,2008-11-29) AS DiffDate -- 返回1 SELECT DATEDIFF(2008-11-29,2008-11-30) AS DiffDate -- 返回-1 2、char_length(s) 返回字符串 s 的字符数 3、round(x,d)…...

Linux下find命令的使用方法详解

文章目录 **一、基本语法****二、常用搜索条件****1. 按名称搜索****2. 按类型搜索****3. 按时间搜索****4. 按大小搜索****5. 按权限/所有者搜索** **三、组合条件&#xff08;逻辑运算符&#xff09;****四、执行操作****1. 直接输出&#xff08;默认&#xff09;****2. 删除…...

Day(19)--IO流(三)

文件加密 ps:^异或: 两边相同就是false 两边不同就是true 如果比较的是数字,那就会把它转换成为二进制,从右自左依次比较 总结:如果一个数字被异或两次,结果还是原来的数字 缓冲流 字节缓冲流 BufferedInputStream------字节缓冲输入流 BufferedOutputStream----字节…...

数据类型——long long

在C语言中&#xff0c;long long 类型是一种有符号的64位整数&#xff0c;其取值范围由二进制补码表示法决定。以下是具体数值及解释&#xff1a; 1. long long 的最大值 最大值&#xff08;正数&#xff09;&#xff1a;9223372036854775807 计算方式&#xff1a;2^63 - 1 这是…...

网络安全通信架构图

&#x1f345; 点击文末小卡片 &#xff0c;免费获取网络安全全套资料&#xff0c;资料在手&#xff0c;涨薪更快 在安全通信里面我经常听到的2个东西就是SSL和TLS&#xff0c;这2个有什么区别呢&#xff1f;以及HTTPS是怎么通信的&#xff1f;包括对称加密、非对称加密、摘要、…...

AMD(xilinx) FPGA书籍推荐

理论到实践&#xff0c;五年磨一剑 以应用为主&#xff0c;书中全部例程均来自工程实践&#xff1b;目的在于培养FPGA工程师良好的代码编写习惯&#xff0c;掌握vivado常用高级技巧。本书详细讲解了&#xff1a; &#xff08;0&#xff09;vivado操作基础从工程建立到bit/mcs文…...

考前冲刺,消防设施操作员考试最后一击

考前冲刺&#xff0c;消防设施操作员考试最后一击 考前冲刺阶段至关重要。首先要回归教材&#xff0c;快速浏览重点知识点&#xff0c;强化记忆。同时&#xff0c;对之前做过的错题进行集中复习&#xff0c;分析错误原因&#xff0c;避免在考试中再次犯错。进行全真模拟考试&a…...

【GoTeams】-3:构建api、重构错误码

本文目录 1. 构建api梳理调用关系api包的作用路由梳理注册Register代码语法 2. 重构错误码 1. 构建api 首先复制project-user&#xff0c;改名为project-api&#xff0c;放在总的路径下&#xff0c;然后在工作区中进行导入。 运行命令go work use .\project-api\新建工作区之…...

MuBlE:为机器人操作任务规划提供了逼真的视觉观察和精确的物理建模

2025-03-05&#xff0c;由华为诺亚方舟实验室、捷克技术大学和帝国理工学院联合开发的MuBlE&#xff08;MuJoCo and Blender simulation Environment&#xff09;模拟环境和基准测试。通过结合MuJoCo物理引擎和Blender高质量渲染&#xff0c;为机器人操作任务规划提供了逼真的视…...

基于STC89C52的4x4矩阵键盘对应键值显示测试

引言 在众多单片机应用系统中,用户输入功能至关重要。4x4 矩阵键盘因其布局紧凑、按键数量适中,能有效节省 I/O 口资源,成为常用的输入设备。STC89C52 作为一款经典的 8 位单片机,以其丰富的外设资源和简易的开发流程,为矩阵键盘的应用提供了良好平台。同时,LCD1602 作为…...

android13打基础: timepicker控件

public class Ch4_TimePickerActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {private TextView tv_time; // 声明一个文本视图对象private TimePicker tp_time; // 声明一个时间选择器对象Overrideprotected void onCreate(Nullable Bund…...

【虚拟仿真】Unity3D中实现激光/射线的发射/折射/反射的效果(3D版)

推荐阅读 CSDN主页GitHub开源地址Unity3D插件分享QQ群:398291828小红书小破站大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。...

【预测】-双注意LSTM自动编码器记录

预测-双注意LSTM自动编码器 1 预测-双注意LSTM自动编码器1.1 复现环境配置1.2 数据流记录1.2.1 **构建Dataset**(1) **X 的取数**(2) **y 的取数**(3) **target 的取数** 1.2.2 **举例说明**(1)**X 的取数**(2)**y 的取数**(3)**target 的取数** 1.2.3 **y 取数的问题****修正后…...

S32K3 MCU时钟部分

S32K3 MCU时钟部分 1.系统时钟发生器SCG 系统时钟发生器SCG模块提供MCU的系统时钟,SCG包含一个系统锁相环SPLL,一个慢速的内部参考时钟SIRC,一个快速内部参考时钟FIRC和系统振荡时钟SOSC. 时钟生成的电路提供了多个时钟分频器和选择器允许为不同的模块提供以特定于该模块的频率…...

java开发常用注解

在Java开发中&#xff0c;注解&#xff08;Annotation&#xff09;广泛用于简化代码、配置元数据、框架集成等场景。以下是不同场景下常用的注解分类整理&#xff1a; 一、核心Java注解&#xff08;内置&#xff09; Override 表示方法重写父类或接口的方法&#xff0c;编译器会…...

Doris vs ClickHouse 企业级实时分析引擎怎么选?

Apache Doris 与 ClickHouse 同作为OLAP领域的佼佼者&#xff0c;在企业级实时分析引擎该如何选择呢。本文将详细介绍 Doris 的优势&#xff0c;并通过直观对比展示两者的关键差异&#xff0c;同时分享一个企业成功用 Doris 替换 ClickHouse 的实践案例&#xff0c;帮助您做出明…...

解锁Egg.js:从Node.js小白到Web开发高手的进阶之路

一、Egg.js 是什么 在当今的 Web 开发领域&#xff0c;Node.js 凭借其事件驱动、非阻塞 I/O 的模型&#xff0c;在构建高性能、可扩展的网络应用方面展现出独特的优势 &#xff0c;受到了广大开发者的青睐。它让 JavaScript 不仅局限于前端&#xff0c;还能在服务器端大展身手&…...

学习前端前需要了解的一些概念(详细版)

网站的定义与概述 网站&#xff08;Website&#xff09;是一个由网络服务器托管的、通过网络访问的、由相关网页和资源组成的集合。它为用户提供信息、服务或娱乐平台&#xff0c;是现代互联网的重要组成部分。网站的基本功能是展示信息和提供服务&#xff0c;用户可以通过浏览…...

NPU vs GPU:为什么你的AI项目需要专用神经网络处理器?

NPU vs GPU&#xff1a;为什么你的AI项目需要专用神经网络处理器&#xff1f; 当你在深夜调试一个实时人脸识别模型时&#xff0c;GPU风扇的轰鸣声是否让你担心电费账单&#xff1f;当部署在边缘设备的图像分类服务因为响应延迟被客户投诉时&#xff0c;是否考虑过硬件选型可能…...

STM32智能婴儿床系统设计与实现

基于STM32的智能婴儿床系统设计1. 项目概述1.1 系统架构本智能婴儿床系统采用模块化设计架构&#xff0c;以STM32F103RCT6微控制器为核心处理单元&#xff0c;集成多种传感器模块和执行机构。系统通过蓝牙与手机APP建立双向通信&#xff0c;实现环境参数监测、异常报警和远程控…...

从苹果AirTag到国产车钥匙:拆解UWB芯片厂商格局与选型指南(附功耗实测参考)

从苹果AirTag到国产车钥匙&#xff1a;拆解UWB芯片厂商格局与选型指南 当你的手机靠近车门自动解锁&#xff0c;或是通过AirTag精准定位背包位置时&#xff0c;背后都离不开一项关键技术——UWB&#xff08;超宽带&#xff09;。这种厘米级精度的空间感知能力&#xff0c;正在重…...

Docker Compose 多服务编排实战:从零搭建微服务架构

Docker Compose 多服务编排实战&#xff1a;从零搭建微服务架构 目录 为什么需要 Docker Compose&#xff1f;实战项目架构环境准备核心服务搭建高级特性&#xff1a;负载均衡与服务发现日志集中管理&#xff08;EFK 栈&#xff09;生产环境最佳实践常见问题排查 为什么需要 …...

Anaconda Prompt卡在solving environment?别慌,三步搞定清华镜像源配置(附.condarc文件)

Anaconda环境配置卡顿&#xff1f;清华镜像源优化全指南 刚接触Python数据科学的新手们&#xff0c;十有八九会在Anaconda环境配置这一步栽跟头。特别是当看到命令行窗口里"solving environment"的提示一直转圈却迟迟没有进展时&#xff0c;那种等待的煎熬简直让人抓…...

给嵌入式新手的Cortex-M0内核超详细图解:从寄存器到中断,一篇搞定STM32/GD32入门

给嵌入式新手的Cortex-M0内核超详细图解&#xff1a;从寄存器到中断&#xff0c;一篇搞定STM32/GD32入门 刚拿到STM32开发板时&#xff0c;看着密密麻麻的引脚和上百页的芯片手册&#xff0c;我完全不知道从哪里开始。直到导师指着原理图说&#xff1a;"把芯片想象成一个忙…...

OpCore Simplify:零基础黑苹果配置的终极自动化解决方案

OpCore Simplify&#xff1a;零基础黑苹果配置的终极自动化解决方案 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为复杂的OpenCore EFI配置而烦…...

从YOLOv5到YOLOv8:停车位检测模型演进与实战性能对比

1. YOLO系列模型的技术演进路径 YOLO&#xff08;You Only Look Once&#xff09;系列模型作为目标检测领域的标杆算法&#xff0c;从2015年诞生至今已经经历了多次重大迭代。每次版本更新都带来了显著的性能提升和架构创新&#xff0c;这使得YOLO系列在实时目标检测任务中始终…...

什么是分段锁

面试 线程只锁自己要用的那一段代码&#xff0c;不同段可以同时操作。这样可以减少锁竞争、提高并发。...

如何快速打造微信风格视频编辑功能?推荐开源神器WeiXinRecordedDemo

如何快速打造微信风格视频编辑功能&#xff1f;推荐开源神器WeiXinRecordedDemo 【免费下载链接】WeiXinRecordedDemo 仿微信视频拍摄UI, 基于ffmpeg的视频录制编辑 项目地址: https://gitcode.com/gh_mirrors/we/WeiXinRecordedDemo WeiXinRecordedDemo是一款基于FFmpe…...