question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: onDragEnd group children coordinates

See original GitHub issue

Thank 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);

Edit charming-morning-pcsvb

But I see some jumping after it. Where is a problem?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lavrtoncommented, Apr 22, 2020

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.

0reactions
lavrtoncommented, Sep 14, 2020

You can just use e.target inside dragend to get access to Konva nodes.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found