• Stars
    star
    283
  • Rank 146,066 (Top 3 %)
  • Language
    JavaScript
  • Created over 14 years ago
  • Updated almost 12 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript layout algorithms

jLayout — JavaScript Layout Algorithms

The jLayout JavaScript library provides layout algorithms for laying out components. A component is an abstraction; it can be implemented in many ways, for example as items in a HTML5 Canvas drawing or as HTML elements. The jLayout library allows you to focus on drawing the individual components instead of on how to arrange them on your screen.

The library currently provides four layout algorithms: border, which lays out components in five different regions; grid, which lays out components in a user defined grid, flex-grid which offers a grid with flexible column and row sizes, and flow which flows components in a user defined direction. Using the grid and flex-grid algorithms you can also create horizontal and vertical layouts. A jQuery plugin to lay out HTML elements is also available.

Usage

We start with the definition of a component; a component is something that has a minimum size, a preferred size, and a maximum size. It also has a method to set its size and position. A container is a component that contains other components and lays them out according to a layout algorithm (which are provided in the jLayout library.) Components need to satisfy the following interface requirements in order to be used with the layout algorithms.

bounds()
Returns the component's position and size as a value object with properties `x`, `y`, `width` and `height`. All properties should be present in the returned object.
<dt>bounds(value)</dt>
<dd>Set the position and size of a component by using a value object with properties `x`, `y`, `width` and `height`. The individual properties are all optional, it is thus legal to supply an empty object, or any combination of properties.</dd>

<dt>preferredSize()</dt>
<dd>Returns the component's preferred size (i.e. the size it wishes to have) as an object with properties `width` and `height`. This is only a hint, there is no guarantee the component will get the size it prefers.</dd>

<dt>minimumSize()</dt>
<dd>Returns the component's minimum size (i.e. the size it should at least have) as an object with properties `width` and `height`.</dd>

<dt>maximumSize()</dt>
<dd>Returns the component's maximum size (i.e. the size it should stay below or equal to) as an object with properties `width` and `height`.</dd>

<dt>isVisible()</dt>
<dd>Returns `true` if the component is visible and should be taken into account when calculating the layout. Returns `false` otherwise.</dd>

<dt>insets()</dt>
<dd>Returns the offset between a container and its contents as an object with properties: `top`, `bottom`, `left`, and `right`.</dd>

<dt>doLayout()</dt>
<dd>Calls the `layout` method on the layout algorithm used to lay out the component (container) it is called on. If the component is not a container (and does not have a layout algorithm) this method can be left empty.</dd>

Note that the distinction between containers and components is artificial, both implement the same interface.

Layout Algorithms

All algorithms are in the jLayout namespace and implement the following interface.

preferred(container)
Returns the preferred size of the container and its children according to the layout algorithm.
<dt>minimum(container)</dt>
<dd>Returns the minimum size the container and its children are allowed to have according to the layout algorithm.</dd>

<dt>maximum(container)</dt><dd>

Returns the maximum size the container and its children are allowed to have according to the layout algorithm.

<dt>layout(container)</dt>
<dd>Performs the layout according to the algorithm; resizing and positioning children if necessary.</dd>

The layout method will not resize the container to accommodate its children's preferred size. If a resize is desired, set the bounds of the container to its preferred size. The example below shows both ways of laying out a container; resizing the children to fit in the container, and resizing the container to fit the children.

// create a layout algorithm
var myLayout = (…)

// lay out without resizing the container
myLayout.layout(container);

// resize the container, then lay it out
container.bounds(myLayout.preferred());
myLayout.layout(container);

A layout algorithm can be created by calling either the grid or border constructor in the jLayout namespace. Both constructors take an option object containing layout specific properties. Both layouts have the following properties in common:

hgap
The horizontal space between the laid out components. Should be a number in a coordinate space of your choice. Defaults to 0. Optional.
<dt>vgap</dt>
<dd>The vertical space between the laid out components. Should be a number in a coordinate space of your choice. Defaults to 0. Optional.</dd>

The other properties are specific to the layout algorithm and are discussed below.

Border layout

The border algorithm lays out components in five different regions. These regions are called center, north, south, east and west. The center component will be laid out in the center of the container, north on top of it, south beneath it and west and east on the left and right side respectively. The layout can only contain one of each region, but all are optional. Below is a visualization of a layout using all five regions.

The border algorithm takes an option object as parameter which can contain the following properties:

center, north, south east, west
The center, north, south, east or west component. All are optional.

The following example lays out a west, center and north component with a vertical space of 5 units between each component. There may be additional space between the components and the container if the container returns non-zero insets.

var borderLayout = jLayout.border({
    west:   myWestComponent,
    center: myCenterComponent,
    north:  myNorthComponent,
    vgap: 5
});

borderLayout.layout(myContainer);

