layout
Organize and layout items based on various algorithms
Visualizations of output data:
top-down |
left-right |
diagonal |
alt-diagonal |
binary-tree |
---|---|---|---|---|
Getting Started
Install the module with: npm install layout
// Load in layoutvar layout = ; // Generate a new layer to organize items onvar layer = ; // Add items that you want to organizelayer;layer;layer; // Export the infovar info = layer'export'; // We get back the width and height of the pack as well as organized items height: 80 width: 40 items: height: 10 width: 10 meta: 'small' x: 0 y: 0 height: 20 width: 10 meta: 'medium' x: 0 y: 10 height: 50 width: 40 meta: 'large' x: 0 y: 30
Documentation
Layout is a constructor function
/** * Layout adds items in an algorithmic fashion * @constructor * @param * Available algorithms are listed in the Algorithms section * @param */
Items can be added via addItem
which are required to have a height
and width
. Any additional info should be stored inside of meta
.
/** * @param * @param * @param * @param */
export
is how you take your items and organize them.
/** * @returns * @returns * @returns * @returns */
Algorithms
Currently layout
supports 5 different layout types which are listed below.
top-down
The top-down
algorithm places items vertically.
By default, it sorts from smallest (top) to largest (bottom). However, this can be disabled via sort: false
.
Options:
- sort
Boolean
Flag to enable/disable sorting from smallest (top) to largest (bottom)- By default, this is enabled (
true
)
- By default, this is enabled (
left-right
The left-right
algorithm places items horizontally.
By default, it sorts from smallest (left) to largest (right). However, this can be disabled via sort: false
.
Options:
- sort
Boolean
Flag to enable/disable sorting from smallest (left) to largest (right)- By default, this is enabled (
true
)
- By default, this is enabled (
diagonal
The diagonal
algorithm places items diagonally (top-left to bottom-right).
By default, it sorts from smallest (top-left) to largest (bottom-right). However, this can be disabled via sort: false
.
Options:
- sort
Boolean
Flag to enable/disable sorting from smallest (top-left) to largest (bottom-right)- By default, this is enabled (
true
)
- By default, this is enabled (
alt-diagonal
The alt-diagonal
algorithm places items diagonally (top-right to bottom-left).
By default, it sorts from smallest (top-right) to largest (bottom-left). However, this can be disabled via sort: false
.
Options:
- sort
Boolean
Flag to enable/disable sorting from smallest (top-right) to largest (bottom-left)- By default, this is enabled (
true
)
- By default, this is enabled (
binary-tree
The binary-tree
algorithm packs items via the binary tree algorithm.
This is an efficient way to pack items into the smallest container possible.
Custom algorithms
You can add your own algorithm via layout.addAlgorithm
/** * Method to add new algorithms via * @param * @param * @param * @param */
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint
and test via npm test
.
Donating
Support this project and others by twolfson via gratipay.
License
Copyright (c) 2012-2014 Todd Wolfson Licensed under the MIT license.