Event handling in Flextime is somewhat similar to jQuery/DOM event handling, but homogenized, with many of the quirks and corner-cases concealed.
Event handlers can be attached to a Flextime instance by using any of the convenience methods
.keyup(). More generally, event handlers can be attached with
.on(eventname, handler) and removed with
.off(eventname,handler) (if the handler argument is omitted, all events of the specified name are removed).
Multiple handlers can be attached for a given event type; handlers will be called in the order they are added. (See the multiple event handlers sample for a demonstration of how this can often simplify code involving multiple independent objects needing to respond to events.)
All event handlers have
this bound to the active Flextime instance, and are passed an event object. The event object is simply the standard jQuery event object, augmented with a few additional convenience fields:
.mouse-- Contains the current state of the mouse; this is always available, even in event handlers that normally would not provide any mouse-related information.
mousecontains the mouse position in fields
mouse.y, the displacement between the previous mouse position and the current position in
mouse.dy, and the state of the mouse buttons in
.keyboard-- If keyboard state handling has been enabled, this contains an object indicating the state of every key on the keyboard.
.keyboard[k]is true if the key with code k is depressed, and false if it is not.
draw event is called once per frame; as described above, it also has access to the current mouse (and possibly keyboard) state. In addition, if at least one handler is bound to the
draw event, then the event object will have these additional timing-related fields:
frameTime-- The time, in milliseconds, at which the current frame started.
ellapsedTime-- The number of milliseconds since the start of the previous frame.
frameCount-- The number of frames that have been displayed since the first
drawhandler was registered.
Note that the
event object is shared by all event handlers of a given type. Thus, any changes made to it will be seen by event handlers later in the chain.