OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Predefined zoom factors to be used when zooming in/out. These are in | 6 * Predefined zoom factors to be used when zooming in/out. These are in |
7 * ascending order. | 7 * ascending order. |
8 */ | 8 */ |
9 var ZOOM_FACTORS = [0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1.0, | 9 var ZOOM_FACTORS = [0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1.0, |
10 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0]; | 10 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0]; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 getCurrentViewportRect_: function() { | 147 getCurrentViewportRect_: function() { |
148 return { | 148 return { |
149 x: this.window_.pageXOffset / this.zoom_, | 149 x: this.window_.pageXOffset / this.zoom_, |
150 y: this.window_.pageYOffset / this.zoom_, | 150 y: this.window_.pageYOffset / this.zoom_, |
151 width: this.window_.innerWidth / this.zoom_, | 151 width: this.window_.innerWidth / this.zoom_, |
152 height: this.window_.innerHeight / this.zoom_, | 152 height: this.window_.innerHeight / this.zoom_, |
153 }; | 153 }; |
154 }, | 154 }, |
155 | 155 |
156 /** | 156 /** |
| 157 * @private |
| 158 * @param {integer} y the y-coordinate to get the page at. |
| 159 * @return {integer} the index of a page overlapping the given y-coordinate. |
| 160 */ |
| 161 getPageAtY_: function(y) { |
| 162 var min = 0; |
| 163 var max = this.pageDimensions_.length - 1; |
| 164 while (max >= min) { |
| 165 var page = Math.floor(min + ((max - min) / 2)); |
| 166 var top = this.pageDimensions_[page].y; |
| 167 var bottom = top + this.pageDimensions_[page].height; |
| 168 if (top <= y && bottom > y) |
| 169 return page; |
| 170 else if (top > y) |
| 171 max = page - 1; |
| 172 else |
| 173 min = page + 1; |
| 174 } |
| 175 return 0; |
| 176 }, |
| 177 |
| 178 /** |
157 * Returns the page with the most pixels in the current viewport. | 179 * Returns the page with the most pixels in the current viewport. |
158 * @return {int} the index of the most visible page. | 180 * @return {int} the index of the most visible page. |
159 */ | 181 */ |
160 getMostVisiblePage: function() { | 182 getMostVisiblePage: function() { |
161 // TODO(raymes): Do a binary search here. | 183 var firstVisiblePage = this.getPageAtY_(this.getCurrentViewportRect_().y); |
162 var mostVisiblePage = {'number': 0, 'area': 0}; | 184 var mostVisiblePage = {'number': 0, 'area': 0}; |
163 for (var i = 0; i < this.pageDimensions_.length; i++) { | 185 for (var i = firstVisiblePage; i < this.pageDimensions_.length; i++) { |
164 var area = getIntersectionArea(this.pageDimensions_[i], | 186 var area = getIntersectionArea(this.pageDimensions_[i], |
165 this.getCurrentViewportRect_()); | 187 this.getCurrentViewportRect_()); |
| 188 // If we hit a page with 0 area overlap, we must have gone past the |
| 189 // pages visible in the viewport so we can break. |
| 190 if (area == 0) |
| 191 break; |
166 if (area > mostVisiblePage.area) { | 192 if (area > mostVisiblePage.area) { |
167 mostVisiblePage.area = area; | 193 mostVisiblePage.area = area; |
168 mostVisiblePage.number = i; | 194 mostVisiblePage.number = i; |
169 } | 195 } |
170 } | 196 } |
171 return mostVisiblePage.number; | 197 return mostVisiblePage.number; |
172 }, | 198 }, |
173 | 199 |
174 /** | 200 /** |
175 * @private | 201 * @private |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 * Set the dimensions of the document. | 325 * Set the dimensions of the document. |
300 * @param {Object} documentDimensions the dimensions of the document | 326 * @param {Object} documentDimensions the dimensions of the document |
301 */ | 327 */ |
302 setDocumentDimensions: function(documentDimensions) { | 328 setDocumentDimensions: function(documentDimensions) { |
303 this.documentDimensions_ = documentDimensions; | 329 this.documentDimensions_ = documentDimensions; |
304 this.pageDimensions_ = this.documentDimensions_.pageDimensions; | 330 this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
305 this.contentSizeChanged_(); | 331 this.contentSizeChanged_(); |
306 this.updateViewport_(); | 332 this.updateViewport_(); |
307 } | 333 } |
308 }; | 334 }; |
OLD | NEW |