Line

This demo presents the line 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 line demo

Full Code

 1'''
 22D line 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)
16
17    # ─── Definitions
18
19    # Number of lines
20    self.N = 20
21
22    # x-positions
23    self.X = np.linspace(0.1, 0.9, self.N)
24
25    # ─── Items
26
27    Y = self.generate(0)
28
29    for i in range(self.N):
30      self.item[f'line_{i}'] = anim.plane.line(
31        position = [self.X[i], 0.5-Y[i]/8],
32        dimension = [0,  Y[i]],
33        color = 'white'
34      )
35
36  # ────────────────────────────────────────────────────────────────────────
37  def generate(self, t):
38
39    return np.sin(self.X*10+ t/10)/4
40
41  # ────────────────────────────────────────────────────────────────────────
42  def update(self, t):
43
44    Y = self.generate(t.step)
45    for i in range(self.N):
46      self.item[f'line_{i}'].position = [self.X[i], 0.5-Y[i]/8]
47      self.item[f'line_{i}'].dimension = [0, Y[i]]
48    
49    # Confirm update
50    super().update(t)
51
52# ═══ Main ═════════════════════════════════════════════════════════════════
53
54W = anim.window('Line animation')
55
56# Add animation
57W.add(Canva)
58
59# Allow backward animation
60W.allow_backward = True
61W.allow_negative_time = True
62
63W.show()