@spearwolf/twopoint5d
Version:
a library to create 2.5d realtime graphics and pixelart with three.js
43 lines • 2.02 kB
JavaScript
import { Vector2 } from 'three';
export function fitIntoRectangle(rect, specs, target = new Vector2()) {
if ('pixelZoom' in specs) {
target.copy(rect).divideScalar(specs.pixelZoom);
}
else if (specs.fit === 'fill') {
target.copy(rect);
}
else if (specs.fit === 'contain' || specs.fit === 'cover') {
if ('width' in specs && specs.width !== 0 && (!('height' in specs) || specs.height === 0)) {
target.width = specs.width;
target.height = rect.height * (specs.width / rect.width);
}
else if ((!('width' in specs) || specs.width === 0) && 'height' in specs && specs.height !== 0) {
target.width = rect.width * (specs.height / rect.height);
target.height = specs.height;
}
else if ('width' in specs && specs.width !== 0 && 'height' in specs && specs.height !== 0) {
const rectRatio = rect.width / rect.height;
const specsRatio = specs.width / specs.height;
const isContain = specs.fit === 'contain';
if ((isContain && rectRatio > specsRatio) || (!isContain && rectRatio < specsRatio)) {
target.width = rect.width * (specs.height / rect.height);
target.height = specs.height;
}
else if ((isContain && rectRatio < specsRatio) || (!isContain && rectRatio > specsRatio)) {
target.width = specs.width;
target.height = rect.height * (specs.width / rect.width);
}
else {
target.set(specs.width, specs.height);
}
}
if ('minPixelZoom' in specs && rect.width / target.width < specs.minPixelZoom) {
target.copy(rect).divideScalar(specs.minPixelZoom);
}
else if ('maxPixelZoom' in specs && rect.width / target.width > specs.maxPixelZoom) {
target.copy(rect).divideScalar(specs.maxPixelZoom);
}
}
return target;
}
//# sourceMappingURL=fitIntoRectangle.js.map