Moved draw action helpers into new file to fix import errors with workers

This commit is contained in:
Mitchell McCaffrey 2021-02-04 09:59:03 +11:00
parent fa1f6fe18f
commit 924b3e2481
5 changed files with 41 additions and 42 deletions

View File

@ -1,3 +1,5 @@
// Load Diff for auto complete
// eslint-disable-next-line no-unused-vars
import { Diff } from "deep-diff";
import { diff, revertChanges } from "../helpers/diff";

View File

@ -4,7 +4,7 @@ import Action from "./Action";
import {
addPolygonDifferenceToShapes,
addPolygonIntersectionToShapes,
} from "../helpers/drawing";
} from "../helpers/actions";
class CutShapeAction extends Action {
constructor(shapes) {

View File

@ -1,7 +1,7 @@
import polygonClipping from "polygon-clipping";
import Action from "./Action";
import { addPolygonDifferenceToShapes } from "../helpers/drawing";
import { addPolygonDifferenceToShapes } from "../helpers/actions";
class SubtractShapeAction extends Action {
constructor(shapes) {

37
src/helpers/actions.js Normal file
View File

@ -0,0 +1,37 @@
export function addPolygonDifferenceToShapes(shape, difference, shapes) {
for (let i = 0; i < difference.length; i++) {
let newId = `${shape.id}-dif-${i}`;
// Holes detected
let holes = [];
if (difference[i].length > 1) {
for (let j = 1; j < difference[i].length; j++) {
holes.push(difference[i][j].map(([x, y]) => ({ x, y })));
}
}
shapes[newId] = {
...shape,
id: newId,
data: {
points: difference[i][0].map(([x, y]) => ({ x, y })),
holes,
},
};
}
}
export function addPolygonIntersectionToShapes(shape, intersection, shapes) {
for (let i = 0; i < intersection.length; i++) {
let newId = `${shape.id}-int-${i}`;
shapes[newId] = {
...shape,
id: newId,
data: {
points: intersection[i][0].map(([x, y]) => ({ x, y })),
holes: [],
},
// Default intersection visibility to false
visible: false,
};
}
}

View File

@ -10,7 +10,6 @@ const snappingThreshold = 1 / 5;
export function getBrushPosition(map, mapStage, useGridSnappning, gridSize) {
const mapImage = mapStage.findOne("#mapImage");
let position = getRelativePointerPositionNormalized(mapImage);
if (useGridSnappning) {
// Snap to corners of grid
// Subtract offset to transform into offset space then add it back transform back
@ -20,7 +19,6 @@ export function getBrushPosition(map, mapStage, useGridSnappning, gridSize) {
offset
);
const gridDistance = Vector2.length(Vector2.subtract(gridSnap, position));
// Snap to center of grid
// Subtract offset and half size to transform it into offset half space then transform it back
const halfSize = Vector2.multiply(gridSize, 0.5);
@ -219,44 +217,6 @@ export function simplifyPoints(points, gridSize, scale) {
);
}
export function addPolygonDifferenceToShapes(shape, difference, shapes) {
for (let i = 0; i < difference.length; i++) {
let newId = `${shape.id}-dif-${i}`;
// Holes detected
let holes = [];
if (difference[i].length > 1) {
for (let j = 1; j < difference[i].length; j++) {
holes.push(difference[i][j].map(([x, y]) => ({ x, y })));
}
}
shapes[newId] = {
...shape,
id: newId,
data: {
points: difference[i][0].map(([x, y]) => ({ x, y })),
holes,
},
};
}
}
export function addPolygonIntersectionToShapes(shape, intersection, shapes) {
for (let i = 0; i < intersection.length; i++) {
let newId = `${shape.id}-int-${i}`;
shapes[newId] = {
...shape,
id: newId,
data: {
points: intersection[i][0].map(([x, y]) => ({ x, y })),
holes: [],
},
// Default intersection visibility to false
visible: false,
};
}
}
export function mergeShapes(shapes) {
if (shapes.length === 0) {
return shapes;