Skip to content

Instantly share code, notes, and snippets.

@alisterburt
Created August 19, 2021 18:13
Show Gist options
  • Save alisterburt/8134f0cd08f9479bb7386aea247abd0b to your computer and use it in GitHub Desktop.
Save alisterburt/8134f0cd08f9479bb7386aea247abd0b to your computer and use it in GitHub Desktop.
2d interactivity with physics simulation in the background
from time import sleep
import napari
import numpy as np
from napari.qt.threading import thread_worker
viewer = napari.Viewer()
bbox_layer = viewer.add_points(
[[-1, -1], [-1, 1], [1, -1], [1, 1]], face_color='green')
points_layer = viewer.add_points([0, 0], face_color='magenta')
@thread_worker
def move_point_towards_center():
while True:
sleep(1 / 60)
current_position = np.array(points_layer.data, dtype=float)
points_layer.data = current_position - current_position / 10
return
worker = move_point_towards_center()
worker.start()
napari.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment