fixed calculations for poster defaults, added tests (#102)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
preset: 'ts-jest',
|
preset: 'ts-jest',
|
||||||
testEnvironment: 'node',
|
testEnvironment: 'node',
|
||||||
|
verbose: true,
|
||||||
};
|
};
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -19,18 +19,26 @@ export const calculateDefaultPositioning = (
|
|||||||
): TPosterDefaultPositioning => {
|
): TPosterDefaultPositioning => {
|
||||||
// Because we dont want to be dependent on different environments
|
// Because we dont want to be dependent on different environments
|
||||||
// number of supported decimals to round to 10 decimal places.
|
// 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 size70x50ratio = parseFloat((70 / 50).toFixed(10));
|
||||||
const size50x70ratio = parseFloat((50 / 70).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;
|
const width_height_ratio = productWidth / productHeight;
|
||||||
|
|
||||||
// If focuspoints are null we set to 50 to make calculations
|
// If focuspoints are null we set to 50 to make calculations
|
||||||
// below a bit clearer.
|
// below a bit clearer.
|
||||||
const fpX = focusXpoint2 === null ? 50 : focusXpoint2;
|
const fpX = (focusXpoint2 === null ? 50 : focusXpoint2) / 100;
|
||||||
const fpY = focusYpoint2 === null ? 50 : focusYpoint2;
|
const fpY = (focusYpoint2 === null ? 50 : focusYpoint2) / 100;
|
||||||
|
|
||||||
const width_mm = width_height_ratio >= size70x50ratio ? 700 : 500;
|
const width_mm = width_height_ratio >= betweenSquareAndLandscape ? 700 : 500;
|
||||||
const height_mm = width_height_ratio <= size50x70ratio ? 700 : 500;
|
const height_mm = width_height_ratio <= betweenSquareAndPortrait ? 700 : 500;
|
||||||
|
|
||||||
// Calculate where to put the crop_x percent value
|
// Calculate where to put the crop_x percent value
|
||||||
// We place the center of the crop area as close to
|
// 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
|
// The goal for the calculations is to get the smallest amount of area
|
||||||
// that we crop away.
|
// that we crop away.
|
||||||
const crop_x = (() => {
|
const crop_x = (() => {
|
||||||
|
// Landscape in landscape
|
||||||
if (width_height_ratio >= size70x50ratio) {
|
if (width_height_ratio >= size70x50ratio) {
|
||||||
const centerOfRatioArea = (productHeight * 1.4) / 2;
|
const centerOfRatioArea = (productHeight * size70x50ratio) / 2;
|
||||||
let x = Math.max(centerOfRatioArea, (fpX / 100) * productWidth); // Place center close to focuspointX but not outside the left edge
|
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 = Math.min(productWidth - centerOfRatioArea, x); // Dont place outside the right edge
|
||||||
x = x - centerOfRatioArea; // Convert center to left edge
|
x = x - centerOfRatioArea; // Convert center to left edge
|
||||||
x = x / productWidth; // Convert to percent;
|
x = x / productWidth; // Convert to percent;
|
||||||
@@ -52,10 +72,10 @@ export const calculateDefaultPositioning = (
|
|||||||
} else if (
|
} else if (
|
||||||
// Square in landscape
|
// Square in landscape
|
||||||
width_height_ratio > 1 &&
|
width_height_ratio > 1 &&
|
||||||
width_height_ratio < size70x50ratio
|
width_height_ratio < betweenSquareAndLandscape
|
||||||
) {
|
) {
|
||||||
const centerOfRatioArea = productHeight / 2;
|
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 = Math.min(productWidth - centerOfRatioArea, x); // Dont place outside the right edge
|
||||||
x = x - centerOfRatioArea; // Convert center to left edge
|
x = x - centerOfRatioArea; // Convert center to left edge
|
||||||
x = x / productWidth; // Convert to percent
|
x = x / productWidth; // Convert to percent
|
||||||
@@ -69,10 +89,13 @@ export const calculateDefaultPositioning = (
|
|||||||
|
|
||||||
// Read comment for crop_x above.
|
// Read comment for crop_x above.
|
||||||
const crop_y = (() => {
|
const crop_y = (() => {
|
||||||
if (width_height_ratio < 1 && width_height_ratio > size50x70ratio) {
|
if (
|
||||||
|
width_height_ratio < 1 &&
|
||||||
|
width_height_ratio > betweenSquareAndPortrait
|
||||||
|
) {
|
||||||
// Square in portrait
|
// Square in portrait
|
||||||
const centerOfRatioArea = productWidth / 2;
|
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 = Math.min(productHeight - centerOfRatioArea, y); // Dont place outside bottom edge
|
||||||
y = y - centerOfRatioArea; // Convert center to top edge
|
y = y - centerOfRatioArea; // Convert center to top edge
|
||||||
y = y / productHeight; // Convert to percent
|
y = y / productHeight; // Convert to percent
|
||||||
@@ -80,7 +103,18 @@ export const calculateDefaultPositioning = (
|
|||||||
} else if (width_height_ratio <= size50x70ratio) {
|
} else if (width_height_ratio <= size50x70ratio) {
|
||||||
// Portrait in portrait
|
// Portrait in portrait
|
||||||
const centerOfRatioArea = productWidth / size50x70ratio / 2;
|
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 = Math.min(productHeight - centerOfRatioArea, y); // Dont place outside bottom edge
|
||||||
y = y - centerOfRatioArea; // Convert center to top edge
|
y = y - centerOfRatioArea; // Convert center to top edge
|
||||||
y = y / productHeight; // Convert to percent
|
y = y / productHeight; // Convert to percent
|
||||||
|
|||||||
Reference in New Issue
Block a user