Skip to content

Commit 1bc2b4c

Browse files
author
Lanny McNie
committed
Minor update to tutorials.
Signed-off-by: Lanny McNie <lanny@gskinner.com>
1 parent 4191770 commit 1bc2b4c

File tree

1 file changed

+71
-33
lines changed

1 file changed

+71
-33
lines changed

tutorials/Mouse Interaction/index.html

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
<h1>EaselJS: Mouse Interaction</h1>
2121
<p>
2222
<strong>Synopsis:</strong> Learn about mouse events on display objects and the stage.<br>
23-
<strong>Topics:</strong> MouseEvent, addEventListener, on, click, dblclick, mouseover, mouseout, mousemove, mousedown, mouseup, enableMouseOver, drag and drop, mouseMoveOutside<br>
23+
<strong>Topics:</strong> MouseEvent, addEventListener, on, click, dblclick, mouseover, mouseout,
24+
mousemove, mousedown, mouseup, enableMouseOver, drag and drop, mouseMoveOutside<br>
2425
<strong>Target:</strong> EaselJS v0.7.0
2526
</p>
2627
</header>
2728
<p class="highlight">
28-
This tutorial is part of the <a href="https://github.com/CreateJS/EaselJS/" target="_blank">EaselJS GitHub repository</a>.<br />
29+
This tutorial is part of the <a href="https://github.com/CreateJS/EaselJS/" target="_blank">EaselJS GitHub
30+
repository</a>.<br />
2931
Check out the repository for more tutorials and a handful of helpful samples.
3032
</p>
3133