If a region is not specified or the component is not visible its space will be taken up by the other components.

Grid layout

The grid algorithm lays out the components in a grid, and resizes each component to the same size. The number of columns and rows can be specified by the user. Below is a visualization of a grid layout with four components in a 2x2 grid.

The grid algorithm takes an option object as parameter which can contain the following properties:

rows
The number of rows in the grid. Optional.
<dt>columns</dt>
<dd>The number of columns in the grid. Optional.</dd>

<dt>items</dt>
<dd>An array containing the components to be laid out by the algorithm. Optional.</dd>

<dt>fill</dt>
<dd>The direction in which the grid is filled in. Valid values are `horizontal` or `vertical` for filling in the grid left to right and top to bottom, or top to bottom and left to right respectivily. Optional.</dd>

The following example lays out four components in a 2x2 grid, without any spacing between the components.

var gridLayout = jLayout.grid({
    rows: 2,
    columns: 2,
    items: [myComponent1, myComponent2, myComponent3, myComponent4]
});

gridLayout.layout(myContainer);

If the number of rows is given, the number of columns is calculated automatically by taking the number of components into account. If the number of rows is not given (or set to zero), and the number of columns is given, the number of rows will be automatically calculated using the number of components. If neither is given, the number of rows is set equal to the number of components and the number of rows is set to zero.

Flex grid layout

The flex grid algorithm lays out the components in a grid with flexible row and columns sizes. The number of columns and rows can be specified by the user. Below is a visualization of a flex grid layout with six components in a 3x2 grid.

The flex grid algorithm takes an option object as parameter which can contain the following properties:

rows
The number of rows in the flex grid. Optional.
<dt>columns</dt>
<dd>The number of columns in the flex grid. Optional.</dd>

<dt>items</dt>
<dd>An array containing the components to be laid out by the algorithm. Optional.</dd>

The following example lays out six components in a 3x2 grid, without any spacing between the components.

var flexGridLayout = jLayout.flexGrid({
    rows: 2,
    columns: 2,
    items: [myComponent1, myComponent2, myComponent3, 
            myComponent4, myComponent5, myComponent6]
});

flexGridLayout.layout(myContainer);

If the number of rows is given, the number of columns is calculated automatically by taking the number of components into account. If the number of rows is not given (or set to zero,) and the number of columns is given, the number of rows will be automatically calculated using the number of components. If neither is given, the number of rows is set equal to the number of components and the number of rows is set to zero.

Flow layout

The flow algorithm lays out the components on a row. When the component does not fit on the current row it is moved to the next row. The alignment within the row can be user specified. Below is an example of a flow layout using five components with the alignment for each row set to left.

The flow algorithm takes an option object as parameter which can contain the following properties:

alignment
A string of either `center`, `left`, or `right`. Defaults to `left`.
<dt>items</dt>
<dd>An array containing the components to be laid out by the algorithm. Optional.</dd>

The following example lays out five components using a center alignment, without any spacing between the components.

var flowLayout = jLayout.flow({
    alignment: 'center',
    items: [myComponent1, myComponent2, myComponent3, 
            myComponent4, myComponent5]
});

flowLayout.layout(myContainer);

Horizontal and vertical layouts

Horizontal and vertical layouts can be achieved by using a grid or a flexGrid layout with one row for horizontal layouts, and one column for vertical layouts. The choice of a grid layout or a flexGrid layout depends on whether or not you want the items in the grid to have uniform sizes (grid) or their natural sizes (flexGrid.) The following layout uses a flex grid so that all items are laid out in the horizontal direction while still allowing the individual items to take up their natural size (i.e. the second component is longer than the other two.)

var horizontalLayout = jLayout.flexGrid({
    rows: 1,
    items: [myComponent1, myComponent2, myComponent3]
});

You can also lay out components vertically, by just changing the rows parameter to columns as shown in the next example.

var verticalLayout = jLayout.flexGrid({
    columns: 1,
    items: [myComponent1, myComponent2, myComponent3]
});

Questions, suggestions, or problems?

Please use the jLayout Google Group for any questions, suggestions, ideas or problems you might have using the jLayout library or the jQuery plugin. Feedback is much appreciated.

License

This libary and the jQuery plugin are licensed under the new BSD license. To summarize the license; the library is completely free for commercial and non-commercial use and you can do with it whatever you want, except claim it as your own work.

More Repositories

1

fontfaceobserver

Webfont loading. Simple, small, and efficient.
JavaScript
4,235
star
2

typeset

TeX line breaking algorithm in JavaScript
JavaScript
980
star
3

hypher

A fast and small JavaScript hyphenation engine
JavaScript
564
star
4

trmix

apply CSS based on your browser's text rendering engine
JavaScript
500
star
5

homebrew-webfonttools

Homebrew formulae for font tools
Ruby
359
star
6

