
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
An exceptionally small and fast masonry layout resolver.
$ npm install tiny-pack
tiny-pack
resolves a tight grid layout for a set of input elements of varying dimensions. It does not interact directly with the DOM and can be used in any JS environment.
The resolver is agnostic to absolute element and container dimensions, and operates using only relative ratios.
All elements are positioned to have the same height as all other elements within each row. No element can span multiple rows, regardless of how tall and narrow it is.
The resolved layout preserves the aspect ratios of all elements, only scale transformations are necessary to conform the input elements to the layout.
The layout resolver output is a single array of scale values that represent each input element's width relative to the absolute container. It does not return absolute dimensions or coordinates, however, absolute coordinates of each element can be calculated by iterating through the returned scale values.
Below is a contrived minimal example that demonstrates creating an HTML layout for a set of input element of known dimensions.
import tinyPack from 'tiny-pack';
// Assuming we have a set of images of known dimensions
const images = [
{width: 450, height: 400, src: '...'},
{width: 300, height: 200, src: '...'},
{width: 500, height: 350, src: '...'},
{width: 400, height: 350, src: '...'},
{width: 100, height: 600, src: '...'},
...
];
// Generate an array of aspect ratios (width/height) from the input dimensions.
const ratios = images.map((element) => {
return element.width / element.height;
});
// Define the target ratio for each packed row. Each row will be 4 units wide
// and 1 unit high.
const targetRatio = 4;
// `tiny-pack` returns an array of scale values that, when applied to each input
// element, will pack them tightly into rows.
const scales = tinyPack(targetRatio, ratios);
// Create a container element that left-aligns and wraps all child elements.
const container = document.createElement('div');
document.body.appendChild(container);
container.style.display = 'flex';
container.style.flexWrap = 'flex-wrap';
container.style.outline = '1px solid black';
// Append a img tag for each input image definition and set its width
// (as flex-basis) relative to the parent container.
images.map((image, i) => {
const img = document.createElement('img');
container.appendChild(img);
img.src = image.src;
img.style.flex = `0 1 ${scales[i] * 100} %`;
});
FAQs
A really tiny bin packing algorithm < 500 bytes minfied.
We found that tiny-pack demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.