Skip to content

Add hitTest() wrappers for common DOM events #6

@RyanMullins

Description

@RyanMullins

MegaPlot provides a basic Scene.hitTest(x, y) API that returns the full list of Sprites at that location. This requires API consumers to write essentially the same boilerplate code, shown below and also in the debugging demo:

let hitTestTask;
const {left, top} = container.getBoundingClientRect();

container.addEventListener('someMouseEvent', async (event: MouseEvent) => {
  if (hitTestTask) hitTestTask.cancel();

  const {x, y} = event
  hitTestTask = scene.hitTest((x - left), (y - top));
  const result: HitTestResult = await hitTestTask;

  // Do something with the results...
});

A common use case for interactive visualization is to get the top-most Sprite in response to a click or mousemove event and update that Sprite's state to indicate that it is selected of hovered. This could be done directly with Sprite.update() as in the debugging demo, or via a more circuitous path in a coordinated, linked visualization context, e.g., through LIT's centralized Selection and Focus services.

MegaPlot could add convenience APIs for .click(), .mousemove(), etc. that only focus on the single-sprite use case, attaching the event listener to the <canvas> element it injects into the DOM. This would allow consumers to define more complex behavior via the .hitTest() API is they care about something other than the top-most Sprite at the location.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions