DisplayObject class mouse event binding interfaces

This page explains the DisplayObject class mouse event binding interfaces.

What interfaces are these?

Each DisplayObject instance has the mouse event binding interfaces, like the click, mouse over, mouse move.

These interfaces can bind the mouse event to a DisplayObject instance. So, for instance, you can assign any function to handle when a click of DisplayObject instance.

Basic usage

You can bind an event handler (callable) with each interface like the click, mouseover.

The following example binds the click event handler, and if you click the rectangle, the fill color is changed.

import apysc as ap


def on_click(e: ap.MouseEvent[ap.Rectangle], options: dict) -> None:
    """
    The handler that the rectangle calls when clicked.

    Parameters
    ----------
    e : MouseEvent
        Event instance.
    options : dict
        Optional arguments dictionary.
    """
    rectangle: ap.Rectangle = e.this
    rectangle.fill_color = ap.Color("#f0a")


ap.Stage(
    background_color=ap.Color("#333"),
    stage_width=150,
    stage_height=150,
    stage_elem_id="stage",
)

sprite: ap.Sprite = ap.Sprite()
sprite.graphics.begin_fill(color=ap.Color("#0af"))
rectangle: ap.Rectangle = sprite.graphics.draw_rect(x=50, y=50, width=50, height=50)
rectangle.click(on_click)

ap.save_overall_html(dest_dir_path="display_object_mouse_event_basic_usage/")

See also

For more details, please see the following pages: