forked from FormstoneClassic/Zoomer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fs.zoomer.js
1270 lines (1084 loc) · 33.2 KB
/
jquery.fs.zoomer.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Zoomer v3.0.11 - 2014-07-08
* A jQuery plugin for smooth image exploration. Part of the formstone library.
* http://formstone.it/components/zoomer/
*
* Copyright 2014 Ben Plum; MIT Licensed
*/
;(function ($, window) {
"use strict";
var $window = $(window),
$instances,
animating = false,
transformSupported = false;
/**
* @options
* @param callback [function] <$.noop> ""
* @param controls.postion [string] <"bottom"> "Position of default controls"
* @param controls.zoomIn [string] <> "Custom zoom control selecter"
* @param controls.zoomOut [string] <> "Custom zoom control selecter"
* @param controls.next [string] <> "Custom pagination control selecter"
* @param controls.previous [string] <> "Custom pagination control selecter"
* @param customClass [string] <''> "Class applied to instance"
* @param enertia [number] <0.2> "Zoom smoothing (0.1 = butter, 0.9 = sandpaper)"
* @param increment [number] <0.01> "Zoom speed (0.01 = tortoise, 0.1 = hare)"
* @param marginMin [] <> ""
* @param marginMax [] <> ""
* @param retina [boolean] <false> "Flag for retina image support"
* @param source [string | object] <null> "Source image (string) or tiles (object)"
*/
var options = {
callback: $.noop,
controls: {
position: "bottom",
zoomIn: null,
zoomOut: null,
next: null,
previous: null
},
customClass: "",
enertia: 0.2,
increment: 0.01,
marginMin: 30, // Min bounds
marginMax: 100, // Max bounds
retina: false,
source: null
};
// Internal data
var properties = {
images: [],
aspect: "",
action: "",
lastAction: "",
keyDownTime: 0,
marginReal: 0,
originalDOM: "",
// Gallery
gallery: false,
index: 0,
// Tiles
$tiles: null,
tiled: false,
tilesTotal: 0,
tilesLoaded: 0,
tiledColumns: 0,
tiledRows: 0,
tiledHeight: 0,
tiledWidth: 0,
tiledThumbnail: null,
// Frame
centerLeft: 0,
centerTop: 0,
frameHeight: 0,
frameWidth: 0,
// Original image
naturalHeight: 0,
naturalWidth: 0,
imageRatioWide: 0,
imageRatioTall: 0,
// Dimensions
minHeight: null,
minWidth: null,
maxHeight: 0,
maxWidth: 0,
// Bounds
boundsTop: 0,
boundsBottom: 0,
boundsLeft: 0,
boundsRight: 0,
// Image
imageWidth: 0,
imageHeight: 0,
imageLeft: 0,
imageTop: 0,
targetImageWidth: 0,
targetImageHeight: 0,
targetImageLeft: 0,
targetImageTop: 0,
oldImageWidth: 0,
oldImageHeight: 0,
// Positioner
positionerLeft: 0,
positionerTop: 0,
targetPositionerLeft: 0,
targetPositionerTop: 0,
// Zoom
zoomPositionLeft: 0,
zoomPositionTop: 0,
// Touch Support
offset: null,
touches: [],
zoomPercentage: 1,
pinchStartX0: 0,
pinchStartX1: 0,
pinchStartY0: 0,
pinchStartY1: 0,
pinchEndX0: 0,
pinchEndX1: 0,
pinchEndY0: 0,
pinchEndY1: 0,
lastPinchEndX0: 0,
lastPinchEndY0: 0,
lastPinchEndX1: 0,
lastPinchEndY1: 0,
pinchDeltaStart: 0,
pinchDeltaEnd: 0
};
/**
* @events
* @event zoomer.loaded "Source media loaded"
*/
var pub = {
/**
* @method
* @name defaults
* @description Sets default plugin options
* @param opts [object] <{}> "Options object"
* @example $.zoomer("defaults", opts);
*/
defaults: function(opts) {
options = $.extend(options, opts || {});
return $(this);
},
/**
* @method
* @name destroy
* @description Removes instance of plugin
* @example $(".target").zoomer("destroy");
*/
destroy: function() {
var $targets = $(this).each(function(i, target) {
var data = $(target).data("zoomer");
if (data) {
$window.off(".zoomer");
data.$holder.off(".zoomer");
data.$zoomer.off(".zoomer");
data.controls.$zoomIn.off(".zoomer");
data.controls.$zoomOut.off(".zoomer");
data.controls.$next.off(".zoomer");
data.controls.$previous.off(".zoomer");
data.$target.removeClass("zoomer-element")
.data("zoomer", null)
.empty()
.append(data.originalDOM);
}
});
$instances = $(".zoomer-element");
if ($instances.length < 1) {
_clearAnimation();
}
return $targets;
},
/**
* @method
* @name load
* @description Loads source media
* @param source [string | object] "Source image (string) or tiles (object)"
* @example $(".target").zoomer("load", "path/to/image.jpg");
*/
load: function(source) {
return $(this).each(function(i, target) {
var data = $(target).data("zoomer");
if (data) {
data.source = source;
data.index = 0;
data = _normalizeSource(data);
_load(data);
}
});
},
/**
* @method
* @name pan
* @description Pans plugin instances
* @param left [int] "Percentage to pan to (50 = half)"
* @param top [int] "Percentage to pan to (50 = half)"
* @example $(".target").zoomer("pan", 50, 50);
*/
pan: function(left, top) {
return $(this).each(function(i, target) {
var data = $(target).data("zoomer");
if (data) {
left /= 100;
top /= 100;
data.targetPositionerLeft = Math.round(data.centerLeft - data.targetImageLeft - (data.targetImageWidth * left));
data.targetPositionerTop = Math.round(data.centerTop - data.targetImageTop - (data.targetImageHeight * top));
}
});
},
/**
* @method
* @name resize
* @description Resizes plugin instange
* @example $(".target").zoomer("resize");
*/
resize: function() {
return $(this).each(function(i, target) {
var data = $(target).data("zoomer");
if (data) {
data.frameWidth = data.$target.outerWidth();
data.frameHeight = data.$target.outerHeight();
data.centerLeft = Math.round(data.frameWidth * 0.5);
data.centerTop = Math.round(data.frameHeight * 0.5);
// Set minHeight and minWidth to naturals sizes
data.minHeight = data.naturalHeight;
data.minWidth = data.naturalWidth;
// Recalculate minimum sizes only when the natural size of the image is bigger than the frame size - marginalReal
if (data.naturalHeight > (data.frameHeight - data.marginReal) || data.naturalWidth > (data.frameWidth - data.marginReal)) {
data = _setMinimums(data);
}
}
});
},
/**
* @method
* @name unload
* @description Unload image from plugins instances
* @example $(".target").zoomer("unload");
*/
unload: function() {
return $(this).each(function() {
var data = $(this).data("zoomer");
if (data && typeof data.$image !== 'undefined') {
data.$image.remove();
}
});
}
};
/**
* @method private
* @name _init
* @description Initializes plugin
* @param opts [object] "Initialization options"
*/
function _init(opts) {
// Settings
opts = $.extend({}, options, properties, opts);
transformSupported = _getTransform3DSupport();
// Apply to each element
var $items = $(this);
for (var i = 0, count = $items.length; i < count; i++) {
_build($items.eq(i), opts);
}
// Start main animation loop
$instances = $(".zoomer-element");
_startAnimation();
return $items;
}
/**
* @method private
* @name _build
* @description Builds each instance
* @param $target [jQuery object] "Target jQuery object"
* @param data [object] <{}> "Options object"
*/
function _build($target, data) {
if (!$target.data("zoomer")) {
data = $.extend({}, data, $target.data("zoomer-options"));
data.$target = $target;
data.marginReal = data.marginMin * 2;
data.originalDOM = data.$target.html();
if (data.$target.find("img").length > 0) {
data.source = [];
data.$target.find("img").each(function() {
data.source.push($(this).attr("src"));
});
data.$target.empty();
}
data = _normalizeSource(data);
// Assemble HTML
var html = '<div class="zoomer ' + data.customClass + '">';
html += '<div class="zoomer-positioner">';
html += '<div class="zoomer-holder">';
html += '</div>';
html += '</div>';
html += '</div>';
data.$zoomer = $(html);
data.$target.addClass("zoomer-element")
.html(data.$zoomer);
if (data.controls.zoomIn || data.controls.zoomOut || data.controls.next || data.controls.previous) {
data.controls.$zoomIn = $(data.controls.zoomIn);
data.controls.$zoomOut = $(data.controls.zoomOut);
data.controls.$next = $(data.controls.next);
data.controls.$previous = $(data.controls.previous);
} else {
html = '<div class="zoomer-controls zoomer-controls-' + data.controls.position + '">';
html += '<span class="zoomer-previous">‹</span>';
html += '<span class="zoomer-zoom-out">-</span>';
html += '<span class="zoomer-zoom-in">+</span>';
html += '<span class="zoomer-next">›</span>';
html += '</div>';
data.$zoomer.append(html);
data.controls.$default = data.$zoomer.find(".zoomer-controls");
data.controls.$zoomIn = data.$zoomer.find(".zoomer-zoom-in");
data.controls.$zoomOut = data.$zoomer.find(".zoomer-zoom-out");
data.controls.$next = data.$zoomer.find(".zoomer-next");
data.controls.$previous = data.$zoomer.find(".zoomer-previous");
}
// Cache jquery objects
data.$positioner = data.$zoomer.find(".zoomer-positioner");
data.$holder = data.$zoomer.find(".zoomer-holder");
// Bind events
data.controls.$zoomIn.on("touchstart.zoomer mousedown.zoomer", data, _zoomIn)
.on("touchend.zoomer mouseup.zoomer", data, _clearZoom);
data.controls.$zoomOut.on("touchstart.zoomer mousedown.zoomer", data, _zoomOut)
.on("touchend.zoomer mouseup.zoomer", data, _clearZoom);
data.controls.$next.on("click.zoomer", data, _nextImage);
data.controls.$previous.on("click.zoomer", data, _previousImage);
data.$zoomer.on("mousedown.zoomer", data, _dragStart)
.on("touchstart.zoomer MSPointerDown.zoomer", ":not(.zoomer-controls)", data, _onTouch);
// Kick it off
data.$target.data("zoomer", data);
pub.resize.apply(data.$target);
if (data.images.length > 0) {
_load.apply(data.$target, [ data ]);
}
}
}
/**
* @method private
* @name _load
* @description Delegates loading action
* @param data [object] "Instance data"
*/
function _load(data) {
// If gallery
if (data.gallery) {
data.$zoomer.addClass("zoomer-gallery");
} else {
data.$zoomer.removeClass("zoomer-gallery");
}
if (typeof data.$image !== "undefined") {
data.$holder.animate({ opacity: 0 }, 300, function() {
pub.unload.apply(data.$target);
_loadImage.apply(data.$target, [ data, data.images[data.index] ]);
});
} else {
_loadImage.apply(data.$target, [ data, data.images[data.index] ]);
}
}
/**
* @method private
* @name _loadImage
* @description Handles loading an image or set of tiles
* @param data [object] "Instance data"
* @param source [string | object] "Source URL or object"
*/
function _loadImage(data, source) {
data.loading = true;
if (data.tiled) {
data.tilesTotal = 0;
data.tilesLoaded = 0;
var html = '<div class="zoomer-tiles">';
for (var i in data.images[0]) {
if (data.images[0].hasOwnProperty(i)) {
for (var j in data.images[0][i]) {
if (data.images[0][i].hasOwnProperty(j)) {
html += '<img class="zoomer-image zoomer-tile" src="' + data.images[0][i][j] + '" data-zoomer-tile="' + i + '-' + j + '" />';
data.tilesTotal++;
}
}
}
}
html += '</div>';
data.$image = $(html);
data.$tiles = data.$image.find("img");
data.$tiles.each(function(i, img) {
var $img = $(img);
$img.one("load", data, _onTileLoad);
if ($img[0].complete) {
$img.trigger("load");
}
});
} else {
// Cache current image
data.$image = $('<img class="zoomer-image" />');
data.$image.one("load.zoomer", data, _onImageLoad)
.attr("src", source);
// If image has already loaded into cache, trigger load event
if (data.$image[0].complete) {
data.$image.trigger("load");
}
}
}
/**
* @method private
* @name _onTileLoad
* @description Handles tile load
* @param e [object] "Event data"
*/
function _onTileLoad(e) {
var data = e.data;
data.tilesLoaded++;
if (data.tilesLoaded === data.tilesTotal) {
data.tiledRows = data.images[0].length;
data.tiledColumns = data.images[0][0].length;
data.tiledHeight = data.$tiles.eq(0)[0].naturalHeight * data.tiledRows;
data.tiledWidth = data.$tiles.eq(0)[0].naturalWidth * data.tiledColumns;
_onImageLoad({ data: data });
}
}
/**
* @method private
* @name _onImageLoad
* @description Handles image load
* @param e [object] "Event data"
*/
function _onImageLoad(e) {
var data = e.data;
if (data.tiled) {
data.naturalHeight = data.tiledHeight;
data.naturalWidth = data.tiledWidth;
} else {
data.naturalHeight = data.$image[0].naturalHeight;
data.naturalWidth = data.$image[0].naturalWidth;
}
if (data.retina) {
data.naturalHeight /= 2;
data.naturalWidth /= 2;
}
data.$holder.css({
height: data.naturalHeight,
width: data.naturalWidth
});
// Set target, min, max to naturals sizes
data.targetImageHeight = data.minHeight = data.maxHeight = data.naturalHeight;
data.targetImageWidth = data.minWidth = data.maxWidth = data.naturalWidth;
data.imageRatioWide = data.naturalWidth / data.naturalHeight;
data.imageRatioTall = data.naturalHeight / data.naturalWidth;
// Initial sizing to fit screen
if (data.naturalHeight > (data.frameHeight - data.marginReal) || data.naturalWidth > (data.frameWidth - data.marginReal)) {
data = _setMinimums(data);
data.targetImageHeight = data.minHeight;
data.targetImageWidth = data.minWidth;
}
// SET INITIAL POSITIONS
data.positionerLeft = data.targetPositionerLeft = data.centerLeft;
data.positionerTop = data.targetPositionerTop = data.centerTop;
data.imageLeft = data.targetImageLeft = Math.round(-data.targetImageWidth / 2);
data.imageTop = data.targetImageTop = Math.round(-data.targetImageHeight / 2);
data.imageHeight = data.targetImageHeight;
data.imageWidth = data.targetImageWidth;
if (transformSupported) {
var scaleX = data.imageWidth / data.naturalWidth,
scaleY = data.imageHeight / data.naturalHeight;
data.$positioner.css( _prefix("transform", "translate3d("+data.positionerLeft+"px, "+data.positionerTop+"px, 0)") );
data.$holder.css( _prefix("transform", "translate3d(-50%, -50%, 0) scale("+scaleX+","+scaleY+")") );
} else {
data.$positioner.css({
left: data.positionerLeft,
top: data.positionerTop
});
data.$holder.css({
left: data.imageLeft,
top: data.imageTop,
height: data.imageHeight,
width: data.imageWidth
});
}
data.$holder.append(data.$image);
if (data.tiled) {
data.$holder.css({
background: "url(" + data.tiledThumbnail + ") no-repeat left top",
backgroundSize: "100% 100%"
});
data.tileHeightPercentage = 100 / data.tiledRows;
data.tileWidthPercentage = 100 / data.tiledColumns;
data.$tiles.css({
height: data.tileHeightPercentage + "%",
width: data.tileWidthPercentage + "%"
});
data.$tiles.each(function(i, tile) {
var $tile = $(tile),
position = $tile.data("zoomer-tile").split("-");
$tile.css({
left: (data.tileWidthPercentage * parseInt(position[1], 10)) + "%",
top: (data.tileHeightPercentage * parseInt(position[0], 10)) + "%"
});
});
}
data.$holder.animate({ opacity: 1 }, 300);
data.loading = false;
// Start preloading
if (data.gallery) {
_preloadGallery(data);
}
}
/**
* @method private
* @name _preloadGallery
* @description Preloads previous and next images in gallery for faster rendering
* @param data [object] "Instance Data"
*/
function _preloadGallery(data) {
if (data.index > 0) {
$('<img src="' + data.images[data.index - 1] + '">');
}
if (data.index < data.images.length - 1) {
$('<img src="' + data.images[data.index + 1] + '">');
}
}
/**
* @method private
* @name _setMinimums
* @description Sets minimum dimensions
* @param data [object] "Instance Data"
*/
function _setMinimums(data) {
if (data.naturalHeight > data.naturalWidth) {
// Tall
data.aspect = "tall";
data.minHeight = Math.round(data.frameHeight - data.marginReal);
data.minWidth = Math.round(data.minHeight / data.imageRatioTall);
if (data.minWidth > (data.frameWidth - data.marginReal)) {
data.minWidth = Math.round(data.frameWidth - data.marginReal);
data.minHeight = Math.round(data.minWidth / data.imageRatioWide);
}
} else {
// Wide
data.aspect = "wide";
data.minWidth = Math.round(data.frameWidth - data.marginReal);
data.minHeight = Math.round(data.minWidth / data.imageRatioWide);
if (data.minHeight > (data.frameHeight - data.marginReal)) {
data.minHeight = Math.round(data.frameHeight - data.marginReal);
data.minWidth = Math.round(data.minHeight / data.imageRatioTall);
}
}
return data;
}
/**
* @method private
* @name _render
* @description Main animation loop
*/
function _render() {
for (var i = 0, count = $instances.length; i < count; i++) {
var data = $instances.eq(i).data("zoomer");
if (typeof data === "object") {
// Update image and position values
data = _updateValues(data);
data.lastAction = data.action;
// Update DOM
if (transformSupported) {
var scaleX = data.imageWidth / data.naturalWidth,
scaleY = data.imageHeight / data.naturalHeight;
data.$positioner.css(_prefix("transform", "translate3d(" + data.positionerLeft + "px, " + data.positionerTop + "px, 0)"));
data.$holder.css(_prefix("transform", "translate3d(-50%, -50%, 0) scale(" + scaleX + "," + scaleY + ")"));
} else {
data.$positioner.css({
left: data.positionerLeft,
top: data.positionerTop
});
data.$holder.css({
left: data.imageLeft,
top: data.imageTop,
width: data.imageWidth,
height: data.imageHeight
});
}
// Run callback function
if (data.callback) {
data.callback.apply(data.$zoomer, [
(data.imageWidth - data.minWidth) / (data.maxWidth - data.minWidth)
]);
}
}
}
}
/**
* @method private
* @name _updateValues
* @description Updates current image values
* @param data [object] "Instance Data"
*/
function _updateValues(data) {
// Update values based on current action
if (data.action === "zoom_in" || data.action === "zoom_out") {
// Calculate change
data.keyDownTime += data.increment;
var delta = ((data.action === "zoom_out") ? -1 : 1) * Math.round((data.imageWidth * data.keyDownTime) - data.imageWidth);
if (data.aspect === "tall") {
data.targetImageHeight += delta;
data.targetImageWidth = Math.round(data.targetImageHeight / data.imageRatioTall);
} else {
data.targetImageWidth += delta;
data.targetImageHeight = Math.round(data.targetImageWidth / data.imageRatioWide);
}
}
// Check Max and Min image values; recenter if too small
if (data.aspect === "tall") {
if (data.targetImageHeight < data.minHeight) {
data.targetImageHeight = data.minHeight;
data.targetImageWidth = Math.round(data.targetImageHeight / data.imageRatioTall);
} else if (data.targetImageHeight > data.maxHeight) {
data.targetImageHeight = data.maxHeight;
data.targetImageWidth = Math.round(data.targetImageHeight / data.imageRatioTall);
}
} else {
if (data.targetImageWidth < data.minWidth) {
data.targetImageWidth = data.minWidth;
data.targetImageHeight = Math.round(data.targetImageWidth / data.imageRatioWide);
} else if (data.targetImageWidth > data.maxWidth) {
data.targetImageWidth = data.maxWidth;
data.targetImageHeight = Math.round(data.targetImageWidth / data.imageRatioWide);
}
}
// Calculate new dimensions
data.targetImageLeft = Math.round(-data.targetImageWidth * 0.5);
data.targetImageTop = Math.round(-data.targetImageHeight * 0.5);
if (data.action === "drag" || data.action === "pinch") {
data.imageWidth = data.targetImageWidth;
data.imageHeight = data.targetImageHeight;
data.imageLeft = data.targetImageLeft;
data.imageTop = data.targetImageTop;
} else {
data.imageWidth += Math.round((data.targetImageWidth - data.imageWidth) * data.enertia);
data.imageHeight += Math.round((data.targetImageHeight - data.imageHeight) * data.enertia);
data.imageLeft += Math.round((data.targetImageLeft - data.imageLeft) * data.enertia);
data.imageTop += Math.round((data.targetImageTop - data.imageTop) * data.enertia);
}
// Check bounds of current position and if big enough to drag
// Set bounds
data.boundsLeft = Math.round(data.frameWidth - (data.targetImageWidth * 0.5) - data.marginMax);
data.boundsRight = Math.round((data.targetImageWidth * 0.5) + data.marginMax);
data.boundsTop = Math.round(data.frameHeight - (data.targetImageHeight * 0.5) - data.marginMax);
data.boundsBottom = Math.round((data.targetImageHeight * 0.5) + data.marginMax);
// Check dragging bounds
if (data.targetPositionerLeft < data.boundsLeft) {
data.targetPositionerLeft = data.boundsLeft;
}
if (data.targetPositionerLeft > data.boundsRight) {
data.targetPositionerLeft = data.boundsRight;
}
if (data.targetPositionerTop < data.boundsTop) {
data.targetPositionerTop = data.boundsTop;
}
if (data.targetPositionerTop > data.boundsBottom) {
data.targetPositionerTop = data.boundsBottom;
}
// Zoom to visible area of image
if (data.zoomPositionTop > 0 && data.zoomPositionLeft > 0) {
data.targetPositionerLeft = data.centerLeft - data.targetImageLeft - (data.targetImageWidth * data.zoomPositionLeft);
data.targetPositionerTop = data.centerTop - data.targetImageTop - (data.targetImageHeight * data.zoomPositionTop);
}
if (data.action !== "pinch") {
// Recenter when small enough
if (data.targetImageWidth < data.frameWidth) {
data.targetPositionerLeft = data.centerLeft;
}
if (data.targetImageHeight < data.frameHeight) {
data.targetPositionerTop = data.centerTop;
}
}
// Calculate new positions
if (data.action === "drag" || data.action === "pinch") {
data.positionerLeft = data.targetPositionerLeft;
data.positionerTop = data.targetPositionerTop;
} else {
data.positionerLeft += Math.round((data.targetPositionerLeft - data.positionerLeft) * data.enertia);
data.positionerTop += Math.round((data.targetPositionerTop - data.positionerTop) * data.enertia);
}
data.oldImageWidth = data.imageWidth;
data.oldImageHeight = data.imageHeight;
return data;
}
/**
* @method private
* @name _nextImage
* @description Handles next button click
* @param e [object] "Event Data"
*/
function _nextImage(e) {
var data = e.data;
if (!data.loading && data.index+1 < data.images.length) {
data.index++;
_load.apply(data.$target, [ data ]);
}
}
/**
* @method private
* @name _previousImage
* @description Handles previous button click
* @param e [object] "Event Data"
*/
function _previousImage(e) {
var data = e.data;
if (!data.loading && data.index-1 >= 0) {
data.index--;
_load.apply(data.$target, [ data ]);
}
}
/**
* @method private
* @name _zoomIn
* @description Handles zoom in button click
* @param e [object] "Event Data"
*/
function _zoomIn(e) {
e.preventDefault();
e.stopPropagation();
var data = e.data;
data = _setZoomPosition(data);
data.keyDownTime = 1;
data.action = "zoom_in";
}
/**
* @method private
* @name _zoomOut
* @description Handles zoom out button click
* @param e [object] "Event Data"
*/
function _zoomOut(e) {
e.preventDefault();
e.stopPropagation();
var data = e.data;
data = _setZoomPosition(data);
data.keyDownTime = 1;
data.action = "zoom_out";
}
/**
* @method private
* @name _clearZoom
* @description Clears current zoom action
* @param e [object] "Event Data"
*/
function _clearZoom(e) {
e.preventDefault();
e.stopPropagation();
var data = e.data;
data = _clearZoomPosition(data);
data.keyDownTime = 0;
data.action = "";
}
/**
* @method private
* @name _setZoomPosition
* @description Sets zoom position
* @param data [object] "Instance Data"
* @param left [number] "Left position"
* @param top [number] "Top position"
*/
function _setZoomPosition(data, left, top) {
left = left || (data.imageWidth * 0.5);
top = top || (data.imageHeight * 0.5);
data.zoomPositionLeft = ((left - (data.positionerLeft - data.centerLeft)) / data.imageWidth);
data.zoomPositionTop = ((top - (data.positionerTop - data.centerTop)) / data.imageHeight);
return data;
}
/**
* @method private
* @name _clearZoomPosition
* @description Clears zoom position
* @param data [object] "Instance Data"
*/
function _clearZoomPosition(data) {
data.zoomPositionTop = 0;
data.zoomPositionLeft = 0;
return data;
}
/**
* @method private
* @name _dragStart
* @description Handles drag start
* @param e [object] "Event Data"
*/
function _dragStart(e) {
if (e.preventDefault) {
e.preventDefault();
e.stopPropagation();
}
var data = e.data;
data.action = "drag";
data.mouseX = e.pageX;
data.mouseY = e.pageY;
data.targetPositionerLeft = data.positionerLeft;
data.targetPositionerTop = data.positionerTop;
$window.on("mousemove.zoomer", data, _onDrag)
.on("mouseup.zoomer", data, _dragStop);
}
/**
* @method private
* @name _onDrag
* @description Handles dragging
* @param e [object] "Event Data"
*/
function _onDrag(e) {
if (e.preventDefault) {
e.preventDefault();
e.stopPropagation();
}
var data = e.data;
if (e.pageX && e.pageY) {
data.targetPositionerLeft -= Math.round(data.mouseX - e.pageX);
data.targetPositionerTop -= Math.round(data.mouseY - e.pageY);
data.mouseX = e.pageX;
data.mouseY = e.pageY;
}
}
/**
* @method private
* @name _dragStop
* @description Handles drag end
* @param e [object] "Event Data"
*/
function _dragStop(e) {
if (e.preventDefault) {
e.preventDefault();
e.stopPropagation();
}
var data = e.data;
data.action = "";
$window.off("mousemove.zoomer mouseup.zoomer");
}
/**
* @method private
* @name _onTouch
* @description Delegates touch event
* @param e [object] "Event Data"
*/
function _onTouch(e) {
if ($(e.target).parent(".zoomer-controls").length > 0) {
return;
}
// Stop ms panning and zooming
if (e.preventManipulation) {
e.preventManipulation();
}
e.preventDefault();
e.stopPropagation();
var data = e.data,
oe = e.originalEvent;
if (oe.type.match(/(up|end)$/i)) {
_onTouchEnd(data, oe);
return;
}