Layer Swipe

Example of a Layer swipe map.

Example of a Layer swipe map.

<!DOCTYPE html>
<html>
  <head>
    <title>Layer Swipe</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <input id="swipe" type="range" style="width: 100%">
    <script>
      var osm = new ol.layer.Tile({
        source: new ol.source.OSM()
      });
      var bing = new ol.layer.Tile({
        source: new ol.source.BingMaps({
          key: 'Your Bing Maps Key from http://www.bingmapsportal.com/ here',
          imagerySet: 'Aerial'
        })
      });

      var map = new ol.Map({
        layers: [osm, bing],
        target: 'map',
        controls: ol.control.defaults({
          attributionOptions: {
            collapsible: false
          }
        }),
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      });

      var swipe = document.getElementById('swipe');

      bing.on('precompose', function(event) {
        var ctx = event.context;
        var width = ctx.canvas.width * (swipe.value / 100);

        ctx.save();
        ctx.beginPath();
        ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height);
        ctx.clip();
      });

      bing.on('postcompose', function(event) {
        var ctx = event.context;
        ctx.restore();
      });

      swipe.addEventListener('input', function() {
        map.render();
      }, false);
    </script>
  </body>
</html>