【问题标题】:How to make a connector between two stars move when one of a star drag in react js?当一颗星拖入反应js时,如何使两颗星之间的连接器移动?
【发布时间】:2021-08-14 02:30:22
【问题描述】:

伙计们,我不知道如何在两个被拖动的圆圈和一个圆圈之间建立一条连接线。我想一种方法是更新被拖动的圆圈的实际坐标,但我也在努力实现它。谁能帮帮我???

import React from "react";
import { render } from "react-dom";
import { Stage, Layer, Star, Line, Text } from "react-konva";

function generateShapes() {
  return [...Array(10)].map((_, i) => ({
    id: i.toString(),
    x: Math.random() * window.innerWidth,
    y: Math.random() * window.innerHeight,
    rotation: Math.random() * 180,
    isDragging: false
  }));
}

const INITIAL_STATE = generateShapes();

const App = () => {
  const [stars, setStars] = React.useState(INITIAL_STATE);

  const [connectors, setConnectors] = React.useState([]);
  const [fromShapeId, setFromShapeId] = React.useState(null);

  const handleDragStart = (e) => {
    const id = e.target.id();
    setStars(
      stars.map((star) => {
        return {
          ...star,
          isDragging: star.id === id
        };
      })
    );
  };
  const handleDragEnd = (e) => {
    setStars(
      stars.map((star) => {
        return {
          ...star,
          isDragging: false
          // update coordinate of the star
        };
      })
    );
  };

  return (
    <Stage width={window.innerWidth} height={window.innerHeight}>
      <Layer>
        {connectors.map((con) => {
          const from = stars.find((s) => s.id === con.from);
          const to = stars.find((s) => s.id === con.to);

          return (
            <Line
              key={con.id}
              points={[from.x, from.y, to.x, to.y]}
              stroke="black"
            />
          );
        })}

        <Text text="Try to drag a star" />
        {stars.map((star) => (
          <Star
            key={star.id}
            id={star.id}
            x={star.x}
            y={star.y}
            numPoints={5}
            innerRadius={20}
            outerRadius={40}
            fill={fromShapeId === star.id ? "red" : "green"}
            opacity={0.8}
            draggable
            rotation={star.rotation}
            shadowColor="black"
            shadowBlur={10}
            shadowOpacity={0.6}
            shadowOffsetX={star.isDragging ? 10 : 5}
            shadowOffsetY={star.isDragging ? 10 : 5}
            scaleX={star.isDragging ? 1.2 : 1}
            scaleY={star.isDragging ? 1.2 : 1}
            onDragStart={handleDragStart}
            onDragEnd={handleDragEnd}
            onClick={() => {
              if (fromShapeId) {
                const newConnector = {
                  from: fromShapeId,
                  to: star.id,
                  id: connectors.length
                };
                setConnectors(connectors.concat([newConnector]));
                setFromShapeId(null);
              } else {
                setFromShapeId(star.id);
              }
            }}
          />
        ))}
      </Layer>
    </Stage>
  );
};

render(<App />, document.getElementById("root"));

我的第一次尝试是创建一个函数,通过使用 var x = e.clientX 和 var y = e.clientY 创建一个函数,当星星停止被拖动时检测它的位置。但是,它们都是未定义的。我想也许我必须创建某种数组来存储每颗星的坐标并在移动时更新该星的坐标。尽管如此,我真的不知道如何实现代码:/

【问题讨论】:

    标签: reactjs react-hooks konva


    【解决方案1】:

    使用 var x = e.clientX 和 var y = e.clientY 检测星星停止拖动时的位置

    e 变量是原生 DOM 事件的 Konva 包装器。您可以使用 e.evt.clientX 访问基础事件(类似于在 React 合成事件上使用 e.nativeEvent.clientX 的方式)。

    您还可以使用e.target.x() 从Star 对象中获取坐标。


    我不认为onClick 是在拖动星星时使用的正确事件。我修改了您的代码并将setConnectors 调用从onClick 移到onDragEnd 处理程序中。我已经让它工作,以便它通过每个拖动的星星创建一条连续的线。拖放一颗星会导致该星与行中的最后一个星之间出现一条线。

    const handleDragEnd = (e) => {
      setStars(
        stars.map((star) => {
          return {
            ...star,
            isDragging: false,
            // update coordinates of the active star only
            x: star.isDragging ? e.evt.x : star.x,
            y: star.isDragging ? e.evt.y : star.y
          };
        })
      );
      const id = e.target.id();
      if (fromShapeId && fromShapeId !== id) {
        const newConnector = {
          from: fromShapeId,
          to: id,
          id: connectors.length
        };
        setConnectors(connectors.concat([newConnector]));
      }
      setFromShapeId(id);
    };
    

    CodeSandbox Demo

    【讨论】:

    • 非常感谢!!非常感谢:) !!!,顺便说一句,如何实现删除两颗星之间的连接器的方法???
    • 这里的实际最终目标是什么?我不确定你什么时候想画线。什么操作会触发删除行?
    • 抱歉让您感到困惑,我对您的代码做了一些修改:codesandbox.io/s/boring-lederberg-5szvx?file=/src/App.js。我正在努力创建一个函数,只要用连接器单击两个圆圈两次,就会删除两个圆圈之间的连接器。在代码沙箱中,我已经突出显示了所有问题。
    猜你喜欢
    • 2011-01-11
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2020-02-13
    • 2013-10-19
    • 1970-01-01
    相关资源
    最近更新 更多