When I was browsing some of the old code, I came across this

r := .5-Random[];
p = Array[{8^9 {r,r},r+.5}&, 99];

Dynamic @ Graphics[
  Disk @@@ (
    p = { #2#1 + {r, r} + (1-#2) MousePosition["Graphics",#1], #2
        }& @@@ p
, PlotRange->44

which should be 99 random disks moving towards the position of the mouse. But it lags a lot and I suppose the fps~5 or so. I thought that this couldn't be so slow always, so I grabbed a copy of Mathematica 7-the version when this code was written. Not surprisingly , it was much faster. Why? What should I do to let my version 11.3 run this as fast as the older versions?


Here is one way:

r := .5 - Random[];
p = Array[{8^9 {r, r}, r + .5} &, 99];
task = RunScheduledTask[
  (p = {#2 #1 + {r, 
         r} + (1 - #2) MousePosition["Graphics", #1], #2} & @@@ p),
  Disk @@@ p
  ], PlotRange -> 44]
|improve this answer|||||
  • $\begingroup$ It is faster. But why? I suppose running a task with a set interval doesn't change the overall speed $\endgroup$ – t-smart Apr 24 '18 at 1:29
  • $\begingroup$ @t-smart it simplifies updating flow. No matter what kernel updates p each .05 s. With an intermediate call for MousePosition. In your approach MousePoistion is tracked by Kernel but it needs to know it from FrontEnd and when updating happens then the tracking is confused because FE can't provide the value as Dynamic works on preemptive link etc etc, so it is a little bit messy, $\endgroup$ – Kuba Apr 24 '18 at 9:37

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.