Lume Kiwi is an efficient implementation of the Cassowary constraint solving algorithm, based on the seminal Cassowary paper. It is not a refactoring or port of the original C++ solver, but has been designed from the ground up to be lightweight and fast.
Example
import * as kiwi from '@lume/kiwi'
// Create a solver
const solver = new kiwi.Solver()
// Adjust the max number of solver iterations before an error is thrown if
// more is needed. Default is 10,000.
solver.maxIterations = 20000
// Create and add some editable variables
const left = new kiwi.Variable()
const width = new kiwi.Variable()
solver.addEditVariable(left, kiwi.Strength.strong)
solver.addEditVariable(width, kiwi.Strength.strong)
// Create a variable calculated through a constraint
const centerX = new kiwi.Variable()
const expr = new kiwi.Expression([-1, centerX], left, [0.5, width])
solver.addConstraint(new kiwi.Constraint(expr, kiwi.Operator.Eq, kiwi.Strength.required))
// Suggest some values to the solver
solver.suggestValue(left, 0)
solver.suggestValue(width, 500)
// Lets solve the problem!
solver.updateVariables()
console.assert(centerX.value() === 250)
- @lume/kiwi
- ~Variable
- new Variable([name])
- .name() ⇒
String
- .setName(name)
- .value() ⇒
Number
- .plus(value) ⇒
Expression
- .minus(value) ⇒
Expression
- .multiply(coefficient) ⇒
Expression
- .divide(coefficient) ⇒
Expression
- ~Expression
- new Expression(...args)
- .plus(value) ⇒
Expression
- .minus(value) ⇒
Expression
- .multiply(coefficient) ⇒
Expression
- .divide(coefficient) ⇒
Expression
- ~Strength
- instance
- static
- ~Constraint
- new Constraint(expression, operator, [rhs], [strength])
- .expression() ⇒
Expression
- .op() ⇒
Operator
- .strength() ⇒
Number
- ~Solver
- new Solver()
- .maxIterations :
number
- .createConstraint(lhs, operator, rhs, [strength])
- .addConstraint(constraint)
- .removeConstraint(constraint)
- .hasConstraint(constraint) ⇒
Bool
- .getConstraints() ⇒
[ 'Array' ].<Constraint>
- .addEditVariable(variable, strength)
- .removeEditVariable(variable)
- .hasEditVariable(variable) ⇒
Bool
- .suggestValue(variable, value)
- .updateVariables()
- ~Operator :
enum
- ~Variable
The primary user constraint variable.
Kind: inner class of @lume/kiwi
- ~Variable
- new Variable([name])
- .name() ⇒
String
- .setName(name)
- .value() ⇒
Number
- .plus(value) ⇒
Expression
- .minus(value) ⇒
Expression
- .multiply(coefficient) ⇒
Expression
- .divide(coefficient) ⇒
Expression
Param | Type | Default | Description |
---|---|---|---|
[name] | String |
"" |
The name to associated with the variable. |
Returns the name of the variable.
Kind: instance method of Variable
Returns: String
- name of the variable
Set the name of the variable.
Kind: instance method of Variable
Param | Type | Description |
---|---|---|
name | String |
Name of the variable |
Returns the value of the variable.
Kind: instance method of Variable
Returns: Number
- Calculated value
Creates a new Expression by adding a number, variable or expression to the variable.
Kind: instance method of Variable
Returns: Expression
- expression
Param | Type | Description |
---|---|---|
value | Number | Variable | Expression |
Value to add. |
Creates a new Expression by substracting a number, variable or expression from the variable.
Kind: instance method of Variable
Returns: Expression
- expression
Param | Type | Description |
---|---|---|
value | Number | Variable | Expression |
Value to substract. |
Creates a new Expression by multiplying with a fixed number.
Kind: instance method of Variable
Returns: Expression
- expression
Param | Type | Description |
---|---|---|
coefficient | Number |
Coefficient to multiply with. |
Creates a new Expression by dividing with a fixed number.
Kind: instance method of Variable
Returns: Expression
- expression
Param | Type | Description |
---|---|---|
coefficient | Number |
Coefficient to divide by. |
An expression of variable terms and a constant.
The constructor accepts an arbitrary number of parameters, each of which must be one of the following types:
- number
- Variable
- Expression
- 2-tuple of [number, Variable|Expression]
The parameters are summed. The tuples are multiplied.
Kind: inner class of @lume/kiwi
- ~Expression
- new Expression(...args)
- .plus(value) ⇒
Expression
- .minus(value) ⇒
Expression
- .multiply(coefficient) ⇒
Expression
- .divide(coefficient) ⇒
Expression
Param | Type |
---|---|
...args | number | Variable | Expression | Array |
Creates a new Expression by adding a number, variable or expression to the expression.
Kind: instance method of Expression
Returns: Expression
- expression
Param | Type | Description |
---|---|---|
value | Number | Variable | Expression |
Value to add. |
Creates a new Expression by substracting a number, variable or expression from the expression.
Kind: instance method of Expression
Returns: Expression
- expression
Param | Type | Description |
---|---|---|
value | Number | Variable | Expression |
Value to substract. |
Creates a new Expression by multiplying with a fixed number.
Kind: instance method of Expression
Returns: Expression
- expression
Param | Type | Description |
---|---|---|
coefficient | Number |
Coefficient to multiply with. |
Creates a new Expression by dividing with a fixed number.
Kind: instance method of Expression
Returns: Expression
- expression
Param | Type | Description |
---|---|---|
coefficient | Number |
Coefficient to divide by. |
Kind: inner class of @lume/kiwi
The 'required' symbolic strength.
Kind: instance property of Strength
The 'strong' symbolic strength.
Kind: instance property of Strength
The 'medium' symbolic strength.
Kind: instance property of Strength
The 'weak' symbolic strength.
Kind: instance property of Strength
Create a new symbolic strength.
Kind: static method of Strength
Returns: strength
Param | Default | Description |
---|---|---|
a | strong | |
b | medium | |
c | weak | |
[w] | 1 |
weight |
A linear constraint equation.
A constraint equation is composed of an expression, an operator, and a strength. The RHS of the equation is implicitly zero.
Kind: inner class of @lume/kiwi
- ~Constraint
- new Constraint(expression, operator, [rhs], [strength])
- .expression() ⇒
Expression
- .op() ⇒
Operator
- .strength() ⇒
Number
Param | Type | Default | Description |
---|---|---|---|
expression | Expression |
The constraint expression (LHS). | |
operator | Operator |
The equation operator. | |
[rhs] | Expression |
Right hand side of the expression. | |
[strength] | Number |
Strength.required |
The strength of the constraint. |
Returns the expression of the constraint.
Kind: instance method of Constraint
Returns: Expression
- expression
Returns the relational operator of the constraint.
Kind: instance method of Constraint
Returns: Operator
- linear constraint operator
Returns the strength of the constraint.
Kind: instance method of Constraint
Returns: Number
- strength
The constraint solver class.
Kind: inner class of @lume/kiwi
- ~Solver
- new Solver()
- .maxIterations :
number
- .createConstraint(lhs, operator, rhs, [strength])
- .addConstraint(constraint)
- .removeConstraint(constraint)
- .hasConstraint(constraint) ⇒
Bool
- .getConstraints() ⇒
[ 'Array' ].<Constraint>
- .addEditVariable(variable, strength)
- .removeEditVariable(variable)
- .hasEditVariable(variable) ⇒
Bool
- .suggestValue(variable, value)
- .updateVariables()
Construct a new Solver.
- The max number of solver iterations before an erroris thrown, in order to prevent infinite iteration. Default:
10,000
.
Kind: instance property of Solver
Creates and add a constraint to the solver.
Kind: instance method of Solver
Param | Type | Default | Description |
---|---|---|---|
lhs | Expression | Variable |
Left hand side of the expression | |
operator | Operator |
Operator | |
rhs | Expression | Variable | Number |
Right hand side of the expression | |
[strength] | Number |
Strength.required |
Strength |
Add a constraint to the solver.
Kind: instance method of Solver
Param | Type | Description |
---|---|---|
constraint | Constraint |
Constraint to add to the solver |
Remove a constraint from the solver.
Kind: instance method of Solver
Param | Type | Description |
---|---|---|
constraint | Constraint |
Constraint to remove from the solver |
Test whether the solver contains the constraint.
Kind: instance method of Solver
Returns: Bool
- true or false
Param | Type | Description |
---|---|---|
constraint | Constraint |
Constraint to test for |
Get an array of the current constraints.
Kind: instance method of Solver
Add an edit variable to the solver.
Kind: instance method of Solver
Param | Type | Description |
---|---|---|
variable | Variable |
Edit variable to add to the solver |
strength | Number |
Strength, should be less than Strength.required |
Remove an edit variable from the solver.
Kind: instance method of Solver
Param | Type | Description |
---|---|---|
variable | Variable |
Edit variable to remove from the solver |
Test whether the solver contains the edit variable.
Kind: instance method of Solver
Returns: Bool
- true or false
Param | Type | Description |
---|---|---|
variable | Variable |
Edit variable to test for |
Suggest the value of an edit variable.
Kind: instance method of Solver
Param | Type | Description |
---|---|---|
variable | Variable |
Edit variable to suggest a value for |
value | Number |
Suggested value |
Update the values of the variables.
Kind: instance method of Solver
An enum defining the linear constraint operators.
Value | Operator | Description |
---|---|---|
Le |
<= | Less than equal |
Ge |
>= | Greater than equal |
Eq |
== | Equal |
Kind: inner enum of @lume/kiwi