Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Dynamic Interactivity > Dynamic Interactivity Language >

Dynamic

Dynamic[expr]
represents an object that displays as the dynamically updated current value of expr. If the displayed form of Dynamic[expr] is interactively changed or edited, an assignment expr=val is done to give expr the new value val that corresponds to the displayed form.
Dynamic[expr, None]
does not allow interactive changing or editing.
Dynamic[expr, f]
continually evaluates f[val, expr] during interactive changing or editing of val.
Dynamic[expr, {f, fend}]
also evaluates fend[val, expr] when interactive changing or editing is complete.
Dynamic[expr, {fstart, f, fend}]
also evaluates fstart[val, expr] when interactive changing or editing begins.
  • Dynamic[expr] can appear not only directly as an expression to display, but also for example as a coordinate in a graphics primitive or an object such as a slider, or as a setting for an option.
  • When used to assign values to expr through interactive operations, the expression in Dynamic[expr] will most often be a symbol x, an object x[i], a part e[[i]] or a list {x, y, ...}.
  • In graphics for example, Point[Dynamic[{x, y}]] represents a point whose position is dynamically updated to be given by the current values of x and y. Whenever the point is interactively moved, assignments of the form {x, y}={valx, valy} are done. Point[Dynamic[{x, y}]] can be moved interactively; Dynamic[Point[{x, y}]] cannot.
  • Dynamic[expr] evaluates expr whenever it needs its value and it determines that any type of value of symbols on which it depends might have changed.
  • If Refresh or Clock objects appear in the evaluation of the expression in Dynamic[expr], then expr will always continually be reevaluated.
  • Dynamic has attribute HoldFirst, so that expr is not evaluated until its value is needed for display.
  • Dynamic[expr, f] makes interactive operations not change expr except by virtue of the evaluation of f[val, expr].
  • Objects represented by Dynamic[expr, None] cannot be changed interactively, but will change if the value of expr changes for other reasons.
  • Dynamic[expr, Temporary] allows the displayed form of an object to change in the course of interactive operations, but assigns an updated value to expr only when the operations are complete.
  • For interactive mouse operations Dynamic[expr, {fstart, f, fend}] typically evaluates fstart[val, expr] once when the mouse is pressed, then evaluates f[val, expr] whenever the mouse is moved, and then evaluates fend[val, expr] once when the mouse is released.
  • If the mouse is not moved, fstart, f and fend are each evaluated exactly once.
  • f[val, expr] is always evaluated once immediately after the evaluation of fstart[val, expr], and when it is last evaluated, the value of val is always the same as in fend[val, expr].
  • Interactive operations that can affect Dynamic objects include dragging of elements in graphics or in objects such as Slider, as well as editing of fields in objects such as InputField.
  • The functions fstart, f and fend can be the following:
Automaticassign values using Set
Noneperform no action
Temporaryallow interactive operations to make temporary changes
funcevaluate func[val, expr]
  • Dynamic[expr, f] can be used to enforce constraints on values assigned to expr.
  • Dynamic[expr, f] can be used to do assignments for individual variables when expr is an expression like x+y.
  • A function func[val, expr] should have attribute HoldRest if it is going to perform an assignment to expr. This can be achieved with Function[{v, e}, body, HoldRest].
  • The following options can be given:
DeinitializationNonean expression to evaluate when the Dynamic can no longer be displayed
EditableFalsewhether to allow the textual display of Dynamic to be edited
EvaluatorAutomaticthe kernel to use for evaluations
InitializationNonean expression to evaluate when the Dynamic is first displayed
ShrinkingDelay0.how long to delay before shrinking if the displayed object gets smaller
TrackedSymbolsAllsymbols whose changes trigger an update
UpdateIntervalInfinitytime interval at which to do updates
  • Dynamic[expr] provides an analog of delayed assignment, where values are implicitly requested by the need to display, rather than by evaluation of an expression.
Dynamic simply returns the current value of its argument:
Assign a value to x to update the output above:
Tie the symbol x to a Slider:
Dynamic simply returns the current value of its argument:
In[1]:=
Click for copyable input
Out[1]=
Assign a value to x to update the output above:
In[2]:=
Click for copyable input
Out[2]=
Tie the symbol x to a Slider:
In[3]:=
Click for copyable input
Out[3]=
Any expression can be wrapped with Dynamic:
Dynamic is not shown in StandardForm, but is still present:
Use Dynamic to link the currently displayed value depending on n:
By default, variables inside Dynamic are not localized:
Use DynamicModule to localize variables:
And to initialize variables:
A Dynamic expression can be self-triggering:
Use with Slider:
Expressions such as RandomReal and DateList do not automatically update:
Use UpdateInterval to force dynamic updating:
Plot needs specific numerical values to create the output:
Separate Dynamic expressions update independently, allowing for efficiency:
Use graphics coordinates as control input by placing Dynamic around them:
Use the graphics coordinates as control input and see the effects on 2D sliders:
Use Dynamic to connect variables with interactive controls, such as Slider:
ColorSlider:
Use Dynamic to control view elements, such as TabView:
By default, Dynamic performs an assignment operation when used in an interactive element:
Use the second argument to specify a function to be evaluated during interaction:
Supply a list of two functions to be evaluated during and after interaction:
Supply a list of three functions to be evaluated before, during and after interaction:
Use Temporary to update the dynamic variable at the end of interaction:
Use None to disallow updating the dynamic variable:
Specify expressions to be evaluated when output is no longer displayed:
By default, external definitions are lost between kernel sessions:
Use Initialization to evaluate expressions necessary for displaying the output:
Assign a number or graphics to h:
Use ShrinkingDelay to allow lapse time before shrinking the size of the output:
Updates y only when x changes:
Constrain the coordinates of a point to lie on a circle:
Construct a dynamic calculating interface:
Create a timer to track lapsed minutes and seconds, with Trigger for the controls:
Create a simple interface that looks up the shapes of countries:
Construct custom controls, e.g., an angular slider with range (-pi, pi):
An angular slider with range (-infty, infty):
The following example does not work because the assignment operation fails:
Use the second argument of Dynamic to control the assignment operation:
Self-triggering Dynamic can easily cause infinite loops (delete the output to stop it):
By default, view elements generate the entire contents before displaying:
Use Dynamic and ImageSize->Automatic to generate the contents only when displayed:
Coupled Slider:
Coupled Slider2D:
A "droopy" Slider:
Center a disk at the mouse position as it moves over the graphics area:
Remember the last mouse position as it leaves the graphics area:
Click inside the framed area to see bouncing balls:
New in 6
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team