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:

simCanvas.setBackground('black');
simCanvas.setAlpha(0.05);

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 the getChanged methods are called on all objects in the display hierarchy, otherwise there may be a leftover "changed" flag that was not cleared.

Parameters Created

Events Broadcast

All the Parameters are broadcast when their values change. In addition:

  • GenericEvent named VIEW_ADDED; the value is the SimView being added

  • GenericEvent named VIEW_REMOVED; the value is the SimView being removed

  • GenericEvent named FOCUS_VIEW_CHANGED; the value is the SimView which is the focus, or null if there is no focus view

  • GenericEvent named VIEW_LIST_MODIFIED

  • GenericEvent named SIZE_CHANGED

Hierarchy (view full)

Implements

Constructors

Properties

alpha_: number = 1.0

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.

background_: string = ''

The background color; either a CSS3 color value or the empty string. Transparent black is used if it is the empty string.

counter_: number = 0

Counter for how many frames need to be drawn to erase trails.

focusView_: null | SimView = null

The view which is the main focus and is drawn normally.

FOCUS_VIEW_CHANGED: "FOCUS_VIEW_CHANGED" = 'FOCUS_VIEW_CHANGED'

Name of GenericEvent that is broadcast when the focus view changes.

SIZE_CHANGED: "SIZE_CHANGED" = 'SIZE_CHANGED'

Name of GenericEvent that is broadcast when the size of the HTML canvas changes.

VIEW_ADDED: "VIEW_ADDED" = 'VIEW_ADDED'

Name of GenericEvent that is broadcast when a SimView is added.

VIEW_LIST_MODIFIED: "VIEW_LIST_MODIFIED" = 'VIEW_LIST_MODIFIED'

Name of GenericEvent that is broadcast when the list of SimViews is modified.

VIEW_REMOVED: "VIEW_REMOVED" = 'VIEW_REMOVED'

Name of GenericEvent that is broadcast when a SimView is removed.

Methods

  • Adds an object to the list of Memorizable objects. These object's memorize methods will be called from this object's memorize method.

    Parameters

    • memorizable: Memorizable

      object to add to the list of Memorizable objects

    Returns void

    Throws

    if called during the memorize method.

  • 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.

    Parameters

    Returns void

  • Returns 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.

    Returns number

    transparency used when painting, between 0 and 1.

  • 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).

    Returns string

    the background color; either a CSS3 color value or the empty string

  • Returns the HTML canvas being managed by this LabCanvas.

    Returns HTMLCanvasElement

    the HTML canvas being managed by this LabCanvas

  • Returns whether this LabCanvas has changed, and sets the state to "unchanged".

    Returns boolean

    whether this LabCanvas has changed

  • Returns the CanvasRenderingContext2D used for drawing into the HTML canvas being managed by this LabCanvas.

    Returns CanvasRenderingContext2D

    the CanvasRenderingContext2D used for drawing into the HTML canvas

  • Returns the focus SimView which the user expects to modify by his/her actions.

    Returns null | SimView

    the focus SimView, or null when there is no focus SimView

  • Returns the height of the HTML canvas, in screen coords (pixels).

    Returns number

    the height of the HTML canvas, in screen coords (pixels)

  • 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.

    Returns ScreenRect

    the ScreenRect corresponding to the area of the HTML canvas.

  • Returns the width of the HTML canvas, in screen coords (pixels).

    Returns number

    the width of the HTML canvas, in screen coords (pixels)

  • Memorize the current simulation data, or do some other function that should happen regularly after each simulation time step.

    Returns void

  • Removes an object from the list of Memorizable objects.

    Parameters

    • memorizable: Memorizable

      object to remove from the list of Memorizable objects

    Returns void

    Throws

    if called during the memorize method.

  • 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.

    Parameters

    • view: SimView

      the SimView to remove

    Returns void

  • 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.

    Parameters

    • value: number

      transparency used when painting, between 0 and 1

    Returns void

  • 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).

    Parameters

    • value: string

      the background color; either a CSS3 color value or the empty string

    Returns void

  • Sets 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.

    Parameters

    • value: boolean

      whether this Subject should broadcast events

    Returns boolean

    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.

    Parameters

    • view: null | SimView

      the view that should be the focus; can be null when no SimView has the focus.

    Returns void

    Throws

    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.

    Parameters

    • value: number

      the height of the canvas, in screen coords (pixels),

    Returns void

  • Sets the size of this LabCanvas to the given ScreenRect by calling setSize.

    Parameters

    • sr: ScreenRect

      specifies the width and height; the top-left must be (0,0).

    Returns void

    Throws

    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.

    Parameters

    • width: number

      the width of the canvas, in screen coords (pixels)

    • height: number

      the height of the canvas, in screen coords (pixels)

    Returns void

  • 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.

    Parameters

    • value: number

      the width of the canvas, in screen coords (pixels),

    Returns void

  • 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'}}
    

    Returns string

    a minimal string representation of this object.

Generated using TypeDoc