3

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:

EventHandler[
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 = 
 CurrentValue[MousePosition]; 
size = CurrentValue[
  ImageSize])}, {"MouseDragged" :> (mousecoord2 = 
 CurrentValue[MousePosition]; 
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.

Matt
  • 317
  • 1
  • 7

1 Answers1

1

@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}}, 
EventHandler[
Dynamic[Show[graphics, 
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[]}]
Matt
  • 317
  • 1
  • 7
  • 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. – Nothingtoseehere 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 02:57