##// END OF EJS Templates
Fixed image popup scale
neko259 -
r1351:21f71300 default
parent child Browse files
Show More
@@ -1,160 +1,168 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 26
27 27 var IMAGE_VIEWERS = [
28 28 ['simple', new SimpleImageViewer()],
29 29 ['popup', new PopupImageViewer()]
30 30 ];
31 31
32 32 var FULL_IMG_CLASS = 'post-image-full';
33 33
34 34
35 35 function ImageViewer() {}
36 36 ImageViewer.prototype.view = function (post) {};
37 37
38 38 function SimpleImageViewer() {}
39 39 SimpleImageViewer.prototype.view = function (post) {
40 40 var images = post.find('img');
41 41 images.toggle();
42 42
43 43 // When we first enlarge an image, a full image needs to be created
44 44 if (images.length == 1) {
45 45 var thumb = images.first();
46 46
47 47 var width = thumb.attr('data-width');
48 48 var height = thumb.attr('data-height');
49 49
50 50 if (width == null || height == null) {
51 51 width = '100%';
52 52 height = '100%';
53 53 }
54 54
55 55 var parent = images.first().parent();
56 56 var link = parent.attr('href');
57 57
58 58 var fullImg = $('<img />')
59 59 .addClass(FULL_IMG_CLASS)
60 60 .attr('src', link)
61 61 .attr('width', width)
62 62 .attr('height', height);
63 63
64 64 parent.append(fullImg);
65 65 }
66 66 };
67 67
68 68 function PopupImageViewer() {}
69 69 PopupImageViewer.prototype.view = function (post) {
70 70 var margin = 20; //..change
71 71
72 72 var el = post;
73 73 var thumb_id = 'full' + el.find('img').attr('alt');
74 74
75 75 var existingPopups = $('#' + thumb_id);
76 76 if(!existingPopups.length) {
77 77 var imgElement= el.find('img');
78 78
79 79 var img_w = imgElement.attr('data-width');
80 80 var img_h = imgElement.attr('data-height');
81 81
82 82 var win = $(window);
83 83
84 84 var win_w = win.width();
85 85 var win_h = win.height();
86 //new image size
86
87 // New image size
88 var w_scale = 1;
89 var h_scale = 1;
87 90 if (img_w > win_w) {
88 img_h = img_h * (win_w/img_w) - margin;
89 img_w = win_w - margin;
91 w_scale = img_w / (win_w - margin);
90 92 }
91 93 if (img_h > win_h) {
92 img_w = img_w * (win_h/img_h) - margin;
93 img_h = win_h - margin;
94 h_scale = img_h / (win_h - margin);
94 95 }
95 96
97 var scale = Math.max(w_scale, h_scale)
98 img_w = img_w / scale;
99 img_h = img_h / scale;
100
96 101 var img_pv = new Image();
97 102 var newImage = $(img_pv);
98 103 newImage.addClass('img-full')
99 104 .attr('id', thumb_id)
100 105 .attr('src', $(el).attr('href'))
101 106 .appendTo($(el))
102 107 .css({
103 108 'width': img_w,
104 109 'height': img_h,
105 110 'left': (win_w - img_w) / 2,
106 111 'top': ((win_h - img_h) / 2)
107 112 })
108 113 //scaling preview
109 114 .mousewheel(function(event, delta) {
110 var cx = event.originalEvent.clientX,
111 cy = event.originalEvent.clientY,
112 i_w = parseFloat(newImage.width()),
113 i_h = parseFloat(newImage.height()),
114 newIW = i_w * (delta > 0 ? 1.25 : 0.8),
115 newIH = i_h * (delta > 0 ? 1.25 : 0.8);
115 var cx = event.originalEvent.clientX;
116 var cy = event.originalEvent.clientY;
117
118 var i_w = parseFloat(newImage.width());
119 var i_h = parseFloat(newImage.height());
120
121 var newIW = i_w * (delta > 0 ? 1.25 : 0.8);
122 var newIH = i_h * (delta > 0 ? 1.25 : 0.8);
116 123
117 124 newImage.width(newIW);
118 125 newImage.height(newIH);
119 //set position
126 // Set position
127 var oldPosition = newImage.position();
120 128 newImage.css({
121 left: parseInt(cx - (newIW/i_w) * (cx - parseInt($(img_pv).position().left, 10)), 10),
122 top: parseInt(cy - (newIH/i_h) * (cy - parseInt($(img_pv).position().top, 10)), 10)
129 left: parseInt(cx - (newIW/i_w) * (cx - parseInt(oldPosition.left, 10)), 10),
130 top: parseInt(cy - (newIH/i_h) * (cy - parseInt(oldPosition.top, 10)), 10)
123 131 });
124 132
125 133 return false;
126 134 }
127 135 )
128 136 .draggable({
129 137 addClasses: false,
130 138 stack: '.img-full'
131 139 });
132 140 } else {
133 141 existingPopups.remove();
134 142 }
135 143 };
136 144
137 145 function addImgPreview() {
138 146 var viewerName = $('body').attr('data-image-viewer');
139 147 var viewer = ImageViewer();
140 148 for (var i = 0; i < IMAGE_VIEWERS.length; i++) {
141 149 var item = IMAGE_VIEWERS[i];
142 150 if (item[0] === viewerName) {
143 151 viewer = item[1];
144 152 break;
145 153 }
146 154 }
147 155
148 156 //keybind
149 157 $(document).on('keyup.removepic', function(e) {
150 158 if(e.which === 27) {
151 159 $('.img-full').remove();
152 160 }
153 161 });
154 162
155 163 $('body').on('click', '.thumb', function() {
156 164 viewer.view($(this));
157 165
158 166 return false;
159 167 });
160 168 }
General Comments 0
You need to be logged in to leave comments. Login now