forked from lostinthepines/spazm.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.spazm.js
697 lines (586 loc) · 23.8 KB
/
jquery.spazm.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright 2012 Bearhanded All Rights Reserved.
/**
* @fileoverview JQuery viewer with pan/zoom support
* @author alex@bearhanded.com (Alex Peterson)
*/
(function( $ ){
var panning = false;
var zooming = false;
var oldX = 0;
var oldY = 0;
var startAngle = 0;
var startLevel = 0;
var startX = 0;
var startY = 0;
var panned = false;
var loadingImage = 0;
var loadingAngles = false;
var lastClickTime = 0;
var methods = {
/**
* Initialize the plugin
* @param {object} options The options to initialize the plugin with.
* Options are:
* ...
* @return the modified jquery object
*/
init : function( options ) {
return this.each(function(){
var $this = $(this);
var data = $this.data('viewer');
var settings = $.extend( {
// Required parameters
'prefix' : null,
'num_images' : -1,
'full_width' : -1,
'full_height' : -1,
'num_zoom_levels' : -1,
// Optional parameters
'zoom_level' : 0,
'angle_index' : 0,
'rotate_right' : false,
'format' : 'jpg',
'debug' : false,
// Undocumented (works in progress)
'resizable' : false,
// Undocumented (trying to eliminate)
'tile_width' : 512,
'tile_height' : 384,
'num_loaded' : 0,
'levels' : [],
'min_width' : $(this).width(),
'min_height' : $(this).height()
}, options);
if( !data ) {
$(this).data('viewer', settings);
data = $this.data('viewer');
// Check for required parameters
var unfound = '';
if(!data.prefix)
unfound += 'prefix';
if(data.num_images < 0)
unfound += unfound.length > 0 ? ', num_images' : 'num_images';
if(data.full_width < 0)
unfound += unfound.length > 0 ? ', full_width' : 'full_width';
if(data.full_height < 0)
unfound += unfound.length > 0 ? ', full_height' : 'full_height';
if(data.num_zoom_levels < 0)
unfound += unfound.length > 0 ? ', num_zoom_levels' : 'num_zoom_levels';
if(unfound.length > 0) {
console.log('SPAZM: Please provide the following parameter(s) and try again: ' + unfound);
return;
}
// save some info about each level size
data.levels = getLevelSizes(data.num_zoom_levels, data.min_width, data.min_height, data.full_width, data.full_height);
// load all the top level fully zoomed out images
var txt = '';
// angles are for panning
txt += '<div class="angles">';
txt += '<img class="angle0" src="' + settings.prefix + '0_0_0_0.' + data.format + '" style="width:' + data.min_width + 'px; height:' + data.min_height + 'px;" />';
txt += '</div>';
$(this).append(txt);
// next divs are for tiles
txt = '<div class="visible" style="display:none"></div>';
txt += '<div class="loading" style="display:none"></div>';
// hotspots are for clicking
txt += '<div class="hotspots" style="width:' + data.min_width + 'px; height:' + data.min_height + 'px"></div>';
$(this).append(txt);
// fill tile layers with tiles
var num_tiles = (Math.ceil(data.min_height / data.tile_height) + 1) * (Math.ceil(data.min_width / data.tile_width) + 1);
for(var t = 0; t < num_tiles; t++) {
$(this).find('.visible,.loading').append('<img src="" class="recyclable" />');
}
// apply necessary styling
$(this).css('overflow','hidden').css('cursor','move');
$(this).children().css('position','absolute').css('top','0').css('left','0').css('width','10000px').css('height','10000px');
$(this).find('img').css('position','absolute');
// loading bar
$(this).append('<div class="loader"><div class="bar_empty"><div class="bar"></div></div></div>');
// load all the angle images
loadingAngles = true;
$(this).children('.angles').imagesLoaded(onAngleImageLoaded);
// mouse events
$(this).mousedown(function(e) {
mouse_down(e, $(this), e.pageX, e.pageY);
});
$(this).mousemove(function(e) {
mouse_move(e, $(this), e.pageX, e.pageY);
});
$(this).mouseup(function(e) {
mouse_up(e, $(this));
});
$(this).mousewheel(function(e,delta,deltaX,deltaY) {
if(deltaY > 0) {
$(this).spazm('zoom',data.zoom_level - 1);
}
else if(deltaY < 0) {
$(this).spazm('zoom',data.zoom_level + 1);
}
});
// touch events
var docData = $(document).data('viewer');
if(!docData) {
// initialize the touchmove blocker for the body
// $(document).on('touchmove', function(e) { e.preventDefault; });
// $(document).on('gesturestart', function(e) { e.preventDefault; });
// $(document).on('gesturechange', function(e) { e.preventDefault; });
$(document).data('viewer',{});
}
$(this).on('gesturestart', function(e) {
startLevel = data.zoom_level;
});
$(this).on('gesturechange', function(e) {
// get the current zoom level width
var w = data.levels[startLevel].width * e.originalEvent.scale;
var targetZoomLevel = 0;
if(w < data.min_width) {
targetZoomLevel = 0;
}
else if(w > data.full_width) {
targetZoomLevel = data.num_zoom_levels;
}
else {
targetZoomLevel = Math.round(((w - data.min_width) / (data.full_width - data.min_width)) * data.num_zoom_levels);
}
if(settings.zoom_level != targetZoomLevel) {
$(this).spazm('zoom',targetZoomLevel);
}
});
$(this).on('touchstart', function(e) {
if(e.originalEvent.touches.length > 1) {
// @TODO store the center point for gestures
// for location-based zooming
return;
}
var t = e.originalEvent.changedTouches.item(0);
mouse_down(e, $(this), t.clientX, t.clientY);
});
$(this).on('touchmove', function(e) {
if(e.originalEvent.touches.length > 1) return;
var t = e.originalEvent.changedTouches.item(0);
mouse_move(e, $(this),t.clientX, t.clientY);
});
$(this).on('touchend', function(e) {
if(e.originalEvent.touches.length > 1) return;
mouse_up(e, $(this));
});
}
});
},
/**
* Zoom to a specific level
* @param {int} zoom_level Zoom level to zoom too
* @param {int} x New x position 0..data.min_width
* @param {int} y New y position 0..data.min_height
* @return the modified jquery object
*/
zoom : function (zoom_level,x,y) {
return this.each( function() {
var data = $(this).data('viewer');
if(zoom_level > data.num_zoom_levels || zoom_level < 0 || data.num_zoom_levels === 0) return;
if(data.debug) console.log('SPAZM: Zooming to level ' + zoom_level + '/' + data.num_zoom_levels);
var cur_level = data.zoom_level;
var delta = zoom_level - cur_level;
data.zoom_level = zoom_level;
var w = data.levels[zoom_level].width;
var h = data.levels[zoom_level].height;
var angle_container = $(this).find('.angles');
var angle_img = $(this).find('.angle' + data.angle_index);
var cx,cy,nx,ny;
if(typeof x != "undefined" && typeof y != "undefined") {
cx = -angle_container.offset().left + x;
cy = -angle_container.offset().top + y;
}
else {
cx = -angle_container.offset().left + (data.min_width / 2);
cy = -angle_container.offset().top + (data.min_height / 2);
}
// convert to the center of the image at new zoom width
cx /= angle_img.width();
cy /= angle_img.height();
cx *= w;
cy *= h;
// get new top/left based on new center
nx = -(cx - (data.min_width /2));
ny = -(cy - (data.min_height /2));
// constrain to the viewport
if(nx > 0) nx = 0;
if(nx < -(w - data.min_width)) nx = -(w - data.min_width);
if(ny > 0) ny = 0;
if(ny < -(h - data.min_height)) ny = -(h - data.min_height);
// mark all tiles in the visible tile layer as recyclable and stop animating
//$(this).find('.visible > img, .loading > img').addClass('recyclable');
var old_level = $(this).find('.visible');
var new_level = $(this).find('.loading');
new_level.css('left',nx + 'px').css('top',ny + 'px').hide();
new_level.find('img').stop().hide();
if(cur_level !== 0) {
//old_level.find('img').show();
}
var t = $(this);
// animate the zoomed out image position & size
angle_container.stop().animate({ left:nx, top:ny }, {duration: 500, queue: false, easing: 'swing' });
//var hotspots_container = $(this).find('.hotspots');
$(this).find('.hotspots').stop().animate({ left:nx, top:ny, width: w, height: h }, {duration: 500, queue: false, easing: 'swing' });
zooming = true;
angle_img.css({textIndent:0});
var old_w = parseInt(angle_img.css('width'), 10);
var old_h = parseInt(angle_img.css('height'), 10);
angle_img.stop().animate({
textIndent: 100
//width: w,
//height: h
}, {
duration: 500,
queue: false,
easing: 'swing',
step: function(now, fx) {
now *= 0.01;
angle_img.css({'width':old_w + (w - old_w)*(now), 'height': old_h + (h - old_h)*(now)});
old_level.css('left',angle_container.css('left')).css('top',angle_container.css('top'));
old_level.find('img').each(function() {
var td = $(this).data('viewer');
if(td) {
$(this).css('left',(old_w * td.sx + (w * td.sx - old_w * td.sx) * now) + 'px').css('width',(old_w * td.ex + (td.ex * w - old_w * td.ex) * now) + 'px');
$(this).css('top',(old_h * td.sy + (h * td.sy - old_h * td.sy) * now) + 'px').css('height',(old_h * td.ey + (td.ey * h - old_h * td.ey) * now) + 'px');
}
});
/*
var $this = $(this);
if(fx.prop == "width") {
old_level.css('left',angle_container.css('left')).css('top',angle_container.css('top'));
old_level.find('img').each(function() {
var td = $(this).data('viewer');
if(td) {
$(this).css('left',(td.sx * now) + 'px').css('width',(td.ex * now) + 'px');
}
});
}
else if(fx.prop == "height") {
old_level.css('left',angle_container.css('left')).css('top',angle_container.css('top'));
old_level.find('img').each(function() {
var td = $(this).data('viewer');
if(td) {
$(this).css('top',(td.sy * now) + 'px').css('height',(td.ey * now) + 'px');
}
});
}
*/
},
complete: function() {
// called on animation complete
if(data.debug) console.log('SPAZM: Zoom complete');
// load the new tiles
loadTilesForLevel(t);
new_level.removeClass('loading').addClass('visible');
if(data.zoom_level) {
new_level.fadeIn();
}
else {
old_level.hide();
}
old_level.removeClass('visible').addClass('loading');
zooming = false;
}
});
});
}
};
var onAngleImageLoaded = function($images, $proper, $broken) {
var data = $(this).parent().data('viewer');
var loaderDiv = $(this).parent().children('.loader');
var loadingBar = loaderDiv.find('.bar');
loaderDiv.stop().show();
if(loadingImage === 0) {
$(this).find('img').fadeIn('slow');
}
loadingImage = $(this).find('img').length;
var width = 98 * (loadingImage / data.num_images);
loadingBar.css('width', width + 'px');
if(loadingImage < data.num_images) {
var numToLoad = Math.min(4,data.num_images - loadingImage);
for(var i = 0; i < numToLoad; i++) {
$(this).append('<img class="angle' + (loadingImage + i) + '" src="' + data.prefix + (loadingImage + i) + '_0_0_0.' + data.format + '" style="display:none; width:' + data.min_width + 'px; height:' + data.min_height + 'px" />');
}
$(this).imagesLoaded(onAngleImageLoaded);
}
else {
// all done
loaderDiv.fadeOut();
loadingAngles = false;
}
};
var loadTilesForLevel = function(elem) {
var data = elem.data('viewer');
if(data.zoom_level === 0) return;
var container = elem.find('.loading');
container.find('img').attr('src','').attr('id','').hide().addClass('recyclable');
var x = parseInt(container.css('left'),10);
var y = parseInt(container.css('top'),10);
var w = data.levels[data.zoom_level].width;
var h = data.levels[data.zoom_level].height;
// calculate which tiles will be needed in the new viewport area
var ix = Math.floor(-x / data.tile_width);
var iy = Math.floor(-y / data.tile_height);
var ex = Math.floor((-x + data.min_width) / data.tile_width);
var ey = Math.floor((-y + data.min_height) / data.tile_height);
// load tiles into the new level
for(;iy <= ey; iy++) {
ix = Math.floor(-x / data.tile_width);
for(;ix <= ex; ix++ ) {
// get a recyclable tile
var tile = container.find('.recyclable');
if(!tile.length) {
console.log('SPAZM: ERROR! No recyclable tile found for level ' + data.zoom_level + ' ' + ix + ' ' + iy);
break;
}
// just get the first match
tile = tile.eq(0);
var tw = w - ix * data.tile_width;
var th = h - iy * data.tile_height;
if(tw > data.tile_width) tw = data.tile_width;
if(th > data.tile_width) th = data.tile_height;
// store the tile origin and width relative to the
// size of the container for easy scaling calculations
tile.data('viewer',{
sx:((ix * data.tile_width) / w),
sy:((iy * data.tile_height) / h),
ex:(tw / w),
ey:(th / h)
});
tile.attr('id',data.zoom_level + '_' + ix + '_' + iy);
tile.attr('src',data.prefix + data.angle_index + '_' + data.zoom_level + '_' + ix + '_' + iy +'.' + data.format);
tile.css('width',tw + 'px').css('height',th + 'px');
tile.css('left',ix * data.tile_width).css('top',iy * data.tile_height);
tile.removeClass('recyclable');
}
}
loadImages(container, elem, function(loaded) {
loaded.each(function() {
if(!$(this).is(':visible') && !$(this).is('.recyclable')) {
$(this).fadeIn();
}
});
},
function(images) {
elem.find('.loading').hide();
});
};
/**
* Given specific min and max sizes and # of zoom levels
* create an array of image sizes at each zoom level
* @param {int} numLevels
* @param {int} minWidth
* @param {int} minHeight
* @param {int} maxWidth
* @param {int} maxHeight
* @return {array} Array of level sizes
*/
var getLevelSizes = function(numLevels, minWidth, minHeight, maxWidth, maxHeight) {
var lvlWidth = minWidth;
var lvlHeight = minHeight;
var dx = parseInt((maxWidth - minWidth) / numLevels,10);
var dy = parseInt((maxHeight - minHeight) / numLevels,10);
var levels = [];
for(var level = 0; level <= numLevels; level++) {
if(level == numLevels) {
levels.push({width: maxWidth, height: maxHeight});
}
else {
levels.push({width: lvlWidth, height: lvlHeight });
}
lvlWidth += dx;
lvlHeight += dy;
}
return levels;
};
/**
* Load images inside a specific element.
* @param {object} container The jQuery element containing the images
* @param {object} parent The jQuery element with the viewer data
* @param {function} progressCallback Optional callback on progress
* @param {function} completeCallback Optional callback on complete
*/
var loadImages = function(container, parent, progressCallback, completeCallback) {
var loaderDiv = parent.children('.loader');
var loadingBar = loaderDiv.find('.bar');
loaderDiv.show();
var loader = container.imagesLoaded();
loader.always( function( images ) {
var width = 98 * (parent.data('viewer').num_loaded / images.length);
loadingBar.css('width', width + 'px');
loaderDiv.fadeOut();
if(completeCallback) completeCallback(images);
});
loader.done( function() {
});
loader.progress( function(isBroken, images, proper, broken) {
parent.data('viewer').num_loaded = proper.length;
var width = 98 * (parent.data('viewer').num_loaded / images.length);
loadingBar.css('width', width + 'px');
if(progressCallback) progressCallback(proper);
});
};
var pad = function(num, length) {
var s = num.toString();
while(s.length < length) {
s = '0' + s;
}
return s;
};
var mouse_down = function(e, elem, x, y) {
e.preventDefault();
panning = true;
panned = false;
oldX = x;
oldY = y;
startX = x;
startY = y;
startAngle = elem.data('viewer').angle_index;
};
var mouse_move = function(e, elem, x, y) {
e.preventDefault();
if(!panning || zooming) return;
panned = true;
var data = elem.data('viewer');
var dx,dy;
if(data.zoom_level === 0 && loadingAngles === false) {
// rotate
var old_angle = elem.find('.angle' + data.angle_index );
old_angle.hide();
dx = Math.round((x - startX) / 15);
if(!data.rotate_right) {
dx = -dx;
}
data.angle_index = (startAngle + dx) % data.num_images;
if(data.angle_index < 0) data.angle_index += data.num_images;
var new_angle =elem.find('.angle' + data.angle_index);
new_angle.show();
}
else {
// stop animating any pan
elem.find('.angles').stop();
// pan
var w = data.levels[data.zoom_level].width;
var h = data.levels[data.zoom_level].height;
dx = x - oldX;
dy = y - oldY;
elem.find('.visible,.angles,.loading').each(function() {
var nx = parseFloat($(this).css('left'));
var ny = parseFloat($(this).css('top'));
nx += dx;
ny += dy;
if(nx > 0) nx = 0;
if(nx < -(w - data.min_width)) nx = -(w - data.min_width);
if(ny > 0) ny = 0;
if(ny < -(h - data.min_height)) ny = -(h - data.min_height);
$(this).css('left',nx).css('top',ny);
});
elem.find('.hotspots').stop().css('left',elem.find('.angles').css('left')).css('top',elem.find('.angles').css('top'));
}
oldX = x;
oldY = y;
};
var mouse_up = function(e,elem) {
e.preventDefault();
var data = elem.data('viewer');
if(panned && data.zoom_level > 0) {
var container = elem.find('.visible');
var x = parseInt(container.css('left'),10);
var y = parseInt(container.css('top'),10);
var w = data.levels[data.zoom_level].width;
var h = data.levels[data.zoom_level].height;
// calculate which tiles will be needed in the new viewport area
var ix = Math.floor(-x / data.tile_width);
var iy = Math.floor(-y / data.tile_height);
var ex = Math.floor((-x + data.min_width) / data.tile_width);
var ey = Math.floor((-y + data.min_height) / data.tile_height);
container.find('img').each(function() {
if($(this).hasClass('recyclable')) {
$(this).attr('id','');
return;
}
// flag as recyclable if outside the viewport
var tx = parseInt($(this).css('left'),10);
var ty = parseInt($(this).css('top'),10);
var tw = parseInt($(this).css('width'),10);
var th = parseInt($(this).css('height'),10);
if( tx > (-x + data.min_width) || ty > (-y + data.min_height) ||
(tx + tw < -x) || (ty + th < -y)) {
$(this).attr('id','').attr('src','').hide().addClass('recyclable');
}
});
// load tiles into the new level
for(;iy <= ey; iy++) {
ix = Math.floor(-x / data.tile_width);
for(;ix <= ex; ix++ ) {
var tile = container.find('#' + data.zoom_level + '_' + ix + '_' + iy);
if(tile.length) continue;
// get a recyclable tile
tile = container.find('.recyclable');
if(!tile.length) {
console.log('SPAZM: ERROR! No recyclable tile found for level ' + data.zoom_level + ' ' + ix + ' ' + iy);
break;
}
// just get the first match
tile = tile.eq(0);
tile.removeClass('recyclable');
var tw = w - ix * data.tile_width;
var th = h - iy * data.tile_height;
if(tw > data.tile_width) tw = data.tile_width;
if(th > data.tile_width) th = data.tile_height;
// store the tile origin and width relative to the
// size of the container for easy scaling calculations
tile.data('viewer',{
sx:((ix * data.tile_width) / w),
sy:((iy * data.tile_height) / h),
ex:(tw / w),
ey:(th / h)
});
tile.attr('id',data.zoom_level + '_' + ix + '_' + iy);
tile.attr('src',data.prefix + data.angle_index + '_' + data.zoom_level + '_' + ix + '_' + iy +'.' + data.format);
tile.css('width',tw + 'px').css('height',th + 'px');
tile.css('left',ix * data.tile_width).css('top',iy * data.tile_height);
tile.hide();
}
}
loadImages(container, elem, function(loaded) {
loaded.each(function() {
if(!$(this).is(':visible') && !$(this).is('.recyclable')) {
$(this).fadeIn();
}
});
},
function(images) {
elem.find('.loading').hide();
});
}
else if(panning && !panned) {
var d = new Date();
var m = d.getTime();
// double click
if(m - 500 < lastClickTime) {
if(data.zoom_level == data.num_zoom_levels) {
if(data.debug) console.log('SPAZM: Zooming out');
elem.spazm('zoom',0);
}
else {
if(data.debug) console.log('SPAZM: Zooming in to point (' + oldX + ',' + oldY + ')');
elem.spazm('zoom',data.zoom_level + 1, oldX, oldY);
}
}
lastClickTime = m;
}
panning = false;
panned = false;
};
$.fn.spazm = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.spazm' );
}
};
})( jQuery );