Fix selection from right to left

This commit is contained in:
Christopher Chedeau 2020-01-01 19:53:22 -08:00
parent aeb11989e3
commit 48d9147d87

View File

@ -105,12 +105,25 @@ function generateShape(element) {
} }
function setSelection(selection) { function setSelection(selection) {
// Fix up negative width and height when dragging from right to left
// Note: it's a lot harder to do on mouse move because of rounding issues
let { x, y, width, height } = selection;
if (width < 0) {
x += width;
width = -width;
}
if (height < 0) {
y += height;
height = -height;
}
elements.forEach(element => { elements.forEach(element => {
element.isSelected = element.isSelected =
selection.x < element.x && element.type !== "selection" &&
selection.y < element.y && x <= element.x &&
selection.x + selection.width > element.x + element.width && y <= element.y &&
selection.y + selection.height > element.y + element.height; x + width >= element.x + element.width &&
y + height >= element.y + element.height;
}); });
} }
@ -148,8 +161,10 @@ function App() {
} }
return ( return (
<div> <div>
{/* If using a component, dragging on the canvas also selects the label text which is annoying. {/* Can't use the <ElementOption> form because ElementOption is re-defined
Not sure why that's happening */} on every render, which would blow up and re-create the entire DOM tree,
which in addition to being inneficient, messes up with browser text
selection */}
{ElementOption({ type: "rectangle", children: "Rectangle" })} {ElementOption({ type: "rectangle", children: "Rectangle" })}
{ElementOption({ type: "ellipse", children: "Ellipse" })} {ElementOption({ type: "ellipse", children: "Ellipse" })}
{ElementOption({ type: "arrow", children: "Arrow" })} {ElementOption({ type: "arrow", children: "Arrow" })}
@ -197,6 +212,16 @@ function App() {
elements.pop(); elements.pop();
setSelection(draggingElement); setSelection(draggingElement);
} }
// Fix up negative width and height when dragging from right to left
// Note: it's a lot harder to do on mouse move because of rounding issues
if (draggingElement.width < 0) {
draggingElement.x += draggingElement.width;
draggingElement.width = -draggingElement.width;
}
if (draggingElement.height < 0) {
draggingElement.y += draggingElement.height;
draggingElement.height = -draggingElement.height;
}
drawScene(); drawScene();
}} }}
onMouseMove={e => { onMouseMove={e => {