OLViewer = async function* (attachement, viewHeight = 1080) {
const ol = await require("ol@7.1.0/dist/ol.js").catch(() => window.ol);
if (!ol._style)
ol._style = document.head.appendChild(
html`<link rel=stylesheet href="${await require.resolve(
"ol@7.1.0/ol.css"
)}">`
);
const img = await attachement.image();
const url = await attachement.url();
const target = html`<div tabindex="1" style="height: ${
(img.height / img.width) * viewHeight
}px;">`;
yield target;
const ImageLayer = ol.layer.Image;
const Map = ol.Map;
const Projection = ol.proj.Projection;
const Static = ol.source.ImageStatic;
const View = ol.View;
const { getCenter } = ol.extent;
const { defaults } = ol.interaction.defaults;
const { FullScreen } = ol.control;
const extent = [0, 0, img.width, img.height];
const projection = new Projection({
code: "xkcd-image",
units: "pixels",
extent: extent
});
const map = (target.value = new Map({
controls: defaults().extend([new FullScreen()]),
layers: [
new ImageLayer({
source: new Static({
url: url,
projection: projection,
imageExtent: extent
})
})
],
target,
view: new View({
projection: projection,
center: getCenter(extent),
zoom: 2,
maxZoom: 8
})
}));
map.getViewport().style.cursor = "-webkit-grab";
map.on("pointerdrag", function (evt) {
map.getViewport().style.cursor = "-webkit-grabbing";
});
map.on("pointerup", function (evt) {
map.getViewport().style.cursor = "-webkit-grab";
});
invalidation.then(() => map.dispose());
return target;
}