Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to do:

EventHandler[Graphics[{ (*stuff*) }],{{"MouseDragged","p"}:>(*pan the graphics*),  
{"KeyDown","z"}:>(*zoom in/out as the mouse moves up/down in the graphics*) }]

This would be similar to what we can do with Graphics3D, except when I "pan" the Graphics I want to change the PlotRange rather than move the Graphics around the window. BTW I will be using "MouseDown" for custom modification of the Graphics, and that's one of the reasons why I want pan/zoom associated with pressing keys.

share|improve this question
    
I think either Istvan (?) or Szabolcs (?) have posted what you want here or on Mathgroup. Whoever the author was I know I have seen this code before. –  Mike Honeychurch Jun 19 '12 at 0:02
3  
I think it is Szabolcs's MathGroup post –  kguler Jun 19 '12 at 0:14
1  
@Mike that's not quite the same thing because it zooms to a selected box. I believe Ted's request is for something like the zoom seen in graphics programs, PDF viewers, etc., where you have a zoom control, and separately a smooth pan when zoomed. –  Mr.Wizard Jun 19 '12 at 1:09
add comment

1 Answer

up vote 7 down vote accepted

You could do something like this

DynamicModule[{range, ref, range0, fac = 2/3},
 range = range0 = 2 {{-1, 1}, {-1, 1}};
 EventHandler[
  Dynamic@Graphics[{Disk[]}, Axes -> True, PlotRange -> range],
  {"MouseDown" :> (ref = MousePosition["GraphicsImageScaled"]; range0 = range),
   "MouseDragged" :> (range = range0 + 
       (ref - MousePosition["GraphicsImageScaled"]) (range0[[All, 2]] - range0[[All, 1]])),
   "MouseClicked" :> Which[
     CurrentValue["AltKey"], 
     range = (range - MousePosition["Graphics"])*fac + MousePosition["Graphics"],
     CurrentValue["ShiftKey"], 
     range = (range - MousePosition["Graphics"])/fac +  MousePosition["Graphics"]]}]]

You can pan by dragging, zoom in by clicking somewhere in the plot while keeping the Alt-key depressed (or Cmd if you're on OS X), and zoom out by clicking while keeping the Shift-key depressed. The amount of zoom with each click is determined by fac.

share|improve this answer
add comment

Your Answer

 
discard

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.