JQuery.iviewer
from: http://test.dpetroff.ru/jquery.iviewer/test/#
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 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 | /* * iviewer Widget for jQuery UI * https://github.com/can3p/iviewer * * Copyright (c) 2009 - 2012 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: 0.7.4 */ ( function ( $, undefined ) { //this code was taken from the https://github.com/furf/jquery-ui-touch-punch var mouseEvents = { touchstart: 'mousedown' , touchmove: 'mousemove' , touchend: 'mouseup' }, gesturesSupport = 'ongesturestart' in document.createElement( 'div' ); /** * Convert a touch event to a mouse-like */ function makeMouseEvent (event) { var touch = event.originalEvent.changedTouches[0]; return $.extend(event, { type: mouseEvents[event.type], which: 1, pageX: touch.pageX, pageY: touch.pageY, screenX: touch.screenX, screenY: touch.screenY, clientX: touch.clientX, clientY: touch.clientY, isTouchEvent: true }); } var mouseProto = $.ui.mouse.prototype, _mouseInit = $.ui.mouse.prototype._mouseInit; mouseProto._mouseInit = function () { var self = this ; self._touchActive = false ; this .element.bind( 'touchstart.' + this .widgetName, function (event) { if (gesturesSupport && event.originalEvent.touches.length > 1) { return ; } self._touchActive = true ; return self._mouseDown(makeMouseEvent(event)); }) var self = this ; // these delegates are required to keep context this ._mouseMoveDelegate = function (event) { if (gesturesSupport && event.originalEvent.touches && event.originalEvent.touches.length > 1) { return ; } if (self._touchActive) { return self._mouseMove(makeMouseEvent(event)); } }; this ._mouseUpDelegate = function (event) { if (self._touchActive) { self._touchActive = false ; return self._mouseUp(makeMouseEvent(event)); } }; $(document) .bind( 'touchmove.' + this .widgetName, this ._mouseMoveDelegate) .bind( 'touchend.' + this .widgetName, this ._mouseUpDelegate); _mouseInit.apply( this ); } /** * Simple implementation of jQuery like getters/setters * var val = something(); * something(val); */ var setter = function (setter, getter) { return function (val) { if (arguments.length === 0) { return getter.apply( this ); } else { setter.apply( this , arguments); } } }; /** * Internet explorer rotates image relative left top corner, so we should * shift image when it's rotated. */ var ieTransforms = { '0 ': { marginLeft: 0, marginTop: 0, filter: ' progid:DXImageTransform.Microsoft.Matrix(M11=1, M12=0, M21=0, M22=1, SizingMethod= "auto expand" ) ' }, ' 90 ': { marginLeft: -1, marginTop: 1, filter: ' progid:DXImageTransform.Microsoft.Matrix(M11=0, M12=-1, M21=1, M22=0, SizingMethod= "auto expand" ) ' }, ' 180 ': { marginLeft: 0, marginTop: 0, filter: ' progid:DXImageTransform.Microsoft.Matrix(M11=-1, M12=0, M21=0, M22=-1, SizingMethod= "auto expand" ) ' }, ' 270 ': { marginLeft: -1, marginTop: 1, filter: ' progid:DXImageTransform.Microsoft.Matrix(M11=0, M12=1, M21=-1, M22=0, SizingMethod= "auto expand" ) ' } }, useIeTransforms = (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) <= 8); $.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, /** * whether the zoom should be animated. */ zoom_animation: true, /** * if true plugin doesn' t add its own controls **/ ui_disabled: false , /** * If false mousewheel will be disabled */ mousewheel: true , /** * 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: jQuery.noop, /** * event is triggered when zoom value is changed after image is set to the new dimensions * @param int new zoom value * @return boolean if false zoom action is aborted **/ onAfterZoom: jQuery.noop, /** * 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: jQuery.noop, /** * event is fired on drag action * @param object coords mouse coordinates on the image **/ onDrag: jQuery.noop, /** * event is fired on drag stop * @param object coords mouse coordinates on the image **/ onStopDrag: jQuery.noop, /** * event is fired when mouse moves over image * @param object coords mouse coordinates on the image **/ onMouseMove: jQuery.noop, /** * mouse click event * @param object coords mouse coordinates on the image **/ onClick: jQuery.noop, /** * 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; /* 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 ._angle = 0; 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 = new $.ui.iviewer.ImageObject( this .options.zoom_animation); if ( this .options.mousewheel) { this .img_object.object() .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, container_offset = me.container.offset(), mouse_pos = { x: ev.pageX - container_offset.left, y: ev.pageY - container_offset.top }; me.zoom_by(zoom, mouse_pos); return false ; }); if (gesturesSupport) { var gestureThrottle = + new Date(); var originalScale, originalCenter; this .img_object.object() // .bind('gesturestart', function(ev) { .bind('touchstart ', function(ev) { originalScale = me.current_zoom; var touches = ev.originalEvent.touches, container_offset; if (touches.length == 2) { container_offset = me.container.offset(); originalCenter = { x: (touches[0].pageX + touches[1].pageX) / 2 - container_offset.left, y: (touches[0].pageY + touches[1].pageY) / 2 - container_offset.top }; } else { originalCenter = null; } }).bind(' gesturechange ', function(ev) { //do not want to import throttle function from underscore var d = +new Date(); if ((d - gestureThrottle) < 50) { return; } gestureThrottle = d; var zoom = originalScale * ev.originalEvent.scale; me.set_zoom(zoom, originalCenter); ev.preventDefault(); }).bind(' gestureend ', function(ev) { originalCenter = null; }); } } //init object this.img_object.object() //bind mouse events .click(function(e){return me._click(e)}) .prependTo(this.container); this.container.bind(' mousemove ', function(ev) { me._handleMouseMove(ev); }); this.loadImage(this.options.src); if(!this.options.ui_disabled) { this.createui(); } 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; var me = this; this._trigger(' onStartLoad ', 0, src); this.container.addClass("iviewer_loading"); this.img_object.load(src, function() { me._imageLoaded(src); }); }, _imageLoaded: function(src) { this.container.removeClass("iviewer_loading"); this.container.addClass("iviewer_cursor"); if(this.options.zoom == "fit"){ this.fit(true); } else { this.set_zoom(this.options.zoom, true); } this._trigger(' onFinishLoad ', 0, src); }, /** * fits image in the container * * @param {boolean} skip_animation **/ fit: function(skip_animation) { 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, skip_animation); }, /** * center image in container **/ center: function() { this.setCoords(-Math.round((this.img_object.display_width() - this.options.width)/2), -Math.round((this.img_object.display_height() - this.options.height)/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() - dx; var new_y = this.img_object.y() - dy; this.setCoords(new_x, new_y); }, /** * Get container offset object. */ getContainerOffset: function() { return jQuery.extend({}, this.container.offset()); }, /** * set coordinates of upper left corner of image object **/ setCoords: function(x,y) { //do nothing while image is being loaded if(!this.img_object.loaded()) { return; } var coords = this._correctCoords(x,y); this.img_object.x(coords.x); this.img_object.y(coords.y); }, _correctCoords: function( x, y ) { x = parseInt(x, 10); y = parseInt(y, 10); //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) { var coords = { x : x - this.img_object.x(), y : y - this.img_object.y() }; coords = this.img_object.toOriginalCoords(coords); return { x : util.descaleValue(coords.x, this.current_zoom), y : util.descaleValue(coords.y, this.current_zoom) }; }, /** * convert coordinates on the image (in original size, and zero angle) to the coordinates on the container * * @return object with fields x,y according to coordinates **/ imageToContainer : function (x,y) { var coords = { x : util.scaleValue(x, this.current_zoom), y : util.scaleValue(y, this.current_zoom) }; return this.img_object.toRealCoords(coords); }, /** * 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 containerOffset = this.container.offset(); coords = this.containerToImage(e.pageX - containerOffset.left, e.pageY - containerOffset.top); return coords; }, /** * set image scale to the new_zoom * * @param {number} new_zoom image scale in % * @param {boolean} skip_animation * @param {x: number, y: number} Coordinates of point the should not be moved on zoom. The default is the center of image. **/ set_zoom: function(new_zoom, skip_animation, zoom_center) { if (this._trigger(' onZoom ', 0, new_zoom) == false) { return; } //do nothing while image is being loaded if(!this.img_object.loaded()) { return; } zoom_center = zoom_center || { x: Math.round(this.options.width/2), y: Math.round(this.options.height/2) } 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 = zoom_center.x + Math.round(this.img_object.orig_width()/2); var old_y = zoom_center.y + Math.round(this.img_object.orig_height()/2); this.current_zoom = 100; } else { var old_x = -this.img_object.x() + zoom_center.x; var old_y = -this.img_object.y() + zoom_center.y } 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 = zoom_center.x - new_x; new_y = zoom_center.y - new_y; new_width = Math.floor(new_width); new_height = Math.floor(new_height); new_x = Math.floor(new_x); new_y = Math.floor(new_y); this.img_object.display_width(new_width); this.img_object.display_height(new_height); var coords = this._correctCoords( new_x, new_y ), self = this; this.img_object.setImageProps(new_width, new_height, coords.x, coords.y, skip_animation, function() { self._trigger(' onAfterZoom ', 0, new_zoom ); }); this.current_zoom = 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 * @param {x: number, y: number=} Coordinates of point the should not be moved on zoom. **/ zoom_by: function(delta, zoom_center) { 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, undefined, zoom_center); }, /** * Rotate image * @param {num} deg Degrees amount to rotate. Positive values rotate image clockwise. * Currently 0, 90, 180, 270 and -90, -180, -270 values are supported * * @param {boolean} abs If the flag is true if, the deg parameter will be considered as * a absolute value and relative otherwise. * @return {num|null} Method will return current image angle if called without any arguments. **/ angle: function(deg, abs) { if (arguments.length === 0) { return this.img_object.angle(); } if (deg < -270 || deg > 270 || deg % 90 !== 0) { return; } if (!abs) { deg += this.img_object.angle(); } if (deg < 0) { deg += 360; } if (deg >= 360) { deg -= 360; } if (deg === this.img_object.angle()) { return; } this.img_object.angle(deg); //the rotate behavior is different in all editors. For now we just center the //image. However, it will be better to try to keep the position. this.center(); this._trigger(' angle ', 0, { angle: this.img_object.angle() }); }, /** * 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 + "%"); } } }, /** * Get some information about the image. * Currently orig_(width|height), display_(width|height), angle, zoom and src params are supported. * * @param {string} parameter to check * @param {boolean} withoutRotation if param is orig_width or orig_height and this flag is set to true, * method will return original image width without considering rotation. * */ info: function(param, withoutRotation) { if (!param) { return; } switch (param) { case ' orig_width ': case ' orig_height ': if (withoutRotation) { return (this.img_object.angle() % 180 === 0 ? this.img_object[param]() : param === ' orig_width ' ? this.img_object.orig_height() : this.img_object.orig_width()); } else { return this.img_object[param](); } case ' display_width ': case ' display_height ': case ' angle ': return this.img_object[param](); case ' zoom ': return this.current_zoom; case ' src ': return this.img_object.object().attr(' src '); case ' coords ': return { x: this.img_object.x(), y: this.img_object.y() }; } }, /** * callback for handling mousdown event to start dragging image **/ _mouseStart: function( e ) { $.ui.mouse.prototype._mouseStart.call(this, e); if (this._trigger(' onStartDrag ', 0, this._getMouseCoords(e)) === false) { return false; } /* start drag event*/ this.container.addClass("iviewer_drag_cursor"); //#10: fix movement quirks for ipad this._dragInitialized = !(e.originalEvent.changedTouches && e.originalEvent.changedTouches.length==1); this.dx = e.pageX - this.img_object.x(); this.dy = e.pageY - this.img_object.y(); return true; }, _mouseCapture: function( e ) { return true; }, /** * Handle mouse move if needed. User can avoid using this callback, because * he can get the same information through public methods. * @param {jQuery.Event} e */ _handleMouseMove: function(e) { this._trigger(' onMouseMove ', e, this._getMouseCoords(e)); }, /** * callback for handling mousemove event to drag image **/ _mouseDrag: function(e) { $.ui.mouse.prototype._mouseDrag.call(this, e); //#10: imitate mouseStart, because we can get here without it on iPad for some reason if (!this._dragInitialized) { this.dx = e.pageX - this.img_object.x(); this.dy = e.pageY - this.img_object.y(); this._dragInitialized = true; } var ltop = e.pageY - this.dy; var lleft = e.pageX - this.dx; this.setCoords(lleft, ltop); this._trigger(' onDrag ', e, this._getMouseCoords(e)); return false; }, /** * callback for handling stop drag **/ _mouseStop: function(e) { $.ui.mouse.prototype._mouseStop.call(this, e); this.container.removeClass("iviewer_drag_cursor"); this._trigger(' onStopDrag ', 0, this._getMouseCoords(e)); }, _click: function(e) { this._trigger(' onClick ', 0, this._getMouseCoords(e)); }, /** * create zoom buttons info box **/ createui: function() { var me=this; $("<div>", { ' class ': "iviewer_zoom_in iviewer_common iviewer_button"}) .bind(' mousedown touchstart ',function(){me.zoom_by(1); return false;}) .appendTo(this.container); $("<div>", { ' class ': "iviewer_zoom_out iviewer_common iviewer_button"}) .bind(' mousedown touchstart ',function(){me.zoom_by(- 1); return false;}) .appendTo(this.container); $("<div>", { ' class ': "iviewer_zoom_zero iviewer_common iviewer_button"}) .bind(' mousedown touchstart ',function(){me.set_zoom(100); return false;}) .appendTo(this.container); $("<div>", { ' class ': "iviewer_zoom_fit iviewer_common iviewer_button"}) .bind(' mousedown touchstart ',function(){me.fit(this); return false;}) .appendTo(this.container); this.zoom_object = $("<div>").addClass("iviewer_zoom_status iviewer_common") .appendTo(this.container); $("<div>", { ' class ': "iviewer_rotate_left iviewer_common iviewer_button"}) .bind(' mousedown touchstart ',function(){me.angle(-90); return false;}) .appendTo(this.container); $("<div>", { ' class ': "iviewer_rotate_right iviewer_common iviewer_button" }) .bind(' mousedown touchstart ',function(){me.angle(90); return false;}) .appendTo(this.container); this.update_status(); //initial status update } } ); /** * @class $.ui.iviewer.ImageObject Class represents image and provides public api without * extending image prototype. * @constructor * @param {boolean} do_anim Do we want to animate image on dimension changes? */ $.ui.iviewer.ImageObject = function(do_anim) { this._img = $("<img>") //this is needed, because chromium sets them auto otherwise .css({ position: "absolute", top :"0px", left: "0px"}); this._loaded = false; this._swapDimensions = false; this._do_anim = do_anim || false; this.x(0, true); this.y(0, true); this.angle(0); }; /** @lends $.ui.iviewer.ImageObject.prototype */ (function() { /** * Restore initial object state. * * @param {number} w Image width. * @param {number} h Image height. */ this._reset = function(w, h) { this._angle = 0; this._swapDimensions = false; this.x(0); this.y(0); this.orig_width(w); this.orig_height(h); this.display_width(w); this.display_height(h); }; /** * Check if image is loaded. * * @return {boolean} */ this.loaded = function() { return this._loaded; }; /** * Load image. * * @param {string} src Image url. * @param {Function=} loaded Function will be called on image load. */ this.load = function(src, loaded) { var self = this; loaded = loaded || jQuery.noop; this._loaded = false; //If we assign new image url to the this._img IE9 fires onload event and image width and //height are set to zero. So, we create another image object and load image through it. var img = new Image(); img.onload = function() { self._loaded = true; self._reset(this.width, this.height); self._img .removeAttr("src") .removeAttr("width") .removeAttr("height") .removeAttr("style") //max-width is reset, because plugin breaks in the twitter bootstrap otherwise .css({ position: "absolute", top :"0px", left: "0px", maxWidth: "none"}) self._img[0].src = src; loaded(); }; img.src = src; this.angle(0); }; this._dimension = function(prefix, name) { var horiz = ' _ ' + prefix + ' _ ' + name, vert = ' _ ' + prefix + ' _ ' + (name === ' height ' ? ' width ' : ' height '); return setter(function(val) { this[this._swapDimensions ? horiz: vert] = val; }, function() { return this[this._swapDimensions ? horiz: vert]; }); }; /** * Getters and setter for common image dimensions. * display_ means real image tag dimensions * orig_ means physical image dimensions. * Note, that dimensions are swapped if image is rotated. It necessary, * because as little as possible code should know about rotation. */ this.display_width = this._dimension(' display ', ' width '), this.display_height = this._dimension(' display ', ' height '), this.display_diff = function() { return Math.floor( this.display_width() - this.display_height() ) }; this.orig_width = this._dimension(' orig ', ' width '), this.orig_height = this._dimension(' orig ', ' height '), /** * Setter for X coordinate. If image is rotated we need to additionaly shift an * image to map image coordinate to the visual position. * * @param {number} val Coordinate value. * @param {boolean} skipCss If true, we only set the value and do not touch the dom. */ this.x = setter(function(val, skipCss) { this._x = val; if (!skipCss) { this._img.css("left",this._x + (this._swapDimensions ? this.display_diff() / 2 : 0) + "px"); } }, function() { return this._x; }); /** * Setter for Y coordinate. If image is rotated we need to additionaly shift an * image to map image coordinate to the visual position. * * @param {number} val Coordinate value. * @param {boolean} skipCss If true, we only set the value and do not touch the dom. */ this.y = setter(function(val, skipCss) { this._y = val; if (!skipCss) { this._img.css("top",this._y - (this._swapDimensions ? this.display_diff() / 2 : 0) + "px"); } }, function() { return this._y; }); /** * Perform image rotation. * * @param {number} deg Absolute image angle. The method will work with values 0, 90, 180, 270 degrees. */ this.angle = setter(function(deg) { var prevSwap = this._swapDimensions; this._angle = deg; this._swapDimensions = deg % 180 !== 0; if (prevSwap !== this._swapDimensions) { var verticalMod = this._swapDimensions ? -1 : 1; this.x(this.x() - verticalMod * this.display_diff() / 2, true); this.y(this.y() + verticalMod * this.display_diff() / 2, true); }; var cssVal = ' rotate( ' + deg + ' deg) ', img = this._img; jQuery.each([' ', ' -webkit- ', ' -moz- ', ' -o- ', ' -ms- '], function(i, prefix) { img.css(prefix + ' transform ', cssVal); }); if (useIeTransforms) { jQuery.each([' -ms- ', ' '], function(i, prefix) { img.css(prefix + ' filter ', ieTransforms[deg].filter); }); img.css({ marginLeft: ieTransforms[deg].marginLeft * this.display_diff() / 2, marginTop: ieTransforms[deg].marginTop * this.display_diff() / 2 }); } }, function() { return this._angle; }); /** * Map point in the container coordinates to the point in image coordinates. * You will get coordinates of point on image with respect to rotation, * but will be set as if image was not rotated. * So, if image was rotated 90 degrees, it' s (0,0) point will be on the * top right corner. * * @param {{x: number, y: number}} point Point in container coordinates. * @ return {{x: number, y: number}} */ this .toOriginalCoords = function (point) { switch ( this .angle()) { case 0: return { x: point.x, y: point.y } case 90: return { x: point.y, y: this .display_width() - point.x } case 180: return { x: this .display_width() - point.x, y: this .display_height() - point.y } case 270: return { x: this .display_height() - point.y, y: point.x } } }; /** * Map point in the image coordinates to the point in container coordinates. * You will get coordinates of point on container with respect to rotation. * Note, if image was rotated 90 degrees, it's (0,0) point will be on the * top right corner. * * @param {{x: number, y: number}} point Point in container coordinates. * @return {{x: number, y: number}} */ this .toRealCoords = function (point) { switch ( this .angle()) { case 0: return { x: this .x() + point.x, y: this .y() + point.y } case 90: return { x: this .x() + this .display_width() - point.y, y: this .y() + point.x} case 180: return { x: this .x() + this .display_width() - point.x, y: this .y() + this .display_height() - point.y} case 270: return { x: this .x() + point.y, y: this .y() + this .display_height() - point.x} } }; /** * @return {jQuery} Return image node. this is needed to add event handlers. */ this .object = setter(jQuery.noop, function () { return this ._img; }); /** * Change image properties. * * @param {number} disp_w Display width; * @param {number} disp_h Display height; * @param {number} x * @param {number} y * @param {boolean} skip_animation If true, the animation will be skiped despite the * value set in constructor. * @param {Function=} complete Call back will be fired when zoom will be complete. */ this .setImageProps = function (disp_w, disp_h, x, y, skip_animation, complete) { complete = complete || jQuery.noop; this .display_width(disp_w); this .display_height(disp_h); this .x(x, true ); this .y(y, true ); var w = this ._swapDimensions ? disp_h : disp_w; var h = this ._swapDimensions ? disp_w : disp_h; var params = { width: w, height: h, top: y - ( this ._swapDimensions ? this .display_diff() / 2 : 0) + "px" , left: x + ( this ._swapDimensions ? this .display_diff() / 2 : 0) + "px" }; if (useIeTransforms) { jQuery.extend(params, { marginLeft: ieTransforms[ this .angle()].marginLeft * this .display_diff() / 2, marginTop: ieTransforms[ this .angle()].marginTop * this .display_diff() / 2 }); } var swapDims = this ._swapDimensions, img = this ._img; //here we come: another IE oddness. If image is rotated 90 degrees with a filter, than //width and height getters return real width and height of rotated image. The bad news //is that to set height you need to set a width and vice versa. Fuck IE. //So, in this case we have to animate width and height manually. if (useIeTransforms && swapDims) { var ieh = this ._img.width(), iew = this ._img.height(), iedh = params.height - ieh; iedw = params.width - iew; delete params.width; delete params.height; } if ( this ._do_anim && !skip_animation) { this ._img.stop( true ) .animate(params, { duration: 200, complete: complete, step: function (now, fx) { if (useIeTransforms && swapDims && (fx.prop === 'top ')) { var percent = (now - fx.start) / (fx.end - fx.start); img.height(ieh + iedh * percent); img.width(iew + iedw * percent); img.css(' top', now); } } }); } else { this ._img.css(params); setTimeout(complete, 0); //both if branches should behave equally. } }; }).apply($.ui.iviewer.ImageObject.prototype); var util = { scaleValue: function (value, toZoom) { return value * toZoom / 100; }, descaleValue: function (value, fromZoom) { return value * 100 / fromZoom; } }; } )( jQuery, undefined ); |
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 | <! DOCTYPE html> < html > < head > < meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < title >jquery.iviewer test</ title > < script type="text/javascript" src="jquery.js" ></ script > < script type="text/javascript" src="jqueryui.js" ></ script > < script type="text/javascript" src="jquery.mousewheel.min.js" ></ script > < script type="text/javascript" src="jquery.iviewer.js" ></ script > < script type="text/javascript"> var $ = jQuery; $(document).ready(function(){ var iv1 = $("#viewer").iviewer({ src: "test_image.jpg", update_on_resize: false, zoom_animation: false, mousewheel: false, onMouseMove: function(ev, coords) { }, onStartDrag: function(ev, coords) { return false; }, //this image will not be dragged onDrag: function(ev, coords) { } }); $("#in").click(function(){ iv1.iviewer('zoom_by', 1); }); $("#out").click(function(){ iv1.iviewer('zoom_by', -1); }); $("#fit").click(function(){ iv1.iviewer('fit'); }); $("#orig").click(function(){ iv1.iviewer('set_zoom', 100); }); $("#update").click(function(){ iv1.iviewer('update_container_info'); }); var iv2 = $("#viewer2").iviewer( { src: "test_image2.jpg" }); $("#chimg").click(function() { iv2.iviewer('loadImage', "test_image.jpg"); return false; }); }); </ script > < link rel="stylesheet" href="jquery.iviewer.css" /> < style > .viewer { width: 50%; height: 500px; border: 1px solid black; position: relative; } .wrapper { overflow: hidden; } </ style > </ head > < body > < h1 >JQuery.iviewer test</ h1 > <!-- wrapper div is needed for opera because it shows scroll bars for reason --> < div class="wrapper"> < span > < a id="in" href="#">+</ a > < a id="out" href="#">-</ a > < a id="fit" href="#">fit</ a > < a id="orig" href="#">orig</ a > < a id="update" href="#">update</ a > </ span > < div id="viewer" class="viewer"></ div > < br /> < div id="viewer2" class="viewer"></ div > < br /> < p >< a href="#" id="chimg">Change Image</ a ></ p > </ div > </ body > </ html > |
效果圖:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Ajax&JavaScript
标签:
jQuery
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2012-07-25 csharp Send Skype messages from webform and winform