3
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – Kuba
    Commented Jul 18, 2013 at 19:55
  • $\begingroup$ You want to use only mouse? What with rotation? $\endgroup$
    – Kuba
    Commented Jul 18, 2013 at 20:11

1 Answer 1

1
$\begingroup$

@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[]}]
$\endgroup$
3
  • $\begingroup$ I fixed it, it was a cylinder. @Kuba $\endgroup$
    – Matt
    Commented Jul 18, 2013 at 20:33
  • $\begingroup$ @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. $\endgroup$ Commented Jul 18, 2013 at 21:51
  • $\begingroup$ @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. $\endgroup$
    – Matt
    Commented Jul 19, 2013 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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