Arrow¶
This demo presents the arrow 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 arrow demo
3'''
4
5import types
6import datetime
7import numpy as np
8import anim
9
10# ═══ 2D Animation canva ═══════════════════════════════════════════════════
11
12class Canva(anim.plane.canva):
13
14 # ────────────────────────────────────────────────────────────────────────
15 def __init__(self, window):
16
17 super().__init__(window, boundaries=[[-1,1], [-1,1]])
18
19 D = self.data()
20
21 # ─── Hours
22
23 self.item.hours = anim.plane.arrow(
24 points = D.hp,
25 string = D.h,
26 thickness = 0.02,
27 color = 'white',
28 text_location = 0.70,
29 )
30
31 # ─── Minutes
32
33 self.item.minutes = anim.plane.arrow(
34 points = D.mp,
35 string = D.m,
36 thickness = 0.015,
37 color = 'white',
38 text_location = 0.85,
39 )
40
41 # ─── Seconds
42
43 self.item.seconds = anim.plane.arrow(
44 points = D.sp,
45 string = D.s,
46 thickness = 0.01,
47 color = 'red',
48 head_shape = 'disk',
49 head_location = 0.75,
50 text_location = 0.95,
51 )
52
53 # ─── Decorations
54
55 self.item.center = anim.plane.circle(
56 radius = 0.03
57 )
58
59 r = 0.75
60 for i in range(12):
61 a = i*np.pi/6
62 self.item[f'marker_{i}'] = anim.plane.line(
63 position = [r*np.cos(a), r*np.sin(a)],
64 dimension = [0.2, 0],
65 orientation = a,
66 zvalue = -1
67 )
68
69 # ────────────────────────────────────────────────────────────────────────
70 def data(self):
71
72 D = types.SimpleNamespace()
73
74 now = datetime.datetime.now()
75 D.h = now.hour
76 D.m = now.minute
77 D.s = now.second
78
79 D.hp = [[0,0], [-0.5*np.sin(-D.h/6*np.pi), 0.5*np.cos(-D.h/6*np.pi)]]
80 D.mp = [[0,0], [-0.9*np.sin(-D.m/30*np.pi), 0.9*np.cos(-D.m/30*np.pi)]]
81 D.sp = [[0,0], [-0.9*np.sin(-D.s/30*np.pi), 0.9*np.cos(-D.s/30*np.pi)]]
82
83 return D
84
85 # ────────────────────────────────────────────────────────────────────────
86 def update(self, t):
87
88 D = self.data()
89
90 self.item.hours.string = D.h
91 self.item.hours.points = D.hp
92
93 self.item.minutes.string = D.m
94 self.item.minutes.points = D.mp
95
96 self.item.seconds.string = D.s
97 self.item.seconds.points = D.sp
98
99 # Confirm update
100 super().update(t)
101
102# ═══ Main ═════════════════════════════════════════════════════════════════
103
104W = anim.window('Arrow animation')
105
106# Add animation
107W.add(Canva)
108
109W.show()