Image Array

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

Full Code

 1'''
 22D image array 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    # Number of channels (1 or 3)
16    self.nchannel = 1
17
18    # Number of pixels in the image
19    self.npix = 500
20
21    super().__init__(window, pixelperunit=self.npix)
22
23    self.item.img = anim.plane.image(
24      position = [0.5, 0.5],
25      dimension = [1, 1],
26      array = self.ripple(0),
27      # colormap = anim.colormap('gnuplot', range=[-1, 1])
28    )
29
30  # ────────────────────────────────────────────────────────────────────────
31  def ripple(self, t):
32
33    # Base field
34    x = np.linspace(-1, 1, self.npix)
35    X, Y = np.meshgrid(x, x)
36    base_field = (20 * X**2) + (20 * Y**2)
37
38    match self.nchannel:
39
40      case 1:
41        ''' Single channel '''
42
43        return np.sin(base_field - t/2/np.pi)
44
45      case 3:
46        ''' RGB channels '''
47
48        R = np.sin(base_field - t/2/np.pi)
49        G = np.sin(base_field - t/2/np.pi+np.pi/2)
50        B = np.sin(base_field - t/2/np.pi+np.pi)
51        
52        return np.concatenate((R[:,:,None], G[:,:,None], B[:,:,None]), axis=2)
53
54  # ────────────────────────────────────────────────────────────────────────
55  def update(self, t):
56
57    self.item.img.array = self.ripple(t.step)
58
59    # Confirm update
60    super().update(t)
61
62# ═══ Main ═════════════════════════════════════════════════════════════════
63
64W = anim.window('Image animation')
65
66# Add animation
67W.add(Canva)
68
69# Allow backward animation
70W.allow_backward = True
71W.allow_negative_time = True
72
73W.show()