From 651b0d7c6cee66ff349f2689987e05e761b4cf5c Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Sat, 2 Jan 2021 18:01:16 +1100 Subject: [PATCH] Added konva array convert helper --- src/helpers/konva.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/helpers/konva.js b/src/helpers/konva.js index bfa602d..227a2cc 100644 --- a/src/helpers/konva.js +++ b/src/helpers/konva.js @@ -243,3 +243,17 @@ export function getRelativePointerPositionNormalized(node) { y: relativePosition.y / node.height(), }; } + +/** + * Converts points from alternating array form to vector array form + * @param {number[]} points points in an x, y alternating array + * @returns {Vector2[]} a `Vector2` array + */ +export function convertPointArray(points) { + return points.reduce((acc, _, i, arr) => { + if (i % 2 === 0) { + acc.push({ x: arr[i], y: arr[i + 1] }); + } + return acc; + }, []); +}