本文目录导读:

- Python + Matplotlib(最常用)
- Python + Turtle(简单绘图)
- JavaScript + Canvas(网页动画)
- Bash + ASCII动画
- 动画效果示例
- 实用脚本生成GIF动画
- 使用建议
我来介绍几种用脚本生成简单动画的方法,从基础到进阶:
Python + Matplotlib(最常用)
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
# 创建动画
fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 100)
line, = ax.plot(x, np.sin(x))
def update(frame):
line.set_ydata(np.sin(x + frame/10))
return line,
# 创建动画
ani = FuncAnimation(fig, update, frames=100, interval=50)
plt.show()
# 保存为gif
ani.save('animation.gif', writer='pillow')
Python + Turtle(简单绘图)
import turtle
# 创建窗口
screen = turtle.Screen()
t = turtle.Turtle()
t.speed(0)
# 动画循环
for i in range(360):
t.forward(1)
t.right(1)
# 清除并重画
if i % 10 == 0:
turtle.clearscreen()
turtle.done()
JavaScript + Canvas(网页动画)
<!DOCTYPE html>
<html>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let x = 0;
let y = 200;
function draw() {
// 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制小球
ctx.beginPath();
ctx.arc(x, y, 20, 0, Math.PI * 2);
ctx.fillStyle = 'red';
ctx.fill();
// 更新位置
x = (x + 2) % canvas.width;
// 递归调用动画
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>
Bash + ASCII动画
#!/bin/bash
# ASCII旋转动画
frames=("|" "/" "-" "\\")
while true; do
for frame in "${frames[@]}"; do
echo -ne "\r$frame Loading..."
sleep 0.1
done
done
动画效果示例
正弦波动画
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1.5, 1.5)
x = np.linspace(0, 2*np.pi, 200)
lines = []
def animate(i):
# 清除旧线条
for line in lines:
line.remove()
lines.clear()
# 绘制新波形
y = np.sin(x + i/10)
line, = ax.plot(x, y, 'b-', lw=2)
lines.append(line)
return lines
ani = FuncAnimation(fig, animate, frames=100, interval=20)
plt.show()
3D旋转立方体
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
def animate(angle):
ax.clear()
# 创建立方体
vertices = np.array([[-1,-1,-1],[1,-1,-1],[1,1,-1],[-1,1,-1],
[-1,-1,1],[1,-1,1],[1,1,1],[-1,1,1]])
# 旋转矩阵
theta = np.radians(angle)
rotation = np.array([[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]])
rotated = np.dot(vertices, rotation.T)
# 绘制顶点
ax.scatter(rotated[:,0], rotated[:,1], rotated[:,2])
# 绘制边
edges = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),(0,4),(1,5),(2,6),(3,7)]
for e in edges:
ax.plot(rotated[[e[0],e[1]],0], rotated[[e[0],e[1]],1], rotated[[e[0],e[1]],2], 'b-')
ani = FuncAnimation(fig, animate, frames=360, interval=20)
plt.show()
实用脚本生成GIF动画
from PIL import Image, ImageDraw
import numpy as np
# 创建多帧图像
frames = []
for i in range(30):
# 创建新图像
img = Image.new('RGB', (200, 200), 'white')
draw = ImageDraw.Draw(img)
# 绘制移动的圆
x = i * 6
draw.ellipse([x, 100, x+20, 120], fill='red')
frames.append(np.array(img))
# 保存为GIF
frames[0].save('animation.gif',
save_all=True,
append_images=frames[1:],
duration=50,
loop=0)
使用建议
| 场景 | 推荐工具 | 优点 |
|---|---|---|
| 数学图表动画 | Matplotlib | 专业、易用 |
| 简单图形动画 | Turtle | 直观、适合教学 |
| 网页动画 | JavaScript+Canvas | 交互性强 |
| 终端动画 | Bash | 轻量、无需依赖 |
| 游戏动画 | Pygame | 功能强大 |
选择哪种方式主要取决于你的需求和使用场景,如果需要更具体的帮助或想要特定类型的动画,请告诉我!