Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexlib/fc55fc5e18dec9a2b06623e12c76e096 to your computer and use it in GitHub Desktop.
Save alexlib/fc55fc5e18dec9a2b06623e12c76e096 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