-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo-min.html
325 lines (246 loc) · 10.8 KB
/
demo-min.html
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
<!doctype html>
<html>
<head>
<title>Panzoom for jQuery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="timmywil-jquery.panzoom-734dadf/test/libs/jquery.js"></script>
<script src="timmywil-jquery.panzoom-734dadf/dist/jquery.panzoom.js"></script>
<script src="timmywil-jquery.panzoom-734dadf/test/libs/jquery.mousewheel.js"></script>
<!-- Load livereload if not phantom or github -->
<script>
if ( !window.phantom && !/github/.test(window.location.hostname) ) document.write('<scr' + 'ipt src="http://' + location.hostname + ':35711/livereload.js"></scr' + 'ipt>');
</script>
<!-- Image view filed with zoom https://timmywil.github.io/jquery.panzoom/ -->
<style type="text/css">
body, html {height:100%;}
/* panzoom wrapper div */
#wrapper {
padding-left: 0;
background: #121212;
height:100%;
width:100%;
overflow: hidden;/*no scrollbars*/
border: 0px;
}
body { background: #F5FCFF; color: #333666; }
section { text-align: center; height:100%; border: 0px;}
.panzoom-parent { border: 0px; height:100%;}
.panzoom-parent .panzoom { border: 10px solid #A00; display: inline-block;position: absolute; top: 0%; left:0% }
.buttons { left: 50%;
transform: translateX(-50%);
display: table;
position:fixed;
bottom:0%;
width: 50%;
}
.file-input-wrapper {
height: 3em;
margin: 0;
overflow: hidden;
width: 15em;
background-color: #080808;
cursor: pointer;
left: 0%;
display: table;
position:fixed;
bottom:0%;
}
.file-input-wrapper > input[type="file"] {
font-size: 1em;
position: absolute;
top: 0;
right: 0;
opacity: 0;
cursor: pointer;
}
.file-input-wrapper > .btn-file-input {
/*background-color: #080808;*/
border-radius: 0px;
/*color: #000;*/
display: inline-block;
height: 3em;
margin: 0 0 0 -1px;
padding-left: 0;
width: 10em;
cursor: pointer;
}
.file-input-wrapper:hover > .btn-file-input {
/*background-color: #080808;*/
}
</style>
</head>
<body>
<div id="wrapper">
<section id="pan-when-zoomed">
<div class="panzoom-parent" id="maincontainer">
<div class="panzoom">
<img src="xxx003-512.png" class='cimage' id="imgcontainer" style="border: 2px solid #AAA;">
</div>
</div>
<div class="buttons">
<button class="zoom-in">Zoom In</button>
<button class="zoom-out">Zoom Out</button>
<button class="reset">Reset</button>
<input type="range" class="zoom-range">
</div>
<script>
//typical panzoom setup
(function() {
var $section = $('#pan-when-zoomed');
$section.find('.panzoom').panzoom({
$zoomIn: $section.find(".zoom-in"),
$zoomOut: $section.find(".zoom-out"),
$zoomRange: $section.find(".zoom-range"),
$reset: $section.find(".reset"),
panOnlyWhenZoomed: false,
startTransform: 'scale(1.0)',
minScale: 0.1,
increment: 0.1
});
// mouse wheel
var $panzoom = $section.find('.panzoom').panzoom();
$panzoom.parent().on('mousewheel.focal', function( e ) {
e.preventDefault();
var delta = e.delta || e.originalEvent.wheelDelta;
var zoomOut = delta ? delta < 0 : e.originalEvent.deltaY > 0;
$panzoom.panzoom('zoom', zoomOut, {
animate: false,
focal: e
});
});
})();
//http://stackoverflow.com/questions/26004008/resizing-to-fit-height-jquery-panzoom
// $(document).ready(function(){ //buggy on chrome
var $section = $('#pan-when-zoomed');
var $panzoom = $section.find('.panzoom').panzoom();
var imgref = document.getElementById('imgcontainer');
//Wait for the image to load //when image is loaded and we know its dimensions, we scale the viewport
setinitzoom = function(){
//Get container and image
//var image = $('.cimage');
var container = $('.panzoom-parent');
//Get container dimensions
/*
var container_height = container.height();
var container_width = container.width();
*/
var container_height = window.innerHeight;
var container_width = window.innerWidth;
//Get image dimensions
var imgref2 = document.getElementById('imgcontainer');
//var image_height = image.height();
//var image_width = image.width();
var image_height = imgref2.height;
var image_width = imgref2.width;
//Calculate the center of image since origin is at x:50% y:50%
var image_center_left = image_width / 2.0;
var image_center_top = image_height / 2.0;
//Calculate scaling factor
var zoom_factor;
//Check to determine whether to stretch along width or heigh
/*
if(image_height > image_width)
zoom_factor = container_height / image_height;
else
zoom_factor = container_width / image_width;
*/
//zoom_factor = container_height / image_height;
// I want it to scale according to the biggest dimension
var scalefactor_w = container_width/image_width;
var scalefactor_h = container_height/image_height;
zoom_factor = Math.min(scalefactor_w,scalefactor_h);
//Zoom by zoom_factor
$panzoom.panzoom("zoom", zoom_factor, {animate: false});
//Calculate new image dimensions after zoom
image_width = image_width * zoom_factor;
image_height = image_height * zoom_factor;
//Calculate offset of the image after zoom
var image_offset_left = image_center_left - (image_width / 2.0);
var image_offset_top = image_center_top - (image_height / 2.0);
//Calculate desired offset for image
var new_offset_left = (container_width - image_width) / 2.0;
var new_offset_top = (container_height - image_height) / 2.0;
//Pan to set desired offset for image
var pan_left = new_offset_left - image_offset_left;
var pan_top = new_offset_top - image_offset_top;
$panzoom.panzoom("pan", pan_left, pan_top);
};
//}); // $(document).ready(function()
$(document).ready(function(){
imgref.onload = setinitzoom();
});
</script>
</div>
<div class="file-input-wrapper" style="text-align:center;">
<button class="btn-file-input">Input Image</button>
<input type="file" name="image" id="imageinput" value="" accept="image/*" capture="camera" />
</div>
<script>
//dynamically load images specified by imput dialog
// function that fires event when image size is available. Onload() event works bad for dynamically added content
// I've tried a lot of things, so this solution is a good one
function getImageSize(img, callback) {
var $img = $(img);
var wait = setInterval(function() {
var w = $img[0].naturalWidth,
h = $img[0].naturalHeight;
if (w && h) {
clearInterval(wait);
callback.apply(this, [w, h]);
}
}, 30);
}
// handle file from input dialog
var input = document.getElementById('imageinput');
input.addEventListener('change', handleFiles);
function handleFiles(e) {
// load image into temporary container, no need to affect #imagecontainer at the moment
// garbage collector will handle it at the end
var tempimg = new Image();
tempimg.src = URL.createObjectURL(e.target.files[0]);
// that would not work for dynamically added images!
//imgref.onload = setinitzoom();
getImageSize($(tempimg), function(width, height) {
//alert(width+ ' ' +height);
// take out #imagecontainer
var imgref = document.getElementById('imgcontainer');
// destroying old instance. Unfortunately, this is the only way to get panzoom work really correctly with dynamically loaded images:
// https://github.com/timmywil/jquery.panzoom/issues/132
var $section = $('#pan-when-zoomed');
var panzoom = $section.find('.panzoom').panzoom();
panzoom.panzoom("destroy");
delete panzoom;
// and then attach new image!
imgref.src = tempimg.src;
//let it be loading further then, we have obtained dimensions needed by panzoom at the moment
// new instance
(function() {
var $section = $('#pan-when-zoomed');
$section.find('.panzoom').panzoom({
$zoomIn: $section.find(".zoom-in"),
$zoomOut: $section.find(".zoom-out"),
$zoomRange: $section.find(".zoom-range"),
$reset: $section.find(".reset"),
panOnlyWhenZoomed: false,
startTransform: 'scale(1.0)',
minScale: 0.1,
increment: 0.1
});
var $panzoom = $section.find('.panzoom').panzoom();
$panzoom.parent().on('mousewheel.focal', function( e ) {
e.preventDefault();
var delta = e.delta || e.originalEvent.wheelDelta;
var zoomOut = delta ? delta < 0 : e.originalEvent.deltaY > 0;
$panzoom.panzoom('zoom', zoomOut, {
animate: false,
focal: e
});
});
})();
// resizing and panning the image
setinitzoom(width, height);
});
}
</script>
</body>
</html>