Polygon¶
This demo presents the polygon 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.

Full Code¶
1'''
22D path 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 # ─── Items
18
19 self.item.P = anim.plane.polygon(
20 points = self.generate(0),
21 color = None,
22 stroke = 'pink',
23 thickness = 0.02
24 )
25
26 # ────────────────────────────────────────────────────────────────────────
27 def generate(self, t):
28
29 # Number of points
30 Z = 2 + t/20
31 N = int(Z)
32
33 P = []
34 for i in range(N):
35 P.append([np.cos(i*2*np.pi/Z)*0.8,
36 np.sin(i*2*np.pi/Z)*0.8])
37
38 return P
39
40 # ────────────────────────────────────────────────────────────────────────
41 def update(self, t):
42
43 self.item.P.points = self.generate(t.step)
44
45 # Confirm update
46 super().update(t)
47
48# ═══ Main ═════════════════════════════════════════════════════════════════
49
50W = anim.window('Simple animation')
51
52# Add animation
53W.add(Canva)
54
55# Allow backward animation
56W.allow_backward = True
57W.allow_negative_time = False
58
59W.show()