原文链接及内容

运行界面

Zoomify是一种用于深度缩放高分辨率图像的格式。此示例显示如何将Zoomify数据源与像素投影一起使用,还处理了具有JTL扩展的互联网成像协议(IIP,Internet Imaging Protocol)。

main.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
import Map from 'ol/Map.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import Zoomify from 'ol/source/Zoomify.js';

const imgWidth = 4000;
const imgHeight = 3000;

const zoomifyUrl = 'https://ol-zoomify.surge.sh/zoomify/';

const source = new Zoomify({
url: zoomifyUrl,
size: [imgWidth, imgHeight],
crossOrigin: 'anonymous',
zDirection: -1, // Ensure we get a tile with the screen resolution or higher
// 确保我们得到屏幕分辨率或更高的切片
});
const extent = source.getTileGrid().getExtent();

const retinaPixelRatio = 2;
const retinaSource = new Zoomify({
url: zoomifyUrl,
size: [imgWidth, imgHeight],
crossOrigin: 'anonymous',
zDirection: -1, // Ensure we get a tile with the screen resolution or higher-确保我们得到屏幕分辨率或更高的切片
// 以下两项配置:Display retina tiles from a higher zoom level-显示可用于Retina显示屏的切片数据
tilePixelRatio: retinaPixelRatio,
tileSize: 256 / retinaPixelRatio,
});

const layer = new TileLayer({
source: source,
});

const map = new Map({
layers: [layer],
target: 'map',
view: new View({
// adjust zoom levels to those provided by the source
// 将缩放级别调整为数据源提供的缩放级别
resolutions: layer.getSource().getTileGrid().getResolutions(),
// constrain the center: center cannot be set outside this extent
// 约束中心:不能将中心设置在此范围之外
extent: extent,
constrainOnlyCenter: true,
}),
});
map.getView().fit(extent);

const control = document.getElementById('zoomifyProtocol');
control.addEventListener('change', function (event) {
const value = event.currentTarget.value;
if (value === 'zoomify') {
layer.setSource(source);
} else if (value === 'zoomifyretina') {
layer.setSource(retinaSource);
}
});

界面布局文件index.html代码如下:

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Zoomify</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<div class="controls">
<select id="zoomifyProtocol">
<option value="zoomify">Zoomify</option>
<option value="zoomifyretina">Zoomify Retina</option>
</select>
</div>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>