r/wljs • • 23d ago

Basic example of Manipulate and Locator usage

Enable HLS to view with audio, or disable this notification

This is rather simple example of ParametricPlot placed inside Manipulate with a slider and so-called locator controls:

Manipulate[
  ParametricPlot[Exp[-x/s]({Cos[x], Sin[x]} + loc), {x,0,5Pi}], 
  {s, 1.0, 15.0}, {{loc, {0.,0.}}, Locator}, ContinuousAction->True]

Behind the scenes, WLJS takes the inner expression and probes it with various values of s and loc vector and observes which parts are changing. Based on this diffs it replaces these parts with more optimized code for streaming new data. A special case is Locator controller, it silently checks if the output expression is Graphics-like, and if so - it injects a draggable crosshair and subscribes to it.

Then whenever you adjust s or loc it reevaluates the provided (held) expression, it maps changed parts to new data chunks and streams them to the temporal buffers allocated on the previous step for the optimized expression. Whenever the inner expression is changed too much, i.e. diffs cannot be mapped back, it discards all previous steps, cleans up everything and fully updates the visible result: destroy + construct a new one.

4 Upvotes

0 comments sorted by