capdim
v1.0.0
Published
Caps an N-dimensional array to a maximum total volume (the product of its dimensions) while preserving proportions.
Downloads
145
Maintainers
Readme
capdim
capdim caps an N-dimensional array to a maximum total volume (the product of its dimensions) while preserving proportions. It's minimal, atomic, and high performance.
Details
- Works with any number of dimensions (e.g., 2D, 3D, 4D etc.).
- Caps total volume without distorting proportions.
- Pure & atomic function, suitable for composable utility pipelines.
Install
npm install capdim
Usage
import capDim from 'capdim';
console.log(capDim([1920, 1080], 1_000_000));
// [1333.3333333333333, 750]
🔧 API
capDim(dims: number[], maxVolume: number): number[]
Caps the input dimensions so that their total product does not exceed maxVolume
.
dims
– Array of dimensions (e.g.,[width, height, depth]
).maxVolume
– The maximum allowed product of dimensions.- Returns: The new scaled-down dimensions (or the original if already within bounds).
Example Use Cases
- Image Processing – Prevent excessive pixel count while keeping aspect ratio.
- 3D Modeling – Ensure objects stay within a total volume constraint.
- Physics Simulations – Rescale multi-dimensional datasets.
- Grid/Matrix Operations – Adjust data structures without exceeding capacity.
Related Packages
- https://github.com/alexstevovich/fitdim – Ensures dimensions fit within a bounding box.
- https://github.com/alexstevovich/normalizedim – Normalizes one axis while keeping proportions.
- https://github.com/alexstevovich/percentdim – Tells you the percent difference between two N-dimensional areas.
These links might be suffixed with "-node" in the future if conflicts arise.
Links
Development Homepage
https://github.com/alexstevovich/capdim
This link might be suffixed with "-node" in the future if conflicts arise.
License
Licensed under the Apache License 2.0.