Ellipse

This demo shows a dynamic grid of ellipses whose horizontal and vertical dimensions are continuously swapped over time. The class Canva subclasses anim.plane.canva and updates each ellipse in update using np.cos(t.step/20) so alternating cells animate in opposite deformation directions.

2D ellipse demo

Full Code

 1'''
 22D ellipse 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                     boundaries = [[0, 1],[0,1]],
17                     display_boundaries = True,    
18                     **kwargs)
19
20    # Number of ellipse per axis
21    self.a = 10
22
23    # Mesh size
24    self.b = 1/(self.a-1)
25
26    for i in range(self.a):
27      for j in range(self.a):
28        
29        self.item[f'ellipse_{i}_{j}'] = anim.plane.ellipse(
30          position = [i*self.b, j*self.b],
31          dimension = [0,0],
32          color = 'cyan' if (i+j)%2 else 'blue'
33        )
34    
35  # ────────────────────────────────────────────────────────────────────────
36  def update(self, t):
37
38    for i in range(self.a):
39      for j in range(self.a):
40
41        L = self.b*(1 + np.cos(t.step/20)/2)
42        self.item[f'ellipse_{i}_{j}'].dimension = [L, 2*self.b-L] if (i+j)%2 else [2*self.b-L, L]
43
44    # Confirm update
45    super().update(t)
46
47# ═══ Main ═════════════════════════════════════════════════════════════════
48
49W = anim.window('Ellipse animation')
50
51# Add animation
52W.add(Canva)
53
54# Allow backward animation
55W.allow_backward = True
56W.allow_negative_time = True
57
58W.show()