原文链接及内容

运行界面

国际图像互操作性框架( International Image Interoperability Framework,缩写为IIIF)图像服务的一个切片数据源示例。你可以尝试任何Image API版本1或2的服务。

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

const layer = new TileLayer(),
map = new Map({
layers: [layer],
target: 'map',
}),
notifyDiv = document.getElementById('iiif-notification'),
urlInput = document.getElementById('imageInfoUrl'),
displayButton = document.getElementById('display');

function refreshMap(imageInfoUrl) {
fetch(imageInfoUrl)
.then(function (response) {
response
.json()
.then(function (imageInfo) {
const options = new IIIFInfo(imageInfo).getTileSourceOptions();
if (options === undefined || options.version === undefined) {
notifyDiv.textContent =
'Data seems to be no valid IIIF image information.';
return;
}
options.zDirection = -1;
const iiifTileSource = new IIIF(options);
layer.setSource(iiifTileSource);
map.setView(
new View({
resolutions: iiifTileSource.getTileGrid().getResolutions(),
extent: iiifTileSource.getTileGrid().getExtent(),
constrainOnlyCenter: true,
})
);
map.getView().fit(iiifTileSource.getTileGrid().getExtent());
notifyDiv.textContent = '';
})
.catch(function (body) {
notifyDiv.textContent = 'Could not read image info json. ' + body;
});
})
.catch(function () {
notifyDiv.textContent = 'Could not read data from URL.';
});
}

displayButton.addEventListener('click', function () {
refreshMap(urlInput.value);
});

refreshMap(urlInput.value);

界面布局文件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>IIIF Image API</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">
<div id="iiif-notification">&nbsp;</div>
<label for="imageInfoUrl">Enter <code>info.json</code> URL:</label>
<input type="text" id="imageInfoUrl" value="https://iiif.ub.uni-leipzig.de/iiif/j2k/0000/0107/0000010732/00000072.jpx/info.json">
<button id="display">Display image</button>
</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>