Add selection (#1)
This commit is contained in:
parent
6278cd9366
commit
47e626f510
22
src/index.js
22
src/index.js
@ -12,7 +12,8 @@ function newElement(type, x, y) {
|
|||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0
|
height: 0,
|
||||||
|
isSelected: false
|
||||||
};
|
};
|
||||||
generateShape(element);
|
generateShape(element);
|
||||||
return element;
|
return element;
|
||||||
@ -142,6 +143,11 @@ function App() {
|
|||||||
}}
|
}}
|
||||||
onMouseUp={e => {
|
onMouseUp={e => {
|
||||||
setDraggingElement(null);
|
setDraggingElement(null);
|
||||||
|
if (elementType === "selection") {
|
||||||
|
elements.forEach(element => {
|
||||||
|
element.isSelected = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
drawScene();
|
drawScene();
|
||||||
}}
|
}}
|
||||||
onMouseMove={e => {
|
onMouseMove={e => {
|
||||||
@ -152,6 +158,18 @@ function App() {
|
|||||||
// Make a perfect square or circle when shift is enabled
|
// Make a perfect square or circle when shift is enabled
|
||||||
draggingElement.height = e.shiftKey ? width : height;
|
draggingElement.height = e.shiftKey ? width : height;
|
||||||
generateShape(draggingElement);
|
generateShape(draggingElement);
|
||||||
|
|
||||||
|
if (elementType === "selection") {
|
||||||
|
elements.forEach(element => {
|
||||||
|
element.isSelected =
|
||||||
|
draggingElement.x <= element.x &&
|
||||||
|
draggingElement.y <= element.y &&
|
||||||
|
draggingElement.x + draggingElement.width >=
|
||||||
|
element.x + element.width &&
|
||||||
|
draggingElement.y + draggingElement.height >=
|
||||||
|
element.y + element.height;
|
||||||
|
});
|
||||||
|
}
|
||||||
drawScene();
|
drawScene();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -170,7 +188,7 @@ function drawScene() {
|
|||||||
|
|
||||||
elements.forEach(element => {
|
elements.forEach(element => {
|
||||||
element.draw(rc, context);
|
element.draw(rc, context);
|
||||||
if (true || element.isSelected) {
|
if (element.isSelected) {
|
||||||
const margin = 4;
|
const margin = 4;
|
||||||
context.setLineDash([8, 4]);
|
context.setLineDash([8, 4]);
|
||||||
context.strokeRect(
|
context.strokeRect(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user