Event

This demo illustrates user events on 2D items. It combines a clickable group and a draggable group, then displays event feedback in real time in a central result panel (click values and drag positions).

2D event demo

Full Code

  1'''
  22D event 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    # ─── Click
 21
 22    self.item.group_click = anim.plane.group(
 23      position = [0.5, 0.7],
 24      clickable = True
 25    )
 26
 27    self.item.rect_click = anim.plane.rectangle(
 28      group = self.item.group_click,
 29      position = [0, 0],
 30      dimension = [0.25, 0.1]
 31    )
 32    
 33    self.item.text_click = anim.plane.text(
 34      group = self.item.group_click,
 35      position = [0, 0],
 36      string = 'Click me',
 37      color = 'black'
 38    )
 39
 40    # ─── Drag
 41
 42    self.item.group_drag = anim.plane.group(
 43      position = [0.5, 0.3],
 44      draggable = True
 45    )
 46
 47    self.item.rect_drag = anim.plane.rectangle(
 48      group = self.item.group_drag,
 49      position = [0, 0],
 50      dimension = [0.25, 0.1]
 51    )
 52    
 53    self.item.text_drag = anim.plane.text(
 54      group = self.item.group_drag,
 55      position = [0, 0],
 56      string = 'Drag me',
 57      color = 'black'
 58    )
 59
 60    # ─── Result
 61
 62    self.item.group_result = anim.plane.group(
 63      position = [0.5, 0.5]
 64    )
 65
 66    self.item.rect_result = anim.plane.rectangle(
 67      group = self.item.group_result,
 68      position = [0, 0],
 69      dimension = [0.5, 0.1],
 70      color = 'white'
 71    )
 72    
 73    self.item.text_result = anim.plane.text(
 74      group = self.item.group_result,
 75      position = [0, 0],
 76      string = '',
 77      fontsize = 0.04,
 78      color = 'black'
 79    )
 80
 81
 82  # ────────────────────────────────────────────────────────────────────────
 83  def event(self, item, desc):
 84
 85    if item is self.item.group_click.qitem and not isinstance(desc, str):
 86      self.item.text_result.string = str(desc)
 87
 88    elif desc == 'motion' and item is self.item.group_drag.qitem:
 89      p = self.item.group_drag.qitem.pos()
 90      self.item.text_result.string = f'position ({p.x():.02f},{p.y():.02f})'
 91    
 92# ═══ Main ═════════════════════════════════════════════════════════════════
 93
 94W = anim.window('Event animation')
 95
 96# Add animation
 97W.add(Canva)
 98
 99W.autoplay = False
100W.show()