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.

  • $\begingroup$ 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. $\endgroup$ – Mike Honeychurch Jun 19 '12 at 0:02
  • 3
    $\begingroup$ I think it is Szabolcs's MathGroup post $\endgroup$ – kglr Jun 19 '12 at 0:14
  • 1
    $\begingroup$ @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. $\endgroup$ – Mr.Wizard Jun 19 '12 at 1:09

You could do something like this

DynamicModule[{range, ref, range0, fac = 2/3},
 range = range0 = 2 {{-1, 1}, {-1, 1}};
  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[
     range = (range - MousePosition["Graphics"])*fac + MousePosition["Graphics"],
     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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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