Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am attempting to set an EventHandler to change the radius of a circle by dragging on its circumference. But the response to the dragging is terribly slow. What is wrong with the code?

cnt[p_] := Return[Round /@ p];
grids[min_, max_] := 
  Join[Range[Ceiling[min], Floor[max]], 
   Table[{j + 1, Lighter@Lighter@Lighter@Lighter@Blue}, {j, 
     Round[min], Round[max - 1], 1}]];

DynamicModule[{pnt = {1, 1}, VPOS = {1, 1}, rad = 1},
     Circle[ cnt[pnt] , rad],
      "MouseDragged" :> (
        VPOS = MousePosition["Graphics"];
          rad = EuclideanDistance[pnt, VPOS];
     PassEventsDown -> True
    Locator[Dynamic[pnt], None] 
   Axes -> True,
   GridLines -> grids,
   PlotRange -> {{-10, 10}, {-10, 10}}] 

enter image description here

share|improve this question
Is EventHandler a need? Using Locator might be easier. – Öskå Aug 1 '14 at 10:42
Yes, I am trying to test how to correctly set an EventHandler. – Putterboy Aug 1 '14 at 10:50
It's probably slow because of the EuclideanDistance. – Öskå Aug 1 '14 at 10:56
Is it again the problem of out of focus? Seems it only response when the mouse is right on the top of the circumference. Is there a way of work-around? – Putterboy Aug 1 '14 at 11:06
Can you clarify what you are trying to do? I can move the circle when I click the mouse on its center, and when I mouseclick on the circumference, the size changes. Are you interested in being able to both move the circle and change its size with the mouse? – bobthechemist Aug 1 '14 at 11:38
up vote 3 down vote accepted

The front end's event handling mechanism seems to have a hard time deciding which event should be passed to the locator. Therefore, I think it's best to dispense with the locator. This seems to work fairly well:

cnt[p_] := Return[Round /@ p];

grids[min_, max_] :=
    Range[Ceiling[min], Floor[max]], 
    Table[{j + 1, Lighter @ Lighter @ Lighter @ Lighter @ Blue}, 
      {j, Round[min], Round[max - 1], 1}]];

DynamicModule[{pnt = {1, 1}, VPOS = {1, 1}, rad = 1, d},
   Dynamic @ 
     Graphics[{{PointSize[Medium], Point[cnt[pnt]]}, {Thick, Circle[cnt[pnt], rad]}},
       Axes -> True,
       GridLines -> grids,
      PlotRange -> {{-10, 10}, {-10, 10}}],
  {"MouseDragged" :> (
     VPOS = MousePosition["Graphics"];
     d = EuclideanDistance[pnt, VPOS]; 
     If[d < 1, pnt = VPOS, rad = d])}]]


I made the center point visible as an aid to dragging the circle from grid point to grid point. I also need to point out that, when adjusting the radius, it is best to drag from just outside the circle.

share|improve this answer
It's much better than the OP's answer indeed, but moving the centre too fast catches the circle sometimes.. – Öskå Aug 1 '14 at 14:22
@Öskå. Yeah, that's why I wrote "This seems to work fairly well" rather than "This seems to work well". It does meet the requirement of moving the circumference smoothly. – m_goldberg Aug 1 '14 at 14:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.