This is related to the question i just asked here but not the same.
How could i create a manipulate that lets me move the graph to any position i want by clicking and dragging anywhere. Here is an example of what i want at desmos.com.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityThis is related to the question i just asked here but not the same.
How could i create a manipulate that lets me move the graph to any position i want by clicking and dragging anywhere. Here is an example of what i want at desmos.com.
A version using Manipulate
:
Manipulate[
Graphics[{}, Axes -> True, AxesOrigin -> {0, 0}, PlotRange -> pr,
GridLines -> Range @@@ Round@pr, GridLinesStyle -> LightGray],
{{p, {0, 0}}, Locator,
TrackingFunction -> {p = MousePosition[{"Graphics", Graphics}, {0, 0}]; &,
If[MousePosition["GraphicsScaled"] ∈ Rectangle[],
pr += p - MousePosition[{"Graphics", Graphics}, {0, 0}]]; &, None},
Appearance -> None},
{{pr, {{-5, 5}, {-5, 5}}}, None}]
This is a direct copy-paste of @m_godberg's code, with a few minor changes to get rid of the viscosity
parameter and allow the graphics to move precisely as much as the mouse was dragged.
With[{span = 10.},
Framed @
DynamicModule[{mouseXY, xmin, xmax, ymin, ymax},
{xmin, xmax} = {ymin, ymax} = span {-1., 1.}/2.;
EventHandler[
Dynamic @
Plot[span Sin[t]/2., {t, xmin, xmax},
AspectRatio -> Automatic,
PlotRange -> {{xmin, xmax}, {ymin, ymax}},
GridLines -> {Floor /@ Range[xmin, xmax], Floor /@ Range[ymin, ymax]}],
{"MouseDown" :> (mouseXY = MousePosition["Graphics"])},
{"MouseDragged" :>
Module[{dx, dy},
{dx, dy} = (MousePosition["Graphics"] - mouseXY);
xmin -= dx; xmax -= dx;
ymin -= dy; ymax -= dy]}]]]
viscosity
and having y
increase from top to bottom. I totally forgot about the "Graphics"
argument.
$\endgroup$
Dec 28, 2015 at 13:47
Again I choose a dynamic module to implement your request. This time I don't use a locator, but take direct control of mouse events.
With[{span = 10., viscosity = .01},
Framed @
DynamicModule[{mouseXY, xmin, xmax, ymin, ymax},
{xmin, xmax} = {ymin, ymax} = span {-1., 1.}/2.;
EventHandler[
Dynamic @
Plot[span Sin[t]/2., {t, xmin, xmax},
AspectRatio -> Automatic,
PlotRange -> {{xmin, xmax}, {ymin, ymax}},
GridLines -> {Floor /@ Range[xmin, xmax], Floor /@ Range[ymin, ymax]}],
{"MouseDown" :> (mouseXY = MousePosition[])},
{"MouseDragged" :>
Module[{dx, dy},
{dx, dy} = viscosity (MousePosition[] - mouseXY);
mouseXY = MousePosition[];
xmin -= dx; xmax -= dx;
ymin += dy; ymax += dy]}]]]
Anyone reading this answer should also read Szabolcs' answer too. He makes important simplifications to my code by fixing a coordinate system error I made and then clumsily worked-around.
Manipulate[
Graphics[
AxesOrigin -> a,
Frame -> True,
GridLines -> {b = (Range[-10, 10] /. 0 -> {0, Thick}), b},
PlotRange -> {{a[[1]] - 3, a[[1]] + 3},
{a[[2]] - 3, a[[2]] + 3}}],
{{a, {0, 0}}, {-5, -5}, {5, 5}, Locator, Appearance -> " "}]