Path¶
This demo illustrates how to animate a polyline by updating its point list at each frame. The Canva class subclasses anim.plane.canva, builds an anim.plane.path item, and recomputes its points with a sinusoidal profile in update, which creates a smooth waveform 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, **kwargs):
14
15 super().__init__(window)
16
17 # ─── Definitions
18
19 # Number of points
20 self.N = 30
21
22 # ─── Items
23
24 self.item.P = anim.plane.path(
25 position = [0.5, 0.5],
26 points = self.generate(0),
27 color = 'lemonchiffon',
28 stroke = 'orange'
29 )
30
31 # ────────────────────────────────────────────────────────────────────────
32 def generate(self, t):
33
34 P = []
35 for x in range(self.N):
36 P.append([0.4*(2*x/(self.N-1)-1),
37 np.sin(x*20/self.N + t/20)/5])
38
39 return P
40
41 # ────────────────────────────────────────────────────────────────────────
42 def update(self, t):
43
44 self.item.P.points = self.generate(t.step)
45
46 # Confirm update
47 super().update(t)
48
49# ═══ Main ═════════════════════════════════════════════════════════════════
50
51W = anim.window('Simple animation')
52
53# Add animation
54W.add(Canva)
55
56# Allow backward animation
57W.allow_backward = True
58W.allow_negative_time = True
59
60W.show()