Text

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

Full Code

 1'''
 22D text 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    self.item.time_step = anim.plane.text(
18      x = 0.5,
19      y = 0.7,
20      fontsize = 0.2,
21      string = 't = 0.00 s',
22      color = 'darkblue',
23      style = 'html { background-color: lightblue; }',
24    )
25
26    self.item.time_second = anim.plane.text(
27      x = 0.5,
28      y = 0.7,
29      fontsize = 0.1,
30      string = 't = 0',
31      color = 'orange'
32    )
33
34  # ────────────────────────────────────────────────────────────────────────
35  def update(self, t):
36
37    # String
38    self.item.time_second.string = f't = {t.time:.02f} s'
39    self.item.time_step.string = f't = {t.step}'
40
41    # Motion
42    self.item.time_second.x = np.sin(-t.step/33)*0.3  + 0.5
43    self.item.time_second.y = np.cos(-t.step/20)*0.2  + 0.5
44    self.item.time_second.orientation = -t.step/50
45
46    # Motion
47    self.item.time_step.x = np.sin(t.step/20)*0.3  + 0.5
48    self.item.time_step.y = np.cos(t.step/33)*0.2  + 0.5
49    self.item.time_step.orientation = t.step/50
50
51    # Confirm update
52    super().update(t)
53
54# ═══ Main ═════════════════════════════════════════════════════════════════
55
56W = anim.window('2D text demo')
57
58# Add animation
59W.add(Canva)
60
61# Allow backward animation
62W.allow_backward = True
63W.allow_negative_time = True
64
65# Display
66W.show()