Accepts an array of boxes (with a lot of optional configuration options) and returns geometry for a nice justified layout as seen all over Flickr.
npm install justified-layout
var layoutGeometry = require('justified-layout')([1.33, 1, 0.65] [, config])
There's two options for input. The simplest is an array of numbers representing aspect ratios. Alternatively you can pass in an array of objects with width and height properties.
Aspect ratios option:
[1.33, 1, 0.65]
Size object option:
[{
width: 400,
height: 300
},
{
width: 300,
height: 300
},
{
width: 250,
height: 400
}]
Something like this:
{
"containerHeight": 1269,
"widowCount": 0,
"boxes": [
{
"aspectRatio": 0.5,
"top": 10,
"width": 170,
"height": 340,
"left": 10
},
{
"aspectRatio": 1.5,
"top": 10,
"width": 510,
"height": 340,
"left": 190
},
...
]
}
No configuration is required but chances are you'd like to change some things. Here are your options:
Parameter | Type | Default | Description |
---|---|---|---|
containerWidth |
Integer | 1060 |
The width that boxes will be contained within irrelevant of padding. |
containerPadding |
Integer or Object | 10 |
Provide a single integer to apply padding to all sides or provide an object to apply individual values to each side, like this:
|
boxSpacing |
Integer or Object | 10 |
Provide a single integer to apply spacing both horizontally and vertically or provide an object to apply individual values to each axis, like this:
|
targetRowHeight |
Integer | 320 |
It's called a target because row height is the lever we use in order to fit everything in nicely. The algorithm will get as close to the target row height as it can. |
targetRowHeightTolerance |
Float | 0.25 |
How far row heights can stray from |
maxNumRows |
Integer | Number.POSITIVE_INFINITY |
Will stop adding rows at this number regardless of how many items still need to be laid out. |
forceAspectRatio |
Boolean or Float | false |
Provide an aspect ratio here to return everything in that aspect ratio. Makes the values in your input array irrelevant. The length of the array remains relevant. |
showWidows |
Boolean | true |
By default we'll return items at the end of a justified layout even if they don't make
a full row. If |
fullWidthBreakoutRowCadence |
Boolean or Integer | false |
If you'd like to insert a full width box every |
*All examples scaled to half the size for better presentation here.
var geometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2, 2.1])
var geometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2.2, 1.5], {
containerPadding: 50
})
var geometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7], {
containerPadding: {
top: 50,
right: 20,
left: 200,
bottom: 50
}
})
var geometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2.2, 1.5], {
boxSpacing: {
horizontal: 4,
vertical: 30
}
})
var geometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2.2, 1.5, 0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2.2], {
targetRowHeight: 100
})
var geometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2.2, 1.5], {
forceAspectRatio: 1
})
var geometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2.2], {
showWidows: false
})
var geometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2.2, 1.5], {
fullWidthBreakoutRowCadence: 2
})