Proper fix for negative width
This commit is contained in:
parent
3c8eb862f5
commit
4076cf003f
68
src/index.js
68
src/index.js
@ -104,26 +104,39 @@ function generateShape(element) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setSelection(selection) {
|
// If the element is created from right to left, the width is going to be negative
|
||||||
// Fix up negative width and height when dragging from right to left
|
// This set of functions retrieves the absolute position of the 4 points.
|
||||||
// Note: it's a lot harder to do on mouse move because of rounding issues
|
// We can't just always normalize it since we need to remember the fact that an arrow
|
||||||
let { x, y, width, height } = selection;
|
// is pointing left or right.
|
||||||
if (width < 0) {
|
function getElementAbsoluteX1(element) {
|
||||||
x += width;
|
return element.width >= 0 ? element.x : element.x + element.width;
|
||||||
width = -width;
|
}
|
||||||
}
|
function getElementAbsoluteX2(element) {
|
||||||
if (height < 0) {
|
return element.width >= 0 ? element.x + element.width : element.x;
|
||||||
y += height;
|
}
|
||||||
height = -height;
|
function getElementAbsoluteY1(element) {
|
||||||
}
|
return element.height >= 0 ? element.y : element.y + element.height;
|
||||||
|
}
|
||||||
|
function getElementAbsoluteY2(element) {
|
||||||
|
return element.height >= 0 ? element.y + element.height : element.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setSelection(selection) {
|
||||||
|
const selectionX1 = getElementAbsoluteX1(selection);
|
||||||
|
const selectionX2 = getElementAbsoluteX2(selection);
|
||||||
|
const selectionY1 = getElementAbsoluteY1(selection);
|
||||||
|
const selectionY2 = getElementAbsoluteY2(selection);
|
||||||
elements.forEach(element => {
|
elements.forEach(element => {
|
||||||
|
const elementX1 = getElementAbsoluteX1(element);
|
||||||
|
const elementX2 = getElementAbsoluteX2(element);
|
||||||
|
const elementY1 = getElementAbsoluteY1(element);
|
||||||
|
const elementY2 = getElementAbsoluteY2(element);
|
||||||
element.isSelected =
|
element.isSelected =
|
||||||
element.type !== "selection" &&
|
element.type !== "selection" &&
|
||||||
x <= element.x &&
|
selectionX1 <= elementX1 &&
|
||||||
y <= element.y &&
|
selectionY1 <= elementY1 &&
|
||||||
x + width >= element.x + element.width &&
|
selectionX2 >= elementX2 &&
|
||||||
y + height >= element.y + element.height;
|
selectionY2 >= elementY2;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,16 +244,6 @@ 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 => {
|
||||||
@ -277,13 +280,18 @@ function drawScene() {
|
|||||||
element.draw(rc, context);
|
element.draw(rc, context);
|
||||||
if (element.isSelected) {
|
if (element.isSelected) {
|
||||||
const margin = 4;
|
const margin = 4;
|
||||||
|
|
||||||
|
const elementX1 = getElementAbsoluteX1(element);
|
||||||
|
const elementX2 = getElementAbsoluteX2(element);
|
||||||
|
const elementY1 = getElementAbsoluteY1(element);
|
||||||
|
const elementY2 = getElementAbsoluteY2(element);
|
||||||
const lineDash = context.getLineDash();
|
const lineDash = context.getLineDash();
|
||||||
context.setLineDash([8, 4]);
|
context.setLineDash([8, 4]);
|
||||||
context.strokeRect(
|
context.strokeRect(
|
||||||
element.x - margin,
|
elementX1 - margin,
|
||||||
element.y - margin,
|
elementY1 - margin,
|
||||||
element.width + margin * 2,
|
elementX2 - elementX1 + margin * 2,
|
||||||
element.height + margin * 2
|
elementY2 - elementY1 + margin * 2
|
||||||
);
|
);
|
||||||
context.setLineDash(lineDash);
|
context.setLineDash(lineDash);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user