Skip to content

Commit

Permalink
update fx.js
Browse files Browse the repository at this point in the history
add smoothTimer.js
  • Loading branch information
xiaomaitx committed Mar 11, 2023
1 parent 3ffa3d6 commit 5706ced
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 10 deletions.
28 changes: 18 additions & 10 deletions fx.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* fx.js
* @description A library for providing simple animations in ZeppOS. 一个用于在ZeppOS中提供简单动画的库
* @version 1.0.1
* @date 2023/03/07
* @version 1.0.2
* @date 2023/03/11
* @author CuberQAQ XiaomaiTX
* @license MIT
* https://github.com/XiaomaiTX/zppos-fx
Expand Down Expand Up @@ -30,7 +30,7 @@
* It also provides a @function getMixColor() designed for color gradients, which can get the middle color of two colors.
* 还提供了一个专为颜色渐变设计的函数getMixColor,可以获取两个颜色的中间色
*/

import { SmoothTimer, createSmoothTimer, stopSmoothTimer } from "./smoothTimer";

const bounceOut = function (x) {
/**
Expand Down Expand Up @@ -329,7 +329,7 @@ export class Fx {
}
}
registerTimer() {
this.timer = timer.createTimer(
this.timer = new createSmoothTimer(
this.delay ? this.delay : 0,
this.per_clock,
(option) => {
Expand All @@ -342,7 +342,7 @@ export class Fx {
this.onStop();
}
//停止timer
timer.stopTimer(this.timer);
stopSmoothTimer(this.timer);
this.timer = null;
this.enable = false;
}
Expand Down Expand Up @@ -570,14 +570,17 @@ const fx_inside = {
},
EASE_OUT_BACK: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1 + 1.70158 + 1 * Math.pow(x - 1, 3) + 1.70158 * Math.pow(x - 1, 2);
return (
1 + 1.70158 + 1 * Math.pow(x - 1, 3) + 1.70158 * Math.pow(x - 1, 2)
);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_BACK: function (now_x, begin, end, max_x) {
function math_func(x) {
return x < 0.5
? (Math.pow(2 * x, 2) * ((1.70158 * 1.525 + 1) * 2 * x - 1.70158 * 1.525)) /
? (Math.pow(2 * x, 2) *
((1.70158 * 1.525 + 1) * 2 * x - 1.70158 * 1.525)) /
2
: (Math.pow(2 * x - 2, 2) *
((1.70158 * 1.525 + 1) * (x * 2 - 2) + 1.70158 * 1.525) +
Expand All @@ -592,7 +595,8 @@ const fx_inside = {
? 0
: x === 1
? 1
: -Math.pow(2, 10 * x - 10) * sin(((x * 10 - 10.75) * (2 * Math.PI)) / 3);
: -Math.pow(2, 10 * x - 10) *
sin(((x * 10 - 10.75) * (2 * Math.PI)) / 3);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
Expand All @@ -613,8 +617,12 @@ const fx_inside = {
: x === 1
? 1
: x < 0.5
? -(Math.pow(2, 20 * x - 10) * sin(((20 * x - 11.125) * (2 * Math.PI)) / 4.5)) / 2
: (Math.pow(2, -20 * x + 10) * sin(((20 * x - 11.125) * (2 * Math.PI)) / 4.5)) /
? -(
Math.pow(2, 20 * x - 10) *
sin(((20 * x - 11.125) * (2 * Math.PI)) / 4.5)
) / 2
: (Math.pow(2, -20 * x + 10) *
sin(((20 * x - 11.125) * (2 * Math.PI)) / 4.5)) /
2 +
1;
}
Expand Down
92 changes: 92 additions & 0 deletions smoothTimer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// smoothTimer.js 稳定计时器 用于解决Zepp OS timer计时器不准的问题
// @author CuberQAQ
// @date 2022/12/24
// Open Source Lisence: MIT <https://opensource.org/licenses/mit-license.php>

const SMOOTH_TIMER_TEST_CIRCLE = 1
const SMOOTH_TIMER_SAFE_TIME = 30
const hmTime = hmSensor.createSensor(hmSensor.id.TIME)


export class SmoothTimer {
/**
* Create a smooth timer.
* 创建一个稳定的计时器
* @param {number} delay Time to start. 延迟执行的时间。
* @param {number} circle The Loop Circle(ms). 循环周期(单位:毫秒)。
* @param {(option: any) => void} func Function Callback. 回调函数。
* @param {*} option As the param when call the Callback Function. 回调函数的参数
* @param {SmoothTimer.modes} mode The mode of smoothTimer. 稳定计时器的模式 @see SmoothTimer.modes
* @returns {SmoothTimer} Smooth Timer Object. Will be used when delete timer. 稳定计时器实例,删除计时器时用到
* @author CuberQAQ
*/
constructor(delay, circle, func, option, mode) {
this._lastUtc_ = hmTime.utc + delay - circle
//if (frequency != undefined) { circle = Math.round(1000 / frequency) }
if (circle == undefined)
return null
this.mode = mode || SmoothTimer.modes.DYNAMIC_SMOOTH
this._circle_ = circle
this._hmTimer_ = timer.createTimer(
0,
SMOOTH_TIMER_TEST_CIRCLE,
param => {
// 检测是否到达指定时间
if (hmTime.utc - this._lastUtc_ >= circle) { // 到达并执行
if(this.mode == SmoothTimer.modes.MAX_LIMIT) {
this._lastUtc_ = hmTime.utc - 0.75
}
else if (this.mode == SmoothTimer.modes.DYNAMIC_SMOOTH) {
this._lastUtc_ += circle // 更新上一次执行的时间戳
}

func(param) // 执行
}
// // 检测是否到达指定时间
// while(hmTime.utc - this._lastUtc_ >= circle + SMOOTH_TIMER_SAFE_TIME) { // 到达并执行
// this._lastUtc_ += circle // 更新上一次执行的时间戳
// func(param) // 执行
// }
},
option
)
}
}

/**
* Smooth Timer Mode 稳定计时器运行模式
*
* DYNAMIC_SMOOTH: 动态稳定 Try to make total callback times smooth 尝试稳定总回调次数
*
* MAX_LIMIT: 限制速度 Can't run faster then setting 在限定范围内运行
*/
SmoothTimer.modes = {
DYNAMIC_SMOOTH: 1, // 动态稳定
MAX_LIMIT: 2, // 限制速度
}

/**
* Create a smooth timer.
* 创建一个稳定的计时器
* @param {number} delay Time to start. 延迟执行的时间。
* @param {number} circle The Loop Circle(ms). 循环周期(单位:毫秒)。
* @param {(option: any) => void} func Function Callback. 回调函数。
* @param {*} option As the param when call the Callback Function. 回调函数的参数
* @param {SmoothTimer.modes|undefined} mode The mode of smoothTimer. 稳定计时器的模式 @see SmoothTimer.modes
* @returns {SmoothTimer} Smooth Timer Object. Will be used when delete timer. 稳定计时器实例,删除计时器时用到
* @author CuberQAQ
*/
export function createSmoothTimer(delay, circle, func, option, mode) {
return new SmoothTimer(delay, circle, func, option, mode)
}

/**
* Delete a smooth timer.
* 删除已启用的稳定计时器
* @param {SmoothTimer} instance Smooth Timer Instance. 已启用的SmoothTimer实例
* @returns {boolean} If successfully stop. 是否成功删除
*/
export function stopSmoothTimer(instance) {
if (instance._hmTimer_) { timer.stopTimer(instance._hmTimer_); return true }
return false
}

0 comments on commit 5706ced

Please sign in to comment.