Skip to content

Conversation

@brentyi
Copy link
Collaborator

@brentyi brentyi commented Jan 31, 2025

Screen.Recording.2025-01-31.at.12.26.34.AM.mov
import numpy as np
import viser

server = viser.ViserServer()

# Create an empty image + add it to the GUI.
image = np.zeros((150, 300, 3), dtype=np.uint8)
image_handle = server.gui.add_image(image)
remove_button = server.gui.add_button("Remove click callback forever")

# Draw red squares at clicked locations.
@image_handle.on_click
def _(_):
    x, y = image_handle.clicked_xy
    assert 0 <= x <= 1.0
    assert 0 <= y <= 1.0

    # Scale click to image coordinates.
    h, w, _ = image.shape
    x = int(x * w)
    y = int(y * h)

    # Update the image.
    image[y - 5 : y + 5, x - 5 : x + 5] = (255, 0, 0)
    image_handle.image = image

# Remove callback when button is clicked.
@remove_button.on_click
def _(_) -> None:
    image_handle.remove_click_callback()
    remove_button.label = "Click callback removed!"
    remove_button.color = "green"

server.sleep_forever()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants