UNPKG

ol-rotate-feature

Version:

Rotate vector features interaction for OpenLayers

12 lines (10 loc) 12.5 kB
/*! Rotate vector features interaction for OpenLayers @package ol-rotate-feature @author Vladimir Vershinin <ghettovoice@gmail.com> @version 3.3.0 @licence MIT @copyright (c) 2016-2025, Vladimir Vershinin <ghettovoice@gmail.com> */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("ol/interaction"),require("ol"),require("ol/layer"),require("ol/source/Vector"),require("ol/geom"),require("ol/style"),require("ol/extent"),require("ol/events/condition")):"function"==typeof define&&define.amd?define("ol-rotate-feature",["ol/interaction","ol","ol/layer","ol/source/Vector","ol/geom","ol/style","ol/extent","ol/events/condition"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).RotateFeatureInteraction=t(e.ol.interaction,e.ol,e.ol.layer,e.ol.source.Vector,e.ol.geom,e.ol.style,e.ol.extent,e.ol.events.condition)}(this,(function(e,t,r,n,o,i,a,s){"use strict";function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=u(n);function l(e){return(l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function h(e){var t=function(e,t){if("object"!==l(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!==l(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"===l(t)?t:String(t)}function f(e,t,r){return(t=h(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function g(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function d(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,h(n.key),n)}}function y(e,t,r){return t&&d(e.prototype,t),r&&d(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function p(e,t){if(t&&("object"===l(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function _(e){return(_=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function v(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=_(e)););return e}function w(){return(w="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(e,t,r){var n=v(e,t);if(n){var o=Object.getOwnPropertyDescriptor(n,t);return o.get?o.get.call(arguments.length<3?e:r):o.value}}).apply(this,arguments)}function A(e,t){return(A=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e})(e,t)}function F(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(t=["Assertion failed",t].join(": "),!e)throw new Error(t)}function b(e){return e}function m(e,t){return-1!==e.indexOf(t)}function k(e){return"[object Array]"===Object.prototype.toString.call(e)}var C="rotatestart",O="rotating",E="rotateend",S=function(){return y((function e(t,r,n,o){g(this,e),this.propagationStopped_=!1,this.type_=t,this.features_=r,this.angle_=n,this.anchor_=o}),[{key:"propagationStopped",get:function(){return this.propagationStopped_}},{key:"type",get:function(){return this.type_}},{key:"features",get:function(){return this.features_}},{key:"angle",get:function(){return this.angle_}},{key:"anchor",get:function(){return this.anchor_}},{key:"preventDefault",value:function(){this.propagationStopped_=!0}},{key:"stopPropagation",value:function(){this.propagationStopped_=!0}}])}(),x="undefined"!=typeof navigator?navigator.userAgent.toLowerCase():"",j=-1!==x.indexOf("macintosh"),M=-1!==x.indexOf("webkit")&&-1==x.indexOf("edge");function P(e,t,r){return t=_(t),p(e,function(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(e){}return function(){return!!e}()}()?Reflect.construct(t,r||[],_(e).constructor):t.apply(e,r))}var R=function(e){function n(){var e,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(g(this,n),(e=P(this,n,[{handleDownEvent:G,handleUpEvent:T,handleDragEvent:U,handleMoveEvent:q}])).previousCursor_=void 0,e.anchorFeature_=void 0,e.arrowFeature_=void 0,e.lastCoordinate_=void 0,e.anchorMoving_=!1,e.overlay_=new r.Vector({style:o.style||I(),source:new c.default({features:new t.Collection})}),e.condition_=o.condition?o.condition:s.always,e.features_=void 0,o.features)if(k(o.features))e.features_=new t.Collection(o.features);else{if(!(o.features instanceof t.Collection))throw new Error("Features option should be an array or collection of features, got "+l(o.features));e.features_=o.features}else e.features_=new t.Collection;return e.allowAnchorMovement=void 0===o.allowAnchorMovement||o.allowAnchorMovement,e.setAnchor(o.anchor||D(e.features_)),e.setAngle(o.angle||0),e.features_.on("add",e.onFeatureAdd_.bind(e)),e.features_.on("remove",e.onFeatureRemove_.bind(e)),e.on("change:angle",e.onAngleChange_.bind(e)),e.on("change:anchor",e.onAnchorChange_.bind(e)),e.createOrUpdateAnchorFeature_(),e.createOrUpdateArrowFeature_(),e}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&A(e,t)}(n,e),y(n,[{key:"features",get:function(){return this.features_}},{key:"angle",get:function(){return this.getAngle()},set:function(e){this.setAngle(e)}},{key:"anchor",get:function(){return this.getAnchor()},set:function(e){this.setAnchor(e)}},{key:"map",get:function(){return this.getMap()},set:function(e){this.setMap(e)}},{key:"active",get:function(){return this.getActive()},set:function(e){this.setActive(e)}},{key:"setMap",value:function(e){this.overlay_.setMap(e),w(_(n.prototype),"setMap",this).call(this,e)}},{key:"setActive",value:function(e){this.overlay_&&this.overlay_.setMap(e?this.map:void 0),w(_(n.prototype),"setActive",this).call(this,e)}},{key:"setAngle",value:function(e){F(!isNaN(parseFloat(e)),"Numeric value passed"),this.set("angle",parseFloat(e))}},{key:"getAngle",value:function(){return this.get("angle")}},{key:"setAnchor",value:function(e){F(null==e||k(e)&&2===e.length,"Array of two elements passed"),this.set("anchor",null!=e?e.map(parseFloat):D(this.features_))}},{key:"getAnchor",value:function(){return this.get("anchor")}},{key:"createOrUpdateAnchorFeature_",value:function(){var e=this.getAngle(),r=this.getAnchor();r&&(this.anchorFeature_?(this.anchorFeature_.getGeometry().setCoordinates(r),this.anchorFeature_.set("angle",e)):(this.anchorFeature_=new t.Feature(f(f({geometry:new o.Point(r)},"angle",e),"rotate-anchor",!0)),this.overlay_.getSource().addFeature(this.anchorFeature_)))}},{key:"createOrUpdateArrowFeature_",value:function(){var e=this.getAngle(),r=this.getAnchor();r&&(this.arrowFeature_?(this.arrowFeature_.getGeometry().setCoordinates(r),this.arrowFeature_.set("angle",e)):(this.arrowFeature_=new t.Feature(f(f({geometry:new o.Point(r)},"angle",e),"rotate-arrow",!0)),this.overlay_.getSource().addFeature(this.arrowFeature_)))}},{key:"resetAngleAndAnchor_",value:function(){this.resetAngle_(),this.resetAnchor_()}},{key:"resetAngle_",value:function(){this.set("angle",0,!0),this.arrowFeature_&&this.arrowFeature_.set("angle",this.getAngle()),this.anchorFeature_&&this.anchorFeature_.set("angle",this.getAngle())}},{key:"resetAnchor_",value:function(){this.set("anchor",D(this.features_),!0),this.getAnchor()&&(this.arrowFeature_&&this.arrowFeature_.getGeometry().setCoordinates(this.getAnchor()),this.anchorFeature_&&this.anchorFeature_.getGeometry().setCoordinates(this.getAnchor()))}},{key:"onFeatureAdd_",value:function(){this.resetAngleAndAnchor_(),this.createOrUpdateAnchorFeature_(),this.createOrUpdateArrowFeature_()}},{key:"onFeatureRemove_",value:function(){this.resetAngleAndAnchor_(),this.features_.getLength()?(this.createOrUpdateAnchorFeature_(),this.createOrUpdateArrowFeature_()):(this.overlay_.getSource().clear(),this.anchorFeature_=this.arrowFeature_=void 0)}},{key:"onAngleChange_",value:function(e){var t=this,r=e.oldValue;this.features_.forEach((function(e){return e.getGeometry().rotate(t.getAngle()-r,t.getAnchor())})),this.arrowFeature_&&this.arrowFeature_.set("angle",this.getAngle()),this.anchorFeature_&&this.anchorFeature_.set("angle",this.getAngle())}},{key:"onAnchorChange_",value:function(){var e=this.getAnchor();e&&(this.anchorFeature_&&this.anchorFeature_.getGeometry().setCoordinates(e),this.arrowFeature_&&this.arrowFeature_.getGeometry().setCoordinates(e))}},{key:"dispatchRotateStartEvent_",value:function(e){this.dispatchEvent(new S(C,e,this.getAngle(),this.getAnchor()))}},{key:"dispatchRotatingEvent_",value:function(e){this.dispatchEvent(new S(O,e,this.getAngle(),this.getAnchor()))}},{key:"dispatchRotateEndEvent_",value:function(e){this.dispatchEvent(new S(E,e,this.getAngle(),this.getAnchor()))}}])}(e.Pointer);function G(e){if(!(s.mouseOnly(e)||s.touchOnly(e)||s.penOnly(e)))return!1;if(0==(r=e.originalEvent).button&&!(M&&j&&r.ctrlKey)&&this.condition_(e)){var t=e.map.forEachFeatureAtPixel(e.pixel,b);if(m(["click","singleclick","dblclick"],e.type)&&m([this.anchorFeature_,this.arrowFeature_],t))return!1;if(t&&!this.lastCoordinate_&&(m(this.features_.getArray(),t)||t===this.arrowFeature_))return this.lastCoordinate_=e.coordinate,q.call(this,e),this.dispatchRotateStartEvent_(this.features_),!0;if(t&&t===this.anchorFeature_&&this.allowAnchorMovement)return this.anchorMoving_=!0,q.call(this,e),!0}var r;return!1}function T(e){return this.lastCoordinate_?(this.lastCoordinate_=void 0,q.call(this,e),this.dispatchRotateEndEvent_(this.features_),!0):!!this.anchorMoving_&&(this.anchorMoving_=!1,q.call(this,e),!0)}function U(e){var t=e.coordinate,r=this.anchorFeature_.getGeometry().getCoordinates();if(this.lastCoordinate_){var n=[this.lastCoordinate_[0]-r[0],this.lastCoordinate_[1]-r[1]],o=[t[0]-r[0],t[1]-r[1]],i=Math.atan2(n[0]*o[1]-o[0]*n[1],n[0]*o[0]+n[1]*o[1]);this.setAngle(this.getAngle()+i),this.dispatchRotatingEvent_(this.features_),this.lastCoordinate_=t}else this.anchorMoving_&&this.setAnchor(t)}function q(e){var t=e.map,r=e.pixel,n=t.getTargetElement(),o=t.forEachFeatureAtPixel(r,b),i=function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];t&&(n.style.cursor="-webkit-"+e,n.style.cursor="-moz-"+e),n.style.cursor=e};this.lastCoordinate_?(this.previousCursor_=n.style.cursor,i("grabbing",!0)):o&&(m(this.features_.getArray(),o)||o===this.arrowFeature_)?(this.previousCursor_=n.style.cursor,i("grab",!0)):o&&o===this.anchorFeature_&&this.allowAnchorMovement||this.anchorMoving_?(this.previousCursor_=n.style.cursor,i("crosshair")):(i(this.previousCursor_||""),this.previousCursor_=void 0)}function I(){var e=[255,255,255,.8],t=[0,153,255,.8],r=[255,255,255,.01],n=f(f({},"rotate-anchor",[new i.Style({image:new i.RegularShape({fill:new i.Fill({color:[0,153,255,.8]}),stroke:new i.Stroke({color:t,width:1}),radius:4,points:6}),zIndex:1/0})]),"rotate-arrow",[new i.Style({fill:new i.Fill({color:r}),stroke:new i.Stroke({color:e,width:4}),text:new i.Text({font:"12px sans-serif",offsetX:20,offsetY:-20,fill:new i.Fill({color:"blue"}),stroke:new i.Stroke({color:e,width:3})}),zIndex:1/0}),new i.Style({fill:new i.Fill({color:r}),stroke:new i.Stroke({color:t,width:2}),zIndex:1/0})]);return function(e,t){var r,i=e.get("angle")||0;switch(!0){case e.get("rotate-anchor"):return(r=n["rotate-anchor"])[0].getImage().setRotation(-i),r;case e.get("rotate-arrow"):r=n["rotate-arrow"];var a=e.getGeometry().getCoordinates(),s=new o.Polygon([[[a[0],a[1]-6*t],[a[0]+8*t,a[1]-12*t],[a[0],a[1]+30*t],[a[0]-8*t,a[1]-12*t],[a[0],a[1]-6*t]]]);return s.rotate(i,a),r[0].setGeometry(s),r[1].setGeometry(s),r[0].getText().setText(Math.round(180*-i/Math.PI)+"°"),r}}}function D(e){if((e=e instanceof t.Collection?e.getArray():e).length)return a.getCenter(function(e){if((e=e instanceof t.Collection?e.getArray():e).length)return new o.GeometryCollection(e.map((function(e){return e.getGeometry()}))).getExtent()}(e))}return"undefined"!=typeof window&&window.ol&&window.ol.interaction&&(window.ol.interaction.RotateFeature=R),R})); //# sourceMappingURL=ol-rotate-feature.umd.min.js.map