MouseEvent Class
This is passed as the parameter to all mouse/pointer/touch related events on DisplayObject instances.
Constructor
MouseEvent
-
type
-
bubbles
-
cancelable
-
stageX
-
stageY
-
nativeEvent
-
pointerID
-
primary
-
rawX
-
rawY
Parameters:
-
type
StringThe event type.
-
bubbles
BooleanIndicates whether the event will bubble through the display list.
-
cancelable
BooleanIndicates whether the default behaviour of this event can be cancelled.
-
stageX
NumberThe normalized x position relative to the stage.
-
stageY
NumberThe normalized y position relative to the stage.
-
nativeEvent
MouseEventThe native DOM event related to this mouse event.
-
pointerID
NumberThe unique id for the pointer.
-
primary
BooleanIndicates whether this is the primary pointer in a multitouch environment.
-
rawX
NumberThe raw x position relative to the stage.
-
rawY
NumberThe raw y position relative to the stage.
Item Index
Methods
Properties
Methods
_dispatchEvent
-
eventObj
-
eventPhase
addEventListener
-
type
-
listener
-
[useCapture]
Adds the specified event listener. Note that adding multiple listeners to the same function will result in multiple callbacks getting fired.
Example
displayObject.addEventListener("click", handleClick);
function handleClick(event) {
// Click happened.
}
Parameters:
-
type
StringThe string type of the event.
-
listener
Function | ObjectAn object with a handleEvent method, or a function that will be called when the event is dispatched.
-
[useCapture]
Boolean optionalFor events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
clone
()
MouseEvent
Returns a clone of the MouseEvent instance.
Returns:
a clone of the MouseEvent instance.
dispatchEvent
-
eventObj
-
[target]
Dispatches the specified event to all listeners.
Example
// Use a string event
this.dispatchEvent("complete");
// Use an Event instance
var event = new createjs.Event("progress");
this.dispatchEvent(event);
Parameters:
-
eventObj
Object | String | EventAn object with a "type" property, or a string type. While a generic object will work, it is recommended to use a CreateJS Event instance. If a string is used, dispatchEvent will construct an Event instance with the specified type.
-
[target]
Object optionalThe object to use as the target property of the event object. This will default to the dispatching object. This parameter is deprecated and will be removed.
Returns:
Returns the value of eventObj.defaultPrevented.
hasEventListener
-
type
Indicates whether there is at least one listener for the specified event type and useCapture
value.
Parameters:
-
type
StringThe string type of the event.
Returns:
Returns true if there is at least one listener for the specified event.
initialize
-
type
-
bubbles
-
cancelable
-
stageX
-
stageY
-
nativeEvent
-
pointerID
-
primary
-
rawX
-
rawY
Initialization method.
Parameters:
-
type
StringThe event type.
-
bubbles
BooleanIndicates whether the event will bubble through the display list.
-
cancelable
BooleanIndicates whether the default behaviour of this event can be cancelled.
-
stageX
NumberThe normalized x position relative to the stage.
-
stageY
NumberThe normalized y position relative to the stage.
-
nativeEvent
MouseEventThe native DOM event related to this mouse event.
-
pointerID
NumberThe unique id for the pointer.
-
primary
BooleanIndicates whether this is the primary pointer in a multitouch environment.
-
rawX
NumberThe raw x position relative to the stage.
-
rawY
NumberThe raw y position relative to the stage.
off
-
type
-
listener
-
[useCapture]
A shortcut to the removeEventListener method, with the same parameters and return value. This is a companion to the .on method.
on
-
type
-
listener
-
[scope]
-
[once=false]
-
[data]
-
[useCapture=false]
A shortcut method for using addEventListener that makes it easier to specify an execution scope, have a listener only run once, associate arbitrary data with the listener, and remove the listener.
This method works by creating an anonymous wrapper function and subscribing it with addEventListener. The created anonymous function is returned for use with .removeEventListener (or .off).
Example
var listener = myBtn.on("click", handleClick, null, false, {count:3});
function handleClick(evt, data) {
data.count -= 1;
console.log(this == myBtn); // true - scope defaults to the dispatcher
if (data.count == 0) {
alert("clicked 3 times!");
myBtn.off("click", listener);
// alternately: evt.remove();
}
}
Parameters:
-
type
StringThe string type of the event.
-
listener
Function | ObjectAn object with a handleEvent method, or a function that will be called when the event is dispatched.
-
[scope]
Object optionalThe scope to execute the listener in. Defaults to the dispatcher/currentTarget for function listeners, and to the listener itself for object listeners (ie. using handleEvent).
-
[once=false]
Boolean optionalIf true, the listener will remove itself after the first time it is triggered.
-
[data]
optionalArbitrary data that will be included as the second parameter when the listener is called.
-
[useCapture=false]
Boolean optionalFor events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
Returns:
Returns the anonymous function that was created and assigned as the listener. This is needed to remove the listener later using .removeEventListener.
preventDefault
()
Sets Event/defaultPrevented to true. Mirrors the DOM event standard.
remove
()
Causes the active listener to be removed via removeEventListener();
myBtn.addEventListener("click", function(evt) {
// do stuff...
evt.remove(); // removes this listener.
});
removeAllEventListeners
-
[type]
Removes all listeners for the specified type, or all listeners of all types.
Example
// Remove all listeners
displayObject.removeAllEvenListeners();
// Remove all click listeners
displayObject.removeAllEventListeners("click");
Parameters:
-
[type]
String optionalThe string type of the event. If omitted, all listeners for all types will be removed.
removeEventListener
-
type
-
listener
-
[useCapture]
Removes the specified event listener.
Important Note: that you must pass the exact function reference used when the event was added. If a proxy function, or function closure is used as the callback, the proxy/closure reference must be used - a new proxy or closure will not work.
Example
displayObject.removeEventListener("click", handleClick);
stopImmediatePropagation
()
Sets Event/propagationStopped and Event/immediatePropagationStopped to true. Mirrors the DOM event standard.
stopPropagation
()
Sets Event/propagationStopped to true. Mirrors the DOM event standard.
Properties
cancelable
Boolean
Indicates whether the default behaviour of this event can be cancelled via preventDefault. This is set via the Event constructor.
Default: false
currentTarget
Object
The current target that a bubbling event is being dispatched from. For non-bubbling events, this will always be the same as target. For example, if childObj.parent = parentObj, and a bubbling event is generated from childObj, then a listener on parentObj would receive the event with target=childObj (the original target) and currentTarget=parentObj (where the listener was added).
Default: null
eventPhase
Number
For bubbling events, this indicates the current event phase:
- capture phase: starting from the top parent to the target
- at target phase: currently being dispatched from the target
- bubbling phase: from the target to the top parent
Default: 0
immediatePropagationStopped
Boolean
Indicates if stopImmediatePropagation has been called on this event.
Default: false
nativeEvent
MouseEvent
The native MouseEvent generated by the browser. The properties and API for this event may differ between browsers. This property will be null if the EaselJS property was not directly generated from a native MouseEvent.
Default: null
pointerID
Number
The unique id for the pointer (touch point or cursor). This will be either -1 for the mouse, or the system supplied id value.
primary
Boolean
Indicates whether this is the primary pointer in a multitouch environment. This will always be true for the mouse. For touch pointers, the first pointer in the current stack will be considered the primary pointer.
propagationStopped
Boolean
Indicates if stopPropagation or stopImmediatePropagation has been called on this event.
Default: false
rawX
Number
The raw x position relative to the stage. Normally this will be the same as the stageX value, unless stage.mouseMoveOutside is true and the pointer is outside of the stage bounds.
rawY
Number
The raw y position relative to the stage. Normally this will be the same as the stageY value, unless stage.mouseMoveOutside is true and the pointer is outside of the stage bounds.
stageX
Number
The normalized x position on the stage. This will always be within the range 0 to stage width.
stageY
Number
The normalized y position on the stage. This will always be within the range 0 to stage height.
Events
mousemove
deprecated
For MouseEvent objects of type "mousedown", mousemove events will be dispatched from the event object until the user releases the mouse anywhere. This enables you to listen to mouse move interactions for the duration of a press, which can be very useful for operations such as drag and drop.
See the MouseEvent class for a listing of event properties.
mouseup
deprecated
For MouseEvent objects of type "mousedown", a mouseup event will be dispatched from the event object when the user releases the mouse anywhere. This enables you to listen for a corresponding mouse up from a specific press, which can be very useful for operations such as drag and drop.
See the MouseEvent class for a listing of event properties.