JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://openlayers.org/en/v3.6.0/build/ol-debug.js"></script>
<div id="map" class="map"></div>
CSS
.map {
height: 200px;
width: 400px;
border:solid orange 1px;
}
.ol-mouse-position {
top: 8px;
right: 8px;
position: absolute;
}
.ol-scale-line {
background: #95b9e6;
background: rgba(0,60,136,0.3);
border-radius: 4px;
bottom: 8px;
left: 8px;
padding: 2px;
position: absolute;
}
.ol-scale-line-inner {
border: 1px solid #eeeeee;
border-top: none;
color: #eeeeee;
font-size: 10px;
text-align: center;
margin: 1px;
will-change: contents, width;
}
.ol-overlay-container {
will-change: left,right,top,bottom;
}
.ol-unsupported {
display: none;
}
.ol-viewport .ol-unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
.ol-control {
position: absolute;
background-color: #eee;
background-color: rgba(255,255,255,0.4);
border-radius: 4px;
padding: 2px;
}
.ol-control:hover {
background-color: rgba(255,255,255,0.6);
}
.ol-zoom {
top: .5em;
left: .5em;
}
.ol-rotate {
top: .5em;
right: .5em;
transition: opacity .25s linear, visibility 0s linear;
}
.ol-rotate.ol-hidden {
opacity: 0;
visibility: hidden;
transition: opacity .25s linear, visibility 0s linear .25s;
}
.ol-zoom-extent {
top: 4.643em;
left: .5em;
}
.ol-full-screen {
right: .5em;
top: .5em;
}
@media print {
.ol-control {
display: none;
}
}
.ol-control button {
display: block;
margin: 1px;
padding: 0;
color: white;
font-size: 1.14em;
font-weight: bold;
text-decoration: none;
text-align: center;
height: 1.375em;
width: 1.375em;
line-height: .4em;
background-color: #7b98bc;
background-color: rgba(0,60,136,0.5);
border: none;
border-radius: 2px;
}
.ol-control button::-moz-focus-inner {
border: none;
padding: 0;
}
.ol-zoom-extent button {
line-height: 1.4em;
}
.ol-compass {
display: block;
font-weight:...
JavaScript
// landscape map url
//var img = 'https://kriezz.files.wordpress.com/2009/05/earth_map_3200-x-1200-dual-monitor.jpg';
//var extent = [0, 0, 3200, 1200];
// portrait map url
var img = 'http://www.mapsofworld.com/united-kingdom/maps/united-kingdom-map.gif';
var extent = [0, 0,1000, 1393];
var projection = new ol.proj.Projection({
code: 'image',
units: 'pixels',
extent: extent
});
var imgW = extent[2];
var imgH = extent[3];
var boxW = document.getElementById('map').offsetWidth;
var boxH = document.getElementById('map').offsetHeight;
var xRes = imgW / boxW;
var yRes = imgH / boxH;
var maxRes = xRes < yRes ? yRes : xRes;
window.map = new ol.Map({
controls: ol.control.defaults().extend([
new ol.control.ZoomToExtent({
extent:extent
})
]),
layers: [
new ol.layer.Image({
source: new ol.source.ImageStatic({
url: img,
projection: projection,
imageExtent: extent
})
})
],
target: 'map',
view: new ol.View({
projection: projection,
center: ol.extent.getCenter(extent),
zoom: 0,
maxResolution:maxRes
})
});
//set the resolution to that of the xAxis.
map.getView().setResolution(xRes);
console.log('xRes: ' + xRes + ', yRes: ' + yRes + ', maxRes:' + maxRes);