Coder Social home page Coder Social logo

w8r / leaflet.path.transform Goto Github PK

View Code? Open in Web Editor NEW
167.0 11.0 57.0 5.5 MB

Drag/rotate/resize handler for leaflet vector features.

Home Page: http://w8r.github.io/Leaflet.Path.Transform

CSS 68.00% HTML 2.79% JavaScript 28.50% TypeScript 0.71%
leaflet leaflet-plugins geometry edit transformations rotate scale

leaflet.path.transform's Introduction

Leaflet.Path.Transform npm version

Drag/rotate/resize handler for leaflet vector features.

screenshot 2016-03-21 15 31 48

Includes L.Path.Drag, so you don't need to include it once again.

Requirements

Leaflet 1.0+

API

npm install leaflet-path-transform --save

or include dist/L.Path.Transform.js file

require('leaflet-path-transform');

var map = L.map('map-canvas').setView(center, zoom);
var polygon = L.polygon([..., ...], { transform: true }).addTo(map);

polygon.transform.enable();
// or partially:
polygon.transform.enable({rotation: true, scaling: false});
// or, on an already enabled handler:
polygon.transform.setOptions({rotation: true, scaling: false});

If you have changed the geometry of the transformed layer and want the tool to reflect the changes, use:

// you have changed the geometry here
layer.setLatLngs([...]);
// and want to update handlers:
layer.transform.reset();

options

  • options.handlerOptions - <Path_options> - edge markers options
  • options.boundsOptions - <Polyline_options> - bounding rectangle options
  • options.rotateHandleOptions - <Polyline_options> - rotation handle line styles
  • options.handleLength - Number - Length of the rotation handle in pixels. Defaults to 20.
  • options.rotation - Boolean - Enable/disable rotation. Default true
  • options.scaling - Boolean - Enable/disable scaling. Default true
  • options.uniformScaling - Boolean - Use uniform scaling (maintain aspect ratio). Default true

Handles

For the corner and rotation handles plugin provides 2 classes: L.PathTransform.Handle and L.PathTransform.RotateHandle, they are derived from L.CircleMarker and you can adjust them as you want. Also you can use some other compatible marker types by providing respective constructors through options.handleClass and options.rotateHandleClass.

Cursors:

Handler assigns resize cursors to handles. You can override that by setting options.handlerOptions.setCursor and options.rotateHandleOptions.setCursor to false

Events

Following events are fired on the transformed layer

  • rotatestart, rotate, rotateend - { rotation: <Radians> }
  • scalestart, scale, scaleend - { scale: <L.Point> }
  • transformstart, transform, transformed - { rotation: ..., scale: ..., matrix: <L.Matrix> }

Dragging

To control features dragging, see L.Path.Drag docs.

polygon.dragging.disable();
polygon.dragging.enable();

TODO

License

Copyright (c)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

leaflet.path.transform's People

Contributors

ammd1313 avatar dependabot[bot] avatar eeroki avatar lc-spxl avatar muka avatar strfx avatar teddyward avatar w8r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leaflet.path.transform's Issues

_transformGeometries overrides matrix with null values

Whenever I rotate a shape, _transformGeometries executes this._path._transform(null); before this._transformPoints(this._path);, this yields exception Uncaught Error: Invalid LatLng object: (NaN, NaN) since the non existent matrix values recalculates the lat/lng values to NULL.

how to adapt this to imageOverlay ?

Thanks for creating this plugin. Could you advise on how to adapt it to imageOverlay? It looks like all the calculations that are applied to 'transform' style would handle as well as elements.

ios13 won't support it

I am a plug-in user, but there is a compatibility problem at present. I hope the author can help me. I need your help. Compatibility ios13

GeoJson

Does this is compatible with GeoJSON polygons?

Plugin doesn't give correct rotation value for some cases.

Here I'm considering 360 degree rotation. Rotation values are correctly given for until we rotate it up to(rotated clock wise ) 4800mils (270 degrees.). But After the point it suddenly gives incorrect rotation value. Even for the small rotate angle its return something unacceptable value from radian. Any suggestions for the issue.
Steps to replicate

  1. Create polygon and rotate it up to angle < 4800mils. It returns correct angle value.
  2. Then rotate the polygon(from current position) again up to angle >4800mills .
    Result : it return very high rotation negative value than expected.

Add transform if layer exists

Maybe you could add this in README or create a own function:

polygon.transform = new L.Handler.PathTransform(polygon, polygon.options.transform);

Different rotation origin

Hello, I very like your profile. However, I have case when I need to rotate my polygon where pivot is not center of polygon but bottom-left corner. So, I have overriden _getRotationOrigin method. Looks like this

_getRotationOrigin: function() { return this._rect._latlngs[0][0]; },

For me this is perfect. However, when I am rotating then preview matrix looks perfect. Unfortunately, after finishing rotation (_onrotateend) my polygon has been rotated but pivot is around mid again, looks different than on preview. What should I change yet to make this rotation origin adequate at the end? I was figthing with it around 2 days and still can do nothing...

