Class DiagramBuilder

Construct a diagram with a Penrose-like API. You may find it useful to destructure an object of this type:

const {
type,
predicate,
circle,
line,
ensure,
// ...
} = new DiagramBuilder(canvas(400, 400), "seed");

Constructors

  • Create a new diagram builder.

    Parameters

    • canvas: Canvas

      Local dimensions of the SVG. This has no effect on the rendered size of your diagram--only the size of the local coordinate system.

    • variation: string = ""

      Randomness seed

    • lassoStrength: number = 0

      Strength of the optimizers lasso term. Higher values encourage diagram continuity, while lower values encourage reactivity. Default is 0.

    Returns DiagramBuilder

Methods

  • Parameters

    • shape: Shape
    • event: string
    • listener: ((e: any, diagram: Diagram) => void)
        • (e, diagram): void
        • Parameters

          Returns void

    Returns void

  • Create a new input, and constrain that input to a calculated value. This is useful for reading calculated values from the diagram using getInput, and for creating draggable values.

    Parameters

    • num: Num

      Value to bind the input to

    • Optionalinfo: InputOpts

      InputOpts for the input

    Returns Var

  • Register an objective with the diagram.

    Parameters

    • objective: Num

      A Num created with the objectives module (or, if you know what you're doing, by yourself. You can read about creating custom constaints at https://penrose.cs.cmu.edu/docs/ref/constraints)

    • Optionalweight: number

      An optional weight to multiply the objective by. If you find that your objectives are not being satisfied, you may want to try increasing the weight.

    Returns void

  • Register a constraint with the diagram.

    Parameters

    • constraint: Num

      A Num created with the constraints module (or, if you know what you're doing, by yourself. You can read about creating custom constaints at https://penrose.cs.cmu.edu/docs/ref/constraints)

    • Optionalweight: number

      An optional weight to multiply the constraint by. If you find that your constraints are not being satisfied, you may want to try increasing the weight.

    Returns void

  • Create a new predicate over substances.

    The returned predicate function can be used to declare relationships between substances (by calling the predicate) and to query existing relation (by calling the predicate's .test method):

    const Vector = type();
    const Orthogonal = predicate();

    const v1 = Vector();
    const v2 = Vector();

    Orthogonal.test(v1, v2); // returns false
    Orthogonal(v1, v2);
    Orthogonal.test(v1, v2); // returns true

    Returns Predicate

  • Create an input from a SharedInput.

    Parameters

    • input: SharedInput

      SharedInput to create an input from

    • OptionalinitOverride: number

      Override the initial value of the shared input

    Returns Var

  • Instantiate a new substance. If no type is given, this is identical to creating and empty object. If a type T is given, this method is equivalent to calling T().

    Parameters

    • Optionaltype: Type

      Type to instantiate

    Returns Substance

  • An input giving the current time since the diagram was mounted. You can use this to create animations. However, you must use the AnimatedRenderer component to render the diagram, otherwise this input will not be updated.

    Returns Var

  • Create a new substance type. The type object serves as a constructor for new substances:

    const Vector = type();

    const v1 = Vector();
    const v2 = Vector();
    ...

    Returns Type