2

I am trying to emulate a feature on GoodNotes that allows me to draw a glowing curve on a graphic and the moment I let go of the mouse, the curve gradually disappears. See gif below. The gif is a little jumpy because of the limited frames. The curve disappears after maybe 3 seconds.

enter image description here

Thanks to @cvgmt the code below gets us part way there. Now I want the curve to gradually disappear. This works pretty well on the first run... but can't reset the clock for some reason.

DynamicModule[{list = {}, d = 1}, EventHandler[Dynamic[
   Graphics[{{Opacity[.2 d], EdgeForm[Blue], FaceForm[], 
      Rectangle[{-5, -5}, {5, 5}]},
     Opacity[.1 d], Red, AbsoluteThickness[16], BezierCurve@list,
     Opacity[.2 d], Red, AbsoluteThickness[14], BezierCurve@list,
     Opacity[.3 d], Red, AbsoluteThickness[12], BezierCurve@list,
     Opacity[.4 d], Red, AbsoluteThickness[10], BezierCurve@list,
     Opacity[.5 d], Red, AbsoluteThickness[9], BezierCurve@list,
     Opacity[.9 d], Red, AbsoluteThickness[7], BezierCurve@list,
     Opacity[.95 d], Red, AbsoluteThickness[5], BezierCurve@list,
     Opacity[1 d], Red, AbsoluteThickness[4], BezierCurve@list,
     White, AbsoluteThickness[3], BezierCurve@list
     }]],
  {"MouseDragged" :> (d = 1; 
     AppendTo[list, MousePosition["Graphics"]]),
   "MouseUp" :> (d = Dynamic[1 - Clock[{0, 1, .01}, 3, 1]])
   }]]
user64494
  • 26,149
  • 4
  • 27
  • 56
B flat
  • 5,523
  • 2
  • 14
  • 36

2 Answers2

4

The problem is the clock only runs once and doesn't reinitialize. You'll also need to reset list to empty every time you click. Add:

DynamicModule[{list = {}, d = 1, t0 =AbsoluteTime[],ti= AbsoluteTime[], 
  ti = AbsoluteTime[],decaytime=1}, 
 EventHandler[
...
  ,
  {
   "MouseDown" :> (list = {};),
   "MouseDragged" :> (d = 1; AppendTo[list, MousePosition["Graphics"]]),
   "MouseUp" :> (ti = AbsoluteTime[];d = 
      Dynamic[Clip[(ti + decaytime - Clock[{t0, \[Infinity]}])/decaytime,{0,1}]])
   }
  ]
 ]

This will reinitialize list when you click. It also initializes your clock at the time the code is run and lets it run to infinity, then updates ti so that the decay resets when you lift your mouse.

N.J.Evans
  • 5,093
  • 19
  • 25
1

If you can live with the curve disappearing at once when "MouseUp", the following will do the job:

DynamicModule[{list = {}},
 draw := 
  Dynamic[Graphics[{{Print[rr]; Opacity[.2], EdgeForm[Blue], 
      FaceForm[], Rectangle[{-5, -5}, {5, 5}]}, Red, 
     AbsoluteThickness[10], BezierCurve@list, White, 
     AbsoluteThickness[4], BezierCurve@list}]];
 EventHandler[
  draw, {"MouseDragged" :> (AppendTo[list, 
      MousePosition["Graphics"]]), "MouseUp" :> (list = {};)}]]
Daniel Huber
  • 51,463
  • 1
  • 23
  • 57
  • already got that one Daniel... trying to make it disappear gradually...almost got in in code above. works once. – B flat Dec 15 '22 at 11:14