Want Interactive PlotRange of Graphics[{}]



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.

Ted Ersek

Posted 2012-06-18T23:22:39.537

Reputation: 5 036

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 – 2012-06-19T00:02:37.650


I think it is Szabolcs's MathGroup post

– kglr – 2012-06-19T00:14:15.873

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 – 2012-06-19T01:09:34.743



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.


Posted 2012-06-18T23:22:39.537

Reputation: 34 748