fixed calculations for poster defaults, added tests (#102)

This commit is contained in:
Arwid Thornström
2022-01-12 11:24:06 +01:00
committed by GitHub
parent 6741e82479
commit ea086568d0
3 changed files with 2995 additions and 3157 deletions
+1
View File
@@ -1,4 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
verbose: true,
};
File diff suppressed because it is too large Load Diff
+46 -12
View File
@@ -19,18 +19,26 @@ export const calculateDefaultPositioning = (
): TPosterDefaultPositioning => {
// Because we dont want to be dependent on different environments
// number of supported decimals to round to 10 decimal places.
// And I know 70/50 is 1.4 so its not needed but added for clarity.
// Number is the value between a square and landscape (70x50) or portrait (50x70).
// So for landscape its between 1 and 1.4 = 1.2. For portrait its between 1 and 50/70
// calculated with (1 + (50/70 - 1) / 2) which is 0.8571428571.
const size70x50ratio = parseFloat((70 / 50).toFixed(10));
const size50x70ratio = parseFloat((50 / 70).toFixed(10));
const betweenSquareAndLandscape = parseFloat(
(1 + (70 / 50 - 1) / 2).toFixed(10),
);
const betweenSquareAndPortrait = parseFloat(
(1 + (50 / 70 - 1) / 2).toFixed(10),
);
const width_height_ratio = productWidth / productHeight;
// If focuspoints are null we set to 50 to make calculations
// below a bit clearer.
const fpX = focusXpoint2 === null ? 50 : focusXpoint2;
const fpY = focusYpoint2 === null ? 50 : focusYpoint2;
const fpX = (focusXpoint2 === null ? 50 : focusXpoint2) / 100;
const fpY = (focusYpoint2 === null ? 50 : focusYpoint2) / 100;
const width_mm = width_height_ratio >= size70x50ratio ? 700 : 500;
const height_mm = width_height_ratio <= size50x70ratio ? 700 : 500;
const width_mm = width_height_ratio >= betweenSquareAndLandscape ? 700 : 500;
const height_mm = width_height_ratio <= betweenSquareAndPortrait ? 700 : 500;
// Calculate where to put the crop_x percent value
// We place the center of the crop area as close to
@@ -42,9 +50,21 @@ export const calculateDefaultPositioning = (
// The goal for the calculations is to get the smallest amount of area
// that we crop away.
const crop_x = (() => {
// Landscape in landscape
if (width_height_ratio >= size70x50ratio) {
const centerOfRatioArea = (productHeight * 1.4) / 2;
let x = Math.max(centerOfRatioArea, (fpX / 100) * productWidth); // Place center close to focuspointX but not outside the left edge
const centerOfRatioArea = (productHeight * size70x50ratio) / 2;
let x = Math.max(centerOfRatioArea, fpX * productWidth); // Place center close to focuspointX but not outside the left edge
x = Math.min(productWidth - centerOfRatioArea, x); // Dont place outside the right edge
x = x - centerOfRatioArea; // Convert center to left edge
x = x / productWidth; // Convert to percent;
return x;
} else if (
width_height_ratio > size50x70ratio &&
width_height_ratio <= betweenSquareAndPortrait
) {
// Portrait in portrait with higher ratio
const centerOfRatioArea = (productHeight * size50x70ratio) / 2;
let x = Math.max(centerOfRatioArea, fpX * productWidth); // Place center close to focuspointX but not outside the left edge
x = Math.min(productWidth - centerOfRatioArea, x); // Dont place outside the right edge
x = x - centerOfRatioArea; // Convert center to left edge
x = x / productWidth; // Convert to percent;
@@ -52,10 +72,10 @@ export const calculateDefaultPositioning = (
} else if (
// Square in landscape
width_height_ratio > 1 &&
width_height_ratio < size70x50ratio
width_height_ratio < betweenSquareAndLandscape
) {
const centerOfRatioArea = productHeight / 2;
let x = Math.max(centerOfRatioArea, (fpX / 100) * productWidth); // Place center close to focuspointX but not outside the left side
let x = Math.max(centerOfRatioArea, fpX * productWidth); // Place center close to focuspointX but not outside the left side
x = Math.min(productWidth - centerOfRatioArea, x); // Dont place outside the right edge
x = x - centerOfRatioArea; // Convert center to left edge
x = x / productWidth; // Convert to percent
@@ -69,10 +89,13 @@ export const calculateDefaultPositioning = (
// Read comment for crop_x above.
const crop_y = (() => {
if (width_height_ratio < 1 && width_height_ratio > size50x70ratio) {
if (
width_height_ratio < 1 &&
width_height_ratio > betweenSquareAndPortrait
) {
// Square in portrait
const centerOfRatioArea = productWidth / 2;
let y = Math.max(centerOfRatioArea, (fpY / 100) * productHeight); // Place center close to focuspointY but not ouside top edge
let y = Math.max(centerOfRatioArea, fpY * productHeight); // Place center close to focuspointY but not ouside top edge
y = Math.min(productHeight - centerOfRatioArea, y); // Dont place outside bottom edge
y = y - centerOfRatioArea; // Convert center to top edge
y = y / productHeight; // Convert to percent
@@ -80,7 +103,18 @@ export const calculateDefaultPositioning = (
} else if (width_height_ratio <= size50x70ratio) {
// Portrait in portrait
const centerOfRatioArea = productWidth / size50x70ratio / 2;
let y = Math.max(centerOfRatioArea, (fpY / 100) * productHeight); // Place center close to focuspointY but not ouside top edge
let y = Math.max(centerOfRatioArea, fpY * productHeight); // Place center close to focuspointY but not ouside top edge
y = Math.min(productHeight - centerOfRatioArea, y); // Dont place outside bottom edge
y = y - centerOfRatioArea; // Convert center to top edge
y = y / productHeight; // Convert to percent
return y;
} else if (
width_height_ratio >= betweenSquareAndLandscape &&
width_height_ratio < size70x50ratio
) {
// Landscape in landscape with less ratio
const centerOfRatioArea = productWidth / size70x50ratio / 2;
let y = Math.max(centerOfRatioArea, fpY * productHeight); // Place center close to focuspointY but not ouside top edge
y = Math.min(productHeight - centerOfRatioArea, y); // Dont place outside bottom edge
y = y - centerOfRatioArea; // Convert center to top edge
y = y / productHeight; // Convert to percent