-
Notifications
You must be signed in to change notification settings - Fork 0
/
jcp-animator.js
257 lines (214 loc) · 7.02 KB
/
jcp-animator.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Animation handler class
*
* Manages animation events & timing between a Viewport and Layer.
* This level of abstraction allows multiple animations to exist on
* and update a layer simultaneously - these animations may or may not be tied
* to the same input event.
*
* @requires jcparallax.js
* @requires jcp-viewport.js
* @requires jcp-layer.js
* @author Sam Pospischil <[email protected]>
*/
(function($) {
jcparallax.Animator = function(layer, options)
{
var defaults = {
movementRangeX : true, // autodetect
movementRangeY : true,
inputEvent : 'mousemove',
inputHandler : 'mousemove',
animHandler : 'position',
};
this.layer = layer;
this.viewport = layer.viewport;
options = $.extend(true, defaults, options);
// set default range calculation callbacks for builtin animHandlers passed as strings
if (options.movementRangeX === true || options.movementRangeY === true && !$.isFunction(options.animHandler)) {
// infer layer movement range calculators for builtin animation handlers
switch (options.animHandler) {
case 'position':
case 'padding':
case 'margins':
case 'background':
case 'stretch':
switch (options.inputHandler) {
case 'scroll':
options.movementRangeX = jcparallax.Layer.rangeCalculators.scrollWidth;
options.movementRangeY = jcparallax.Layer.rangeCalculators.scrollHeight;
break;
default:
options.movementRangeX = jcparallax.Layer.rangeCalculators.width;
options.movementRangeY = jcparallax.Layer.rangeCalculators.height;
break;
}
break;
case 'textShadow':
options.movementRangeX = jcparallax.Layer.rangeCalculators.fontSize;
options.movementRangeY = jcparallax.Layer.rangeCalculators.lineHeight;
break;
case 'opacity':
options.movementRangeX = [0, 0];
options.movementRangeY = jcparallax.Layer.rangeCalculators.opacity;
break;
}
}
if (typeof options.movementRangeX == 'string') {
options.movementRangeX = jcparallax.Layer.rangeCalculators[options.movementRangeX];
}
if (typeof options.movementRangeY == 'string') {
options.movementRangeY = jcparallax.Layer.rangeCalculators[options.movementRangeY];
}
// interpret animation handler
if ($.isFunction(options.animHandler)) { // custom handler callback
this.animHandler = options.animHandler;
} else { // single callback from the predefined set
this.animHandler = jcparallax.Animator.animHandlers[options.animHandler];
}
this.options = options;
// refresh target element coordinates
this.refreshCoords();
// bind input events
this.bindEvent(options.inputEvent, options.inputHandler);
};
$.extend(jcparallax.Animator.prototype, {
inputEvent : null, // DOM input event this animation is bound to
inputHandler : null, // input event handler callback to output range normalised value
animHandler : null, // animation update handler callback
minX : null,
minY : null,
rangeX : null, // scaling factors for the animation over input 0-1
rangeY : null, // call refreshCoords() to update from the callbacks supplied in options
// last input sampling coordinates
lastSampledX : 0,
lastSampledY : 0,
lastProcessedX : 0,
lastProcessedY : 0,
/**
* Binds the DOM event responsible for handling our updates
* @param {string} eventName name of the DOM event to bind to for updates
* @param {function} handler Callback for handling the event.
* Accepts the bound layer element, x position (0 <= x <= 1), y position and event object as parameters.
*/
bindEvent : function(eventName, handler)
{
var that = this;
eventName += jcparallax.eventNamespace;
// infer handler from predefined set if a string
if (typeof handler == 'string') {
handler = jcparallax.Viewport.inputHandlers[handler];
}
// detach old callback first if present
if (this.inputHandler && this.inputEvent) {
this.viewport.element.off(this.inputEvent, this.inputHandler);
}
// create new callback & bind it to the viewport
this.inputHandler = function(e) {
handler.call(that, that.viewport.element, e);
};
this.inputEvent = eventName;
this.viewport.element.on(eventName, this.inputHandler);
},
/**
* Updates the sampled input position for calculating the effect.
* This method should be called for every event encountered to update
* the X and Y values, which are then applied at the next frame interval.
*/
updateLastSamplePos : function(xVal, yVal)
{
this.lastSampledX = xVal;
this.lastSampledY = yVal;
},
/**
* Refreshes all computed coordinates from our movement range handler
* callbacks after layer DOM element is modified externally.
*/
refreshCoords : function()
{
var xRange, yRange;
if ($.isFunction(this.options.movementRangeX)) {
xRange = this.options.movementRangeX.call(this.layer, this.layer.element, this.viewport);
} else {
xRange = this.options.movementRangeX;
}
if ($.isFunction(this.options.movementRangeY)) {
yRange = this.options.movementRangeY.call(this.layer, this.layer.element, this.viewport);
} else {
yRange = this.options.movementRangeY;
}
this.minX = parseFloat(xRange[0]);
this.rangeX = parseFloat(xRange[1] - xRange[0]);
this.minY = parseFloat(yRange[0]);
this.rangeY = parseFloat(yRange[1] - yRange[0]);
},
/**
* Generate a CSS object for modification of our layer,
* using our last sampled input values or the ones provided.
* Input values should be 0 < x < 1.
*
* @return {object} CSS properties to pass to jQuery .css()
*/
makeCss : function(xVal, yVal)
{
// when positions not passed, just redraw
if (xVal === undefined && yVal === undefined) {
xVal = this.lastSampledX;
yVal = this.lastSampledY;
}
if (xVal == this.lastProcessedX && yVal == this.lastProcessedY) {
return {}; // no change in input
}
this.lastProcessedX = xVal;
this.lastProcessedY = yVal;
return this.animHandler.call(this, xVal, yVal);
}
});
//------------------------------------------------------------------------------
// Layer animation handlers
//------------------------------------------------------------------------------
jcparallax.Animator.animHandlers = {
// standard css attributes - minimal support
position : function(xVal, yVal)
{
return {
left : this.minX + (xVal * this.rangeX),
top : this.minY + (yVal * this.rangeY),
};
},
padding : function(xVal, yVal)
{
},
margins : function(xVal, yVal)
{
},
background : function(xVal, yVal)
{
return {
'background-position' : (this.minX + (xVal * this.rangeX)) + 'px ' + (this.minY + (yVal * this.rangeY)) + 'px',
};
},
stretch : function(xVal, yVal)
{
},
// CSS3 attributes
Xrotate : function(xVal, yVal) // rotate based on X input
{
var css = {};
css[jcparallax.support.transforms] = 'rotate(' + (this.minX + (xVal * this.rangeX)) + 'deg)';
return css;
},
Yrotate : function(xVal, yVal) // rotate based on Y input
{
var css = {};
css[jcparallax.support.transforms] = 'rotate(' + (this.minY + (yVal * this.rangeY)) + 'deg)';
return css;
},
textShadow : function(xVal, yVal)
{
},
opacity : function(xVal, yVal)
{
}
};
})(jQuery);