forked from natashawylie/iviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.iviewer.js
588 lines (498 loc) · 16.8 KB
/
jquery.iviewer.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
/*
* iviewer Plugin for jQuery JavaScript Library
* https://github.com/can3p/iviewer
*
* Copyright (c) 2009 - 2011 Dmitry Petrov
* Dual licensed under the MIT and GPL licenses.
* - http://www.opensource.org/licenses/mit-license.php
* - http://www.gnu.org/copyleft/gpl.html
*
* Author: Dmitry Petrov
* Version: dev
* Date:
*/
( function( $, undefined ) {
$.widget( "ui.iviewer", $.ui.mouse, {
widgetEventPrefix: "iviewer",
options : {
/**
* start zoom value for image, not used now
* may be equal to "fit" to fit image into container or scale in %
**/
zoom: "fit",
/**
* base value to scale image
**/
zoom_base: 100,
/**
* maximum zoom
**/
zoom_max: 800,
/**
* minimum zoom
**/
zoom_min: 25,
/**
* base of rate multiplier.
* zoom is calculated by formula: zoom_base * zoom_delta^rate
**/
zoom_delta: 1.4,
/**
* if true plugin doesn't add its own controls
**/
ui_disabled: false,
/**
* if false, plugin doesn't bind resize event on window and this must
* be handled manually
**/
update_on_resize: true,
/**
* event is triggered when zoom value is changed
* @param int new zoom value
* @return boolean if false zoom action is aborted
**/
onZoom: null,
/**
* callback is fired after plugin setup
**/
initCallback: null,
/**
* event is fired on drag begin
* @param object coords mouse coordinates on the image
* @return boolean if false is returned, drag action is aborted
**/
onStartDrag: null,
/**
* event is fired on drag action
* @param object coords mouse coordinates on the image
**/
onDrag: null,
/**
* event is fired when mouse moves over image
* @param object coords mouse coordinates on the image
**/
onMouseMove: null,
/**
* mouse click event
* @param object coords mouse coordinates on the image
**/
onClick: null,
/**
* event is fired when image starts to load
*/
onStartLoad: null,
/**
* event is fired, when image is loaded and initially positioned
*/
onFinishLoad: null
},
_create: function() {
var me = this;
//drag variables
this.dx = 0;
this.dy = 0;
this.dragged = false;
/* object containing actual information about image
* @img_object.object - jquery img object
* @img_object.orig_{width|height} - original dimensions
* @img_object.display_{width|height} - actual dimensions
*/
this.img_object = {};
this.zoom_object = {}; //object to show zoom status
this.image_loaded = false;
this.current_zoom = this.options.zoom;
if(this.options.src === null){
return;
}
this.container = this.element;
this._updateContainerInfo();
//init container
this.container.css("overflow","hidden");
if(this.options.update_on_resize == true)
{
$(window).resize(function()
{
me._updateContainerInfo();
});
}
this.img_object.x = 0;
this.img_object.y = 0;
//init object
this.img_object.object = $("<img>").
css({ position: "absolute", top :"0px", left: "0px"}). //this is needed, because chromium sets them auto otherwise
//bind mouse events
click(function(e){return me.click(e)}).
mousewheel(function(ev, delta)
{
//this event is there instead of containing div, because
//at opera it triggers many times on div
var zoom = (delta > 0)?1:-1;
me.zoom_by(zoom);
return false;
});
this.img_object.object.prependTo(me.container);
this.loadImage(this.options.src);
if(!this.options.ui_disabled)
{
this.createui();
}
if(this.options.initCallback)
{
this.options.initCallback.call(this);
}
this._mouseInit();
},
destroy: function() {
this._mouseDestroy();
},
_updateContainerInfo: function()
{
this.options.height = this.container.height();
this.options.width = this.container.width();
},
loadImage: function( src )
{
this.current_zoom = this.options.zoom;
this.image_loaded = false;
var me = this;
if(this.options.onStartLoad)
{
this.options.onStartLoad.call(this);
}
this.img_object.object.unbind('load').
removeAttr("src").
removeAttr("width").
removeAttr("height").
css({ top: 0, left: 0 }).
load(function(){
me.image_loaded = true;
me.img_object.display_width = me.img_object.orig_width = this.width;
me.img_object.display_height = me.img_object.orig_height = this.height;
if(!me.container.hasClass("iviewer_cursor")){
me.container.addClass("iviewer_cursor");
}
if(me.options.zoom == "fit"){
me.fit();
}
else {
me.set_zoom(me.options.zoom);
}
if(me.options.onFinishLoad)
{
me.options.onFinishLoad.call(me);
}
//src attribute is after setting load event, or it won't work
}).attr("src",src);
},
/**
* fits image in the container
**/
fit: function()
{
var aspect_ratio = this.img_object.orig_width / this.img_object.orig_height;
var window_ratio = this.options.width / this.options.height;
var choose_left = (aspect_ratio > window_ratio);
var new_zoom = 0;
if(choose_left){
new_zoom = this.options.width / this.img_object.orig_width * 100;
}
else {
new_zoom = this.options.height / this.img_object.orig_height * 100;
}
this.set_zoom(new_zoom);
},
/**
* center image in container
**/
center: function()
{
this.setCoords(-Math.round((this.img_object.display_height - this.options.height)/2),
-Math.round((this.img_object.display_width - this.options.width)/2));
},
/**
* move a point in container to the center of display area
* @param x a point in container
* @param y a point in container
**/
moveTo: function(x, y)
{
var dx = x-Math.round(this.options.width/2);
var dy = y-Math.round(this.options.height/2);
var new_x = this.img_object.x - this.dx;
var new_y = this.img_object.y - this.dy;
this.setCoords(new_x, new_y);
},
/**
* set coordinates of upper left corner of image object
**/
setCoords: function(x,y)
{
//do nothing while image is being loaded
if(!this.image_loaded)
{
return;
}
$.extend( this.img_object, this._correctCoords( x, y ) );
this.img_object.object.css("top",this.img_object.y + "px")
.css("left",this.img_object.x + "px");
},
_correctCoords: function( x, y )
{
//check new coordinates to be correct (to be in rect)
if(y > 0){
y = 0;
}
if(x > 0){
x = 0;
}
if(y + this.img_object.display_height < this.options.height){
y = this.options.height - this.img_object.display_height;
}
if(x + this.img_object.display_width < this.options.width){
x = this.options.width - this.img_object.display_width;
}
if(this.img_object.display_width <= this.options.width){
x = -(this.img_object.display_width - this.options.width)/2;
}
if(this.img_object.display_height <= this.options.height){
y = -(this.img_object.display_height - this.options.height)/2;
}
return { x: x, y:y };
},
/**
* convert coordinates on the container to the coordinates on the image (in original size)
*
* @return object with fields x,y according to coordinates or false
* if initial coords are not inside image
**/
containerToImage : function (x,y)
{
if(x < this.img_object.x || y < this.img_object.y ||
x > this.img_object.x + this.img_object.display_width ||
y > this.img_object.y + this.img_object.display_height)
{
return false;
}
return { x : util.descaleValue(x - this.img_object.x, this.current_zoom),
y : util.descaleValue(y - this.img_object.y, this.current_zoom)
};
},
/**
* convert coordinates on the image (in original size) to the coordinates on the container
*
* @return object with fields x,y according to coordinates or false
* if initial coords are not inside image
**/
imageToContainer : function (x,y)
{
if(x > this.img_object.orig_width || y > this.img_object.orig_height)
{
return false;
}
return { x : this.img_object.x + util.scaleValue(x, this.current_zoom),
y : this.img_object.y + util.scaleValue(y, this.current_zoom)
};
},
/**
* get mouse coordinates on the image
* @param e - object containing pageX and pageY fields, e.g. mouse event object
*
* @return object with fields x,y according to coordinates or false
* if initial coords are not inside image
**/
getMouseCoords : function(e)
{
var img_offset = this.img_object.object.offset();
return { x : util.descaleValue(e.pageX - img_offset.left, this.current_zoom),
y : util.descaleValue(e.pageY - img_offset.top, this.current_zoom)
};
},
/**
* set image scale to the new_zoom
* @param new_zoom image scale in %
**/
set_zoom: function(new_zoom)
{
if(this.options.onZoom && this.options.onZoom.call(this, new_zoom) == false)
{
return;
}
//do nothing while image is being loaded
if(!this.image_loaded)
{
return;
}
if(new_zoom < this.options.zoom_min)
{
new_zoom = this.options.zoom_min;
}
else if(new_zoom > this.options.zoom_max)
{
new_zoom = this.options.zoom_max;
}
/* we fake these values to make fit zoom properly work */
if(this.current_zoom == "fit")
{
var old_x = Math.round(this.options.width/2 + this.img_object.orig_width/2);
var old_y = Math.round(this.options.height/2 + this.img_object.orig_height/2);
this.current_zoom = 100;
}
else {
var old_x = -parseInt(this.img_object.object.css("left"),10) +
Math.round(this.options.width/2);
var old_y = -parseInt(this.img_object.object.css("top"),10) +
Math.round(this.options.height/2);
}
var new_width = util.scaleValue(this.img_object.orig_width, new_zoom);
var new_height = util.scaleValue(this.img_object.orig_height, new_zoom);
var new_x = util.scaleValue( util.descaleValue(old_x, this.current_zoom), new_zoom);
var new_y = util.scaleValue( util.descaleValue(old_y, this.current_zoom), new_zoom);
new_x = this.options.width/2 - new_x;
new_y = this.options.height/2 - new_y;
this.img_object.display_width = new_width;
this.img_object.display_height = new_height;
$.extend( this.img_object, this._correctCoords( new_x, new_y ) );
this.img_object.object.animate( { width: new_width, height: new_height, top: this.img_object.y, left: this.img_object.x }, 200 );
this.current_zoom = new_zoom;
$.isFunction( this.options.onAfterZoom ) && this.options.onAfterZoom.call( this, new_zoom );
this.update_status();
},
/**
* changes zoom scale by delta
* zoom is calculated by formula: zoom_base * zoom_delta^rate
* @param Integer delta number to add to the current multiplier rate number
**/
zoom_by: function(delta)
{
var closest_rate = this.find_closest_zoom_rate(this.current_zoom);
var next_rate = closest_rate + delta;
var next_zoom = this.options.zoom_base * Math.pow(this.options.zoom_delta, next_rate)
if(delta > 0 && next_zoom < this.current_zoom)
{
next_zoom *= this.options.zoom_delta;
}
if(delta < 0 && next_zoom > this.current_zoom)
{
next_zoom /= this.options.zoom_delta;
}
this.set_zoom(next_zoom);
},
/**
* finds closest multiplier rate for value
* basing on zoom_base and zoom_delta values from settings
* @param Number value zoom value to examine
**/
find_closest_zoom_rate: function(value)
{
if(value == this.options.zoom_base)
{
return 0;
}
function div(val1,val2) { return val1 / val2 };
function mul(val1,val2) { return val1 * val2 };
var func = (value > this.options.zoom_base)?mul:div;
var sgn = (value > this.options.zoom_base)?1:-1;
var mltplr = this.options.zoom_delta;
var rate = 1;
while(Math.abs(func(this.options.zoom_base, Math.pow(mltplr,rate)) - value) >
Math.abs(func(this.options.zoom_base, Math.pow(mltplr,rate+1)) - value))
{
rate++;
}
return sgn * rate;
},
/* update scale info in the container */
update_status: function()
{
if(!this.options.ui_disabled)
{
var percent = Math.round(100*this.img_object.display_height/this.img_object.orig_height);
if(percent)
{
this.zoom_object.html(percent + "%");
}
}
},
/**
* callback for handling mousdown event to start dragging image
**/
_mouseStart: function( e )
{
if(this.options.onStartDrag &&
this.options.onStartDrag.call(this,this.getMouseCoords(e)) == false)
{
return false;
}
/* start drag event*/
this.dragged = true;
this.container.addClass("iviewer_drag_cursor");
this.dx = e.pageX - this.img_object.x;
this.dy = e.pageY - this.img_object.y;
return true;
},
_mouseCapture: function( e ) {
return true;
},
/**
* callback for handling mousmove event to drag image
**/
_mouseDrag: function(e)
{
this.options.onMouseMove &&
this.options.onMouseMove.call(this,this.getMouseCoords(e));
if(this.dragged){
this.options.onDrag &&
this.options.onDrag.call(this,this.getMouseCoords(e));
var ltop = e.pageY -this.dy;
var lleft = e.pageX -this.dx;
this.setCoords(lleft, ltop);
return false;
}
},
/**
* callback for handling stop drag
**/
_mouseStop: function(e)
{
this.container.removeClass("iviewer_drag_cursor");
this.dragged=false;
},
click: function(e)
{
this.options.onClick &&
this.options.onClick.call(this,this.getMouseCoords(e));
},
/**
* create zoom buttons info box
**/
createui: function()
{
var me=this;
$("<div>").addClass("iviewer_zoom_in").addClass("iviewer_common").
addClass("iviewer_button").
mousedown(function(){me.zoom_by(1); return false;}).appendTo(this.container);
$("<div>").addClass("iviewer_zoom_out").addClass("iviewer_common").
addClass("iviewer_button").
mousedown(function(){me.zoom_by(- 1); return false;}).appendTo(this.container);
$("<div>").addClass("iviewer_zoom_zero").addClass("iviewer_common").
addClass("iviewer_button").
mousedown(function(){me.set_zoom(100); return false;}).appendTo(this.container);
$("<div>").addClass("iviewer_zoom_fit").addClass("iviewer_common").
addClass("iviewer_button").
mousedown(function(){me.fit(this); return false;}).appendTo(this.container);
this.zoom_object = $("<div>").addClass("iviewer_zoom_status").addClass("iviewer_common").
appendTo(this.container);
this.update_status(); //initial status update
}
} );
var util = {
scaleValue: function(value, toZoom)
{
return value * toZoom / 100;
},
descaleValue: function(value, fromZoom)
{
return value * 100 / fromZoom;
}
};
} )( jQuery, undefined );