Replace viewport.getImageZoomRatio by conversion methods between viewport zoom and image zoom

This commit is contained in:
Antoine Vandecreme 2013-10-18 15:16:49 -04:00
parent 4cd98a5da9
commit 54d02ada78
2 changed files with 37 additions and 10 deletions

View File

@ -918,17 +918,39 @@ $.Viewport.prototype = {
}, },
/** /**
* Get the zoom ratio of the image. 1 means original image size, 0.5 half size... * Convert a viewport zoom to an image zoom.
* Image zoom: ratio of the original image size to displayed image size.
* 1 means original image size, 0.5 half size...
* Viewport zoom: ratio of the displayed image's width to viewport's width.
* 1 means identical width, 2 means image's width is twice the viewport's width...
* @function * @function
* @param {Boolean} current If true gives the current zoom otherwise gives the * @param {Number} viewportZoom The viewport zoom
* target zoom. * target zoom.
* @returns {Number} * @returns {Number} imageZoom The image zoom
*/ */
getImageZoomRatio: function( current ) { viewportToImageZoom: function( viewportZoom ) {
var imageWidth = this.viewer.source.dimensions.x; var imageWidth = this.viewer.source.dimensions.x;
var containerWidth = this.getContainerSize().x; var containerWidth = this.getContainerSize().x;
var zoomToZoomLevelRatio = containerWidth / imageWidth; var viewportToImageZoomRatio = containerWidth / imageWidth;
return this.getZoom( current ) * zoomToZoomLevelRatio; return viewportZoom * viewportToImageZoomRatio;
},
/**
* Convert an image zoom to a viewport zoom.
* Image zoom: ratio of the original image size to displayed image size.
* 1 means original image size, 0.5 half size...
* Viewport zoom: ratio of the displayed image's width to viewport's width.
* 1 means identical width, 2 means image's width is twice the viewport's width...
* @function
* @param {Number} imageZoom The image zoom
* target zoom.
* @returns {Number} viewportZoom The viewport zoom
*/
imageToViewportZoom: function( imageZoom ) {
var imageWidth = this.viewer.source.dimensions.x;
var containerWidth = this.getContainerSize().x;
var viewportToImageZoomRatio = imageWidth / containerWidth;
return imageZoom * viewportToImageZoomRatio;
} }
}; };

View File

@ -91,9 +91,14 @@
function checkZoom() { function checkZoom() {
var currentImageWidth = getCurrentImageWidth(); var currentImageWidth = getCurrentImageWidth();
var expected = currentImageWidth / imageWidth; var expectedImageZoom = currentImageWidth / imageWidth;
var actual = viewport.getImageZoomRatio(true); var expectedViewportZoom = viewport.getZoom(true);
equal(actual, expected); var actualImageZoom = viewport.viewportToImageZoom(
expectedViewportZoom);
equal(actualImageZoom, expectedImageZoom);
var actualViewportZoom = viewport.imageToViewportZoom(actualImageZoom);
equal(actualViewportZoom, expectedViewportZoom);
} }
checkZoom(); checkZoom();