fontloader

A fontloader polyfill
JavaScript
324
star
7

xsltjson

XSLTJSON - Convert XML to JSON using XSLT
XSLT
305
star
8

funcy

An experiment in adding functional pattern matching to JavaScript
JavaScript
247
star
9

url-template

A JavaScript URI template implementation (RFC 6570 compliant)
JavaScript
179
star
10

opentype

An OpenType, TrueType, WOFF, and WOFF2 parser in JavaScript
JavaScript
133
star
11

sfnt2woff-zopfli

WOFF utilities with Zopfli compression
C
126
star
12

promis

A small embeddable Promise polyfill
JavaScript
97
star
13

postcss-scale

PostCSS plugin to scale values from one range to another.
HTML
80
star
14

bit-array

JavaScript implementation of bit arrays.
JavaScript
78
star
15

hyphenation-patterns

Hyphenation patterns for use with Hypher
JavaScript
74
star
16

stateofwebtype

Up-to-date data on support for type and typographic features on the web.
JavaScript
65
star
17

junify

JUnify ― JavaScript Unification Library
JavaScript
48
star
18

text-overflow

jQuery Text Overflow plugin
JavaScript
43
star
19

jsizes

jQuery CSS size properties plugin
JavaScript
37
star
20

characterset

A library for creating and manipulating character sets
JavaScript
29
star
21

css-font-parser

A parser for the CSS font values
JavaScript
26
star
22

jslint

JSLint: The JavaScript Quality Tool, command line version (Node.js)
JavaScript
25
star
23

datrie

A JavaScript Double Array Trie
JavaScript
21
star
24

unicode-tokenizer

Unicode Tokenizer following the Unicode Line Breaking algorithm
JavaScript
20
star
25

nanofont

A nano font for testing font format support
Makefile
19
star
26

node-typekit

A minimal Typekit API client in Node.js
JavaScript
19
star
27

knockout.selection

A selection binding for Knockout.js
JavaScript
19
star
28

javascript

Various JavaScript projects & tools.
JavaScript
17
star
29

knockout.dragdrop

A HTML5 drag and drop binding for Knockout.
JavaScript
16
star
30

text-align

jQuery Text Alignment plugin
JavaScript
13
star
31

tpo

Next generation of browser typesetting
JavaScript
13
star
32

closure-compiler-inline

A Closure Compiler fork with more control over function inlining
Java
11
star
33

calcdeps

A Node.js port of Google Closure library calcdeps.py
JavaScript
11
star
34

js-preprocess

JavaScript Preprocessor
JavaScript
9
star
35

column-selector

jQuery Column Selector
JavaScript
9
star
36

fonzie

A tiny @font-face loader
JavaScript
8
star
37

phantomjs-typekit

A simple demo of using Typekit with PhantomJS
JavaScript
8
star
38

epub2ts

ePub to Treesaver conversion
JavaScript
8
star
39

php-typekit

A PHP client for the Typekit API
PHP
7
star
40

shp2json

Simple tool to convert Shapefiles (GIS) to JSON
JavaScript
6
star
41

hyphenation-justification-vf

JavaScript
5
star
42

nanoserver

A simple web server for development
JavaScript
5
star
43

emfont

A font with a single character filling the entire em-box
HTML
5
star
44

ui

C++/OpenGL User Interface library
5
star
45

jslint-core

JSLint: The JavaScript Code Quality Tool packaged as a CommonJS module
JavaScript
5
star
46

mocha-browserstack

A Mocha reporter that can be used to run Mocha tests automatically on BrowserStack
JavaScript
4
star
47

sfnt2woff

C
4
star
48

node-browserstack

A Node.js client for the BrowserStack API (v3 and screenshot)
JavaScript
4
star
49

website

bramstein.com website source
JavaScript
3
star
50

unicode-data-parser

JavaScript
3
star
51

markup

JavaScript
2
star
52

closure-dom

JavaScript
2
star
53

ui-test

C++/OpenGL User Interface library test project
2
star
54

closureloader

Load code using the Closure library dependency syntax in Node.js
JavaScript
2
star
55

cssvalue

Parsers (and generators) for common CSS values.
JavaScript
2
star
56

thesis

Master Thesis: "Visualizations on the Web"
2
star
57

sowt-test

Automated browser tests for State of Web Type
JavaScript
2
star
58

closure-fetch

JavaScript
1
star
59

detect-writing-script

Detect the writing script given an array of codepoints.
JavaScript
1
star
60

ui-demo

C++/OpenGL User Interface library demo
1
star
61

font-weight-test

Test case for font-weight fallback behaviour
Makefile
1
star
62

amd-to-closure

Transform AMD modules to Closure Compiler dependencies
JavaScript
1
star
63

fontformatdetection

Detect browser support for font formats using feature detection
JavaScript
1
star