-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviewer.html
222 lines (207 loc) · 8.15 KB
/
viewer.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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/ol.css" type="text/css">
<style>
.map {
display: block;
position: absolute;
height: auto;
bottom: 0;
top: 0;
left: 0;
right: 0;
background-color: white;
}
.ol-scale-line {
background: rgba(255, 255, 255, 0.5);
border-radius: 1px;
bottom: 8px;
left: 8px;
padding: 2px;
position: absolute;
}
.ol-scale-line-inner {
border-bottom: 1px solid black;
border-left: 1px solid black;
border-right: 1px solid black;
color: black;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 15px;
text-align: center;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
class LabeledTile extends ol.Tile {
constructor(tileCoord, tileSize, text) {
super(tileCoord, 2);
this.tileSize_ = tileSize;
this.text_ = text;
this.canvas_ = null;
}
getImage() {
if (this.canvas_) {
return this.canvas_;
} else {
const tileSize = this.tileSize_;
const context = ol.dom.createCanvasContext2D(tileSize[0], tileSize[1]);
context.strokeStyle = 'grey';
context.strokeRect(0.5, 0.5, tileSize[0] + 0.5, tileSize[1] + 0.5);
context.fillStyle = 'black';
context.strokeStyle = 'white';
context.textAlign = 'center';
context.textBaseline = 'middle';
context.font = '16px sans-serif';
context.lineWidth = 2;
context.strokeText(
this.text_,
tileSize[0] / 2,
tileSize[1] / 2,
tileSize[0]
);
context.fillText(
this.text_,
tileSize[0] / 2,
tileSize[1] / 2,
tileSize[0]
);
this.canvas_ = context.canvas;
return context.canvas;
}
}
load() { }
}
</script>
<title>viewer</title>
</head>
<body>
<div id="map" class="map"></div>
<script type="text/javascript">
function setupViewer(pixelSizeNm, extent, tileSize, numLevels, resolutions, minZoom, z, useDebugLayer, plugin) {
let projection = new ol.proj.Projection({
code: 'pixels',
units: 'pixels',
metersPerUnit: pixelSizeNm * 1e-9,
getPointResolution: function (resolution) {
return resolution
}
})
ol.proj.addProjection(projection)
let tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
tileSize: tileSize,
minZoom: minZoom
})
let view = new ol.View({
resolutions: resolutions,
extent: extent,
projection: 'pixels',
center: ol.extent.getCenter(extent),
enableRotation: false,
showFullExtent: true,
constrainOnlyCenter: true
})
function tileUrlFunction(tileCoord) {
let level = numLevels - tileCoord[0] - 1
let x = tileCoord[1]
let y = tileCoord[2]
return `/v3/slides/REPLACE_SLIDE_ID/tile/level/${level}/tile/${x}/${y}?z=${z}&plugin=${plugin}`
}
let source = new ol.source.XYZ({
tileUrlFunction: tileUrlFunction,
tileGrid: tileGrid,
projection: 'pixels',
transition: 0,
})
let histoLayer = new ol.layer.Tile({
source: source,
preload: Infinity
})
let map = new ol.Map({
target: 'map',
layers: [histoLayer],
view: view,
controls: []
})
if (useDebugLayer) {
function toSize(size, opt_size) {
if (Array.isArray(size)) {
return size;
} else {
if (opt_size === undefined) {
opt_size = [size, size];
} else {
opt_size[0] = size;
opt_size[1] = size;
}
return opt_size;
}
}
let debugSource = new ol.source.TileDebug({
tileGrid: tileGrid,
projection: 'pixels',
transition: 0,
})
debugSource.getTile = function (inverseLevel, x, y) {
tileCoordKey = inverseLevel + '/' + x + '/' + y;
if (debugSource.tileCache.containsKey(tileCoordKey)) {
return (debugSource.tileCache.get(tileCoordKey));
} else {
const tileSize = toSize(tileGrid.getTileSize(z));
const tileCoord = [inverseLevel, x, y];
const textTileCoord = debugSource.getTileCoordForTileUrlFunction(tileCoord);
let level = (numLevels - inverseLevel - 1).toString()
let text;
if (textTileCoord) {
text = `${level}/${x}/${y}`
} else {
text = '';
}
const tile = new LabeledTile(tileCoord, tileSize, text);
debugSource.tileCache.set(tileCoordKey, tile);
return tile;
}
}
let debugLayer = new ol.layer.Tile({
source: debugSource
})
map.addLayer(debugLayer)
}
let scaleLineControl = new ol.control.ScaleLine({ minWidth: 100 })
map.addControl(scaleLineControl)
map.getView().fit(extent, map.getSize())
map.render()
}
function getResolutions(levels) {
let resolutions = []
if (levels[levels.length - 1].downsample_factor < 128) {
resolutions.push(128)
}
for (let i = levels.length - 1; i >= 0; i--) {
resolutions.push(levels[i].downsample_factor)
}
return resolutions
}
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const useDebugLayer = urlParams.get("debug") == 'true'
const zString = urlParams.get("z") ? urlParams.get("z") : '0';
const plugin = urlParams.get("plugin") ? urlParams.get("plugin") : '';
const z = parseInt(zString)
axios.get(`/v3/slides/REPLACE_SLIDE_ID/info?plugin=${plugin}`)
.then(function (response) {
wsiInfo = response.data
let pixelSizeNm = (wsiInfo.pixel_size_nm.x + wsiInfo.pixel_size_nm.y) / 2.0
let extent = [0, 0, wsiInfo.extent.x, wsiInfo.extent.y]
let tileSize = [wsiInfo.tile_extent.x, wsiInfo.tile_extent.y]
let resolutions = getResolutions(wsiInfo.levels)
let numLevels = resolutions.length
let minZoom = numLevels - wsiInfo.levels.length
setupViewer(pixelSizeNm, extent, tileSize, numLevels, resolutions, minZoom, z, useDebugLayer, plugin)
})
</script>
</body>
</html>