problems with drag

When a separate marker is added, there is an occasional event conflict when drag scale icon or rotate icon

Scaling stops working when setting rotation to false

After setting rotation to false (polygon.transform.enable({rotation: false, scaling: true});) I start receiving the following error every time I try to scale the polygon:

leaflet-src.js:72

Uncaught TypeError: Cannot read property '_leaflet_id' of null
at stamp (leaflet-src.js:72)
at NewClass.removeLayer (leaflet-src.js:6557)
at NewClass._onScaleStart (Path.Transform.js:612)
at NewClass.fire (leaflet-src.js:593)
at NewClass._fireDOMEvent (leaflet-src.js:4342)
at NewClass._handleDOMEvent (leaflet-src.js:4299)
at HTMLDivElement.handler (leaflet-src.js:2280)

It works perfectly if I just enable transform without passing any options (polygon.transform.enable();), any idea why?

Thanks,

Carlos

Bounding box persistence after shape modification

Hi there,
when using the Leaflet.Editable plugin, after doing modifications to the shape points the bounding box stays the same as when the shape was initialized. Plus, sometimes when editing the shape the bounding box gets distorted by the points modified.

I've tried disabling and enabling the transform settings but that wouldn't work.

Is there anything we can do to avoid this?

error

Thank you

setLatLngs on polygon after scaling does not update dashed resize box

If I create a transformable polygon like

var printBoundary = L.polygon([...],{ transform:true }).addTo(map)

I can then drag its corners and the polygon resizes accordingly

If I then change the polygons lat/lngs using setLatLngs
printBoundary.setLatLngs([new lat lngs ])

The polygon will resize accordingly but the dashed transform box does not update (see below). Could I setLatLngs on the resize box to account for this?

resize

L.Util.merge is not a function

When i try to set rotateHandleOptions it throws L.Util.merge is not a function.
in Util line 50, when i changed it to L.PathTransform.merge, it works fine, i assume it is a bug?

Rotate multiple polygons at once?

Is it possible to rotate and move multiple polygons at once?

For example if i'm doing the following

<svg>

  <!-- First group of polygons -->
  <g>
     <path ... />
     <path ... />
  </g> 
 
  <!-- Second group of polygons -->
  <g>
     <path ... />
     <path ... />
  </g>

</svg>

Could i move or rotate the polygons inside a group (<g>) at once, for example the first group?

BUG: transform.reset() doesn't update handlers when fired after an event

There seems to be a problem with transform,reset() when it is being called from an event handler. It does not update the path handlers.

See here: https://jsfiddle.net/hammad2506/Lhszk8ur/5/

When run, after 3 seconds, the line gets its coordinates updated and the handlers get updated which is working as expected.

Now scale the line, make it bigger or smaller, the same code as above gets fired, but only the line gets updated, the handlers do not!

Is there a workaround for this? Basically, I want to set the scaled line to its previous coordinates if it exceeds a certain length. So, I need to do this when the 'transformed' or 'scaleend' event gets fired.

Thank you.

Using along with Leaflet PM

I am currently using Leaflet PM. But I want to use this for scaling and rotating my rectangles. How can I make it work with it?

current angle

Is it possible to get the current angle of the object?
proposal api
polygon.transform.angle()

Uncaught Error: Invalid LatLng object: (NaN, NaN)

Using the latest version of Leaflet and Leaflet.Path.Transform, zoom is always equals to infinity and matrix is made of NaN values. transformGeometries() does not pass in scale or angle data.
EDIT: This is caused if user does not specify maxzoom / min zoom.
If these values aren't manually set, I suggest assigning maxzoom to 18 and minzoom to 0.

Sample demo for using Leaflet.Path.Transform with Mapbox JS

Hey w8r,

I'm using Mapbox JS with Leaflet. I've been trying to find a way to rotate a polygon by dragging a corner or something similar, and I found that your Leaflet.Path.Transform does exactly what I need, like this example (mainly the rotating and dragging part). Thanks a lot for your work on this!!

I looked at your example, and tried to use that for my particular situation. But I haven't been able to figure out how to access the script file required for that. The GitHub page tells me to include the dist/L.Path.Transform.js file. So I went to this page and copied the code and pasted it in the script.js file in Plunker, and linked to it in the code. This is what I have so far in Plunker.

For now, I'm just trying to add a new polygon and try to rotate/drag that. But it is giving me some Javascript errors. Can you please point out to me what I'm doing wrong here? Or if there is some other simple demo that I can refer to, that'll be great as well.

Please let me know if you have any questions about my issue.

Thanks,
Rajkishan

Rectangle Scaling

I want to scale rectangle to different height/width ratio, along with keeping it as a rectangle, like this image shows. Currently I do not know a way of doing this
rectangle scaling

Error: Invalid LatLng object: (NaN, NaN)

