|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Animating Along a Curved Path</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Animating Along a Curved Path</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>This demonstrates how to animate the position of an element along a <code>curve</code>.</p> |
|
|
30 |
<p>Click the button or drag the dots to see the animation. You can use this |
|
|
31 |
example as a tool to find the right XY values for start, end, and control points |
|
|
32 |
that will give you the right shape path for your own animation on a curve.</p> |
|
|
33 |
<p>The curved line shown here between points 0 and 3 is just for preview |
|
|
34 |
purposes, and is not part of animation on a curve. The draggable points 0 - 3 |
|
|
35 |
are displayed only for UI interactivity.</p> |
|
|
36 |
</div> |
|
|
37 |
|
|
|
38 |
<div class="example"> |
|
|
39 |
<style> |
|
|
40 |
.example { |
|
|
41 |
position: relative; |
|
|
42 |
padding: 0; |
|
|
43 |
margin: 0; |
|
|
44 |
width: 100%; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
.example div, .example p, .example span{ |
|
|
48 |
padding: 0; |
|
|
49 |
margin: 0; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
#mygraphiccontainer { |
|
|
53 |
width: 650px; |
|
|
54 |
height: 400px; |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
.example #demo { |
|
|
58 |
position: absolute; |
|
|
59 |
left: 92px; |
|
|
60 |
top: 103px; |
|
|
61 |
background-color: #f00; |
|
|
62 |
text-align: center; |
|
|
63 |
line-height: 1.5em; |
|
|
64 |
border: solid 1px #cc0000; |
|
|
65 |
border-radius: 0; |
|
|
66 |
-moz-box-shadow: 3px 3px 7px rgba(0,0,0,0.4); |
|
|
67 |
-webkit-box-shadow: 3px 3px 7px rgba(0,0,0,0.4); |
|
|
68 |
width: 20px; |
|
|
69 |
height: 20px; |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
.dot { |
|
|
73 |
position: absolute; |
|
|
74 |
color: #396491; |
|
|
75 |
font-size: 20px; |
|
|
76 |
height: 0; |
|
|
77 |
line-height: 12px; |
|
|
78 |
opacity: 0.8; |
|
|
79 |
width: 0; |
|
|
80 |
cursor: move; |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
.dot .point { |
|
|
84 |
background-color: #000; |
|
|
85 |
position: absolute; |
|
|
86 |
left: -3px; |
|
|
87 |
top: -3px; |
|
|
88 |
width: 6px; |
|
|
89 |
height: 6px; |
|
|
90 |
border-radius: 3px; |
|
|
91 |
font-size: 0px; |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
.number-label { |
|
|
95 |
position: absolute; |
|
|
96 |
top: -1em; |
|
|
97 |
left: -0.9em; |
|
|
98 |
width: 1em; |
|
|
99 |
height: 1em; |
|
|
100 |
line-height: 1em; |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
/* Gives the points a larger target area to drag */ |
|
|
104 |
.dot .fat-finger { |
|
|
105 |
position: absolute; |
|
|
106 |
top: -30px; /* minus half the fat-finger height */ |
|
|
107 |
left: -30px; |
|
|
108 |
width: 60px; |
|
|
109 |
height: 60px; |
|
|
110 |
-moz-border-radius: 30px; |
|
|
111 |
-webkit-border-radius: 30px; |
|
|
112 |
border-radius: 30px; |
|
|
113 |
background-color: #abc; |
|
|
114 |
opacity: 0.3; |
|
|
115 |
filter: alpha (opacity = 30); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
#dot-3 .fat-finger { |
|
|
119 |
background-color: #f80; |
|
|
120 |
opacity: 0.4; |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
#dot-0 .fat-finger { |
|
|
124 |
background-color: #8DAA16; |
|
|
125 |
opacity: 0.4; |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
#dot-0, #demo { |
|
|
129 |
top: 103px; |
|
|
130 |
left: 92px; |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
#dot-1 { |
|
|
134 |
top: 317px; |
|
|
135 |
left: 147px; |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
#dot-2 { |
|
|
139 |
top: 179px; |
|
|
140 |
left: 532px; |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
#dot-3 { |
|
|
144 |
left: 538px; |
|
|
145 |
top: 288px; |
|
|
146 |
} |
|
|
147 |
|
|
|
148 |
#info { |
|
|
149 |
position: absolute; |
|
|
150 |
width: 450px; |
|
|
151 |
height: 15em; |
|
|
152 |
right: 1em; |
|
|
153 |
top: 1em; |
|
|
154 |
} |
|
|
155 |
|
|
|
156 |
code span { |
|
|
157 |
color: #CC3300; |
|
|
158 |
} |
|
|
159 |
|
|
|
160 |
.point { |
|
|
161 |
font-size: 24px; |
|
|
162 |
left: 5px; |
|
|
163 |
position: absolute; |
|
|
164 |
top: -5px; |
|
|
165 |
} |
|
|
166 |
|
|
|
167 |
</style> |
|
|
168 |
<!-- In order for the script to run, this HTML markup must be nested in a <div class="example"> --> |
|
|
169 |
<div id="mygraphiccontainer"></div> <!-- Container for the preview of the curve line --> |
|
|
170 |
<div id="info"> |
|
|
171 |
<button id="btn-animate" class='yui3-button'>Animate On Path</button> |
|
|
172 |
<p>To see anim along a path, click the button above, or drag point 0, 1, 2, or 3.</p> |
|
|
173 |
<p>The X and Y of point 0 is <code>[<span class="arr0">92,103</span>]</code></p> |
|
|
174 |
<p>The X and Y of 1, 2, and 3, are the sub-arrays in array value of "curve:" in the anim.set()</p> |
|
|
175 |
<p><code>anim.set('to', {curve: [ |
|
|
176 |
[<span class="arr1">147,317</span>], |
|
|
177 |
[<span class="arr2">532,179</span>], |
|
|
178 |
[<span class="arr3">538,288</span>] ]});</code></p> |
|
|
179 |
</div> |
|
|
180 |
<div id="dot-0" class="dot zero"> |
|
|
181 |
<div class="point"></div> |
|
|
182 |
<div class="fat-finger" title="Drag to change start point"></div> <!-- Gives the points a larger target area to drag --> |
|
|
183 |
<div class="number-label">0</div> |
|
|
184 |
</div> |
|
|
185 |
<div id="dot-1" class="dot one"> |
|
|
186 |
<div class="point"></div> |
|
|
187 |
<div class="fat-finger" title="Drag to change path"></div> |
|
|
188 |
<div class="number-label">1</div> |
|
|
189 |
</div> |
|
|
190 |
<div id="dot-2" class="dot two"> |
|
|
191 |
<div class="point"></div> |
|
|
192 |
<div class="fat-finger" title="Drag to change path"></div> |
|
|
193 |
<div class="number-label">2</div> |
|
|
194 |
</div> |
|
|
195 |
<div id="dot-3" class="dot three"> |
|
|
196 |
<div class="point"></div> |
|
|
197 |
<div class="fat-finger" title="Drag to change end point"></div> |
|
|
198 |
<div class="number-label">3</div> |
|
|
199 |
</div> |
|
|
200 |
<div id="demo">A</div> |
|
|
201 |
|
|
|
202 |
<script> |
|
|
203 |
YUI().use('anim', 'dd-drag', 'graphics', 'cssbutton', function(Y){ |
|
|
204 |
|
|
|
205 |
var mygraphic = new Y.Graphic({render:"#mygraphiccontainer"}), |
|
|
206 |
origin = Y.one('.example'), // The XY values for the animation are based on the upper-left corner of this element |
|
|
207 |
demoA = Y.one('#demo'), // The animated element |
|
|
208 |
dotList = Y.all('.dot'); |
|
|
209 |
// Draggable points |
|
|
210 |
dot0 = Y.one('#dot-0'), dot1 = Y.one('#dot-1'), dot2 = Y.one('#dot-2'), dot3 = Y.one('#dot-3'), |
|
|
211 |
|
|
|
212 |
// Array of XY arrays of draggable points |
|
|
213 |
arrDot = [ |
|
|
214 |
[parseInt(dotList.item(0).getStyle('left'), 10), parseInt(dotList.item(0).getStyle('top'), 10)], |
|
|
215 |
[parseInt(dotList.item(1).getStyle('left'), 10), parseInt(dotList.item(1).getStyle('top'), 10)], |
|
|
216 |
[parseInt(dotList.item(2).getStyle('left'), 10), parseInt(dotList.item(2).getStyle('top'), 10)], |
|
|
217 |
[parseInt(dotList.item(3).getStyle('left'), 10), parseInt(dotList.item(3).getStyle('top'), 10)] |
|
|
218 |
], |
|
|
219 |
|
|
|
220 |
// Make points draggable |
|
|
221 |
dd0 = new Y.DD.Drag({ |
|
|
222 |
node: '#dot-0' |
|
|
223 |
}), |
|
|
224 |
dd1 = new Y.DD.Drag({ |
|
|
225 |
node: '#dot-1' |
|
|
226 |
}), |
|
|
227 |
dd2 = new Y.DD.Drag({ |
|
|
228 |
node: '#dot-2' |
|
|
229 |
}), |
|
|
230 |
dd3 = new Y.DD.Drag({ |
|
|
231 |
node: '#dot-3' |
|
|
232 |
}); |
|
|
233 |
|
|
|
234 |
// Puts current XY values of points into displayed code snippet |
|
|
235 |
var updateCodeSnippetValues = function(){ |
|
|
236 |
Y.one('.arr0').setHTML(arrDot[0][0] + ',' + arrDot[0][1]); // Start |
|
|
237 |
Y.one('.arr1').setHTML(arrDot[1][0] + ',' + arrDot[1][1]); // Control point 1 |
|
|
238 |
Y.one('.arr2').setHTML(arrDot[2][0] + ',' + arrDot[2][1]); // Control point 2 |
|
|
239 |
Y.one('.arr3').setHTML(arrDot[3][0] + ',' + arrDot[3][1]); // End |
|
|
240 |
} |
|
|
241 |
|
|
|
242 |
/** |
|
|
243 |
* Stops the animation |
|
|
244 |
* Updates the array of point XY values |
|
|
245 |
* Updates the curve preview |
|
|
246 |
* Updates displayed XY point values in code snippet |
|
|
247 |
*/ |
|
|
248 |
var dotDragHandler = function(dot){ |
|
|
249 |
Y.Anim.stop(); |
|
|
250 |
arrDot[dotList.indexOf(dot)] = [parseInt(dot.getStyle('left'), 10), parseInt(dot.getStyle('top'), 10)]; |
|
|
251 |
drawCurve(); |
|
|
252 |
updateCodeSnippetValues(); |
|
|
253 |
} |
|
|
254 |
|
|
|
255 |
dd0.on('drag:drag', function(e){ |
|
|
256 |
dotDragHandler(this.get('dragNode')); |
|
|
257 |
}); |
|
|
258 |
dd1.on('drag:drag', function(e){ |
|
|
259 |
dotDragHandler(this.get('dragNode')); |
|
|
260 |
}); |
|
|
261 |
dd2.on('drag:drag', function(e){ |
|
|
262 |
dotDragHandler(this.get('dragNode')); |
|
|
263 |
}); |
|
|
264 |
dd3.on('drag:drag', function(e){ |
|
|
265 |
dotDragHandler(this.get('dragNode')); |
|
|
266 |
}); |
|
|
267 |
|
|
|
268 |
// button handler |
|
|
269 |
Y.one('#btn-animate').on('click', function(){ |
|
|
270 |
Y.Anim.stop(); |
|
|
271 |
setTimeout(startAnim, 500); |
|
|
272 |
}); |
|
|
273 |
|
|
|
274 |
Y.all('.dot').on('mouseup', function(e){ |
|
|
275 |
setTimeout(startAnim, 500); |
|
|
276 |
}); |
|
|
277 |
|
|
|
278 |
// Create the animation instance |
|
|
279 |
var anim = new Y.Anim({ |
|
|
280 |
node: demoA, |
|
|
281 |
duration: 1.5, |
|
|
282 |
easing: Y.Easing.easeOut |
|
|
283 |
}); |
|
|
284 |
|
|
|
285 |
/** |
|
|
286 |
* Sets the anim curve values with the XY values from the arrDot array |
|
|
287 |
* Adds the origin offset values because anim works off page coordinates |
|
|
288 |
*/ |
|
|
289 |
var startAnim = function(e){ |
|
|
290 |
var oX = origin.getX(), |
|
|
291 |
oY = origin.getY(); |
|
|
292 |
|
|
|
293 |
// Reset the animated element to the start position. |
|
|
294 |
// This is needed for running the animation multiple times |
|
|
295 |
demoA.setStyles({'left':arrDot[0][0], 'top':arrDot[0][1]}); |
|
|
296 |
|
|
|
297 |
anim.set('to', { |
|
|
298 |
curve: [ [(arrDot[1][0] + oX), (arrDot[1][1] + oY)], [ (arrDot[2][0] + oX), (arrDot[2][1] + oY) ], [ (arrDot[3][0] + oX), (arrDot[3][1] + oY) ] ] |
|
|
299 |
}); |
|
|
300 |
anim.run(); |
|
|
301 |
}; |
|
|
302 |
|
|
|
303 |
// Adds a YUI Graphics path shape to the Graphics instance mygraphic |
|
|
304 |
var animPath = mygraphic.addShape({ |
|
|
305 |
type: "path", |
|
|
306 |
stroke: { |
|
|
307 |
weight: 4, |
|
|
308 |
color: "#aabbcc" |
|
|
309 |
} |
|
|
310 |
}); |
|
|
311 |
|
|
|
312 |
// Draw a preview curve with the Graphics animPath shape to match the Anim curve |
|
|
313 |
var drawCurve = function(){ |
|
|
314 |
animPath.clear(); |
|
|
315 |
animPath.moveTo(arrDot[0][0],arrDot[0][1]); |
|
|
316 |
animPath.curveTo(arrDot[1][0],arrDot[1][1], arrDot[2][0],arrDot[2][1], arrDot[3][0],arrDot[3][1]); |
|
|
317 |
animPath.end(); |
|
|
318 |
} |
|
|
319 |
|
|
|
320 |
drawCurve(); // Initial drawing of the preview curve |
|
|
321 |
updateCodeSnippetValues(); // Initial setting of code snippet XY values |
|
|
322 |
|
|
|
323 |
}); |
|
|
324 |
|
|
|
325 |
</script> |
|
|
326 |
|
|
|
327 |
|
|
|
328 |
</div> |
|
|
329 |
|
|
|
330 |
<h2>Setting up the HTML</h2> |
|
|
331 |
<p>First we add some HTML to animate.</p> |
|
|
332 |
<pre class="code prettyprint"><div class="example"> |
|
|
333 |
<button id="btn-animate" class='yui3-button'>Animate On Path</button> |
|
|
334 |
<div id="demo">A</div> |
|
|
335 |
</div></pre> |
|
|
336 |
|
|
|
337 |
|
|
|
338 |
<h2>Creating the Anim Instance</h2> |
|
|
339 |
<p>Now we create an instance of <code>Y.Anim</code>, passing it a configuration object that includes the <code>node</code> we wish to animate.</p> |
|
|
340 |
|
|
|
341 |
<pre class="code prettyprint">var demoA = Y.one('#demo'); |
|
|
342 |
|
|
|
343 |
var anim = new Y.Anim({ |
|
|
344 |
node: demoA, |
|
|
345 |
duration: 1.5, |
|
|
346 |
easing: Y.Easing.easeOut |
|
|
347 |
});</pre> |
|
|
348 |
|
|
|
349 |
|
|
|
350 |
<h2>Changing Attributes</h2> |
|
|
351 |
<p>A click handler will set the <code>to</code> value before <code>run</code> is called.</p> |
|
|
352 |
|
|
|
353 |
<pre class="code prettyprint">var startAnim = function(e) { |
|
|
354 |
anim.set('to', { |
|
|
355 |
curve: [ [x1, y1], [x2, y2], [x3, y3] ] // Where 1 and 2 are curve control points, and 3 is the end point. |
|
|
356 |
}); |
|
|
357 |
anim.run(); |
|
|
358 |
};</pre> |
|
|
359 |
|
|
|
360 |
|
|
|
361 |
<h2>Running the Animation</h2> |
|
|
362 |
<p>If the animation will be run multiple times, you'll need to reset the position of the animated element.</p> |
|
|
363 |
<p>Finally we add an event handler to run the animation.</p> |
|
|
364 |
|
|
|
365 |
<pre class="code prettyprint">var resetToAnimStart = function(){ |
|
|
366 |
demoA.setStyles({'left': x0, 'top': y0}); // Where x0, y0 is the animation starting point |
|
|
367 |
} |
|
|
368 |
|
|
|
369 |
Y.one('#btn-animate').on('click', function(){ |
|
|
370 |
resetToAnimStart(); |
|
|
371 |
startAnim(); |
|
|
372 |
});</pre> |
|
|
373 |
|
|
|
374 |
|
|
|
375 |
<h2>Complete Example Source</h2> |
|
|
376 |
<p>The code shown above does the basics. |
|
|
377 |
This example however, allows dragging the points, displays xy values for each point |
|
|
378 |
while you drag, and displays a path preview of the animation using YUI Graphics |
|
|
379 |
Utility. This adds some complexity.</p> |
|
|
380 |
<pre class="code prettyprint"><style> |
|
|
381 |
.example { |
|
|
382 |
position: relative; |
|
|
383 |
padding: 0; |
|
|
384 |
margin: 0; |
|
|
385 |
width: 100%; |
|
|
386 |
} |
|
|
387 |
|
|
|
388 |
.example div, .example p, .example span{ |
|
|
389 |
padding: 0; |
|
|
390 |
margin: 0; |
|
|
391 |
} |
|
|
392 |
|
|
|
393 |
#mygraphiccontainer { |
|
|
394 |
width: 650px; |
|
|
395 |
height: 400px; |
|
|
396 |
} |
|
|
397 |
|
|
|
398 |
.example #demo { |
|
|
399 |
position: absolute; |
|
|
400 |
left: 92px; |
|
|
401 |
top: 103px; |
|
|
402 |
background-color: #f00; |
|
|
403 |
text-align: center; |
|
|
404 |
line-height: 1.5em; |
|
|
405 |
border: solid 1px #cc0000; |
|
|
406 |
border-radius: 0; |
|
|
407 |
-moz-box-shadow: 3px 3px 7px rgba(0,0,0,0.4); |
|
|
408 |
-webkit-box-shadow: 3px 3px 7px rgba(0,0,0,0.4); |
|
|
409 |
width: 20px; |
|
|
410 |
height: 20px; |
|
|
411 |
} |
|
|
412 |
|
|
|
413 |
.dot { |
|
|
414 |
position: absolute; |
|
|
415 |
color: #396491; |
|
|
416 |
font-size: 20px; |
|
|
417 |
height: 0; |
|
|
418 |
line-height: 12px; |
|
|
419 |
opacity: 0.8; |
|
|
420 |
width: 0; |
|
|
421 |
cursor: move; |
|
|
422 |
} |
|
|
423 |
|
|
|
424 |
.dot .point { |
|
|
425 |
background-color: #000; |
|
|
426 |
position: absolute; |
|
|
427 |
left: -3px; |
|
|
428 |
top: -3px; |
|
|
429 |
width: 6px; |
|
|
430 |
height: 6px; |
|
|
431 |
border-radius: 3px; |
|
|
432 |
font-size: 0px; |
|
|
433 |
} |
|
|
434 |
|
|
|
435 |
.number-label { |
|
|
436 |
position: absolute; |
|
|
437 |
top: -1em; |
|
|
438 |
left: -0.9em; |
|
|
439 |
width: 1em; |
|
|
440 |
height: 1em; |
|
|
441 |
line-height: 1em; |
|
|
442 |
} |
|
|
443 |
|
|
|
444 |
/* Gives the points a larger target area to drag */ |
|
|
445 |
.dot .fat-finger { |
|
|
446 |
position: absolute; |
|
|
447 |
top: -30px; /* minus half the fat-finger height */ |
|
|
448 |
left: -30px; |
|
|
449 |
width: 60px; |
|
|
450 |
height: 60px; |
|
|
451 |
-moz-border-radius: 30px; |
|
|
452 |
-webkit-border-radius: 30px; |
|
|
453 |
border-radius: 30px; |
|
|
454 |
background-color: #abc; |
|
|
455 |
opacity: 0.3; |
|
|
456 |
filter: alpha (opacity = 30); |
|
|
457 |
} |
|
|
458 |
|
|
|
459 |
#dot-3 .fat-finger { |
|
|
460 |
background-color: #f80; |
|
|
461 |
opacity: 0.4; |
|
|
462 |
} |
|
|
463 |
|
|
|
464 |
#dot-0 .fat-finger { |
|
|
465 |
background-color: #8DAA16; |
|
|
466 |
opacity: 0.4; |
|
|
467 |
} |
|
|
468 |
|
|
|
469 |
#dot-0, #demo { |
|
|
470 |
top: 103px; |
|
|
471 |
left: 92px; |
|
|
472 |
} |
|
|
473 |
|
|
|
474 |
#dot-1 { |
|
|
475 |
top: 317px; |
|
|
476 |
left: 147px; |
|
|
477 |
} |
|
|
478 |
|
|
|
479 |
#dot-2 { |
|
|
480 |
top: 179px; |
|
|
481 |
left: 532px; |
|
|
482 |
} |
|
|
483 |
|
|
|
484 |
#dot-3 { |
|
|
485 |
left: 538px; |
|
|
486 |
top: 288px; |
|
|
487 |
} |
|
|
488 |
|
|
|
489 |
#info { |
|
|
490 |
position: absolute; |
|
|
491 |
width: 450px; |
|
|
492 |
height: 15em; |
|
|
493 |
right: 1em; |
|
|
494 |
top: 1em; |
|
|
495 |
} |
|
|
496 |
|
|
|
497 |
code span { |
|
|
498 |
color: #CC3300; |
|
|
499 |
} |
|
|
500 |
|
|
|
501 |
.point { |
|
|
502 |
font-size: 24px; |
|
|
503 |
left: 5px; |
|
|
504 |
position: absolute; |
|
|
505 |
top: -5px; |
|
|
506 |
} |
|
|
507 |
|
|
|
508 |
</style> |
|
|
509 |
<!-- In order for the script to run, this HTML markup must be nested in a <div class="example"> --> |
|
|
510 |
<div id="mygraphiccontainer"></div> <!-- Container for the preview of the curve line --> |
|
|
511 |
<div id="info"> |
|
|
512 |
<button id="btn-animate" class='yui3-button'>Animate On Path</button> |
|
|
513 |
<p>To see anim along a path, click the button above, or drag point 0, 1, 2, or 3.</p> |
|
|
514 |
<p>The X and Y of point 0 is <code>[<span class="arr0">92,103</span>]</code></p> |
|
|
515 |
<p>The X and Y of 1, 2, and 3, are the sub-arrays in array value of "curve:" in the anim.set()</p> |
|
|
516 |
<p><code>anim.set('to', {curve: [ |
|
|
517 |
[<span class="arr1">147,317</span>], |
|
|
518 |
[<span class="arr2">532,179</span>], |
|
|
519 |
[<span class="arr3">538,288</span>] ]});</code></p> |
|
|
520 |
</div> |
|
|
521 |
<div id="dot-0" class="dot zero"> |
|
|
522 |
<div class="point"></div> |
|
|
523 |
<div class="fat-finger" title="Drag to change start point"></div> <!-- Gives the points a larger target area to drag --> |
|
|
524 |
<div class="number-label">0</div> |
|
|
525 |
</div> |
|
|
526 |
<div id="dot-1" class="dot one"> |
|
|
527 |
<div class="point"></div> |
|
|
528 |
<div class="fat-finger" title="Drag to change path"></div> |
|
|
529 |
<div class="number-label">1</div> |
|
|
530 |
</div> |
|
|
531 |
<div id="dot-2" class="dot two"> |
|
|
532 |
<div class="point"></div> |
|
|
533 |
<div class="fat-finger" title="Drag to change path"></div> |
|
|
534 |
<div class="number-label">2</div> |
|
|
535 |
</div> |
|
|
536 |
<div id="dot-3" class="dot three"> |
|
|
537 |
<div class="point"></div> |
|
|
538 |
<div class="fat-finger" title="Drag to change end point"></div> |
|
|
539 |
<div class="number-label">3</div> |
|
|
540 |
</div> |
|
|
541 |
<div id="demo">A</div> |
|
|
542 |
|
|
|
543 |
<script> |
|
|
544 |
YUI().use('anim', 'dd-drag', 'graphics', 'cssbutton', function(Y){ |
|
|
545 |
|
|
|
546 |
var mygraphic = new Y.Graphic({render:"#mygraphiccontainer"}), |
|
|
547 |
origin = Y.one('.example'), // The XY values for the animation are based on the upper-left corner of this element |
|
|
548 |
demoA = Y.one('#demo'), // The animated element |
|
|
549 |
dotList = Y.all('.dot'); |
|
|
550 |
// Draggable points |
|
|
551 |
dot0 = Y.one('#dot-0'), dot1 = Y.one('#dot-1'), dot2 = Y.one('#dot-2'), dot3 = Y.one('#dot-3'), |
|
|
552 |
|
|
|
553 |
// Array of XY arrays of draggable points |
|
|
554 |
arrDot = [ |
|
|
555 |
[parseInt(dotList.item(0).getStyle('left'), 10), parseInt(dotList.item(0).getStyle('top'), 10)], |
|
|
556 |
[parseInt(dotList.item(1).getStyle('left'), 10), parseInt(dotList.item(1).getStyle('top'), 10)], |
|
|
557 |
[parseInt(dotList.item(2).getStyle('left'), 10), parseInt(dotList.item(2).getStyle('top'), 10)], |
|
|
558 |
[parseInt(dotList.item(3).getStyle('left'), 10), parseInt(dotList.item(3).getStyle('top'), 10)] |
|
|
559 |
], |
|
|
560 |
|
|
|
561 |
// Make points draggable |
|
|
562 |
dd0 = new Y.DD.Drag({ |
|
|
563 |
node: '#dot-0' |
|
|
564 |
}), |
|
|
565 |
dd1 = new Y.DD.Drag({ |
|
|
566 |
node: '#dot-1' |
|
|
567 |
}), |
|
|
568 |
dd2 = new Y.DD.Drag({ |
|
|
569 |
node: '#dot-2' |
|
|
570 |
}), |
|
|
571 |
dd3 = new Y.DD.Drag({ |
|
|
572 |
node: '#dot-3' |
|
|
573 |
}); |
|
|
574 |
|
|
|
575 |
// Puts current XY values of points into displayed code snippet |
|
|
576 |
var updateCodeSnippetValues = function(){ |
|
|
577 |
Y.one('.arr0').setHTML(arrDot[0][0] + ',' + arrDot[0][1]); // Start |
|
|
578 |
Y.one('.arr1').setHTML(arrDot[1][0] + ',' + arrDot[1][1]); // Control point 1 |
|
|
579 |
Y.one('.arr2').setHTML(arrDot[2][0] + ',' + arrDot[2][1]); // Control point 2 |
|
|
580 |
Y.one('.arr3').setHTML(arrDot[3][0] + ',' + arrDot[3][1]); // End |
|
|
581 |
} |
|
|
582 |
|
|
|
583 |
/** |
|
|
584 |
* Stops the animation |
|
|
585 |
* Updates the array of point XY values |
|
|
586 |
* Updates the curve preview |
|
|
587 |
* Updates displayed XY point values in code snippet |
|
|
588 |
*/ |
|
|
589 |
var dotDragHandler = function(dot){ |
|
|
590 |
Y.Anim.stop(); |
|
|
591 |
arrDot[dotList.indexOf(dot)] = [parseInt(dot.getStyle('left'), 10), parseInt(dot.getStyle('top'), 10)]; |
|
|
592 |
drawCurve(); |
|
|
593 |
updateCodeSnippetValues(); |
|
|
594 |
} |
|
|
595 |
|
|
|
596 |
dd0.on('drag:drag', function(e){ |
|
|
597 |
dotDragHandler(this.get('dragNode')); |
|
|
598 |
}); |
|
|
599 |
dd1.on('drag:drag', function(e){ |
|
|
600 |
dotDragHandler(this.get('dragNode')); |
|
|
601 |
}); |
|
|
602 |
dd2.on('drag:drag', function(e){ |
|
|
603 |
dotDragHandler(this.get('dragNode')); |
|
|
604 |
}); |
|
|
605 |
dd3.on('drag:drag', function(e){ |
|
|
606 |
dotDragHandler(this.get('dragNode')); |
|
|
607 |
}); |
|
|
608 |
|
|
|
609 |
// button handler |
|
|
610 |
Y.one('#btn-animate').on('click', function(){ |
|
|
611 |
Y.Anim.stop(); |
|
|
612 |
setTimeout(startAnim, 500); |
|
|
613 |
}); |
|
|
614 |
|
|
|
615 |
Y.all('.dot').on('mouseup', function(e){ |
|
|
616 |
setTimeout(startAnim, 500); |
|
|
617 |
}); |
|
|
618 |
|
|
|
619 |
// Create the animation instance |
|
|
620 |
var anim = new Y.Anim({ |
|
|
621 |
node: demoA, |
|
|
622 |
duration: 1.5, |
|
|
623 |
easing: Y.Easing.easeOut |
|
|
624 |
}); |
|
|
625 |
|
|
|
626 |
/** |
|
|
627 |
* Sets the anim curve values with the XY values from the arrDot array |
|
|
628 |
* Adds the origin offset values because anim works off page coordinates |
|
|
629 |
*/ |
|
|
630 |
var startAnim = function(e){ |
|
|
631 |
var oX = origin.getX(), |
|
|
632 |
oY = origin.getY(); |
|
|
633 |
|
|
|
634 |
// Reset the animated element to the start position. |
|
|
635 |
// This is needed for running the animation multiple times |
|
|
636 |
demoA.setStyles({'left':arrDot[0][0], 'top':arrDot[0][1]}); |
|
|
637 |
|
|
|
638 |
anim.set('to', { |
|
|
639 |
curve: [ [(arrDot[1][0] + oX), (arrDot[1][1] + oY)], [ (arrDot[2][0] + oX), (arrDot[2][1] + oY) ], [ (arrDot[3][0] + oX), (arrDot[3][1] + oY) ] ] |
|
|
640 |
}); |
|
|
641 |
anim.run(); |
|
|
642 |
}; |
|
|
643 |
|
|
|
644 |
// Adds a YUI Graphics path shape to the Graphics instance mygraphic |
|
|
645 |
var animPath = mygraphic.addShape({ |
|
|
646 |
type: "path", |
|
|
647 |
stroke: { |
|
|
648 |
weight: 4, |
|
|
649 |
color: "#aabbcc" |
|
|
650 |
} |
|
|
651 |
}); |
|
|
652 |
|
|
|
653 |
// Draw a preview curve with the Graphics animPath shape to match the Anim curve |
|
|
654 |
var drawCurve = function(){ |
|
|
655 |
animPath.clear(); |
|
|
656 |
animPath.moveTo(arrDot[0][0],arrDot[0][1]); |
|
|
657 |
animPath.curveTo(arrDot[1][0],arrDot[1][1], arrDot[2][0],arrDot[2][1], arrDot[3][0],arrDot[3][1]); |
|
|
658 |
animPath.end(); |
|
|
659 |
} |
|
|
660 |
|
|
|
661 |
drawCurve(); // Initial drawing of the preview curve |
|
|
662 |
updateCodeSnippetValues(); // Initial setting of code snippet XY values |
|
|
663 |
|
|
|
664 |
}); |
|
|
665 |
|
|
|
666 |
</script></pre> |
|
|
667 |
|
|
|
668 |
</div> |
|
|
669 |
</div> |
|
|
670 |
</div> |
|
|
671 |
|
|
|
672 |
<div class="yui3-u-1-4"> |
|
|
673 |
<div class="sidebar"> |
|
|
674 |
|
|
|
675 |
|
|
|
676 |
|
|
|
677 |
<div class="sidebox"> |
|
|
678 |
<div class="hd"> |
|
|
679 |
<h2 class="no-toc">Examples</h2> |
|
|
680 |
</div> |
|
|
681 |
|
|
|
682 |
<div class="bd"> |
|
|
683 |
<ul class="examples"> |
|
|
684 |
|
|
|
685 |
|
|
|
686 |
<li data-description="Creating and using a simple animation."> |
|
|
687 |
<a href="basic.html">Basic Animation</a> |
|
|
688 |
</li> |
|
|
689 |
|
|
|
690 |
|
|
|
691 |
|
|
|
692 |
<li data-description="The Animation Utility allows you to implement 'easing effects' — for example, when an animation gradually slows down as it nears completion, that's an easing effect known as 'ease-in'. This example shows you how to use easing effects with your animations."> |
|
|
693 |
<a href="easing.html">Easing Effects</a> |
|
|
694 |
</li> |
|
|
695 |
|
|
|
696 |
|
|
|
697 |
|
|
|
698 |
<li data-description="Color animations can be effective indicators of state during the lifespan of a dynamic page. This example shows you how to animate color attributes of an element."> |
|
|
699 |
<a href="colors.html">Animating Colors</a> |
|
|
700 |
</li> |
|
|
701 |
|
|
|
702 |
|
|
|
703 |
|
|
|
704 |
<li data-description="The direction attribute can be used to reverse the animation on alternate iterations."> |
|
|
705 |
<a href="alt-iterations.html">Alternating Iterations</a> |
|
|
706 |
</li> |
|
|
707 |
|
|
|
708 |
|
|
|
709 |
|
|
|
710 |
<li data-description="This example shows you how to animate the xy coordinates of an element."> |
|
|
711 |
<a href="anim-xy.html">Animating XY Position</a> |
|
|
712 |
</li> |
|
|
713 |
|
|
|
714 |
|
|
|
715 |
|
|
|
716 |
<li data-description="This example demonstrates animating an element along a curved path using bezier control points."> |
|
|
717 |
<a href="curve.html">Animating Along a Curved Path</a> |
|
|
718 |
</li> |
|
|
719 |
|
|
|
720 |
|
|
|
721 |
|
|
|
722 |
<li data-description="The reverse attribute allows you to change the run direction of an animation."> |
|
|
723 |
<a href="reverse.html">Reversing an Animation</a> |
|
|
724 |
</li> |
|
|
725 |
|
|
|
726 |
|
|
|
727 |
|
|
|
728 |
<li data-description="This example demonstrates how to use the end event."> |
|
|
729 |
<a href="end-event.html">Using the End Event</a> |
|
|
730 |
</li> |
|
|
731 |
|
|
|
732 |
|
|
|
733 |
|
|
|
734 |
|
|
|
735 |
|
|
|
736 |
|
|
|
737 |
|
|
|
738 |
|
|
|
739 |
</ul> |
|
|
740 |
</div> |
|
|
741 |
</div> |
|
|
742 |
|
|
|
743 |
|
|
|
744 |
|
|
|
745 |
<div class="sidebox"> |
|
|
746 |
<div class="hd"> |
|
|
747 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
748 |
</div> |
|
|
749 |
|
|
|
750 |
<div class="bd"> |
|
|
751 |
<ul class="examples"> |
|
|
752 |
|
|
|
753 |
|
|
|
754 |
|
|
|
755 |
|
|
|
756 |
|
|
|
757 |
|
|
|
758 |
|
|
|
759 |
|
|
|
760 |
|
|
|
761 |
|
|
|
762 |
|
|
|
763 |
|
|
|
764 |
|
|
|
765 |
|
|
|
766 |
|
|
|
767 |
|
|
|
768 |
|
|
|
769 |
|
|
|
770 |
<li data-description="Working with multiple YUI instances."> |
|
|
771 |
<a href="../yui/yui-multi.html">Multiple Instances</a> |
|
|
772 |
</li> |
|
|
773 |
|
|
|
774 |
|
|
|
775 |
|
|
|
776 |
<li data-description="Shows how to create a simple plugin to animate the Overlay's movement and visibility."> |
|
|
777 |
<a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a> |
|
|
778 |
</li> |
|
|
779 |
|
|
|
780 |
|
|
|
781 |
|
|
|
782 |
<li data-description="How to make an animated node a Drop target."> |
|
|
783 |
<a href="../dd/anim-drop.html">Animated Drop Targets</a> |
|
|
784 |
</li> |
|
|
785 |
|
|
|
786 |
|
|
|
787 |
</ul> |
|
|
788 |
</div> |
|
|
789 |
</div> |
|
|
790 |
|
|
|
791 |
</div> |
|
|
792 |
</div> |
|
|
793 |
</div> |
|
|
794 |
</div> |
|
|
795 |
|
|
|
796 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
797 |
<script>prettyPrint();</script> |
|
|
798 |
|
|
|
799 |
<script> |
|
|
800 |
YUI.Env.Tests = { |
|
|
801 |
examples: [], |
|
|
802 |
project: '../assets', |
|
|
803 |
assets: '../assets/anim', |
|
|
804 |
name: 'curve', |
|
|
805 |
title: 'Animating Along a Curved Path', |
|
|
806 |
newWindow: '', |
|
|
807 |
auto: false |
|
|
808 |
}; |
|
|
809 |
YUI.Env.Tests.examples.push('basic'); |
|
|
810 |
YUI.Env.Tests.examples.push('easing'); |
|
|
811 |
YUI.Env.Tests.examples.push('colors'); |
|
|
812 |
YUI.Env.Tests.examples.push('alt-iterations'); |
|
|
813 |
YUI.Env.Tests.examples.push('anim-xy'); |
|
|
814 |
YUI.Env.Tests.examples.push('curve'); |
|
|
815 |
YUI.Env.Tests.examples.push('reverse'); |
|
|
816 |
YUI.Env.Tests.examples.push('end-event'); |
|
|
817 |
YUI.Env.Tests.examples.push('yui-multi'); |
|
|
818 |
YUI.Env.Tests.examples.push('overlay-anim-plugin'); |
|
|
819 |
YUI.Env.Tests.examples.push('anim-drop'); |
|
|
820 |
|
|
|
821 |
</script> |
|
|
822 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
823 |
|
|
|
824 |
|
|
|
825 |
|
|
|
826 |
</body> |
|
|
827 |
</html> |