-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarousel.js
229 lines (186 loc) · 6.84 KB
/
carousel.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
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
(function($) {
var Utils = {
clamp: function(num, min, max) { // Returns num in the range [min, max]
return Math.min(Math.max(num, min), max);
},
lerp: function(source, dest, amount) { // Linearly interpolates source to dest by amount
return source + (dest - source) * amount;
},
toDegrees: function(radians) { // Returns the given radians as degrees
return radians * (180 / Math.PI);
},
toRadians: function(degrees) { // Returns the given degrees as radians
return degrees * (Math.PI / 180);
},
wrap: function(num, value) { // Returns num in the range [-value, value);
num = num % (value * 2);
if(num >= value)
num -= value * 2;
else if(num < -value)
num += value * 2;
return num;
},
};
var settings = {
container: null,
// Speed settings (all in degrees/second)
idleSpeed: 25,
idleSpeedAcceleration: 0.05,
maxSpeed: 90,
// -- Target centring
targetCentringAcceleration: 0.15,
targetCentringDrag: 0.85,
// -- Mouse
mouseAcceleration: 0.25,
mouseSpeed: 5,
// Z-depth
scaleMin: 0.5,
scaleMax: 1.0,
zIndexMax: 1000,
// Peek effect
verticalShiftMax: 150, // in pixels
// Link callback
linkCallback: null,
};
var running = false;
var elementCount = -1;
var verticalShift = 0;
var speed = 0;
var mouseDown = false;
var currMousePos = { x: -1, y: -1 };
var prevMousePos = { x: -1, y: -1 };
var currTime = -1;
var prevTime = -1;;
var targetElement = null;
function getInterval(position) {
return Math.sin(Utils.toRadians(position)) / 2.0 + 0.5;
}
function getInterpolatedInterval(position, min, max) {
return min + (max - min) * getInterval(position);
}
function initChild() {
var self = $(this);
self.click(handleClick);
self.data("width", self.width());
self.data("height", self.height());
self.data("position", self.index() / elementCount * 360);
}
function stepChild() {
var self = $(this);
self.data("position", (self.data("position") + speed * (currTime - prevTime) * 0.001) % 360);
positionChild(self);
}
function positionChild(child) {
var position = child.data("position");
var scale = getInterpolatedInterval(position, settings.scaleMin, settings.scaleMax);
var halfWidth = child.data("width") / 2;
var halfHeight = child.data("height") / 2;
child.css({
"left": (getInterpolatedInterval(position + 90, 0, settings.container.width()) - halfWidth * scale) + "px",
"top": (getInterval(position) * verticalShift - halfHeight + settings.container.height() / 2) + "px",
"width": child.data("width") * scale + "px",
"height": child.data("height") * scale + "px",
"z-index": Math.round(getInterval(position) * settings.zIndexMax),
})
}
function handleClick(e) {
var self = $(this);
if(self.is(targetElement) && self.attr("href") && settings.linkCallback) {
if(!settings.linkCallback(self.attr("href")))
e.preventDefault();
}
else {
e.preventDefault();
targetElement = self;
}
}
function handleMouseMove(e) {
currMousePos.x = e.pageX;
currMousePos.y = e.pageY;
}
function handleMouseDown(e) {
mouseDown = true;
}
function handleMouseUp(e) {
mouseDown = false;
}
function updateSpeed() {
if(mouseDown) {
speed = Utils.lerp(speed, (prevMousePos.x - currMousePos.x) * settings.mouseSpeed, settings.mouseAcceleration);
}
else if(targetElement != null) {
var direction = Utils.wrap(targetElement.data("position") - 90, 180);
if(Math.abs(direction) < 0.1) {
speed = 0;
}
else {
speed *= settings.targetCentringDrag;
speed = Utils.lerp(speed, speed - direction, settings.targetCentringAcceleration);
}
}
else {
speed = Utils.lerp(speed, (speed < 0) ? -settings.idleSpeed : settings.idleSpeed, settings.idleSpeedAcceleration);
}
}
function updateTarget() {
if(targetElement == null)
return;
var direction = Utils.wrap(targetElement.data("position") - 90, 180);
if((mouseDown && Math.abs(direction) > 10) || (direction * speed >= 0 && Math.abs(speed) > 10)) {
targetElement = null;
}
}
function update() {
prevTime = currTime;
currTime = Date.now();
if(mouseDown)
verticalShift = Utils.clamp(verticalShift + currMousePos.y - prevMousePos.y, 0, settings.verticalShiftMax);
updateSpeed();
updateTarget();
prevMousePos.x = currMousePos.x;
prevMousePos.y = currMousePos.y;
if(speed != 0)
settings.container.children().each(stepChild);
if(running)
window.requestAnimationFrame(update);
}
$.fn.carousel = function(action, options) {
if(action === "start") {
running = true;
settings.container = this;
settings = $.extend(settings, options);
elementCount = this.children().size();
// Events
$(window).mousemove(handleMouseMove).mouseup(handleMouseUp);
this.mousedown(handleMouseDown);
this.children().each(initChild);
this.children().each(function() { positionChild($(this)); });
window.requestAnimationFrame(update);
}
else {
running = false;
}
}
}(jQuery));