diff --git a/src/utils/getGrid.js b/src/utils/getGrid.js index db1dee5..39f1e2e 100644 --- a/src/utils/getGrid.js +++ b/src/utils/getGrid.js @@ -43,7 +43,7 @@ const processSteps = (steps) => } }) -const insertGutters = (steps, gutter = 0) => { +const insertGutters = (steps, gutter) => { const lastStepIndex = steps.length - 1 const hasGutter = gutter > 0 return steps.reduce((acc, step, idx) => { @@ -95,18 +95,33 @@ const stepIsNotGutter = (step) => !step.isGutter // ----------------------------------------------------------------------------- const getGrid = (boundingCurves, grid) => { + // Don't destructure arg so we can pass it around as is + const gridWithDefaults = { + ...{ + gutter: 0, + interpolationStrategy: INTERPOLATION_STRATEGY_ID.EVEN, + precision: 20, + lineStrategy: LINE_STRATEGY_ID.STRAIGHT_LINES, + }, + ...grid, + } + console.log('BOUNDS', boundingCurves) + console.log('GRID', gridWithDefaults) + validateBoundingCurves(boundingCurves) - validateGrid(grid) + validateGrid(gridWithDefaults) - const { gutter } = grid + const { gutter } = gridWithDefaults - const [columns, rows] = [grid.columns, grid.rows].map(prepareSteps(gutter)) + const [columns, rows] = [gridWithDefaults.columns, gridWithDefaults.rows].map( + prepareSteps(gutter) + ) // Choose the function to use for interpolating the location of a point on a // curve. - const interpolatePointOnCurve = getInterpolationStrategy(grid) + const interpolatePointOnCurve = getInterpolationStrategy(gridWithDefaults) - const [getLineOnXAxis, getLineOnYAxis] = getLineStrategy(grid) + const [getLineOnXAxis, getLineOnYAxis] = getLineStrategy(gridWithDefaults) const getPoint = memoize((ratioX, ratioY) => { validateGetPointArguments(ratioX, ratioY) @@ -153,13 +168,14 @@ const getGrid = (boundingCurves, grid) => { const { xAxis, yAxis } = getLines() - const g = grid.gutter > 0 ? 2 : 1 + // If there is a gutter, we need to skip over the gutter space + const gutterMultiplier = gutter > 0 ? 2 : 1 return { - top: xAxis[y * g][x], - bottom: xAxis[y * g + 1][x], - left: yAxis[x * g][y], - right: yAxis[x * g + 1][y], + top: xAxis[y * gutterMultiplier][x], + bottom: xAxis[y * gutterMultiplier + 1][x], + left: yAxis[x * gutterMultiplier][y], + right: yAxis[x * gutterMultiplier + 1][y], } }) diff --git a/src/utils/interpolate/even.js b/src/utils/interpolate/even.js index 62fa344..5ecf5c2 100644 --- a/src/utils/interpolate/even.js +++ b/src/utils/interpolate/even.js @@ -4,12 +4,6 @@ import { getDistanceBetweenPoints, roundTo10 } from '../math' import { validateT } from '../validation' import { interpolatePointOnCurveLinear } from './linear' -// ----------------------------------------------------------------------------- -// Const -// ----------------------------------------------------------------------------- - -const DEFAULT_PRECISION = 20 - // ----------------------------------------------------------------------------- // Utils // ----------------------------------------------------------------------------- @@ -64,7 +58,7 @@ export const interpolatePointOnCurveEvenlySpaced = ( // Get an approximation using an arbitrary number of points. Increase for // more accuracy at cost of performance - { precision = DEFAULT_PRECISION } = {} + { precision } = {} ) => (t, curve) => { // Round the ratio to 10 decimal places to avoid rounding issues where the diff --git a/src/utils/interpolate/linear.js b/src/utils/interpolate/linear.js index a1822ca..584d4c8 100644 --- a/src/utils/interpolate/linear.js +++ b/src/utils/interpolate/linear.js @@ -13,12 +13,12 @@ const lerpPoint = (point1, point2, t) => { return { x: lerp(point1.x, point2.x, t), y: lerp(point1.y, point2.y, t) } } -// Alternativly: Bernstein polynomials +// Alternativly we could use Bernstein polynomials for interpolation // return ( -// startPoint[coordinate] * (-tCubed + 3 * tSquared - 3 * t + 1) + -// controlPoint1[coordinate] * (3 * tCubed - 6 * tSquared + 3 * t) + -// controlPoint2[coordinate] * (-3 * tCubed + 3 * tSquared) + -// endPoint[coordinate] * tCubed +// startPoint[coordinateName] * (-tCubed + 3 * tSquared - 3 * t + 1) + +// controlPoint1[coordinateName] * (3 * tCubed - 6 * tSquared + 3 * t) + +// controlPoint2[coordinateName] * (-3 * tCubed + 3 * tSquared) + +// endPoint[coordinateName] * tCubed // ) const interpolate = ( t, diff --git a/src/utils/validation.js b/src/utils/validation.js index 74fd537..dd3d159 100644 --- a/src/utils/validation.js +++ b/src/utils/validation.js @@ -86,10 +86,6 @@ export const validateBoundingCurves = (boundingCurves) => { } export const validateGrid = (grid) => { - if (isNil(grid)) { - throw new Error('You must supply a grid(Object)') - } - const { rows, columns, interpolationStrategy, lineStrategy, precision } = grid if (isNil(columns)) { diff --git a/tests/getGrid.unit.test.js b/tests/getGrid.unit.test.js index 01dd50d..c6feafd 100644 --- a/tests/getGrid.unit.test.js +++ b/tests/getGrid.unit.test.js @@ -73,12 +73,6 @@ describe(`getGrid`, () => { }) describe(`grid`, () => { - it(`throws if no grid is supplied`, () => { - expect(() => { - getGrid(boundsValid) - }).toThrow('You must supply a grid(Object)') - }) - describe(`columns and rows`, () => { it(`throws if no columns are supplied`, () => { expect(() => {