2D Grid

This demo presents the grid scene in 2D and shows how the animation state evolves over time with lib-anim. The script defines a canva, adds the corresponding items, and updates them frame by frame to produce the final motion.

2D grid demo

Full Code

 1'''
 22D grid demo
 3'''
 4
 5import numpy as np
 6import anim
 7
 8# ═══ 2D Animation canva ═══════════════════════════════════════════════════
 9
10class Canva(anim.plane.canva):
11
12  # ────────────────────────────────────────────────────────────────────────
13  def __init__(self, window):
14
15    super().__init__(window, boundaries=[[-1,1], [-1,1]])
16
17    self.pos = np.zeros((100, 2))
18
19    # ─── Grid
20
21    self.grid = anim.plane.grid(spacing = 0.4)
22
23    # ─── Dot
24
25    self.item.dot = anim.plane.circle(
26      position = [0,0],
27      radius = 0.02,
28      color = 'red'
29    )
30
31    # ─── Tail
32
33    self.item.tail = anim.plane.path(
34      position = [0,0],
35      points = self.pos,
36      stroke = 'red'
37    )
38
39  # ────────────────────────────────────────────────────────────────────────
40  def update(self, t):
41
42    # ─── Update dot position
43
44    new_pos = np.array(self.pos[-1]) + np.random.randn(2)/50
45    self.pos = np.roll(self.pos, -1, axis=0)
46    self.pos[-1] = new_pos
47
48    self.item.dot.position = self.pos[-1,:]
49    self.item.tail.points = self.pos
50
51    # ─── Update grid
52
53    self.grid.shift = np.mean(self.pos, axis=0)
54
55    # Confirm update
56    super().update(t)
57
58# ═══ Main ═════════════════════════════════════════════════════════════════
59
60W = anim.window('Grid animation')
61
62# Add animation
63W.add(Canva)
64
65W.show()