When I want to resize rectangles , they dont resize, and this message is appears:
Error: Invalid LatLng object: (NaN, NaN)
I use leaflet 0.7.3 and Leaflet.Path.Transform for Leaflet 0.7.3

Working examples for mobile?

Hi there. Thanks for this good project. I have trouble making it work on mobile devices. Are there some working examples if it's possible at all?

Circle radius is not scaled

Hi,
after scaling a circle the radius is not updated (the handlers are resized but the shape stay at the previous size)

I have a proposed fix in PR #46

Thanks

How can I get current coordinates of rectangle corners?

When I try using the events (e.g. rectangle.on('transform', (e) => { ... } )), how can I get live coordinates of my rectangle as I scale/rotate it?

When I move, I can access them via rectangle.getLatLngs(), but in event of rotate or scale the coordinates remain the same until I start the next zoom/rotate.

I need this in order to properly rotate an imageOverlay linked to the rectangle.

Support non-uniform scaling

It would be great if there was an option to allow non-uniform scaling. In this mode the scale handles would freely follow the mouse pointer when dragged and the feature would stretch accordingly.

Final rotation value

Is it possible to tell the amount by which a path has been rotated, even after several transformations?rotate and transform events return the rotation of that event, but I'm looking for a way to track the final rotation of a path.

null _handleLine and _rotationMarker not handled properly

In the example you have provided, if I disable 'rotate' and keep the rest on, I will get some errors in_.nScaleStart and _onScaleEnd

Util.js:59 Uncaught TypeError: Cannot read property '_leaflet_id' of null
    at stamp (Util.js:59)
    at NewClass.removeLayer (Layer.js:180)
    at NewClass._onScaleStart (L.Path.Transform.js:formatted:804)
    at NewClass.fire (Events.js:190)
    at NewClass._fireDOMEvent (Map.js:1433)
    at NewClass._handleDOMEvent (Map.js:1390)
    at HTMLDivElement.handler (DomEvent.js:79)
stamp @ Util.js:59
removeLayer @ Layer.js:180
_onScaleStart @ L.Path.Transform.js:formatted:804
fire @ Events.js:190
_fireDOMEvent @ Map.js:1433
_handleDOMEvent @ Map.js:1390
handler @ DomEvent.js:79
Layer.js:158 Uncaught TypeError: Cannot read property '_layerAdd' of null
    at NewClass.addLayer (Layer.js:158)
    at NewClass._onScaleEnd (L.Path.Transform.js:formatted:827)
    at NewClass.fire (Events.js:190)
    at NewClass._fireDOMEvent (Map.js:1433)
    at NewClass._handleDOMEvent (Map.js:1390)
    at HTMLDivElement.handler (DomEvent.js:79)

Seems that the this._handleLine and this._rotationMarker are null when rotation is disabled. Can anyone else confirm this ?

Rectangle not supported

Leaflet offers the Rectangle vector shape right out the box, but whenever I attempt to enable transformation and rotate it, the rectangle reverts back to an upright state.
Are there any plans to support rotation of rectangles?

Transforming layerGroup

Hi! I have a project were I have several small polygons within a large one. I want to group all these polygons together, so that all the small polygons are rotated when you rotate the big polygon. I tried doing it like this:

var group = L.layerGroup([bigPolygon, smallPolygons], { transform: true });
group.addTo(map)
group.transform.enable({rotation: true, scaling: false});

But then I get:

Cannot read property 'enable' of undefined

For reference, here is an example of the polygons Im talking about:
polygon

Do you have any suggestions on how I might solve this?

Dragging doesn't work properly once you remove a polygon from the map and re-add it

Hi,

I'm using Leaflet.Path.Transform with Mapbox JS, and it has been really useful to me for doing what I needed to do. Thanks for your work on this.

Now there are scenarios in which I would sometimes have to hide a polygon (and disable the transform and dragging for that polygon), say like when a checkbox is selected. Then, when it is unchecked, I add that polygon to the map again, and re-enable the transforming and the dragging.

However, once I remove the polygon and add it to the map again, dragging the polygon is causing the bounding box (the blue border) to get reformed somewhere to the side of the polygon, and not exactly around the border of the polygon. If I don't do any of this removing and re-adding polygon business, then everything works exactly as expected.

This is the Plunker I have in which you'll be able to reproduce this issue. When you load the Plunker for the first time, you can move the polygon around, and the bounding box goes perfectly around it. But if you check and uncheck the "Hide Polygon" checkbox, and then try to drag the polygon around, the bounding box starts getting reformed somewhere else.

Can you please tell me what I'm doing incorrectly here?

Thanks,
Rajkishan

Is it possible to rotate programmatically?

First, thank you for the library!
I would like to rotate a polygon without the transform handler.

I'm trying to use the html range input.
At the moment i'm using the following code to rotate

var myAngle = 45;
polygon.transform.rotate(myAngle, null);

But it doesn't rotate in 45°, it rotate randomly, maybe i'm doing something wrong.
Is it possible? If yes, which methods to i have to use.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.