diff --git a/README.md b/README.md index 62b3084..03ad75d 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ mapbox-gl-compare --- -Swipe and sync between two maps +Compare between two synced maps. +Support modes: + - Slider + - Lens -![Swipe example](http://i.imgur.com/MvjwVLu.gif) +![Slider example](http://i.imgur.com/MvjwVLu.gif) Map movements are synced with [mapbox-gl-sync-move](https://github.com/mapbox/mapbox-gl-sync-move). @@ -24,8 +27,9 @@ var after = new mapboxgl.Map({ var container = '#comparison-container'; new mapboxgl.Compare(before, after, container, { + mode: 'slider', // Optional. Set the mode, defaults to slider. mousemove: true, // Optional. Set to true to enable swiping during cursor movement. - orientation: 'vertical' // Optional. Sets the orientation of swiper to horizontal or vertical, defaults to vertical + orientation: 'vertical', // Optional. Sets the orientation of swiper to horizontal or vertical, defaults to vertical }); ``` @@ -50,6 +54,18 @@ compare.on('slideend', (e) => { //Remove - this will remove the compare control from the DOM and stop synchronizing the two maps. compare.remove(); + +// Compare in lens mode. +lensCompare = new mapboxgl.Compare(before, after, container, { + mode: 'lens', + size: 80 // Optional. Set the size of the lens, defaults to 100. +}); + +//Set Size - this will set the size of the lens +lensCompare.setSize(100); + +//Remove - this will remove the compare control from the DOM and stop synchronizing the two maps. +lensCompare.remove(); ``` Demo: https://www.mapbox.com/mapbox-gl-js/example/mapbox-gl-compare/ diff --git a/dist/mapbox-gl-compare.js b/dist/mapbox-gl-compare.js index fb72b1b..466b4d9 100644 --- a/dist/mapbox-gl-compare.js +++ b/dist/mapbox-gl-compare.js @@ -1 +1 @@ -!function r(s,h,u){function a(t,e){if(!h[t]){if(!s[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(c)return c(t,!0);var i=new Error("Cannot find module '"+t+"'");throw i.code="MODULE_NOT_FOUND",i}var o=h[t]={exports:{}};s[t][0].call(o.exports,function(e){return a(s[t][1][e]||e)},o,o.exports,r,s,h,u)}return h[t].exports}for(var c="function"==typeof require&&require,e=0;ethis._bounds.width&&(t=this._bounds.width),t},_getY:function(e){var t=(e=e.touches?e.touches[0]:e).clientY-this._bounds.top;return t<0&&(t=0),t>this._bounds.height&&(t=this._bounds.height),t},setSlider:function(e){this._setPosition(e)},on:function(e,t){return this._ev.on(e,t),this},fire:function(e,t){return this._ev.emit(e,t),this},off:function(e,t){return this._ev.removeListener(e,t),this},remove:function(){this._clearSync(),this._mapB.off("resize",this._onResize);var e=this._mapA.getContainer();e&&(e.style.clip=null,e.removeEventListener("mousemove",this._onMove));var t=this._mapB.getContainer();t&&(t.style.clip=null,t.removeEventListener("mousemove",this._onMove)),this._swiper.removeEventListener("mousedown",this._onDown),this._swiper.removeEventListener("touchstart",this._onDown),this._controlContainer.remove()}},window.mapboxgl?mapboxgl.Compare=i:void 0!==t&&(t.exports=i)},{"@mapbox/mapbox-gl-sync-move":2,events:3}],2:[function(e,t,n){t.exports=function(){var t,e=arguments.length;if(1===e)t=arguments[0];else{t=[];for(var n=0;no){s.warned=!0;var h=new Error("Possible EventEmitter memory leak detected. "+s.length+' "'+String(t)+'" listeners added. Use emitter.setMaxListeners() to increase limit.');h.name="MaxListenersExceededWarning",h.emitter=e,h.type=t,h.count=s.length,"object"==typeof console&&console.warn&&console.warn("%s: %s",h.name,h.message)}}else s=r[t]=n,++e._eventsCount;return e}function f(){if(!this.fired)switch(this.target.removeListener(this.type,this.wrapFn),this.fired=!0,arguments.length){case 0:return this.listener.call(this.target);case 1:return this.listener.call(this.target,arguments[0]);case 2:return this.listener.call(this.target,arguments[0],arguments[1]);case 3:return this.listener.call(this.target,arguments[0],arguments[1],arguments[2]);default:for(var e=new Array(arguments.length),t=0;tthis._bounds.width&&(e=this._bounds.width),e},_getY:function(t){var e=(t=t.touches?t.touches[0]:t).clientY-this._bounds.top;return e<0&&(e=0),e>this._bounds.height&&(e=this._bounds.height),e},setSize:function(t){this._halfSize=(t||100)/2},setSlider:function(t){this._horizontal?(t<0&&(t=0),t>this._bounds.height&&(t=this._bounds.height),this._setPosition([0,t])):(t<0&&(t=0),t>this._bounds.width&&(t=this._bounds.width),this._setPosition([t,0]))},on:function(t,e){return this._ev.on(t,e),this},fire:function(t,e){return this._ev.emit(t,e),this},off:function(t,e){return this._ev.removeListener(t,e),this},remove:function(){this._clearSync(),this._mapB.off("resize",this._onResize);var t=this._mapA.getContainer();t&&(t.style.clip=null,t.removeEventListener("mousemove",this._onMove));var e=this._mapB.getContainer();e&&(e.style.clip=null,e.removeEventListener("mousemove",this._onMove)),this._swiper.removeEventListener("mousedown",this._onDown),this._swiper.removeEventListener("touchstart",this._onDown),this._controlContainer.remove()}},window.mapboxgl?mapboxgl.Compare=i:void 0!==e&&(e.exports=i)},{"@mapbox/mapbox-gl-sync-move":2,events:3}],2:[function(t,e,n){e.exports=function(){var e,t=arguments.length;if(1===t)e=arguments[0];else{e=[];for(var n=0;no){s.warned=!0;var h=new Error("Possible EventEmitter memory leak detected. "+s.length+' "'+String(e)+'" listeners added. Use emitter.setMaxListeners() to increase limit.');h.name="MaxListenersExceededWarning",h.emitter=t,h.type=e,h.count=s.length,"object"==typeof console&&console.warn&&console.warn("%s: %s",h.name,h.message)}}else s=r[e]=n,++t._eventsCount;return t}function f(){if(!this.fired)switch(this.target.removeListener(this.type,this.wrapFn),this.fired=!0,arguments.length){case 0:return this.listener.call(this.target);case 1:return this.listener.call(this.target,arguments[0]);case 2:return this.listener.call(this.target,arguments[0],arguments[1]);case 3:return this.listener.call(this.target,arguments[0],arguments[1],arguments[2]);default:for(var t=new Array(arguments.length),e=0;e this._bounds.height) y = this._bounds.height; return y; }, + + /** + * Set the size of the lens. + * + * @param {number} size Lens size. + */ + setSize: function(size) { + this._halfSize = (size ? size : 100) / 2; + }, /** * Set the position of the slider. @@ -152,7 +191,15 @@ Compare.prototype = { * @param {number} x Slider position in pixels from left/top. */ setSlider: function(x) { - this._setPosition(x); + if (this._horizontal) { + if (x < 0) x = 0; + if (x > this._bounds.height) x = this._bounds.height; + this._setPosition([0, x]); + } else { + if (x < 0) x = 0; + if (x > this._bounds.width) x = this._bounds.width; + this._setPosition([x, 0]); + } }, /** diff --git a/package.json b/package.json index 234bd45..f8040ed 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mapbox-gl-compare", - "version": "0.4.0", - "description": "Swipe and sync between two maps", + "version": "0.5.0", + "description": "Compare between two synced maps", "main": "index.js", "directories": { "example": "example"