Question: onDragEnd group children coordinates
See original GitHub issueThank you for perfect library. I have some troubles with group with Rect children in it. I want to update coordinates of rects in onDragEnd group function:
import React, { useState } from "react";
import ReactDOM from "react-dom";
import { Stage, Layer, Group, Rect } from "react-konva";
import "./styles.css";
const RECTS = [
{ x: 190, y: 220 },
{ x: 200, y: 130 },
{ x: 260, y: 130 },
{ x: 260, y: 220 }
];
const DOT_LENGTH = 15;
function App() {
const [rects, setRects] = useState(RECTS);
const [group, setGroup] = useState({ x: 0, y: 0 });
const onDragEnd = e => {
const group = e.target;
const newRects = [...rects].map((item, index) => {
const rect = group.findOne(node => {
return node.className === "Rect" && node.name() === `${index}`;
});
const { x, y } = rect.getAbsolutePosition();
// let x = rect.x(); let y = rect.y();
// console.log(rect.getAbsolutePosition())
return { x, y };
});
setRects(newRects);
setGroup({ x: group.x(), y: group.y() });
};
return (
<div className="App">
<Stage width={1000} height={1000}>
<Layer>
<Group x={group.x} y={group.y} onDragEnd={onDragEnd} draggable>
{rects.map((item, index) => (
<Rect
offsetX={DOT_LENGTH / 2}
offsetY={DOT_LENGTH / 2}
x={item.x}
y={item.y}
key={index}
name={`${index}`}
stroke="#000"
fill="#000"
strokeWidth={0}
width={DOT_LENGTH}
height={DOT_LENGTH}
/>
))}
</Group>
</Layer>
</Stage>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
But I see some jumping after it. Where is a problem?
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Get X and Y of a shape inside a group after dragging in Konva ...
First thing that stands out is you are using mouseout as the dropped according to the documentation there is a dragend , I...
Read more >HTML5 Canvas Drag and Drop Events | Konva - Konva
I have a scenario where -- on dragmove mode- group on moving across fixed X-coordinate group.children[i].fontSize() changes, But the group.position is OFF with ......
Read more >Draggable and resetting to original x/y coordinates? - GSAP
What is the best way to position a Draggable to a x/y coord within its bounds element? Or I suppose the question could...
Read more >505521 - Set screen coordinates during HTML5 drag event
As an special exception, the screen coordinates where the drop occurred are available during the dragend event. I don't see a problem retrieving...
Read more >ondragover Event - W3Schools
Note: While dragging an element, the ondrag event fires every 350 milliseconds. On the Drop Target: Event, Occurs When. ondragenter, A dragged element...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sure. It really depends how you store you data and how you structure your components.
You can use
node.getAbsolutePosition()
to know where a node is after parent drag. Or just calculate it manually.You can just use
e.target
insidedragend
to get access to Konva nodes.