The following works:
mymouse := Block[{}, pos = MousePosition["Graphics"];
If[pos =!= None, AppendTo[posList, pos]]]
posList = {};
plot = Plot[Sin[x], {x, 0, 4 Pi}]; (* your plot *)
Dynamic[Show[plot, ListLinePlot[posList, PlotStyle -> Red]]]
Dynamic[mymouse]
(This amazing artwork is copyrighted ©anderstood)
Edit Maybe this is a bit smoother?
mymouse := Block[{}, pos = MousePosition["Graphics"];
If[pos =!= None, AppendTo[posList, pos];
posList = DeleteDuplicates@posList;]]
(* If[pos =!= None && Not@MemberQ[postList,pos], AppendTo[posList, pos]; *)
posList = {};
plot = Plot[Sin[x], {x, 0, 4 Pi}];(*your plot*)Dynamic[
Show[plot, ListLinePlot[posList, PlotStyle -> Red]]]
Dynamic[mymouse]
And this to avoid the slow AppendTo
:
i = 1;
mymouse := Block[{}, pos = MousePosition["Graphics"];
If[pos =!= None, posList[[i]] = pos; i++;]]
posList = ConstantArray[{0, 0}, 1000];
plot = Plot[Sin[x], {x, 0, 4 Pi}];(*your plot*)Dynamic[
Show[plot, ListLinePlot[posList, PlotStyle -> Red]]]
Dynamic[mymouse]