This repository has been archived by the owner on Nov 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblocks_2.js
237 lines (186 loc) · 5.88 KB
/
blocks_2.js
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
(function() {
"use strict";
angular
.module("AnimationChainsApp", [ 'ng' ])
.service("$timeline", TimelineService )
.controller("AnimationController", AnimationController );
/**
* MainController
* @param $scope
* @param $timeline
* @constructor
*/
function AnimationController($scope, $timeline, $timeout, $q) {
$scope.animating = false;
$scope.progress = init();
$scope.animate = startAnimation;
$scope.reset = resetAnimations;
// ****************************
// Internal Methods
// ****************************
/**
* Reset animations and enable buttons
*/
function resetAnimations() {
var orange = $("div.block.orange");
var purple = $("div.block.purple");
$scope.progress = init();
$scope.elapsedTime = "";
$timeline.reset([orange, purple]);
}
/**
* Start animation sequences
*/
function startAnimation() {
var startedAt = Date.now();
var orange = $("div.block.orange");
var purple = $("div.block.purple");
$scope.animating=true;
$scope.reset();
$timeline.reset([orange, purple]);
// Animation with `purple` delayed 1sec
$q.all([
startChain(orange, "orange"),
startChain(purple, "purple", 1000)
]).then( function(){
// When all done...
$scope.animating=false;
$scope.elapsedTime = (Date.now() - startedAt);
});
/**
* Use $timeline to start the sequence with specific callbacks
* @returns {*} Promise to notify when finished
*/
function startChain(target, name, delay) {
var deferred = $q.defer();
$timeout(function() {
return $timeline.start( target,
function( action ){ $scope.progress[name][action] = "running..."; },
function( action ){
$scope.progress[name][action] = "finished";
if ( action == "fade" ) {
deferred.resolve( action );
}
}
);
},delay || 0, false);
return deferred.promise;
}
}
/**
* Initialize a `progress` structure
* @returns {{purple: (void|Object|*), orange: (void|Object|*)}}
*/
function init() {
var steps = {move : "--", rotate: "--", scale: "--", fade: "--"};
return {
purple : angular.extend({},steps),
orange : angular.extend({},steps)
};
}
}
/**
* TimelineService that publishes pre-defined TweenMax animation sequences
* @returns {{start: startTimeline, reset: resetTimeline}}
* @constructor
*/
function TimelineService($q, $log, $rootScope) {
var tweenSteps = [
{
name: "move",
tween : function(element, start, done) {
TweenMax.to(element, 1.0, { x: 400, repeat: 1, yoyo: true, onStart: start, onComplete: done });
}
},
{
name: "rotate",
tween : function(element, start, done) {
TweenMax.to(element, 1.0, { delay:0.03, rotation: 180, onStart: start, onComplete: done });
}
},
{
name: "scale",
tween : function(element, start, done) {
TweenMax.to(element, 1.0, { delay:0.03, scaleX: 2, x: 50, onStart: start, onComplete: done });
}
},
{
name: "fade",
tween : function(element, start, done) {
TweenMax.to(element, 1.0, { delay:0.03, autoAlpha: 0.2, scaleX: 1, scaleY: 0.5, onStart: start, onComplete: done });
}
}
];
var tweenReset = function(element) {
TweenMax.set(element, { autoAlpha: 1, scale: 1, rotation: 0, x: 0 });
};
// Publish API
return {
start : startTimeline,
reset : resetTimeline
};
/**
* Reset the target(s) for future animations...
* @param target
*/
function resetTimeline(target) {
var targets = angular.isArray(target) ? target : [target];
targets.forEach(function(it){
tweenReset(it);
});
}
/**
* Start the timeline (sequence of tweens) on the target element
* @returns {*} Promise
*/
function startTimeline( target, start, done ) {
var animationSteps = [].concat(tweenSteps);
start = start || angular.noop;
done = done || angular.noop;
return createChain(target);
// ********************************
// Chain Steps
// ********************************
/**
* Create and auto-start a sequential promise
* chain of tweens
*/
function createChain(target){
// Create sequence of animations
return animationSteps.reduce(function(promise, step){
return promise.then(function(){
return buildStep(step, target);
});
}, $q.when(true));
}
/**
* Run the tween step with start & done callbacks
*/
function buildStep(step, target) {
var deferred = $q.defer();
var startFn = function(start) {
return function() {
// use $apply() since animation callbacks are out-of-scope
$rootScope.$apply(function(){
$log.debug("TweenMax('{0}' on '{1}') started.".supplant([step.name, target.selector]));
start(step.name);
});
};
};
var doneFn = function(done) {
return function() {
// use $apply() since animation callbacks are out-of-scope
$rootScope.$apply(function(){
$log.debug("TweenMax('{0}' on '{1}') finished.".supplant([step.name, target.selector]));
done(step.name);
});
deferred.resolve(step.name);
}
};
// Start the tweening with head-hooks to the callbacks...
step.tween(target, startFn(start), doneFn(done) );
return deferred.promise;
}
}
}
})();