Skip to content

Event Handling

It provides a complete event handling mechanism similar to HTML, supporting capture/bubble phases, and also supports simulated interaction.

Listening to Events

Add mouse enter/leave event effects to a rectangle:

ts
// #监听事件 [通过 on 方法监听]
import { Leafer, Rect, PointerEvent } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    fill: '#32cd79', // 背景色
    stroke: 'black', // 边框
    strokeWidth: 2, // 边框粗细
    draggable: true
})

leafer.add(rect)


function onEnter(e: PointerEvent) {
    (e.current as Rect).fill = '#42dd89'
}

function onLeave(e: PointerEvent) {
    (e.current as Rect).fill = '#32cd79'
}

rect.on(PointerEvent.ENTER, onEnter)
rect.on(PointerEvent.LEAVE, onLeave)
js
// #监听事件 [通过 on 方法监听]
import { Leafer, Rect, PointerEvent } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    fill: '#32cd79', // 背景色
    stroke: 'black', // 边框
    strokeWidth: 2, // 边框粗细
    draggable: true
})

leafer.add(rect)


function onEnter(e) {
    e.current.fill = '#42dd89'
}

function onLeave(e) {
    e.current.fill = '#32cd79'
}

rect.on(PointerEvent.ENTER, onEnter)
rect.on(PointerEvent.LEAVE, onLeave)

Initializing Events

Supports passing an event object during initialization (cannot be exported as JSON), for quickly binding events:

ts
// #监听事件 [通过 event 对象监听]
import { Leafer, Rect, PointerEvent } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = Rect.one({
    fill: '#32cd79',
    draggable: true,
    event: {
        [PointerEvent.ENTER]: function (e: PointerEvent) {
            (e.current as Rect).fill = '#42dd89'
        },
        [PointerEvent.LEAVE]: function (e: PointerEvent) {
            (e.current as Rect).fill = '#32cd79'
        }
    }
}, 100, 100, 200, 200)

leafer.add(rect)
js
// #监听事件 [通过 event 对象监听]
import { Leafer, Rect, PointerEvent } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = Rect.one({
    fill: '#32cd79',
    draggable: true,
    event: {
        [PointerEvent.ENTER]: function (e) {
            e.current.fill = '#42dd89'
        },
        [PointerEvent.LEAVE]: function (e) {
            e.current.fill = '#32cd79'
        }
    }
}, 100, 100, 200, 200)

leafer.add(rect)

Using Strings

Learn about the naming conventions of event type strings.

ts
rect.on('pointer.enter', onEnter)
rect.on('pointer.leave', onLeave)

Removing Events

ts
rect.off('pointer.enter', onEnter)
rect.off('pointer.leave', onLeave)

Dispatching Events

ts
rect.emit('pointer.enter', { current: rect })

Understanding Element Interaction Properties

NameDescription
Interaction
hittableWhether the element responds to mouse, touch, or other pointer interactions, similar to the CSS pointer-events property
hitChildrenFurther defines interactivity of child elements
hitSelfFurther defines interactivity of the element itself (excluding children)
hitFillFurther defines interactivity of the element's fill. Setting to pixel enables pixel-level detection for PNG/SVG images, filtering transparent pixels
hitStrokeFurther defines interactivity of the element's stroke
editableWhether editing is allowed; requires installing the graphic editor plugin
draggableWhether the element can be dragged
dragBoundsRestricts the draggable area of the element
dragBoundsTypeType of drag boundary, whether inside or outside the element
cursorCursor style when hovering over the element, supports all CSS cursor names
States
statesState list for predefining complex element/game states, supporting switching at any time, and transition effects; requires installing the state plugin
stateCurrent state; requires installing the state plugin
buttonMarks element as a button; child elements will automatically sync interaction states such as state, hover, press; requires installing the state plugin
hoverStyleStyle applied when hovered; automatically restored on mouse leave; requires installing the state plugin
pressStyleStyle applied when pressed; automatically restored on release; requires installing the state plugin
focusStyleStyle applied when focused via focus(); restored when focus is lost; requires installing the state plugin
selectedWhether selected; requires installing the state plugin
selectedStyleStyle applied when selected is true; restored when false; requires installing the state plugin
disabledWhether disabled; requires installing the state plugin
disabledStyleStyle applied when disabled is true; restored when false; requires installing the state plugin

Element Event Methods

NameDescription
on()Listen to events
on_()Listen to events with a bound this context for the listener, returns an event id, used with off_()
once()Listen to an event only once
off()Remove event listener
off_()Remove event listener, used with on_()
emit()Manually dispatch event
emitEvent()Manually dispatch event; parameter must be an IEvent object

Naming Rules    Capture/Bubble    Simulated Interaction

Common Events

PointerEvent    DragEvent    DropEvent    SwipeEvent

MoveEvent    ZoomEvent    RotateEvent

ImageEvent    ChildEvent    PropertyEvent    BoundsEvent

LeaferEvent    ResizeEvent    KeyEvent

RenderEvent    LayoutEvent    WatchEvent

Next Step

Remove Elements

Released under the MIT License.