-
Notifications
You must be signed in to change notification settings - Fork 110
/
freezeframe.js
executable file
·358 lines (280 loc) · 11.7 KB
/
freezeframe.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
// pass references around in a cleaner way
// remove jquery dependency
var freezeframe = (function($) {
var images, options, is_touch_device;
//////////////////////////////////////////////////////////////////////////////
// //
// Private Methods //
// //
//////////////////////////////////////////////////////////////////////////////
// decorated console.warn message
var warn = function(_message) {
console.warn('✨ freezeframe.js ✨ : ' + _message);
}
// does freezeframe instance have any captured images?
var has_images = function() {
return this.images.length == 0 ? false : true;
}
// filter captured images by selector and warn if none found
var filter = function(_selector, _images) {
var filtered_images;
if(_selector != undefined && _images.length > 1) {
filtered_images = _images.filter( $(_selector) );
if (filtered_images.length == 0) {
warn("no images found for selector '" + _selector + "'")
return false;
}
} else {
filtered_images = _images;
}
return filtered_images;
}
// reset .gif to first frame and write to canvas
var process = function ($_image) {
var ff = this,
$canvas = $_image.siblings('canvas'),
transitionEnd = 'transitionend webkitTransitionEnd oTransitionEnd otransitionend',
image_width = $_image[0].clientWidth,
image_height = $_image[0].clientHeight;
$canvas.attr({
'width': image_width,
'height': image_height
});
context = $canvas[0].getContext('2d');
context.drawImage($_image[0], 0, 0, image_width, image_height);
$canvas.addClass('ff-canvas-ready').on(transitionEnd, function() {
$(this).off(transitionEnd);
$_image.addClass('ff-image-ready');
})
}
//////////////////////////////////////////////////////////////////////////////
// //
// Constructor //
// //
//////////////////////////////////////////////////////////////////////////////
function freezeframe(_options) {
var options;
// default options
this.options = {
selector : '.freezeframe',
animation_play_duration: 5000,
non_touch_device_trigger_event: 'hover'
}
// new selector as string
options = typeof _options == 'string' ? { 'selector': _options } : _options;
// new options
if(options) {
for (attribute in options) {
if (attribute in this.options) {
this.options[attribute] = options[attribute]
} else {
warn(attribute + 'not a valid option')
}
}
}
// is this a touch device?
this.is_touch_device = ('ontouchstart' in window || 'onmsgesturechange' in window);
}
//////////////////////////////////////////////////////////////////////////////
// //
// Capture Images //
// //
//////////////////////////////////////////////////////////////////////////////
freezeframe.prototype.capture = function(_selector) {
var selector;
// Passed in string or default string
if(_selector !== undefined) {
selector = _selector;
} else if (this.options.selector !== undefined) {
selector = this.options.selector;
} else {
warn("no selector passed to capture function or set in options")
return false;
}
// Empty jQuery object to add into
if(this.images == undefined) {
this.images = $();
}
// Add new selection, jQuery keeps it non redundant
this.images = this.images.add( $('img' + selector) );
// Get non gifs outta there
for (i = 0; i < this.images.length; i++) {
if (this.images[i].src.split('.').pop().toLowerCase().substring(0, 3) !== 'gif') {
this.images.splice(i, 1);
}
}
// If nothing was found, throw a fit
if(this.images.length == 0) {
console.warn('freezeframe : no gifs found for selector "' + selector + '"');
return false;
}
return this;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Setup Elements //
// //
//////////////////////////////////////////////////////////////////////////////
freezeframe.prototype.setup = function(_selector) {
var ff = this,
setup_required = this.images.not('.ff-setup'),
container_classnames = ['ff-container'];
if(!has_images.call(ff)) {
warn("unable to run setup(), no images captured")
return false;
} else if(setup_required.length == 0) {
warn("unable to run setup(), no images require setup")
return false;
}
filter.call(ff, _selector, setup_required).each(function(e) {
var $image = $(this);
$image.addClass('ff-setup ff-image');
if($image.hasClass('freezeframe-responsive')) {
container_classnames.push('ff-responsive');
}
$canvas = $('<canvas />', {
class: 'ff-canvas'
}).attr({
width: 0,
height: 0
}).insertBefore($image);
$image.add($canvas).wrapAll(
$('<div />', {
class: container_classnames.join(' ')
})
);
});
imagesLoaded(setup_required).on('progress', function(instance, image) {
process.call(ff, $(image.img));
});
return this;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Attach Hover / Click Events //
// //
//////////////////////////////////////////////////////////////////////////////
freezeframe.prototype.attach = function(_selector) {
var ff = this,
click_timeout,
images;
if(!has_images.call(ff)) {
warn("unable to run attach(), no images captured")
return false;
}
filter.call(ff, _selector, ff.images).each(function(e) {
var $image = $(this);
var $canvas = $(this).siblings('canvas');
// hover /////////////////////////////////////////////////////////////////
if((!ff.is_touch_device && ff.options.non_touch_device_trigger_event == 'hover') || (ff.is_touch_device)) {
$image.mouseenter(function() {
(function() {
if($image.hasClass('ff-image-ready')) {
$image.attr('src', $image[0].src);
$canvas.removeClass('ff-canvas-ready').addClass('ff-canvas-active');
}
})();
})
$image.mouseleave(function() {
(function() {
if($image.hasClass('ff-image-ready')) {
$canvas.removeClass('ff-canvas-active').addClass('ff-canvas-ready');
}
})();
})
}
// click /////////////////////////////////////////////////////////////////
if((!ff.is_touch_device && ff.options.non_touch_device_trigger_event == 'click') || (ff.is_touch_device)) {
var click_timeout;
$image.click(function() {
(function() {
var clicked = $canvas.hasClass('ff-canvas-active');
if($image.hasClass('ff-image-ready')) {
if(clicked) {
if(ff.options.animation_play_duration != Infinity) {
clearTimeout(click_timeout);
}
$canvas.removeClass('ff-canvas-active').addClass('ff-canvas-ready');
} else {
$image.attr('src', $image[0].src);
$canvas.removeClass('ff-canvas-ready').addClass('ff-canvas-active');
if(ff.options.animation_play_duration != Infinity) {
click_timeout = setTimeout(function() {
$canvas.removeClass('ff-canvas-active').addClass('ff-canvas-ready');
}, ff.options.animation_play_duration);
}
}
}
})();
})
}
})
return this;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Trigger Animation //
// //
//////////////////////////////////////////////////////////////////////////////
freezeframe.prototype.trigger = function(_selector) {
var ff = this,
errors = 0;
filter.call(ff, _selector, ff.images).each(function(e) {
if($(this).hasClass('ff-image-ready')) {
$(this).attr('src', $(this)[0].src);
$(this).siblings('canvas').removeClass('ff-canvas-ready').addClass('ff-canvas-active');
} else {
warn("image not done processing ! " + $(this).attr("src"));
errors ++;
}
});
return errors == 0 ? true : false;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Release Animation //
// //
//////////////////////////////////////////////////////////////////////////////
freezeframe.prototype.release = function(_selector) {
var ff = this,
errors = 0;
filter.call(ff, _selector, ff.images).each(function(e) {
if($(this).hasClass('ff-image-ready')) {
$(this).siblings('canvas').removeClass('ff-canvas-active').addClass('ff-canvas-ready');
} else {
warn("image not done processing ! " + $(this).attr("src"));
errors ++;
}
});
return errors == 0 ? true : false;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Freeze Images //
// //
//////////////////////////////////////////////////////////////////////////////
freezeframe.prototype.freeze = function() {
this.capture().setup().attach(); // ✨ tada ✨
return this;
}
return freezeframe;
})(jQuery);
// jQuery plugin
$.fn.freezeframe = function(_options) {
if (this.length == 0) {
console.warn('✨ freezeframe.js ✨ : no images found for selector ' + this.selector);
return false;
}
var ff = new freezeframe(_options);
ff.images = this;
ff.setup().attach();
var self = this;
var methods = ['trigger', 'release'];
methods.forEach(function(method) {
self[method] = function() {
ff[method](self.selector);
return self;
};
});
return this;
};