API Docs for:
Show:

LSystem Class

Constructor

LSystem

(
  • config
)

Parameters:

  • config Object

    Configuration Object.

Methods

_resolveMatch

(
  • ruleValue
  • index
  • value
  • match
)
String protected

Parameters:

Returns:

String: Returns null if the rule doesn't apply.

iterate

(
  • [iterations]
)
chainable

Iterates the l-system's value.

Parameters:

  • [iterations] Number optional

    Optional. The number of iterations to perform. Defaults to 1. The l-system can only iterate forward so negative values aren't accepted.

restart

() chainable

This method restarts the l-system and resets its value.

Attributes

axiom

String

The axiom is the initial value of the l-system. Note: If the axiom is changed after the l-system has been iterated, the l-system will be restarted.

Default: ''

Fires event axiomChange

Fires when the value for the configuration attribute axiom is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

iterations

Number

The number of iterations the l-system has been set to. This attribute is read only; use the iterate method to iterate the l-system.

Default: 0

Fires event iterationsChange

Fires when the value for the configuration attribute iterations is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

pattern

RegExp protected

The regular expression that is used to help apply the rules during iteration.

Fires event patternChange

Fires when the value for the configuration attribute pattern is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

rules

Object

The l-system's rules. Note: If the rules are changed after the l-system has been iterated, the l-system will be restarted. If you read this object and manipulate it directly, you must call the restart method before iterating the l-system again otherwise it will yield unknown results.

The rules are key/value pairs. The rules object's keys should be single characters. In the simplest case, the rules object's values are strings. For example, with this rules object {a: 'abc'}, every time the character 'a' is found in the l-system's value, it will be replaced with the string 'abc'.

The rules object's values may also be objects for more control over when the rule is applied. For example, with this rules object {a: {prev: 'ccc', value: 'abc'}}, every time the character 'a' is found in the l-system's value and the previous characters in the l-system's value are 'ccc', the 'a' will be replaced with the string 'abc'. In the simplest case, the value property is a string but it may be any value that can be one of the rule object's values. Here is a list of the properties that affect when this rule is applied:

first
If the first property is defined and is a truthy value, the rule will only be applied if the matched character is the first character in the l-system's value. If the first property is set to false, the rule will only be applied if the matched character is not the first character in the l-system's value.
last
If the last property is defined and is a truthy value, the rule will only be applied if the matched character is the last character in the l-system's value. If the last property is set to false, the rule will only be applied if the matched character is not the last character in the l-system's value.
next
If the next property is defined, it should be a string of one or more characters. The rule will only be applied if the matched character is followed by this string of characters exactly. The next property may also be a weighted list of strings of one or more characters. In this case, the value will be selected from the weighted list at random.
notNext
If the notNext property is defined, it should be a string of one or more characters. The rule will only be applied if the matched character is not followed by this string of characters exactly. The notNext property may also be a weighted list of strings of one or more characters. In this case, the value will be selected from the weighted list at random.
notPrev
If the notPrev property is defined, it should be a string of one or more characters. The rule will only be applied if the matched character is not preceded by this string of characters exactly. The notPrev property may also be a weighted list of strings of one or more characters. In this case, the value will be selected from the weighted list at random.
prev
If the prev property is defined, it should be a string of one or more characters. The rule will only be applied if the matched character is preceded by this string of characters exactly. The prev property may also be a weighted list of strings of one or more characters. In this case, the value will be selected from the weighted list at random.

For even further control, the rules object's values may be objects with a fn property. The fn property should be a function that accepts three arguments:

match
String. The single character that was matched.
index
Number. The index of the matched character within the l-system's value.
value
String. The l-system's current value.

The function should return a string or null. The rule will only be applied if the function does not return null. The object may also have a ctx property which will be used as the execution context for the function.

Multiple rules may be given for a single character by providing an array as a rules object's value. The array may contain any value that is acceptable as a rules object's value. The array items will be processed in order; the first rule that applies will be used. Because of this, the array items should usually be one of the ojects described above. The array may contain string items, but since string rules are always applied, there should only ever be one string item in the array and it should be the last item in the array.

A rules object's value may also be a weighted list of any of the other values described above. In this case, the values will be selected from the weighted list at random.

Default: {}

Fires event rulesChange

Fires when the value for the configuration attribute rules is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

value

String

The current value of the l-system.

Default: ''

Fires event valueChange

Fires when the value for the configuration attribute value is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.