Reputation: 1751
I have created a polygon using raphael path. The vertices of the polygon can be dragged to stretched to change the shape/size of the polygon.
The test is running here
Now what I want to implement is that if I dblclick on the edge, it should create a new vertex. So that it can act as a dragging point.
Can anyone help me out on identifying the position of a point in the path:
var p = r.path("M100,300L100,100L250,300z");
and if a mouse event happens on 200,250, how to identify where in the path array, should the new point command fit?
var p = r.path("M100,300L200,250L100,100L250,300z");
OR
var p = r.path("M100,300L100,100L200,250L250,300z");
Upvotes: 0
Views: 926
Reputation: 1748
My implementation is slightly different but you could always tweak it if that's too much (I actually insert new points in the polygon upon mouse click wherever it could be on the paper).
Raphael.el.MakeDraggable
: Add handler to dragg any element (namely points or polygons)Raphael.el.InsertPoint
: Insert the given point (argument) in the polygonPaper.circle()
var oPaper = Raphael('#paper', '100%', '100%');
var oPoint = oPaper.circle(nX, nY, nRadius);
oPoint.MakeDraggable();
Paper.path()
oPolygon.data()
var oPaper = Raphael('#paper', '100%', '100%');
var oPolygon = oPaper.path(sPath);
oPolygon.InsertPoint(oPoint);
These are the 2 steps I followed in order to insert the newly created point in the path of the polygon:
Those 2 steps are easy to understand, but hard to implement (especially the first one). Here is the first step detailed. Say you are looping over each side of the polygon (a side equals 2 points), we need to feed an array with all the distances so we can get the lowest.
C +-------+ A + M
\ | The shortest / The shortest
\ | distance is [MG] / distance is [MA]
\ G +--------+ M C +----+ A
\ | \ |
\ | \ |
\ | \ |
\| \|
+ B + B
Your array containing the distances should now contain all the distances between oM and the sides of the polygon. We need to find the lower ones (there can be multiple with the same value). So loop over it and build another array which will contain the indexes of the lowest distances.
Once you have this new array, check its length:
// Don't forget to bind points to their polygon
oPolygon.data('points', oPoints); // oPoints is a Raphael set containing the points
// There are different approaches, mine was to bind the other way as well
oPoint.data('polygon', oPolygon);
Upvotes: 2