Private
alpha_The transparency used when painting the background color; a number between 0.0 (fully transparent) and 1.0 (fully opaque). When alpha_ < 1 then a "trails" effect happens.
Private
background_The background color; either a CSS3 color value or the empty string. Transparent black is used if it is the empty string.
Private
counter_Counter for how many frames need to be drawn to erase trails.
Private
focusThe view which is the main focus and is drawn normally.
Static
Readonly
FOCUS_Name of GenericEvent that is broadcast when the focus view changes.
Static
Readonly
SIZE_Name of GenericEvent that is broadcast when the size of the HTML canvas changes.
Static
Readonly
VIEW_Name of GenericEvent that is broadcast when a SimView is added.
Static
Readonly
VIEW_Name of GenericEvent that is broadcast when the list of SimViews is modified.
Static
Readonly
VIEW_Name of GenericEvent that is broadcast when a SimView is removed.
Adds an object to the list of Memorizable objects. These object's memorize
methods will be called from this object's memorize
method.
object to add to the list of Memorizable objects
if called during the memorize
method.
Adds the given Observer to this Subject's list of Observers, so that the Observer
will be notified of changes in this Subject. An Observer may call Subject.addObserver
during its observe
method.
the Observer to add
Adds the Parameter to the list of this Subject's available Parameters.
the Parameter to add
if a Parameter with the same name already exists.
Adds the SimView to the end of the list of SimViews displayed and memorized by this LabCanvas. Makes the SimView the focus view if there isn't currently a focus view. Notifies any Observers by broadcasting GenericEvents named VIEW_ADDED and VIEW_LIST_MODIFIED and possibly also FOCUS_VIEW_CHANGED.
the SimView to add
Notifies all Observers that this Subject has changed by calling observe on each Observer.
An Observer may call addObserver or removeObserver during its observe
method.
a SubjectEvent with information relating to the change
Notifies all Observers that the Parameter with the given name has changed by calling observe on each Observer.
the language-independent or English name of the Parameter that has changed
if there is no Parameter with the given name
Returns the background color; either a CSS3 color value or the empty string. Empty string means that background is cleared to transparent black (which actually appears as a white background unless there is something already drawn underneath).
the background color; either a CSS3 color value or the empty string
Protected
getReturns whether broadcasting is enabled for this Subject. See setBroadcast.
whether broadcasting is enabled for this Subject
Returns the list of Memorizable objects stored in this MemoList.
the list of Memorizable objects
Returns the ParameterBoolean with the given name.
the language-independent or English name of the ParameterBoolean
the ParameterBoolean with the given name
if there is no ParameterBoolean with the given name
Returns the ParameterNumber with the given name.
the language-independent or English name of the ParameterNumber
the ParameterNumber with the given name
if there is no ParameterNumber with the given name
Returns the ParameterString with the given name.
the language-independent or English name of the ParameterString
the ParameterString with the given name
if there is no ParameterString with the given name
Returns the ScreenRect corresponding to the area of the HTML canvas. The top-left coordinate is always (0,0). This does not represent the location of the canvas within the document or window.
the ScreenRect corresponding to the area of the HTML canvas.
Clears the canvas to the background color; then paints each SimView. See setBackground and setAlpha.
Removes an object from the list of Memorizable objects.
object to remove from the list of Memorizable objects
if called during the memorize
method.
Removes the Observer from this Subject's list of Observers. An Observer may
call removeObserver
during its observe
method.
the Observer to detach from list of Observers
Removes the Parameter from the list of this Subject's available Parameters.
the Parameter to remove
Removes the SimView from the list of SimViews displayed and memorized by this LabCanvas. Sets the focus view to be the first view in remaining list of SimViews. Notifies any Observers by broadcasting GenericEvents named VIEW_LIST_MODIFIED and VIEW_REMOVED and possibly also FOCUS_VIEW_CHANGED.
the SimView to remove
Sets the transparency used when painting the background color; a number between 0.0 (fully transparent) and 1.0 (fully opaque). Only has an effect if the background color is non-empty string.
A value less than 1 gives a "trails" effect where the older positions of objects gradually fade out over a second or two. The trails are longer for smaller alpha.
transparency used when painting, between 0 and 1
Sets the background color; either a CSS3 color value or the empty string. Empty string means that background is cleared to transparent black (which actually appears as a white background unless there is something already drawn underneath).
the background color; either a CSS3 color value or the empty string
Protected
setSets whether this Subject will broadcast events, typically used to temporarily disable broadcasting. Intended to be used in situations where a subclass overrides a method that broadcasts an event. This allows the subclass to prevent the superclass broadcasting that event, so that the subclass can broadcast the event when the method is completed.
whether this Subject should broadcast events
the previous value
Sets the focus SimView which is the main focus of the LabCanvas. Notifies any observers that the focus has changed by broadcasting the GenericEvent named FOCUS_VIEW_CHANGED.
the view that should be the focus; can be
null
when no SimView has the focus.
if view
is not contained by this LabCanvas
Sets the height of the HTML canvas, and sets the screen rectangle of all the SimViews. Notifies any Observers by broadcasting a GenericEvent named SIZE_CHANGED.
the height of the canvas, in screen coords (pixels),
Sets the size of this LabCanvas to the given ScreenRect by calling setSize.
specifies the width and height; the top-left must be (0,0).
if the top-left of the given ScreenRect is not (0,0).
Sets the size of the HTML canvas. All SimViews are set to have the same screen rectangle as this LabCanvas by calling SimView.setScreenRect. Notifies any Observers by broadcasting a GenericEvent named SIZE_CHANGED.
the width of the canvas, in screen coords (pixels)
the height of the canvas, in screen coords (pixels)
Sets the width of the HTML canvas, and sets the screen rectangle of all the SimViews. Notifies any Observers by broadcasting a GenericEvent named SIZE_CHANGED.
the width of the canvas, in screen coords (pixels),
Returns a minimal string representation of this object, usually giving just identity information like the class name and name of the object.
For an object whose main purpose is to represent another Printable object, it is
recommended to include the result of calling toStringShort
on that other object.
For example, calling toStringShort()
on a DisplayShape might return something like
this:
DisplayShape{polygon:Polygon{'chain3'}}
a minimal string representation of this object.
Generated using TypeDoc
Manages an HTML canvas and contains a list of SimView's which are drawn into the canvas. The SimViews are drawn overlapping so that the last SimView appears on top of the others.
Canvas Size
The HTML canvas has a specified width and height, which determines the screen rectangle of the canvas. The canvas screen rectangle has
(0, 0)
for the top-left corner and(width, height)
for the bottom-right corner. The vertical coordinates increase downwards.The size can be changed via setWidth, setHeight, setSize or setScreenRect. When the size of the HTML canvas changes, the SimViews are set to have the same screen rectangle as the canvas.
Each SimView has a simulation rectangle and a screen rectangle, and these are aligned by a CoordMap. The simulation rectangle specifies the simulation coordinates, and the CoordMap translates simulation coordinates to screen coordinates. Pan and zoom can be accomplished by changing the simulation rectangle of a SimView (which changes its CoordMap accordingly).
Focus View
The focus view is the SimView that the user expects to modify by his/her actions. For example, SimController will pan the focus SimView when a particular set of modifier keys are pressed during a mouse drag.
The first SimView that is added becomes the initial focus view, but the focus view can be changed via setFocusView.
Background Color
Whenever paint is called to draw a new frame, the first step is to clear the old frame from the HTML canvas. What happens depends on the background color.
If no background color is specified then we use JavaScript
canvas.clearRect()
which clears to transparent black pixels.If a background color is specified, then we use JavaScript
canvas.fillRect()
which fills the HTML canvas with that color.The background color can be set with setBackground.
Trails Effect
A visual effect where moving objects leave behind a smeared out trail can be done by setting the background color and the alpha transparency, see setAlpha. Here are example settings, which can be done in a Terminal session:
When
alpha
is 1.0 then there is no trails effect because the old frame is entirely painted over with an opaque color.The trails effect happens when
alpha
is less than 1 because we paint a translucent rectangle over the old frame, which gradually makes the old image disappear after several iterations of painting.Draw Only When There Are Changes
LabCanvas avoids unnecessary drawing (for example when the simulation is paused). It does this by asking each of its SimViews whether they have changed, via SimView.getChanged. The SimView similarly asks each of its DisplayObjects via DisplayObject.getChanged. If there are no changes, then LabCanvas avoids drawing.
Note that the
getChanged
methods on the various display objects have a side-effect: they reset their state to "unchanged". Be sure that all thegetChanged
methods are called on all objects in the display hierarchy, otherwise there may be a leftover "changed" flag that was not cleared.Parameters Created
ParameterNumber named
WIDTH
, see setWidthParameterNumber named
HEIGHT
, see setHeightParameterNumber named
ALPHA
, see setAlphaParameterString named
BACKGROUND
, see setBackgroundEvents Broadcast
All the Parameters are broadcast when their values change. In addition:
GenericEvent named
VIEW_ADDED
; the value is the SimView being addedGenericEvent named
VIEW_REMOVED
; the value is the SimView being removedGenericEvent named
FOCUS_VIEW_CHANGED
; the value is the SimView which is the focus, ornull
if there is no focus viewGenericEvent named
VIEW_LIST_MODIFIED
GenericEvent named
SIZE_CHANGED