fitdim
Version:
Scales an N-dimensional array to perfectly fit within a bounding box, maintaining proportions.
79 lines (77 loc) • 2.73 kB
JavaScript
/* *******************************************************
* fitdim
*
* @license
*
* Apache-2.0
*
* Copyright 2015-2025 Alex Stevovich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @meta
*
* package_name: fitdim
* file_name: gen/index.cjs
* purpose: Core functionality and exports combined.
*
* @system
*
* generated_on: 2025-03-15T03:33:47.089Z
* certified_version: 1.0.0
* file_uuid: 6ce858a0-923d-4bd0-83a5-606373f111ee
* file_size: 2679 bytes
* file_hash: f2ff29b8b207e1d383871e3de73526821308ea3f370d49754fda3cf647ef61f7
* mast_hash: 08529ac1d5484f39bb36a88da868fa18a00896cac8aa9b249890a52c771858b6
* generated_by: preamble on npm!
*
* [Preamble Metadata]
********************************************************/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var index_exports = {};
__export(index_exports, {
default: () => index_default,
fitDim: () => fitDim
});
module.exports = __toCommonJS(index_exports);
function fitDim(dims, maxDims) {
if (dims.length !== maxDims.length) {
throw new Error(
`Mismatched dimensions: got ${dims.length} dims but expected ${maxDims.length}`
);
}
const scaleFactors = maxDims.map((max, i) => max / dims[i]);
const scaleFactor = Math.min(...scaleFactors);
return dims.map((dim) => dim * scaleFactor);
}
var index_default = fitDim;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fitDim
});