| 1 | |
| 2 | _initZoom: function () { |
| 3 | this.scrollerStyle[utils.style.transformOrigin] = '0 0'; |
| 4 | }, |
| 5 | |
| 6 | _zoomStart: function (e) { |
| 7 | var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ), |
| 8 | c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY ); |
| 9 | |
| 10 | this.touchesDistanceStart = Math.sqrt(c1 * c1 + c2 * c2); |
| 11 | this.startScale = this.scale; |
| 12 | |
| 13 | this.originX = Math.abs(e.touches[0].pageX + e.touches[1].pageX) / 2 + this.wrapperOffset.left - this.x; |
| 14 | this.originY = Math.abs(e.touches[0].pageY + e.touches[1].pageY) / 2 + this.wrapperOffset.top - this.y; |
| 15 | |
| 16 | this._execEvent('zoomStart'); |
| 17 | }, |
| 18 | |
| 19 | _zoom: function (e) { |
| 20 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | if ( this.options.preventDefault ) { |
| 25 | e.preventDefault(); |
| 26 | } |
| 27 | |
| 28 | var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ), |
| 29 | c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY ), |
| 30 | distance = Math.sqrt( c1 * c1 + c2 * c2 ), |
| 31 | scale = 1 / this.touchesDistanceStart * distance * this.startScale, |
| 32 | lastScale, |
| 33 | x, y; |
| 34 | |
| 35 | this.scaled = true; |
| 36 | |
| 37 | if ( scale < this.options.zoomMin ) { |
| 38 | scale = 0.5 * this.options.zoomMin * Math.pow(2.0, scale / this.options.zoomMin); |
| 39 | } else if ( scale > this.options.zoomMax ) { |
| 40 | scale = 2.0 * this.options.zoomMax * Math.pow(0.5, this.options.zoomMax / scale); |
| 41 | } |
| 42 | |
| 43 | lastScale = scale / this.startScale; |
| 44 | x = this.originX - this.originX * lastScale + this.startX; |
| 45 | y = this.originY - this.originY * lastScale + this.startY; |
| 46 | |
| 47 | this.scale = scale; |
| 48 | |
| 49 | this.scrollTo(x, y, 0); |
| 50 | }, |
| 51 | |
| 52 | _zoomEnd: function (e) { |
| 53 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if ( this.options.preventDefault ) { |
| 58 | e.preventDefault(); |
| 59 | } |
| 60 | |
| 61 | var newX, newY, |
| 62 | lastScale; |
| 63 | |
| 64 | this.isInTransition = 0; |
| 65 | this.initiated = 0; |
| 66 | |
| 67 | if ( this.scale > this.options.zoomMax ) { |
| 68 | this.scale = this.options.zoomMax; |
| 69 | } else if ( this.scale < this.options.zoomMin ) { |
| 70 | this.scale = this.options.zoomMin; |
| 71 | } |
| 72 | |
| 73 | // Update boundaries |
| 74 | this.refresh(); |
| 75 | |
| 76 | lastScale = this.scale / this.startScale; |
| 77 | |
| 78 | newX = this.originX - this.originX * lastScale + this.startX; |
| 79 | newY = this.originY - this.originY * lastScale + this.startY; |
| 80 | |
| 81 | if ( newX > 0 ) { |
| 82 | newX = 0; |
| 83 | } else if ( newX < this.maxScrollX ) { |
| 84 | newX = this.maxScrollX; |
| 85 | } |
| 86 | |
| 87 | if ( newY > 0 ) { |
| 88 | newY = 0; |
| 89 | } else if ( newY < this.maxScrollY ) { |
| 90 | newY = this.maxScrollY; |
| 91 | } |
| 92 | |
| 93 | if ( this.x != newX || this.y != newY ) { |
| 94 | this.scrollTo(newX, newY, this.options.bounceTime); |
| 95 | } |
| 96 | |
| 97 | this.scaled = false; |
| 98 | |
| 99 | this._execEvent('zoomEnd'); |
| 100 | }, |
| 101 | |
| 102 | zoom: function (scale, x, y, time) { |
| 103 | if ( scale < this.options.zoomMin ) { |
| 104 | scale = this.options.zoomMin; |
| 105 | } else if ( scale > this.options.zoomMax ) { |
| 106 | scale = this.options.zoomMax; |
| 107 | } |
| 108 | |
| 109 | if ( scale == this.scale ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | var relScale = scale / this.scale; |
| 114 | |
| 115 | x = x === undefined ? this.wrapperWidth / 2 : x; |
| 116 | y = y === undefined ? this.wrapperHeight / 2 : y; |
| 117 | time = time === undefined ? 300 : time; |
| 118 | |
| 119 | x = x + this.wrapperOffset.left - this.x; |
| 120 | y = y + this.wrapperOffset.top - this.y; |
| 121 | |
| 122 | x = x - x * relScale + this.x; |
| 123 | y = y - y * relScale + this.y; |
| 124 | |
| 125 | this.scale = scale; |
| 126 | |
| 127 | this.refresh(); // update boundaries |
| 128 | |
| 129 | if ( x > 0 ) { |
| 130 | x = 0; |
| 131 | } else if ( x < this.maxScrollX ) { |
| 132 | x = this.maxScrollX; |
| 133 | } |
| 134 | |
| 135 | if ( y > 0 ) { |
| 136 | y = 0; |
| 137 | } else if ( y < this.maxScrollY ) { |
| 138 | y = this.maxScrollY; |
| 139 | } |
| 140 | |
| 141 | this.scrollTo(x, y, time); |
| 142 | }, |
| 143 | |
| 144 | _wheelZoom: function (e) { |
| 145 | var wheelDeltaY, |
| 146 | deltaScale, |
| 147 | that = this; |
| 148 | |
| 149 | // Execute the zoomEnd event after 400ms the wheel stopped scrolling |
| 150 | clearTimeout(this.wheelTimeout); |
| 151 | this.wheelTimeout = setTimeout(function () { |
| 152 | that._execEvent('zoomEnd'); |
| 153 | }, 400); |
| 154 | |
| 155 | if ( 'deltaX' in e ) { |
| 156 | wheelDeltaY = -e.deltaY / Math.abs(e.deltaY); |
| 157 | } else if ('wheelDeltaX' in e) { |
| 158 | wheelDeltaY = e.wheelDeltaY / Math.abs(e.wheelDeltaY); |
| 159 | } else if('wheelDelta' in e) { |
| 160 | wheelDeltaY = e.wheelDelta / Math.abs(e.wheelDelta); |
| 161 | } else if ('detail' in e) { |
| 162 | wheelDeltaY = -e.detail / Math.abs(e.wheelDelta); |
| 163 | } else { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | deltaScale = this.scale + wheelDeltaY / 5; |
| 168 | |
| 169 | this.zoom(deltaScale, e.pageX, e.pageY, 0); |
| 170 | }, |