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
var kiwi = require('kiwi');
// Create a solver
var solver = new kiwi.Solver();
// Create and add some editable variables
var left = new kiwi.Variable();
var width = new kiwi.Variable();
solver.addEditVariable(left, kiwi.Strength.strong);
solver.addEditVariable(width, kiwi.Strength.strong);
// Create a variable calculated through a constraint
var centerX = new kiwi.Variable();
var 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();
assert(centerX.value(), 250);
- 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
- .required
- .strong
- .medium
- .weak
- .create(a, b, c, [w]) ⇒
Number
- ~Constraint
- new Constraint(expression, operator, [rhs], [strength])
- .expression() ⇒
Expression
- .op() ⇒
Operator
- .strength() ⇒
Number
- ~Solver
- new Solver()
- .createConstraint(lhs, operator, rhs, [strength])
- .addConstraint(constraint)
- .removeConstraint(constraint)
- .hasConstraint(constraint) ⇒
Bool
- .addEditVariable(variable, strength)
- .removeEditVariable(variable)
- .hasEditVariable(variable) ⇒
Bool
- .suggestValue(variable, value)
- .updateVariables()
- ~Operator :
enum
- ~Variable
The primary user constraint variable.
Kind: inner class of 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 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 kiwi
The 'required' symbolic strength.
Kind: static property of Strength
The 'strong' symbolic strength.
Kind: static property of Strength
The 'medium' symbolic strength.
Kind: static property of Strength
The 'weak' symbolic strength.
Kind: static property of Strength
Create a new symbolic strength.
Kind: static method of Strength
Returns: Number
- strength
Param | Type | Default | Description |
---|---|---|---|
a | Number |
strong | |
b | Number |
medium | |
c | Number |
weak | |
[w] | Number |
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 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 kiwi
- ~Solver
- new Solver()
- .createConstraint(lhs, operator, rhs, [strength])
- .addConstraint(constraint)
- .removeConstraint(constraint)
- .hasConstraint(constraint) ⇒
Bool
- .addEditVariable(variable, strength)
- .removeEditVariable(variable)
- .hasEditVariable(variable) ⇒
Bool
- .suggestValue(variable, value)
- .updateVariables()
Construct a new 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 |
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 kiwi