-
Notifications
You must be signed in to change notification settings - Fork 65
/
kenburns.js
423 lines (338 loc) · 15 KB
/
kenburns.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Jquery Kenburns Image Gallery
* Original author: John [at] Toymakerlabs
* Further changes, comments: [at]Toymakerlabs
* Licensed under the MIT license
*
* Copyright (c) 2013 ToymakerLabs
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: The above copyright notice and this
* permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
;(function ( $, window, document, undefined ) {
/* Plugin Parameters
------------------------------------------------------------------------------------------------- */
var pluginName = 'Kenburns',
defaults = {
images:[],
duration:400,
fadeSpeed:500,
scale:1,
ease3d:'cubic-bezier(.81, 0, .26, 1)',
onLoadingComplete:function(){},
onSlideComplete:function(){},
onListComplete:function(){},
getSlideIndex:function(){
return currentSlide;
}
};
var imagesObj = {};
var currentSlide = 0;
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this.maxSlides = this.options.images.length;
this.init();
}
/* 1. Initialization
------------------------------------------------------------------------------------------------- */
/**
* Init
* Initial setup - dermines width, height, and adds the loading icon.
*/
Plugin.prototype.init = function () {
var list = this.options.images;
var that = this;
this.width = $(this.element).width();
this.height = $(this.element).height();
this.has3d = has3DTransforms();
for (i in list) {
imagesObj["image"+i] = {};
imagesObj["image"+i].loaded = false;
this.attachImage(list[i], "image"+i , i);
}
var loader = $('<div/>');
loader.addClass('loader');
loader.css({'position':'absolute','z-index':10000});
$(this.element).prepend(loader);
};
/* 2. Loading and Setup
------------------------------------------------------------------------------------------------- */
/**
* Attach image
* creates a wrapper div for the image along with the image tag. The reason for the additional
* wrapper is that we are transitioning multiple properties at the same time: scale, position, and
* opacity. But we want opacity to finish first. This function also determines if the browser
* has 3d transform capabilities and initializes the starting CSS values.
*/
Plugin.prototype.attachImage = function(url,alt_text,index) {
var that = this;
//put the image in an empty div to separate the animation effects of fading and moving
var wrapper = $('<div/>');
wrapper.attr('class','kb-slide');
wrapper.css({'opacity':0});
var img = $("<img />");
img.attr('src', url);
img.attr('alt', alt_text);
wrapper.html(img);
//First check if the browser supports 3D transitions, initialize the CSS accordingly
if(this.has3d) {
img.css({'-webkit-transform-origin':'left top'});
img.css({'-moz-transform-origin':'left top'});
img.css({'-webkit-transform':'scale('+that.options.scale+') translate3d(0,0,0)'});
img.css({'-moz-transform':'scale('+that.options.scale+') translate3d(0,0,0)'});
}
//Switch the transition to the 3d version if it does exist
this.doTransition = (this.has3d)?this.transition3d:this.transition;
//set up the image OBJ parameters - used to track loading and initial dimensions
img.load(function() {
imagesObj["image"+index].element = this;
imagesObj["image"+index].loaded = true;
imagesObj["image"+index].width = $(this).width();
imagesObj["image"+index].width = $(this).height();
that.insertAt(index,wrapper);
that.resume(index);
});
}
/**
* Resume
* Resume will continue the transition after the stalled image loads
* it also fires the complete action when the series of images finishes loading
*/
Plugin.prototype.resume = function(index){
//first image has loaded
if(index == 0) {
this.startTransition(0);
$(this.element).find('.loader').hide();
}
//if the next image hasnt loaded yet, but the transition has started,
// this will match the image index to the image holding the transition.
// it will then resume the transition.
if(index == this.holdup) {
$('#status').html("");
$(this.element).find('.loader').hide();
this.startTransition(this.holdup);
}
//if the last image in the set has loaded, add the images in order
if(this.checkLoadProgress() == true) {
//reset the opacities and z indexes except the last and first images
$(this.element).find('.stalled').each(function(){
$(this).css({'opacity':1,'z-index':1});
$(this).removeClass('stalled');
});
//fire the complete thing
this.options.onLoadingComplete();
}
}
//if any of the slides are not loaded, the set has not finished loading.
Plugin.prototype.checkLoadProgress = function() {
var imagesLoaded = true;
for(i=0;i<this.maxSlides;i++){
if (imagesObj["image"+i].loaded == false){
imagesLoaded = false;
}
}
return imagesLoaded;
}
/**
* Wait
* Stops the transition interval, shows the loader and
* applies the stalled class to the visible image.
*/
Plugin.prototype.wait = function() {
clearInterval(this.interval);
$('#status').html("loading");
$(this.element).find('.loader').show();
var image = imagesObj["image"+(currentSlide-1)].element;
$(image).parent().stop(true,true);
$(image).parent().addClass('stalled');
}
/* 3. Transitions and Movement
------------------------------------------------------------------------------------------------- */
/**
* startTransition
* Begins the Gallery Transition and tracks the current slide
* Also manages loading - if the interval encounters a slide
* that has not loaded, the transition pauses.
*/
Plugin.prototype.startTransition = function(start_index) {
var that = this;
currentSlide = start_index; //current slide
that.doTransition();
this.interval = setInterval(function(){
//Advance the current slide
if(currentSlide < that.maxSlides-1){
currentSlide++;
}else {
currentSlide = 0;
}
//Check if the next slide is loaded. If not, wait.
if(imagesObj["image"+currentSlide].loaded == false){
that.holdup = currentSlide;
that.wait();
//if the next slide is loaded, go ahead and do the transition.
}else {
that.doTransition();
}
},this.options.duration);
}
/**
* chooseCorner
* This function chooses a random start corner and a random end corner
* that is different from the start. This gives a random direction effect
* it returns coordinates used by the transition functions.
*/
Plugin.prototype.chooseCorner = function() {
var scale = this.options.scale;
var image = imagesObj["image"+currentSlide].element;
var ratio = image.height/image.width;
var sw = Math.floor($(this.element).width()*(1/scale));
var sh = Math.floor($(this.element).width()*ratio*(1/scale));
$(image).width(sw);
$(image).height(sh);
var w = $(this.element).width();
var h = $(this.element).height();
//console.log(sw+ ", " + this.width);
var corners = [
{x:0,y:0},
{x:1,y:0},
{x:0,y:1},
{x:1,y:1}
];
//Pick the first corner. Remove it from the array
var choice = Math.floor(Math.random()*4);
var start = corners[choice];
//Pick the second corner from the subset
corners.splice(choice,1);
var end = corners[Math.floor(Math.random()*3)];
//build the new coordinates from the chosen coordinates
var coordinates = {
startX: start.x * (w - sw*scale) ,
startY: start.y * (h - sh*scale),
endX: end.x * (w - sw),
endY: end.y * (h - sh)
}
//
// console.log(coordinates.startX + " , "+coordinates.startY + " , " +coordinates.endX + " , " +coordinates.endY);
return coordinates;
}
/**
* Transiton3D
* Transition3d Function works by setting the webkit and moz translate3d properties. These
* are hardware accellerated and give a very smooth animation. Since only one animation
* can be applied at a time, I wrapped the images in a div. The shorter fade is applied to
* the parent, while the translation and scaling is applied to the image.
*/
Plugin.prototype.transition3d = function () {
var that = this;
var scale = this.options.scale;
var image = imagesObj["image"+currentSlide].element;
var position = this.chooseCorner();
//First clear any existing transition
$(image).css({'-webkit-transition':'none'});
$(image).css({'-moz-transition':'none'});
$(image).css({'-webkit-transform':'scale('+scale+') translate3d('+position.startX+'px,'+position.startY+'px,0)'});
$(image).css({'-moz-transform':'scale('+scale+') translate3d('+position.startX+'px,'+position.startY+'px,0)'});
//Set the wrapper to fully transparent and start it's animation
$(image).parent().css({'opacity':0,'z-index':'3'});
$(image).parent().animate({'opacity':1},that.options.fadeSpeed);
//Add the transition back in
$(image).css({'-webkit-transition':'-webkit-transform '+(that.options.duration+that.options.fadeSpeed)+'ms '+that.options.ease3d});
$(image).css({'-moz-transition':'-moz-transform '+(that.options.duration+that.options.fadeSpeed)+'ms '+that.options.ease3d});
//set the end position and scale, which fires the transition
$(image).css({'-webkit-transform':'scale(1) translate3d('+position.endX+'px,'+position.endY+'px,0)'});
$(image).css({'-moz-transform':'scale(1) translate3d('+position.endX+'px,'+position.endY+'px,0)'});
this.transitionOut();
this.options.onSlideComplete();
}
/**
* Transition
* The regular JQuery animation function. Sets the currentSlide initial scale and position to
* the value from chooseCorner before triggering the animation. It starts the image moving to
* the new position, starts the fade on the wrapper, and delays the fade out animation. Adding
* fadeSpeed to duration gave me a nice crossfade so the image continues to move as it fades out
* rather than just stopping.
*/
Plugin.prototype.transition = function() {
var that = this;
var scale = this.options.scale;
var image = imagesObj["image"+currentSlide].element;
var sw = $(image).width();
var sh = $(image).height();
var position = this.chooseCorner();
$(image).css({'left':position.startX,'top':position.startY,'width':sw*(scale),'height':sh*(scale)});
$(image).animate({'left':position.endX,'top':position.endY,'width':sw,'height':sh}, that.options.duration + that.options.fadeSpeed);
$(image).parent().css({'opacity':0,'z-index':3});
$(image).parent().animate({'opacity':1},that.options.fadeSpeed);
this.transitionOut();
this.options.onSlideComplete();
}
Plugin.prototype.transitionOut = function() {
var that = this;
var image = imagesObj["image"+currentSlide].element;
$(image).parent().delay(that.options.duration).animate({'opacity':0},that.options.fadeSpeed, function(){
$(this).css({'z-index':1});
});
}
/* 4. Utility Functions
------------------------------------------------------------------------------------------------- */
/**
* has3DTransforms
* Tests the browser to determine support for Webkit and Moz Transforms
* Creates an element, translates the element, and tests the values. If the
* values return true, the browser supports 3D transformations.
*/
function has3DTransforms() {
var el = document.createElement('p'),
has3d,
transforms = {
'WebkitTransform':'-webkit-transform',
'MozTransform':'-moz-transform',
};
document.body.insertBefore(el, null);
for (var t in transforms) {
if (el.style[t] !== undefined) {
el.style[t] = "translate3d(1px,1px,1px)";
has3d = window.getComputedStyle(el).getPropertyValue(transforms[t]);
}
}
document.body.removeChild(el);
return (has3d !== undefined && has3d.length > 0 && has3d !== "none");
}
/**
* insertAt
* Utility function that inserts objects at a specific index
* Used to maintain the order of images as they are loaded and
* added to the DOM
*/
Plugin.prototype.insertAt = function (index, element) {
var lastIndex = $(this.element).children().size();
if (index < 0) {
index = Math.max(0, lastIndex + 1 + index);
}
var imgWrapper = $(this.element).append(element);
if (index < lastIndex) {
$(this.element).children().eq(index).before($(this.element).children().last());
}
}
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin( this, options ));
}
});
}
})( jQuery, window, document );