源码
完整 Demo 已整理为可直接上传 GitHub 的项目,包含 index.html、24 帧 dog-sprite.svg 和 README.md。
最近看到一种挺有意思的前端交互:页面中放着一只小狗,当鼠标绕着它移动时,小狗的脑袋也会跟着鼠标方向旋转。
实现这种效果不一定需要 WebGL,也不需要实时做 3D 渲染。一个非常简单的方法,就是提前准备一张包含多个旋转状态的雪碧图(Sprite Sheet),然后根据鼠标的位置选择应该显示哪一帧。
整个问题可以抽象成一个很简单的数学模型:
$$
(x,y)
\rightarrow
\theta
\rightarrow
frame
\rightarrow
background-position
$$
也就是:
鼠标二维坐标 → 方向角 → 雪碧图帧编号 → CSS 背景位置。
1. 先建立坐标模型
假设小狗图片中心的位置为
$$
C=(x_c,y_c)
$$
鼠标当前位置为
$$
M=(x_m,y_m)
$$
那么从小狗中心指向鼠标的向量就是:
$$
\vec{r}
=
M-C
=
(x_m-x_c,\;y_m-y_c)
$$
记作:
$$
dx=x_m-x_c
$$
$$
dy=y_m-y_c
$$
在浏览器里,这几个值都可以直接获得。
1 2 3 4 5 6 7
| const rect = dog.getBoundingClientRect();
const centerX = rect.left + rect.width / 2; const centerY = rect.top + rect.height / 2;
const dx = event.clientX - centerX; const dy = event.clientY - centerY;
|
现在问题就从”鼠标在哪里”变成了:
鼠标相对于小狗中心处于什么方向?
2. 从二维坐标计算方向角
已知向量
$$
(dx,dy)
$$
就可以通过 atan2 得到方向角:
$$
\theta=\operatorname{atan2}(dy,dx)
$$
对应 JavaScript:
1
| let angle = Math.atan2(dy, dx);
|
为什么不用
$$
\arctan\frac{dy}{dx}
$$
而使用 atan2(dy, dx)?
因为只计算
$$
dy/dx
$$
无法唯一判断向量位于哪个象限,而 atan2 同时使用 dx 和 dy,可以得到完整的方向信息。
它返回:
$$
-\pi \leq \theta \leq \pi
$$
为了方便后面映射动画帧,可以统一转换到:
$$
0\leq\theta<2\pi
$$
1 2 3
| if (angle < 0) { angle += Math.PI * 2; }
|
于是鼠标绕小狗转一圈时:
$$
\theta:0\rightarrow2\pi
$$
也就是:
$$
0^\circ\rightarrow360^\circ
$$
这里还要注意浏览器坐标系与传统数学坐标系不同:浏览器的 Y 轴是向下增长的,因此视觉上的旋转方向与标准笛卡尔坐标系会有所不同。不过这不会影响整个映射模型。
3. 把连续角度映射成离散动画帧
假设设计师已经制作好了 24 帧小狗旋转动画。
整张雪碧图可以理解成:
1
| frame0 | frame1 | frame2 | ... | frame23
|
一圈对应:
$$
2\pi
$$
总帧数:
$$
N=24
$$
因此每一帧覆盖的角度范围约为:
$$
\Delta\theta
=
\frac{2\pi}{N}
$$
对于 24 帧:
$$
\Delta\theta
=
\frac{360^\circ}{24}
=
15^\circ
$$
于是当前方向对应的帧编号就是:
$$
frame
=
\left\lfloor
\frac{\theta}{2\pi}N
\right\rfloor
$$
代码非常简单:
1 2 3 4 5
| const FRAME_COUNT = 24;
const frame = Math.floor( angle / (Math.PI * 2) * FRAME_COUNT );
|
例如:
鼠标角度为 (0^\circ),得到 frame 0。
鼠标角度约为 (90^\circ),得到 frame 6。
鼠标角度约为 (180^\circ),得到 frame 12。
鼠标角度约为 (270^\circ),得到 frame 18。
所以这里真正发生的是一次连续量到离散量的量化:
$$
\theta
\in[0,2\pi)
$$
被离散成:
$$
frame
\in
\{0,1,\cdots,N-1\}
$$
4. 从帧编号定位雪碧图
假设每一帧宽度都是:
$$
W=140px
$$
如果想显示第 (n) 帧,只需要让背景图向左移动:
$$
x_{background}=-nW
$$
因此:
1 2 3 4
| const bgX = -frame * FRAME_WIDTH;
dog.style.backgroundPosition = `${bgX}px 0px`;
|
例如每帧宽 140 px:
1 2 3 4 5
| frame 0 → 0px frame 1 → -140px frame 2 → -280px frame 3 → -420px ...
|
而 DOM 元素本身始终只有一帧大小:
1 2 3 4 5 6 7
| .dog { width: 140px; height: 140px;
background-image: url("./dog-sprite.svg"); background-repeat: no-repeat; }
|
所以用户看到的始终只是整张雪碧图中的一个窗口。
这实际上就是一个非常简单的离散采样过程。
5. 完整核心代码
把前面的模型组合起来,核心逻辑其实只有几十行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| const FRAME_COUNT = 24; const FRAME_WIDTH = 140;
const dog = document.querySelector(".dog");
window.addEventListener("mousemove", (event) => { const rect = dog.getBoundingClientRect();
const centerX = rect.left + rect.width / 2; const centerY = rect.top + rect.height / 2;
const dx = event.clientX - centerX; const dy = event.clientY - centerY;
let angle = Math.atan2(dy, dx);
if (angle < 0) { angle += Math.PI * 2; }
const frame = Math.floor( angle / (Math.PI * 2) * FRAME_COUNT );
const bgX = -frame * FRAME_WIDTH;
dog.style.backgroundPosition = `${bgX}px 0px`; });
|
因此整个交互实际上没有真正意义上的”旋转图片”。
DOM 中的小狗一直没有发生:
真正改变的是:
1 2 3 4 5 6 7
| frame 3 ↓ frame 4 ↓ frame 5 ↓ frame 6
|
只是因为每一帧中的小狗姿态不同,人眼最终把这些离散图片理解成了一个连续旋转的头。
6. 为什么这种动画会显得很自然?
这里有一个比较有意思的地方。
我们实际上有两个空间。
第一个是连续的鼠标状态空间:
$$
(x,y)\in\mathbb{R}^2
$$
经过 atan2 以后变成:
$$
\theta\in[0,2\pi)
$$
第二个则是有限的动画状态空间:
$$
F=\{0,1,\cdots,N-1\}
$$
整个交互就是建立一个映射:
$$
f:[0,2\pi)\rightarrow F
$$
即:
$$
f(\theta)
=
\left\lfloor
\frac{\theta N}{2\pi}
\right\rfloor
$$
设计师负责定义每一个离散状态长什么样,程序负责根据用户输入选择正确的状态。
因此这种方法特别适合复杂插画、角色动画、产品模型、吉祥物等场景。
很多视觉上看起来很复杂的 Motion,背后的运行时逻辑反而非常简单。
7. 更丝滑的版本:加入时间响应
前面的模型还有一个特点:
鼠标移动到哪里,小狗立即切换到对应帧。
数学上相当于:
$$
\theta_{display}
=
\theta_{mouse}
$$
这是一种瞬时响应。
但很多精致的 Motion Design 会故意增加一点惯性,例如:
$$
\theta_{display}(t+\Delta t)
=
\theta_{display}(t)
+
\alpha
[
\theta_{target}
-
\theta_{display}(t)
]
$$
其中:
$$
0<\alpha<1
$$
例如:
1 2
| currentAngle += (targetAngle - currentAngle) * 0.1;
|
然后使用 requestAnimationFrame 持续更新:
1 2 3 4 5 6 7 8
| function animate() { currentAngle += (targetAngle - currentAngle) * 0.1;
render(currentAngle);
requestAnimationFrame(animate); }
|
此时系统就从:
$$
Mouse
\rightarrow
Frame
$$
变成:
$$
Mouse
\rightarrow
TargetAngle
\rightarrow
DynamicResponse
\rightarrow
Frame
$$
鼠标突然移动时,角色不会瞬间跳过去,而是稍微滞后地跟随过去。
很多所谓”丝滑””有生命感”的 UI Motion,本质上就是在前面的几何映射外面再增加一个简单的时间响应模型。