Skip to content

Instantly share code, notes, and snippets.

@cbeer
Forked from mejackreed/LICENSE.txt
Last active February 9, 2017 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbeer/6675b637f66f699b312cdbe95e3b64c5 to your computer and use it in GitHub Desktop.
Save cbeer/6675b637f66f699b312cdbe95e3b64c5 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 aspectRatioPreservingRectangleEditor = function(aspect) {
return L.Editable.RectangleEditor.extend({
extendBounds: function (e) {
var index = e.vertex.getIndex(),
next = e.vertex.getNext(),
previous = e.vertex.getPrevious(),
oppositeIndex = (index + 2) % 4,
opposite = e.vertex.latlngs[oppositeIndex];
if ((index % 2) == 1) {
// calculate horiz. displacement
e.latlng.update([opposite.lat + ((1 / aspect) * (opposite.lng - e.latlng.lng)), e.latlng.lng]);
} else {
// calculate vert. displacement
e.latlng.update([e.latlng.lat, (opposite.lng - (aspect * (opposite.lat - e.latlng.lat)))]);
}
var bounds = new L.LatLngBounds(e.latlng, opposite);
// Update latlngs by hand to preserve order.
previous.latlng.update([e.latlng.lat, opposite.lng]);
next.latlng.update([opposite.lat, e.latlng.lng]);
this.updateBounds(bounds);
this.refreshVertexMarkers();
}
});
};
var map = L.map('map', {
editable: true,
center: [0, 0],
crs: L.CRS.Simple,
zoom: 0,
editOptions: {
rectangleEditorClass: aspectRatioPreservingRectangleEditor(10.0)
}
});
var iiifLayer = L.tileLayer.iiif(baseUrl + '/info.json').addTo(map);
var loaded = false;
iiifLayer.on('load', function() {
if(loaded) {
return;
}
loaded = true;
var b = [100, 100, 500, 50];
var minPoint = L.point(parseInt(b[0]), parseInt(b[1]));
var maxPoint = L.point(parseInt(b[0]) + parseInt(b[2]), parseInt(b[1]) + parseInt(b[3]));
var maxZoom = iiifLayer.maxZoom;
var min = map.unproject(minPoint, iiifLayer.maxZoom);
var max = map.unproject(maxPoint, iiifLayer.maxZoom);
var rec = L.rectangle([
min, max
]);
rec.addTo(map);
rec.enableEdit();
rec.on('dblclick', L.DomEvent.stop).on('dblclick', rec.toggleEdit);
map.on('editable:dragend editable:vertex:dragend', function(e) {
var bounds = e.layer.getBounds();
var min = e.target.project(bounds.getNorthWest(), iiifLayer.maxZoom);
var max = e.target.project(bounds.getSouthEast(), iiifLayer.maxZoom);
var region = [Math.round(min.x), Math.round(min.y), Math.round(max.x - min.x), Math.round(max.y - min.y)]
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.0.2/dist/leaflet.css" />
<link rel="stylesheet" href="styles.css">
<script src="https://unpkg.com/leaflet@1.0.2/dist/leaflet.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/mejackreed/Leaflet-IIIF/v1.0.1/leaflet-iiif.js"></script>
<script src="https://npmcdn.com/leaflet.path.drag/src/Path.Drag.js"></script>
<script src="http://leaflet.github.io/Leaflet.Editable/src/Leaflet.Editable.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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment