openseadragon/test/modules/units.js

335 lines
15 KiB
JavaScript
Raw Permalink Normal View History

2022-12-03 02:52:53 +03:00
/* eslint-disable camelcase */
2017-12-05 04:45:48 +03:00
/* global QUnit, $, Util, testLog */
2015-06-04 22:32:10 +03:00
(function () {
var viewer;
var precision = 0.00000001;
2017-12-05 04:45:48 +03:00
QUnit.module('Units', {
beforeEach: function () {
$('<div id="unitsexample"></div>').appendTo("#qunit-fixture");
testLog.reset();
2022-12-03 02:52:53 +03:00
// eslint-disable-next-line new-cap
viewer = OpenSeadragon({
2015-06-04 22:32:10 +03:00
id: 'unitsexample',
prefixUrl: '/build/openseadragon/images/',
springStiffness: 100 // Faster animation = faster tests
});
},
2017-12-05 04:45:48 +03:00
afterEach: function () {
if (viewer && viewer.close) {
viewer.close();
}
viewer = null;
}
});
2017-12-05 04:45:48 +03:00
function pointEqual(assert, a, b, message) {
Util.assessNumericValue(assert, a.x, b.x, precision, message);
Util.assessNumericValue(assert, a.y, b.y, precision, message);
}
2015-06-04 22:32:10 +03:00
// Check that f^-1 ( f(x) ) = x
2017-12-05 04:45:48 +03:00
function checkPoint(assert, context) {
2015-06-04 22:32:10 +03:00
var viewport = viewer.viewport;
2015-06-04 22:32:10 +03:00
var point = new OpenSeadragon.Point(15, 12);
var result = viewport.viewerElementToImageCoordinates(
viewport.imageToViewerElementCoordinates(point));
2017-12-05 04:45:48 +03:00
pointEqual(assert, result, point, 'viewerElement and image ' + context);
2015-06-04 22:32:10 +03:00
result = viewport.windowToImageCoordinates(
viewport.imageToWindowCoordinates(point));
2017-12-05 04:45:48 +03:00
pointEqual(assert, result, point, 'window and image ' + context);
2015-06-04 22:32:10 +03:00
result = viewport.viewerElementToViewportCoordinates(
viewport.viewportToViewerElementCoordinates(point));
2017-12-05 04:45:48 +03:00
pointEqual(assert, result, point, 'viewerElement and viewport ' + context);
2015-06-04 22:32:10 +03:00
result = viewport.windowToViewportCoordinates(
viewport.viewportToWindowCoordinates(point));
2017-12-05 04:45:48 +03:00
pointEqual(assert, result, point, 'window and viewport ' + context);
2015-06-04 22:32:10 +03:00
for (var i = 0; i < viewer.world.getItemCount(); i++) {
var tiledImage = viewer.world.getItemAt(i);
result = tiledImage.viewportToImageCoordinates(
tiledImage.imageToViewportCoordinates(point));
2017-12-05 04:45:48 +03:00
pointEqual(assert, result, point, 'viewport and tiled image ' + i + context);
2015-06-04 22:32:10 +03:00
result = tiledImage.viewerElementToImageCoordinates(
tiledImage.imageToViewerElementCoordinates(point));
2017-12-05 04:45:48 +03:00
pointEqual(assert, result, point, 'viewerElement and tiled image ' + i + context);
2015-06-04 22:32:10 +03:00
result = tiledImage.windowToImageCoordinates(
tiledImage.imageToWindowCoordinates(point));
2017-12-05 04:45:48 +03:00
pointEqual(assert, result, point, 'window and tiled image ' + i + context);
}
2015-06-04 22:32:10 +03:00
}
// ----------
2017-12-05 04:45:48 +03:00
QUnit.test('Single image coordinates conversions', function (assert) {
var done = assert.async();
viewer.addHandler("open", function () {
var viewport = viewer.viewport;
2015-06-04 22:32:10 +03:00
var tiledImage = viewer.world.getItemAt(0);
var point0_0 = new OpenSeadragon.Point(0, 0);
var point = viewport.viewerElementToViewportCoordinates(point0_0);
2017-12-05 04:45:48 +03:00
pointEqual(assert, point, point0_0, 'When opening, viewer coordinate 0,0 is also point 0,0');
2015-06-04 22:32:10 +03:00
var viewportPixel = viewport.viewerElementToImageCoordinates(point0_0);
2017-12-05 04:45:48 +03:00
pointEqual(assert, viewportPixel, point0_0, 'When opening, viewer coordinate 0,0 is also viewport pixel 0,0');
2015-06-04 22:32:10 +03:00
var imagePixel = tiledImage.viewerElementToImageCoordinates(point0_0);
2017-12-05 04:45:48 +03:00
pointEqual(assert, imagePixel, point0_0, 'When opening, viewer coordinate 0,0 is also image pixel 0,0');
var viewerWidth = $(viewer.element).width();
var imageWidth = viewer.source.dimensions.x;
var point1_0 = new OpenSeadragon.Point(1, 0);
var viewerTopRight = new OpenSeadragon.Point(viewerWidth, 0);
var imageTopRight = new OpenSeadragon.Point(imageWidth, 0);
2015-06-04 22:32:10 +03:00
point = viewport.viewerElementToViewportCoordinates(viewerTopRight);
2017-12-05 04:45:48 +03:00
pointEqual(assert, point, point1_0, 'Viewer top right has viewport coordinates 1,0.');
2015-06-04 22:32:10 +03:00
viewportPixel = viewport.viewerElementToImageCoordinates(viewerTopRight);
2017-12-05 04:45:48 +03:00
pointEqual(assert, viewportPixel, imageTopRight, 'Viewer top right has viewport pixel coordinates imageWidth,0.');
2015-06-04 22:32:10 +03:00
imagePixel = tiledImage.viewerElementToImageCoordinates(viewerTopRight);
2017-12-05 04:45:48 +03:00
pointEqual(assert, imagePixel, imageTopRight, 'Viewer top right has image pixel coordinates imageWidth,0.');
2017-12-05 04:45:48 +03:00
checkPoint(assert, ' after opening');
viewer.addHandler('animation-finish', function animationHandler() {
viewer.removeHandler('animation-finish', animationHandler);
2017-12-05 04:45:48 +03:00
checkPoint(assert, ' after zoom and pan');
done();
});
viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2));
});
viewer.open('/test/data/testpattern.dzi');
});
2015-06-04 22:32:10 +03:00
// ---------
2017-12-05 04:45:48 +03:00
QUnit.test('Multiple images coordinates conversion', function (assert) {
var done = assert.async();
2015-06-04 22:32:10 +03:00
viewer.addHandler("open", function () {
var viewport = viewer.viewport;
var tiledImage1 = viewer.world.getItemAt(0);
var tiledImage2 = viewer.world.getItemAt(1);
var imageWidth = viewer.source.dimensions.x;
var imageHeight = viewer.source.dimensions.y;
var point0_0 = new OpenSeadragon.Point(0, 0);
var point = viewport.viewerElementToViewportCoordinates(point0_0);
2017-12-05 04:45:48 +03:00
pointEqual(assert, point, point0_0, 'When opening, viewer coordinate 0,0 is also point 0,0');
2015-06-04 22:32:10 +03:00
var image1Pixel = tiledImage1.viewerElementToImageCoordinates(point0_0);
2017-12-05 04:45:48 +03:00
pointEqual(assert, image1Pixel, point0_0, 'When opening, viewer coordinate 0,0 is also image 1 pixel 0,0');
2015-06-04 22:32:10 +03:00
var image2Pixel = tiledImage2.viewerElementToImageCoordinates(point0_0);
2017-12-05 04:45:48 +03:00
pointEqual(assert, image2Pixel,
2015-06-04 22:32:10 +03:00
new OpenSeadragon.Point(-2 * imageWidth, -2 * imageHeight),
'When opening, viewer coordinates 0,0 is also image 2 pixel -2*imageWidth, -2*imageHeight');
var viewerWidth = $(viewer.element).width();
var viewerHeight = $(viewer.element).height();
var viewerBottomRight = new OpenSeadragon.Point(viewerWidth, viewerHeight);
point = viewport.viewerElementToViewportCoordinates(viewerBottomRight);
2017-12-05 04:45:48 +03:00
pointEqual(assert, point, new OpenSeadragon.Point(1.5, 1.5),
2015-06-04 22:32:10 +03:00
'Viewer bottom right has viewport coordinates 1.5,1.5.');
image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerBottomRight);
2017-12-05 04:45:48 +03:00
pointEqual(assert, image1Pixel,
2015-06-04 22:32:10 +03:00
new OpenSeadragon.Point(imageWidth * 1.5, imageHeight * 1.5),
'Viewer bottom right has image 1 pixel coordinates imageWidth * 1.5, imageHeight * 1.5');
image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerBottomRight);
2017-12-05 04:45:48 +03:00
pointEqual(assert, image2Pixel,
2015-06-04 22:32:10 +03:00
new OpenSeadragon.Point(imageWidth, imageHeight),
'Viewer bottom right has image 2 pixel coordinates imageWidth,imageHeight.');
2017-12-05 04:45:48 +03:00
checkPoint(assert, ' after opening');
2015-06-04 22:32:10 +03:00
viewer.addHandler('animation-finish', function animationHandler() {
viewer.removeHandler('animation-finish', animationHandler);
2017-12-05 04:45:48 +03:00
checkPoint(assert, ' after zoom and pan');
done();
2015-06-04 22:32:10 +03:00
});
viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2));
});
viewer.open([{
tileSource: "/test/data/testpattern.dzi"
}, {
tileSource: "/test/data/testpattern.dzi",
x: 1,
y: 1,
width: 0.5
}
]);
});
// ---------
2017-12-05 04:45:48 +03:00
QUnit.test('Multiple images coordinates conversion with viewport rotation', function (assert) {
var done = assert.async();
viewer.addHandler("open", function () {
var viewport = viewer.viewport;
var tiledImage1 = viewer.world.getItemAt(0);
var tiledImage2 = viewer.world.getItemAt(1);
var imageWidth = viewer.source.dimensions.x;
var imageHeight = viewer.source.dimensions.y;
var viewerWidth = $(viewer.element).width();
var viewerHeight = $(viewer.element).height();
var viewerMiddleTop = new OpenSeadragon.Point(viewerWidth / 2, 0);
var viewerMiddleBottom = new OpenSeadragon.Point(viewerWidth / 2, viewerHeight);
var point0_0 = new OpenSeadragon.Point(0, 0);
var point = viewport.viewerElementToViewportCoordinates(viewerMiddleTop);
2017-12-05 04:45:48 +03:00
pointEqual(assert, point, point0_0, 'When opening, viewer middle top is also viewport 0,0');
var image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerMiddleTop);
2017-12-05 04:45:48 +03:00
pointEqual(assert, image1Pixel, point0_0, 'When opening, viewer middle top is also image 1 pixel 0,0');
var image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerMiddleTop);
2017-12-05 04:45:48 +03:00
pointEqual(assert, image2Pixel,
new OpenSeadragon.Point(-2 * imageWidth, -2 * imageHeight),
'When opening, viewer middle top is also image 2 pixel -2*imageWidth, -2*imageHeight');
point = viewport.viewerElementToViewportCoordinates(viewerMiddleBottom);
2017-12-05 04:45:48 +03:00
pointEqual(assert, point, new OpenSeadragon.Point(1.5, 1.5),
'Viewer middle bottom has viewport coordinates 1.5,1.5.');
image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerMiddleBottom);
2017-12-05 04:45:48 +03:00
pointEqual(assert, image1Pixel,
new OpenSeadragon.Point(imageWidth * 1.5, imageHeight * 1.5),
'Viewer middle bottom has image 1 pixel coordinates imageWidth * 1.5, imageHeight * 1.5');
image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerMiddleBottom);
2017-12-05 04:45:48 +03:00
pointEqual(assert, image2Pixel,
new OpenSeadragon.Point(imageWidth, imageHeight),
'Viewer middle bottom has image 2 pixel coordinates imageWidth,imageHeight.');
2017-12-05 04:45:48 +03:00
checkPoint(assert, ' after opening');
viewer.addHandler('animation-finish', function animationHandler() {
viewer.removeHandler('animation-finish', animationHandler);
2017-12-05 04:45:48 +03:00
checkPoint(assert, ' after zoom and pan');
//Restore rotation
viewer.viewport.setRotation(0, true);
2017-12-05 04:45:48 +03:00
done();
});
viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2));
});
viewer.viewport.setRotation(45, true);
viewer.open([{
tileSource: "/test/data/testpattern.dzi"
}, {
tileSource: "/test/data/testpattern.dzi",
x: 1,
y: 1,
width: 0.5
}
]);
});
// ----------
2017-12-05 04:45:48 +03:00
QUnit.test('ZoomRatio 1 image', function (assert) {
var done = assert.async();
viewer.addHandler("open", function () {
var viewport = viewer.viewport;
2015-06-04 22:32:10 +03:00
var imageWidth = viewer.source.dimensions.x;
function getCurrentImageWidth() {
return viewport.viewportToViewerElementCoordinates(
2015-06-04 22:32:10 +03:00
new OpenSeadragon.Point(1, 0)).minus(
viewport.viewportToViewerElementCoordinates(
2015-06-04 22:32:10 +03:00
new OpenSeadragon.Point(0, 0))).x;
}
function checkZoom() {
var currentImageWidth = getCurrentImageWidth();
var expectedImageZoom = currentImageWidth / imageWidth;
var expectedViewportZoom = viewport.getZoom(true);
var actualImageZoom = viewport.viewportToImageZoom(
2015-06-04 22:32:10 +03:00
expectedViewportZoom);
2017-12-05 04:45:48 +03:00
Util.assessNumericValue(assert, actualImageZoom, expectedImageZoom, precision);
2015-06-04 22:32:10 +03:00
var actualViewportZoom = viewport.imageToViewportZoom(actualImageZoom);
2017-12-05 04:45:48 +03:00
Util.assessNumericValue(assert, actualViewportZoom, expectedViewportZoom, precision);
}
checkZoom();
2015-06-04 22:32:10 +03:00
var zoomHandler = function () {
viewer.removeHandler('animation-finish', zoomHandler);
checkZoom();
2017-12-05 04:45:48 +03:00
done();
};
viewer.addHandler('animation-finish', zoomHandler);
viewport.zoomTo(2);
});
viewer.open('/test/data/testpattern.dzi');
});
2015-06-04 22:32:10 +03:00
// ----------
2017-12-05 04:45:48 +03:00
QUnit.test('ZoomRatio 2 images', function (assert) {
var done = assert.async();
2015-06-04 22:32:10 +03:00
viewer.addHandler("open", function () {
var viewport = viewer.viewport;
var imageWidth = viewer.source.dimensions.x;
var image1 = viewer.world.getItemAt(0);
var image2 = viewer.world.getItemAt(1);
function getCurrentImageWidth(image) {
var bounds = image.getBounds();
return viewport.viewportToViewerElementCoordinates(
bounds.getTopRight()).minus(
viewport.viewportToViewerElementCoordinates(
bounds.getTopLeft())).x;
}
function checkZoom(image) {
var currentImageWidth = getCurrentImageWidth(image);
var expectedImageZoom = currentImageWidth / imageWidth;
var expectedViewportZoom = viewport.getZoom(true);
var actualImageZoom = image.viewportToImageZoom(
expectedViewportZoom);
2017-12-05 04:45:48 +03:00
Util.assessNumericValue(assert, actualImageZoom, expectedImageZoom,
precision);
2015-06-04 22:32:10 +03:00
var actualViewportImage1Zoom = image.imageToViewportZoom(actualImageZoom);
Util.assessNumericValue(
2017-12-05 04:45:48 +03:00
assert, actualViewportImage1Zoom, expectedViewportZoom, precision);
2015-06-04 22:32:10 +03:00
}
checkZoom(image1);
checkZoom(image2);
var zoomHandler = function () {
viewer.removeHandler('animation-finish', zoomHandler);
checkZoom(image1);
checkZoom(image2);
2017-12-05 04:45:48 +03:00
done();
2015-06-04 22:32:10 +03:00
};
viewer.addHandler('animation-finish', zoomHandler);
viewport.zoomTo(2);
});
viewer.open([{
tileSource: "/test/data/testpattern.dzi"
}, {
tileSource: "/test/data/testpattern.dzi",
x: 1,
y: 1,
width: 0.5
}
]);
});
})();