Example demonstrating a spyglass effect

Riffing off of the layer swipe example, this one demonstrates viewing one layer over another though a lens.
This commit is contained in:
Tim Schaub
2014-01-10 13:32:54 -07:00
parent ab0268a8f1
commit 29e3794845
2 changed files with 128 additions and 0 deletions

60
examples/layer-spy.html Normal file
View File

@@ -0,0 +1,60 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="../css/ol.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
<title>Layer Spy Example</title>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./"><img src="../resources/logo.png"> OpenLayers 3 Examples</a>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map"></div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h4 id="title">Layer spy example</h4>
<p id="shortdesc">View a portion of one layer over another</p>
<div id="docs">
<p>
Layer rendering can be manipulated in <code>precompose</code> and <code>postcompose</code> event listeners.
These listeners get an event with a reference to the Canvas rendering context.
In this example, the <code>precompose</code> listener sets a clipping mask around the most
recent mouse position, giving you a spyglass effect for viewing one layer over another.
</p>
<p>
Move around the map to see the effect. Use the ↑ up and ↓ down arrow keys to adjust the spyglass size.
</p>
<p>See the <a href="layer-spy.js" target="_blank">layer-spy.js source</a> to see how this is done.</p>
</div>
<div id="tags">spy, image manipulation</div>
</div>
</div>
</div>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="loader.js?id=layer-spy" type="text/javascript"></script>
<script src="../resources/example-behaviour.js" type="text/javascript"></script>
</body>
</html>

68
examples/layer-spy.js Normal file
View File

@@ -0,0 +1,68 @@
goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.layer.Tile');
goog.require('ol.proj');
goog.require('ol.source.BingMaps');
var key = 'Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3';
var roads = new ol.layer.Tile({
source: new ol.source.BingMaps({key: key, imagerySet: 'Road'})
});
var imagery = new ol.layer.Tile({
source: new ol.source.BingMaps({key: key, imagerySet: 'Aerial'})
});
var map = new ol.Map({
layers: [roads, imagery],
renderer: ol.RendererHint.CANVAS,
target: 'map',
view: new ol.View2D({
center: ol.proj.transform([-109, 46.5], 'EPSG:4326', 'EPSG:3857'),
zoom: 6
})
});
var radius = 75;
$(document).keydown(function(evt) {
if (evt.which === 38) {
radius = Math.min(radius + 5, 150);
map.requestRenderFrame();
} else if (evt.which === 40) {
radius = Math.max(radius - 5, 25);
map.requestRenderFrame();
}
});
// get the pixel position with every move
var mousePosition = null;
$(map.getViewport()).on('mousemove', function(evt) {
mousePosition = map.getEventPixel(evt.originalEvent);
map.requestRenderFrame();
}).on('mouseout', function() {
mousePosition = null;
map.requestRenderFrame();
});
// before rendering the layer, do some clipping
imagery.on('precompose', function(event) {
var ctx = event.getContext();
ctx.save();
ctx.beginPath();
if (mousePosition) {
// only show a circle around the mouse
ctx.arc(mousePosition[0], mousePosition[1], radius, 0, 2 * Math.PI);
ctx.lineWidth = 5;
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
ctx.stroke();
}
ctx.clip();
});
// after rendering the layer, restore the canvas context
imagery.on('postcompose', function(event) {
var ctx = event.getContext();
ctx.restore();
});