IjzerenHein / kiwi.js

Fast TypeScript implementation of the Cassowary constraint solving algorithm 🖖
Other
249 stars 24 forks source link

a proposal for improved ergonomics #14

Open mreinstein opened 5 years ago

mreinstein commented 5 years ago

kiwi.js is really cool! One thing I've found painful is having to construct all of the constraints in javascript. I've developed a simplistic parser and wrapper interface to make this easier. Here's what I've currently got:

import constraints from './lib/constraints.js'   // the wrapper for kiwi.js

const layout = constraints({
    editableVariables: [
        {
            name: 'mine', 
            strength: 'strong'   // optional parameter
        }
    ],
    constraints: `
        windowWidth  <= (windowHeight - modalHeight) / 25
        windowHeight == 45 / 6
        modalHeight  == mine * 10
        windowWidth  >= 0 required
        windowHeight >= 0 required
    `
})

layout.suggestValue('mine', 102)

layout.updateVariables()

console.log(layout.getValues())
// { mine: 102, windowWidth: 0, windowHeight: 7.5, modalHeight: 1020 }

Would something like this be a welcome addition to kiwi? Open for suggestions on changing the API etc.

thanks for a great module!!

mreinstein commented 5 years ago

current working implementation of this https://github.com/mreinstein/constraint-solver

mreinstein commented 5 years ago

updated implementation, replaced my janky parser with one based on pegjs. (adapted the grammar from cassowary.js)

https://github.com/mreinstein/constraint-solver