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 recently started to work with the EventHandler function to make it possible to Zoom, Pan, etc, with only the use of a mouse. I have found a way to zoom that is somewhat working without a keyboard, but was wondering if anyone had any tips to improve it. It works very slowly, often asking me if I'd like to disable dynamic updating. Obviously, this is unacceptable for any practical purpose. The code so far is as follows:

Dynamic[Graphics3D[Cylinder[], ViewAngle -> Dynamic[viewval]], 
Initialization :> (mousecoord1 = {0, 0}; mousecoord2 = {0, 0}; 
size = {0, 0}; fracmag = 0; difference = 0; anglediff = 0; 
viewval = 7*Pi/36)], {"MouseDown" :> (mousecoord1 = 
size = CurrentValue[
  ImageSize])}, {"MouseDragged" :> (mousecoord2 = 
difference = mousecoord2 - mousecoord1; 
fracmag = Norm[difference]/Norm[size]; 
anglediff = fracmag*(Pi - viewval); 
Which[difference[[2]] < 0, 
 Min[viewval = viewval + anglediff, Pi - 0.00001], 
 difference[[2]] > 0, Max[viewval = viewval - anglediff, 0.00001],
  difference[[2]] = 0, 
 viewval = viewval])}, {"MouseUp" :> (anglediff = 0; 
difference = {0, 0}; mousecoord1 = {0, 0}; mousecoord2 = {0, 0})}]

The code above uses a simple 3d Graphic, a cylinder, and the ViewAngle option to track a mouse click and drag which then uses the ratio between the drag and the image size to determine how much to zoom in or out.

share|improve this question
Maybe you will find it useful for your purposes: how to manipulate 2D plots?. It can not be applied directly but I think using Overlay is all you need then. – Kuba Jul 18 '13 at 19:55
You want to use only mouse? What with rotation? – Kuba Jul 18 '13 at 20:11

@Kuba The mouse defaults to rotation, but I'm trying to use the mouse work for zoom by default, with a checkbox that would make it go back to rotation. I have my zoom function working nicely now, but when I try to put it into a Manipulate function, the checkbox makes it lose all functionality. Any ideas on implementing

DynamicModule[{frac = 0, viewval = 7*Pi/36, drag = {0, 0}}, 
ViewAngle -> (Which[drag[[2]] < 0, 
   viewval = Min[Pi - 0.0000001, viewval + frac*(Pi - viewval)], 
   drag[[2]] > 0, 
   viewval = Max[viewval - frac*(Pi - viewval), 0.001], 
   drag[[2]] == 0, viewval = viewval]), 
RotationAction -> "Clip"]], {{"MouseDown", 
 2} :> (a = CurrentValue[MousePosition];
 pt = CurrentValue[ImageSize]), {"MouseDragged", 
 2} :> {b = CurrentValue[MousePosition], drag = b - a, 
 frac = Norm[drag]/Norm[pt]}, {"MouseUp", 2} :> {frac = 0, 
 drag = {0, 0}}}]]

work within a Manipulate?

graphics above was simply a cylinder:

graphics = Graphics3D[{Red, Cylinder[]}]
share|improve this answer
I fixed it, it was a cylinder. @Kuba – Matt Jul 18 '13 at 20:33
@Matt +1 for a good start. Now the zoom-in and out max at pinpoint or too big. Adding limits for zoom would make this code more user friendly. – R Hall Jul 18 '13 at 21:51
@RHall Thank you for the advice, I'll further restrict the limits. However, do you have any idea how I can get that working within Manipulate? I am fairly happy with what I have working, but it would be infinitely better if I could Manipulate it, especially to turn the ability to zoom with the mouse on and off. – Matt Jul 19 '13 at 2:57

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.