Quickstart: Using the Lume Kiwi Solver
mainLume Kiwi is a lightweight and fast implementation of the Cassowary constraint solving algorithm. To use it, you create a Solver, define Variable objects, add them to the solver as editable variables, define Constraint objects using Expression math, and then call updateVariables() to solve the system.
Note: You can adjust solver.maxIterations (default is 10,000) if the solver requires more iterations to converge.
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)