@@ -34,20 +36,28 @@ <h1>EaselJS: Mouse Interaction</h1>
3436
<h2>The Basics</h2>
3537
</header>
3638
<p>
37-
At its core, the EaselJS mouse interaction model is very simple to use - just assign a listener to a mouse events via the <code>addEventListener</code> method on a display object:
39+
At its core, the EaselJS mouse interaction model is very simple to use - just assign a listener to a
40+
mouse events via the <code>addEventListener</code> method on a display object:
3841
<textarea class="brush: js;" readonly>
3942
circle.addEventListener("click", function(event) { alert("clicked"); })
4043
</textarea>
4144
</p>
4245
<iframe src="basic.html" class="demo" longDesc="the onClick handler" width="100%" height="220px"></iframe>
4346
<p>
44-
There are a number of events you can listen for on display objects: <code>click</code>, <code>mousedown</code>, <code>mouseup</code>, <code>dblclick</code>, <code>pressmove</code>, <code>pressup</code>, <code>mouseover</code> / <code>mouseout</code>, and <code>rollover</code> / <code>rollout</code>.
47+
There are a number of events you can listen for on display objects: <code>click</code>,
48+
<code>mousedown</code>, <code>mouseup</code>, <code>dblclick</code>, <code>pressmove</code>,
49+
<code>pressup</code>, <code>mouseover</code> / <code>mouseout</code>, and <code>rollover</code> /
50+
<code>rollout</code>.
4551
</p>
4652
<p>
47-
The latter four events have some overhead associated with them, so you need to enable them with <code>stage.enableMouseOver(frequency)</code>. The <code>frequency</code> parameter indicates how many times per second EaselJS should calculate what is currently under the pointer. A higher number is more responsive, but also more computationally expensive. It defaults to 20 times per second.
53+
The latter four events have some overhead associated with them, so you need to enable them with
54+
<code>stage.enableMouseOver(frequency)</code>. The <code>frequency</code> parameter indicates how many
55+
times per second EaselJS should calculate what is currently under the pointer. A higher number is more
56+
responsive, but also more computationally expensive. It defaults to 20 times per second.
4857
</p>
4958
<p>
50-
The <code>on</code> method provides a shortcut to <code>addEventListener</code>, and adds additional functionality. See the API docs for more details.
59+
The <code>on</code> method provides a shortcut to <code>addEventListener</code>, and adds additional
60+
functionality. See the API docs for more details.
5161
<textarea class="brush: js;" readonly>
5262
circle.on(type, listener, scope, once, data, useCapture);
5363
</textarea>
@@ -59,9 +69,12 @@ <h2>The Basics</h2>
5969
<h2>MouseEvent</h2>
6070
</header>
6171
<p>
62-
When a mouse handler is triggered, it is called with a single parameter holding a MouseEvent instance. You can use this object to see what <code>type</code> of event it was, what the <code>target</code> was, get access to the <code>nativeEvent</code> object it was based on, and to check the pointer's <code>stageX</code> and <code>stageY</code> coordinates.
72+
When a mouse handler is triggered, it is called with a single parameter holding a MouseEvent instance.
73+
You can use this object to see what <code>type</code> of event it was, what the <code>target</code> was,
74+
get access to the <code>nativeEvent</code> object it was based on, and to check the pointer's
75+
<code>stageX</code> and <code>stageY</code> coordinates.
6376
<textarea class="brush: js;" readonly>
64-
circle.addEventListener("click", function(evt) {
77+
circle.on("click", function(evt) {
6578
alert("type: "+evt.type+" target: "+evt.target+" stageX: "+evt.stageX);
6679
});
6780
</textarea>
@@ -74,21 +87,26 @@ <h2>MouseEvent</h2>
7487
<h2>Bubbling</h2>
7588
</header>
7689
<p>
77-
When a mouse event is triggered on a target, the event flows through three phases: the capture phase, the target phase, and the bubbling phase.
90+
When a mouse event is triggered on a target, the event flows through three phases: the capture phase,
91+
the target phase, and the bubbling phase.
7892
</p>
7993
<p>
80-
In the first phase (capture), the event is dispatched starting with the stage, and progressing through the ancestors of the target to its immediate parent.
94+
In the first phase (capture), the event is dispatched starting with the stage, and progressing through
95+
the ancestors of the target to its immediate parent.
8196
Only listeners that were added using the useCapture parameter are triggered in this phase.
8297
</p>
8398
<p>
84-
In the second phase (target), the event is dispatched on the target object (ex. the element that was clicked).
99+
In the second phase (target), the event is dispatched on the target object (ex. the element that was
100+
clicked).
85101
</p>
86102
<p>
87-
In the final phase (bubbling), the event is dispatched from the immediate parent of the target through the ancestors (in the reverse order of the capture phase) to the stage.
103+
In the final phase (bubbling), the event is dispatched from the immediate parent of the target through
104+
the ancestors (in the reverse order of the capture phase) to the stage.
88105
</p>
89106
<p>
90-
The example below has a "button", which is a <code>Container</code> instance containing two children: a background shape, and a text label.
91-
All three display objects and the stage have listeners for the "click" event both with and without the useCapture param.
107+
The example below has a "button", which is a <code>Container</code> instance containing two children: a
108+
background shape, and a text label. All three display objects and the stage have listeners for the
109+
"click" event both with and without the useCapture param.
92110
</p>
93111
<iframe src="bubbling.html" class="demo" longDesc="event bubbling" width="100%" height="120px"></iframe>
94112
</section>
@@ -98,17 +116,21 @@ <h2>Bubbling</h2>
98116
<h2>mouseChildren & mouseEnabled</h2>
99117
</header>
100118
<p>
101-
You can prevent the children of a <code>Container</code> from dispatching mouse events setting <code>mouseChildren</code> to <code>false</code>.
119+
You can prevent the children of a <code>Container</code> from dispatching mouse events setting
120+
<code>mouseChildren</code> to <code>false</code>.
102121
<textarea class="brush: js;" readonly>
103122
myContainer.mouseChildren = false;
104123
</textarea>
105124
</p>
106125
<p>
107-
This causes the container to be treated as an aggregate element for mouse interactions. For example, in the bubbling example above setting <code>button.mouseChildren = false;</code>
108-
would make it so that clicking children of the button (background & label) dispatches the click event with the button as the target instead of the child that was clicked.
126+
This causes the container to be treated as an aggregate element for mouse interactions. For example, in
127+
the bubbling example above setting <code>button.mouseChildren = false;</code>
128+
would make it so that clicking children of the button (background & label) dispatches the click event
129+
with the button as the target instead of the child that was clicked.
109130
</p>
110131
<p>
111-
Similarly, you can completely disable mouse events on any display object without removing its handlers by setting <code>mouseEnabled</code> to <code>false</code>.
132+
Similarly, you can completely disable mouse events on any display object without removing its handlers
133+
by setting <code>mouseEnabled</code> to <code>false</code>.
112134
<textarea class="brush: js;" readonly>
113135
circle.on("click", function() { alert("Clicked!"); });
114136
circle.mouseEnabled = false;
@@ -120,17 +142,21 @@ <h2>mouseChildren & mouseEnabled</h2>
120142
<h2>hitArea</h2>
121143
</header>
122144
<p>
123-
Normally, EaselJS will calculate mouse hits on a display object based on its visible, non-transparent pixels. This usually works pretty well, but there may be cases where you want to define a hit target that is different than what is displayed on screen.
145+
Normally, EaselJS will calculate mouse hits on a display object based on its visible, non-transparent
146+
pixels. This usually works pretty well, but there may be cases where you want to define a hit target that is different than what is displayed on screen.
124147
</p>
125148
<p>
126-
To do this, you can assign any other display object to be the <code>hitArea</code> for your object. It does not need to be on the display list, and will not be visible, but it will be used for the hit test instead.
149+
To do this, you can assign any other display object to be the <code>hitArea</code> for your object.
150+
It does not need to be on the display list, and will not be visible, but it will be used for the hit test instead.
127151
</p>
128152
<p>
129-
Hit area display objects are used within the coordinate system (ie. concatenated transformation) of their owner, and as such you can reuse the same display object as the <code>hitArea</code> of multiple objects.
153+
Hit area display objects are used within the coordinate system (ie. concatenated transformation) of
154+
their owner, and as such you can reuse the same display object as the <code>hitArea</code> of multiple objects.
130155
</p>
131156
<iframe src="hitArea.html" class="demo" longDesc="mouse event handlers on containers" width="100%" height="170px"></iframe>
132157
<p>
133-
Notice how in this demo, as you roll over the red text, it only registers a hit when the pointer is over a non-transparent pixel, whereas the blue text uses the rectangular <code>hitArea</code> to calculate the hit.
158+
Notice how in this demo, as you roll over the red text, it only registers a hit when the pointer is
159+
over a non-transparent pixel, whereas the blue text uses the rectangular <code>hitArea</code> to calculate the hit.
134160
</p>
135161
</section>
136162

@@ -139,12 +165,16 @@ <h2>hitArea</h2>
139165
<h2>Stage mouse events</h2>
140166
</header>
141167
<p>
142-
For the stage, just like every other display object, you will only get events when the mouse is over a non-transparent pixel.
168+
For the stage, just like every other display object, you will only get events when the mouse is over a
169+
non-transparent pixel.
143170
</p>
144171
<p>
145-
Stage has a few special mouse events that come in handy for responding to general mouse interactions anywhere within your canvas. <code>stagemousedown</code>, <code>stagemouseup</code>, and <code>stagemousemove</code> are called any time a relevant mouse interaction happens anywhere on the canvas.
172+
Stage has a few special mouse events that come in handy for responding to general mouse interactions
173+
anywhere within your canvas. <code>stagemousedown</code>, <code>stagemouseup</code>, and
174+
<code>stagemousemove</code> are called any time a relevant mouse interaction happens anywhere on the
175+
canvas.
146176
<textarea class="brush: js;" readonly>
147-
stage.addEventListener("stagemousedown", function(evt) {
177+
stage.on("stagemousedown", function(evt) {
148178
alert("the canvas was clicked at "+evt.stageX+","+evt.stageY);
149179
})
150180
</textarea>
@@ -156,7 +186,11 @@ <h2>Stage mouse events</h2>
156186
By default, you will stop getting <code>stagemousemove</code> events whenever the pointer is outside of the canvas.
157187
</p>
158188
<p>
159-
If you'd like to keep getting <code>stagemousemove</code> events when the pointer leaves the canvas, just set <code>mouseMoveOutside</code> to <code>true</code>. The <code>stageX</code> & <code>stageY</code> properties of <code>MouseEvent</code> will always return a value normalized to within your stage bounds, but you can use <code>rawX</code> and <code>rawY</code> to get values that haven't been normalized (this can cause errors if you aren't careful).
189+
If you'd like to keep getting <code>stagemousemove</code> events when the pointer leaves the canvas,
190+
just set <code>mouseMoveOutside</code> to <code>true</code>. The <code>stageX</code> &
191+
<code>stageY</code> properties of <code>MouseEvent</code> will always return a value normalized to
192+
within your stage bounds, but you can use <code>rawX</code> and <code>rawY</code> to get values that
193+
haven't been normalized (this can cause errors if you aren't careful).
160194
<textarea class="brush: js;" readonly>
161195
stage.mouseMoveOutside = true;
162196
stage.on("stagemousemove", function(evt) {
@@ -166,7 +200,8 @@ <h2>Stage mouse events</h2>
166200
</textarea>
167201
</p>
168202
<p>
169-
You can monitor whether the pointer is over the canvas by using <code>stage.mouseInBounds</code> and the <code>mouseleave</code> / <code>mouseenter</code> events.
203+
You can monitor whether the pointer is over the canvas by using <code>stage.mouseInBounds</code> and the
204+
<code>mouseleave</code> / <code>mouseenter</code> events.
170205
</p>
171206
</section>
172207

@@ -175,9 +210,9 @@ <h2>Stage mouse events</h2>
175210
<h2>Drag and drop</h2>
176211
</header>
177212
<p>
178-
EaselJS makes drag and drop functionality very easy to implement. After the mouse is pressed over a display object, that
179-
object will generate <code>pressmove</code> events until the mouse is released, at which point a <code>pressup</code>
180-
event will be dispatched.
213+
EaselJS makes drag and drop functionality very easy to implement. After the mouse is pressed over a
214+
display object, that object will generate <code>pressmove</code> events until the mouse is released, at
215+
which point a <code>pressup</code> event will be dispatched.
181216
</p>
182217
<textarea class="brush: js;" readonly>
183218
circle.on("pressmove", function(evt) {
@@ -199,9 +234,12 @@ <h2>Other useful APIs</h2>
199234
</header>
200235
<p>
201236
Other methods that are relevant to advanced mouse interactions are:<ul>
202-
<li> <code>Container.getObjectUnderPoint()</code> returns the top most display object under the specified point.
203-
<li> <code>Container.getObjectsUnderPoint()</code> returns all display objects under the specified point.
204-
<li> <code>DisplayObject.hitTest()</code> returns true if the specified point in the display object is non-transparent.
237+
<li> <code>Container.getObjectUnderPoint()</code> returns the top most display object under the
238+
specified point.</li>
239+
<li> <code>Container.getObjectsUnderPoint()</code> returns all display objects under the specified
240+
point.</li>
241+
<li> <code>DisplayObject.hitTest()</code> returns true if the specified point in the display object
242+
is non-transparent.</li>
205243
</ul>
206244
Check out the API documentation and the HitTest tutorial for more information.
207245
</p>

0 commit comments

Comments
 (0)