-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.html
320 lines (274 loc) · 8.56 KB
/
animate.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>ANIMATION</title>
<meta name="description" content="Dev Bootcamp's Orange Jasmine Challenge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="stylesheets/normalize.min.css">
<link rel="stylesheet" href="stylesheets/main.css">
<title>Connect 4</title>
<link rel="stylesheet" href="stylesheets/normalize.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div class="example">
<button id="btn-animate" class='yui3-button'>Animate On Path</button>
<div id="demo">A</div>
</div>
</body>
</html>
<style>
.example {
position: relative;
padding: 0;
margin: 0;
width: 100%;
}
.example div, .example p, .example span{
padding: 0;
margin: 0;
}
#mygraphiccontainer {
width: 650px;
height: 400px;
}
.example #demo {
position: absolute;
left: 92px;
top: 103px;
background-color: #f00;
text-align: center;
line-height: 1.5em;
border: solid 1px #cc0000;
border-radius: 0;
-moz-box-shadow: 3px 3px 7px rgba(0,0,0,0.4);
-webkit-box-shadow: 3px 3px 7px rgba(0,0,0,0.4);
width: 20px;
height: 20px;
}
.dot {
position: absolute;
color: #396491;
font-size: 20px;
height: 0;
line-height: 12px;
opacity: 0.8;
width: 0;
cursor: move;
}
.dot .point {
background-color: #000;
position: absolute;
left: -3px;
top: -3px;
width: 6px;
height: 6px;
border-radius: 3px;
font-size: 0px;
}
.number-label {
position: absolute;
top: -1em;
left: -0.9em;
width: 1em;
height: 1em;
line-height: 1em;
}
/* Gives the points a larger target area to drag */
.dot .fat-finger {
position: absolute;
top: -30px; /* minus half the fat-finger height */
left: -30px;
width: 60px;
height: 60px;
-moz-border-radius: 30px;
-webkit-border-radius: 30px;
border-radius: 30px;
background-color: #abc;
opacity: 0.3;
filter: alpha (opacity = 30);
}
#dot-3 .fat-finger {
background-color: #f80;
opacity: 0.4;
}
#dot-0 .fat-finger {
background-color: #8DAA16;
opacity: 0.4;
}
#dot-0, #demo {
top: 103px;
left: 92px;
}
#dot-1 {
top: 317px;
left: 147px;
}
#dot-2 {
top: 179px;
left: 532px;
}
#dot-3 {
left: 538px;
top: 288px;
}
#info {
position: absolute;
width: 450px;
height: 15em;
right: 1em;
top: 1em;
}
code span {
color: #CC3300;
}
.point {
font-size: 24px;
left: 5px;
position: absolute;
top: -5px;
}
</style>
<!-- In order for the script to run, this HTML markup must be nested in a <div class="example"> -->
<div id="mygraphiccontainer"></div> <!-- Container for the preview of the curve line -->
<div id="info">
<button id="btn-animate" class='yui3-button'>Animate On Path</button>
<p>To see anim along a path, click the button above, or drag point 0, 1, 2, or 3.</p>
<p>The X and Y of point 0 is <code>[<span class="arr0">92,103</span>]</code></p>
<p>The X and Y of 1, 2, and 3, are the sub-arrays in array value of "curve:" in the anim.set()</p>
<p><code>anim.set('to', {curve: [
[<span class="arr1">147,317</span>],
[<span class="arr2">532,179</span>],
[<span class="arr3">538,288</span>] ]});</code></p>
</div>
<div id="dot-0" class="dot zero">
<div class="point"></div>
<div class="fat-finger" title="Drag to change start point"></div> <!-- Gives the points a larger target area to drag -->
<div class="number-label">0</div>
</div>
<div id="dot-1" class="dot one">
<div class="point"></div>
<div class="fat-finger" title="Drag to change path"></div>
<div class="number-label">1</div>
</div>
<div id="dot-2" class="dot two">
<div class="point"></div>
<div class="fat-finger" title="Drag to change path"></div>
<div class="number-label">2</div>
</div>
<div id="dot-3" class="dot three">
<div class="point"></div>
<div class="fat-finger" title="Drag to change end point"></div>
<div class="number-label">3</div>
</div>
<div id="demo">A</div>
<script>
YUI().use('anim', 'dd-drag', 'graphics', 'cssbutton', function(Y){
var mygraphic = new Y.Graphic({render:"#mygraphiccontainer"}),
origin = Y.one('.example'), // The XY values for the animation are based on the upper-left corner of this element
demoA = Y.one('#demo'), // The animated element
dotList = Y.all('.dot');
// Draggable points
dot0 = Y.one('#dot-0'), dot1 = Y.one('#dot-1'), dot2 = Y.one('#dot-2'), dot3 = Y.one('#dot-3'),
// Array of XY arrays of draggable points
arrDot = [
[parseInt(dotList.item(0).getStyle('left'), 10), parseInt(dotList.item(0).getStyle('top'), 10)],
[parseInt(dotList.item(1).getStyle('left'), 10), parseInt(dotList.item(1).getStyle('top'), 10)],
[parseInt(dotList.item(2).getStyle('left'), 10), parseInt(dotList.item(2).getStyle('top'), 10)],
[parseInt(dotList.item(3).getStyle('left'), 10), parseInt(dotList.item(3).getStyle('top'), 10)]
],
// Make points draggable
dd0 = new Y.DD.Drag({
node: '#dot-0'
}),
dd1 = new Y.DD.Drag({
node: '#dot-1'
}),
dd2 = new Y.DD.Drag({
node: '#dot-2'
}),
dd3 = new Y.DD.Drag({
node: '#dot-3'
});
// Puts current XY values of points into displayed code snippet
var updateCodeSnippetValues = function(){
Y.one('.arr0').setHTML(arrDot[0][0] + ',' + arrDot[0][1]); // Start
Y.one('.arr1').setHTML(arrDot[1][0] + ',' + arrDot[1][1]); // Control point 1
Y.one('.arr2').setHTML(arrDot[2][0] + ',' + arrDot[2][1]); // Control point 2
Y.one('.arr3').setHTML(arrDot[3][0] + ',' + arrDot[3][1]); // End
}
/**
* Stops the animation
* Updates the array of point XY values
* Updates the curve preview
* Updates displayed XY point values in code snippet
*/
var dotDragHandler = function(dot){
Y.Anim.stop();
arrDot[dotList.indexOf(dot)] = [parseInt(dot.getStyle('left'), 10), parseInt(dot.getStyle('top'), 10)];
drawCurve();
updateCodeSnippetValues();
}
dd0.on('drag:drag', function(e){
dotDragHandler(this.get('dragNode'));
});
dd1.on('drag:drag', function(e){
dotDragHandler(this.get('dragNode'));
});
dd2.on('drag:drag', function(e){
dotDragHandler(this.get('dragNode'));
});
dd3.on('drag:drag', function(e){
dotDragHandler(this.get('dragNode'));
});
// button handler
Y.one('#btn-animate').on('click', function(){
Y.Anim.stop();
setTimeout(startAnim, 500);
});
Y.all('.dot').on('mouseup', function(e){
setTimeout(startAnim, 500);
});
// Create the animation instance
var anim = new Y.Anim({
node: demoA,
duration: 1.5,
easing: Y.Easing.easeOut
});
/**
* Sets the anim curve values with the XY values from the arrDot array
* Adds the origin offset values because anim works off page coordinates
*/
var startAnim = function(e){
var oX = origin.getX(),
oY = origin.getY();
// Reset the animated element to the start position.
// This is needed for running the animation multiple times
demoA.setStyles({'left':arrDot[0][0], 'top':arrDot[0][1]});
anim.set('to', {
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) ] ]
});
anim.run();
};
// Adds a YUI Graphics path shape to the Graphics instance mygraphic
var animPath = mygraphic.addShape({
type: "path",
stroke: {
weight: 4,
color: "#aabbcc"
}
});
// Draw a preview curve with the Graphics animPath shape to match the Anim curve
var drawCurve = function(){
animPath.clear();
animPath.moveTo(arrDot[0][0],arrDot[0][1]);
animPath.curveTo(arrDot[1][0],arrDot[1][1], arrDot[2][0],arrDot[2][1], arrDot[3][0],arrDot[3][1]);
animPath.end();
}
drawCurve(); // Initial drawing of the preview curve
updateCodeSnippetValues(); // Initial setting of code snippet XY values
});
</script>
</body>
</html>