Layer compose

by Vladimir Vershinin

HTML

<script src="https://unpkg.com/[email protected]/lodash.js"></script>
<script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io/en/v5.3.0/build/ol.js"></script>
<script src="https://unpkg.com/[email protected]/lib/index.umd.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/lib/style.css">
<div id="app">
  <div>
    <input ref="swipe" type="range" style="width: 100%" v-model="swipeValue">
  </div>

  <vl-map ref="map" data-projection="EPSG:4326">
    <vl-view ref="mapView" :zoom.sync="zoom" :center.sync="center"></vl-view>
    
    <vl-layer-tile id="osm">
      <vl-source-osm />
    </vl-layer-tile>
    
    <vl-layer-tile id="bing" @precompose="onBingPrecompose" @postcompose="onBingPostcompose">
      <vl-source-sputnik />
    </vl-layer-tile>
    
  </vl-map>
</div>

CSS

html, body, #app {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

JavaScript

new Vue({
	el: '#app',
  data () {
  	return {
    	zoom: 2,
      center: [0, 0],
      swipeValue: 50,
    }
  },
  methods: {
  	onBingPrecompose (evt) {
    	const ctx = evt.context
      const width = ctx.canvas.width * (this.swipeValue / 100)

      ctx.save()
      ctx.beginPath()
      ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height)
      ctx.clip()
    },
    onBingPostcompose (evt) {
    	var ctx = evt.context
      ctx.restore()
    },
  },
  watch: {
  	swipeValue: {
    	immediate: true,
    	handler () {
    		if (this.$refs.map && this.$refs.map.$map) {
      		this.$refs.map.$map.render()
      	}
    	},
    },
  },
})