OMD Documentation

EventManager

The EventManager class is the central hub for all user interactions with the OMD canvas. It is responsible for listening to raw browser events, normalizing them into a consistent format, and delegating them to the appropriate handlers, such as the active tool (PencilTool, SelectTool, etc.) or the pointerEventHandler for panning and zooming.

Class Definition

export class EventManager {
    // ...
}

Constructor

new EventManager(canvas)

Creates a new EventManager instance.

Public Methods

initialize()

Binds all necessary event listeners to the canvas's SVG element and the document. This includes listeners for pointer events, keyboard events, and the mouse wheel. It also sets the touch-action: none; style on the SVG element to prevent default touch behaviors like scrolling.

getPointerInfo()

Retrieves the current state of the pointers.

destroy()

Removes all event listeners and cleans up the event manager. This should be called when the canvas is no longer in use.

Event Handling Workflow

The EventManager follows a clear workflow for handling events:

  1. Capture Raw Event: Listens for native browser events (pointerdown, keydown, etc.).
  2. Prevent Default: Calls event.preventDefault() for most events to stop the browser's default behavior (e.g., text selection, page scrolling).
  3. Normalize Event: Converts the raw event object into a standardized format. For pointer events, this includes converting screen coordinates to SVG coordinates and normalizing pressure values (see _normalizePointerEvent).
  4. Delegate to Handlers: Passes the normalized event to the relevant handlers:
    • pointerEventHandler: For core interactions like panning and zooming.
    • Active Tool: The currently selected tool (e.g., PencilTool) receives the event to perform its specific function (e.g., drawing a line).
  5. Emit Canvas Event: Emits a high-level event on the canvas instance (e.g., pointerDown, keyDown) that other parts of the application can listen to.

Pointer Event Handler

The EventManager uses an instance of pointerEventHandler to manage view transformations like panning and zooming, which are typically initiated with multi-touch gestures or mouse wheel interactions.

Private Methods (Internal Logic)

The following methods handle the internal logic of the EventManager. While you won't call them directly, understanding their role can be helpful for debugging or extending the library.

Emitted Events

The EventManager emits the following events on the OMDCanvas instance, allowing other modules to react to user input:

↑ Top