# Manipulate Locator warping (snap to cursor)

I'm looking to have a Manipulator Locator that moves in discrete steps and requies the user to click on/near the locator position.. consider these two examples:

Manipulate[
Graphics[Line[{{0, 0}, q}],
PlotRange -> {{-1, 1}, {-1, 1}}], {{x,
0}}, {{q, {0, 0}}, {-1, -1}, {1, 1}, .2, Locator}]


The locator moves in discrete steps, but warps wherever you click.

Alternately this does nearly the same:

p = {0, 0};
Manipulate[
Graphics[{Line[{{0, 0}, p}], Locator[Dynamic[p]]},
PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 0}}]


Here you need to click on the locator to drag, but I have no way to specify the grid snap..

Any way to achieve both effects?

( this hack comes to mind as I'm posting ..

p = {0, 0};
Manipulate[q = Round[5 p]/5;
Graphics[{Line[{{0, 0}, q}], Circle[q, .1], Locator[Dynamic[p]]},
PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 0}}]


not very satisfactory though.. )

Edit: Two working solutions based on Kuba's answer, plus a bit of tinkering:

p = {0, 0}; Manipulate[
Graphics[{Line[{{0, 0}, p}],
Locator[Dynamic[
p, (p = Round[ MousePosition["Graphics", {0, 0}], .2] ) &]]},
PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 0}}]


..

  Manipulate[
DynamicModule[{p = {0, 0}},
Deploy@Graphics[{Line[{{0, 0}, p}],
Locator[Dynamic[p, (p = Round[ #, .2] ) &]]},
PlotRange -> 1]], {{x, 0}}]


I remain a bit puzzled why this doesnt work:

        p = {0, 0}; Manipulate[
Graphics[{Line[{{0, 0}, p}],
Locator[Dynamic[p, (p = Round[#, .2] ) &]]},
PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 0}}]


(The incremental rounding causes the locator to drift away from the mouse pointer). I should rather say I see why this doesnt work, I dont see why DynamicModule fixes it.

• Seems like a job for LocatorPane (possibly with DynamicModule). Commented Oct 17, 2013 at 19:44
• – Kuba
Commented Oct 17, 2013 at 19:49
• Thanks for the link..but LocatorPane seem to have the same issue of warping unless I'm missing something. Commented Oct 17, 2013 at 20:00
• "warp" = "bend or twist" is not a good description of the behavior of the first example. Commented Oct 17, 2013 at 23:52

Is this what you are after?

DynamicModule[{p = {0, 0}},
Deploy@Graphics[{Line[{{0, 0}, p}],
Locator[Dynamic[p, (p = Round /@ #) &]]}
, PlotRange -> 5, Frame -> True]
]


I'm not sure what do you mean by warp, dragging is warping too?

• Warp == In my first example you can click anyplace and the locator jumps to wherever you click. This answer is just about what I need -- for some reason if I do the rounding in Locator[Dynamic[]] inside Manipulate[] the locator doesnt track properly with the mouse cursor. Nesting DynamicModule[] inside Manipulate[] seems to work though. Commented Oct 17, 2013 at 21:01
• I'm glad it works like you need. You can skip Manipulate and work with DynamicModule/Dynamic, I find Manipulate useful only for simple GUIs, in other cases it could be frustrating to achieve the result we are after.
– Kuba
Commented Oct 17, 2013 at 21:07
• In the end I think "dont use manipulate" is the real best answer..related link mathematica.stackexchange.com/questions/2464/…. (halirutan's answer) Commented Oct 18, 2013 at 15:07