Skip to content

Instantly share code, notes, and snippets.

@mejackreed
Last active February 12, 2024 12:16
Show Gist options
  • Save mejackreed/6936585f435b60aa9451ae2bc1c199f2 to your computer and use it in GitHub Desktop.
Save mejackreed/6936585f435b60aa9451ae2bc1c199f2 to your computer and use it in GitHub Desktop.
Leaflet-IIIF Cropping
var baseUrl = 'https://stacks.stanford.edu/image/iiif/hg676jb4964%2F0380_796-44';
var map = L.map('map', {
center: [0, 0],
crs: L.CRS.Simple,
zoom: 0,
});
var iiifLayer = L.tileLayer.iiif(baseUrl + '/info.json', {
tileSize: 512
}).addTo(map);
var areaSelect = L.areaSelect({
width:100, height:100
});
areaSelect.addTo(map);
$('#urlArea').html(baseUrl)
areaSelect.on('change', function() {
var bounds = this.getBounds();
var zoom = map.getZoom();
var min = map.project(bounds.getSouthWest(), zoom);
var max = map.project(bounds.getNorthEast(), zoom);
var imageSize = iiifLayer._imageSizes[zoom];
var xRatio = iiifLayer.x / imageSize.x;
var yRatio = iiifLayer.y / imageSize.y;
var region = [
Math.floor(min.x * xRatio),
Math.floor(max.y * yRatio),
Math.floor((max.x - min.x) * xRatio),
Math.floor((min.y - max.y) * yRatio)
];
var url = baseUrl + '/' + region.join(',') + '/full/0/default.jpg';
$('#urlArea').html(
'<a href="' + url + '" target=_blank>' + url + '</a>'
)
});
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" />
<link rel="stylesheet" href="https://rawgit.com/heyman/leaflet-areaselect/master/src/leaflet-areaselect.css" />
<link rel="stylesheet" href="styles.css">
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/mejackreed/Leaflet-IIIF/v1.2.2/leaflet-iiif.js"></script>
<script src="https://cdn.rawgit.com/heyman/leaflet-areaselect/da8905695db78354ddd5b5d9de6ef13699b9d7a7/src/leaflet-areaselect.js"></script>
</head>
<body>
<div id="map"></div>
<div id="urlArea"></div>
<script src="app.js"></script>
</body>
</html>
MIT License
Copyright (c) 2016 Jack Reed
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
html, body, #map{
height: 100%;
margin: 0;
}
.leaflet-areaselect-container {
position:absolute;
width:100%;
height:100%;
}
#urlArea {
background-color: rgba(255, 255, 255, 0.5);
font-family: sans-serif;
position: absolute;
top: 0px;
left: 50%;
z-index: 100000;
}
@danhorst
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment