缓动函数

参考文章

  1. https://aaron-bird.github.io/2019/03/30/缓动函数(easing%20function)/
  2. https://www.zhangxinxu.com/wordpress/2016/12/how-use-tween-js-animation-easing/

缓动函数

匀速运动

首先我们来认识一个缓动函数的核心。先以一个匀速运动的函数为例子

function fn(currentTime, startValue, changeValue, duration) {
   
   let x = currentTime / duration;
   return changeValue * x + startValue;
}

坐标轴

*对应到坐标轴上理解

  • currentTime:x轴上某点(记为点a),意思是开始运动后到当前的时间
  • duration:x轴长度,意思是运动的总时长
  • startValue:y轴起点,意思是起始位置
  • changeValue:y轴的长度,意思是总移动距离
  • 返回值是点a对应的y轴坐标,(当前运动时间,当前移动距离).
  • 斜率:运动速度

缓入 ease-in

特点是一开始斜率较小,以 为例
在这里插入图片描述

function easeInQuad(currentTime, startValue, changeValue, duration) {
   
  let x = currentTime / duration;
  return changeValue * x * x + startValue;
}

缓出 ease-out

特点是结束前斜率变小,较为平滑,以-x² + 2x为例
在这里插入图片描述

function easeOutQuad(currentTime, startValue, changeValue, duration) {
   
  let x = currentTime / duration;
  return -changeValue * x * (x- 2) + startValue;
}

缓进缓出 ease-in-out

在这里插入图片描述
与单纯ease-in/ease-out不同的是, ease-in-out需要两个阶段,因此把x归一化到[0,2]区间

easeInOutQuad(currentTime, startValue, changeValue, duration) {
   
  // 把currentTime映射到[0,2]的区间
  // [0,1]为ease-in, [1,2]为ease-out 【含义是ease-in/ease-out开始后经过的时间】
  let x = currentTime / duration / 2;
  
  // 是否过半
  if (currentTime < 1) 
  	// changeValue / 2 是因为ease-in和ease-out各占一半的移动距离
  	return changeValue / 2 * x * x + startValue;

  // 过半了 要变成ease-out
  // 由于[0,1]是ease-in状态,要获取时间在ease-out[1,2]上走过的时间
  x--;
  return -changeValue / 2 * (x * (x - 2) - 1) + startValue;
}

用缓动函数做一个抽奖活动


<!DOCTYPE html>
<html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值