javascript - 用元素之间的动画对拖放和连接箭头(或其他任何东西)使用react

标签 javascript reactjs animation drag-and-drop

经过几天的尝试没有成功,我(再次..)来到这里,向你们所有的专家。

首先:现场演示!因为我们都喜欢现场演示。

代码沙盒

https://codesandbox.io/s/drag-with-not-animation-b3eh7?file=/src/App.js

我正在尝试在容器之间制作交互式可拖放箭头(意味着 - 有一个连接到盒子的连接器,您可以将鼠标从一个容器拖动到另一个盒子,然后在它们之间创建一个箭头)。

  • 实现 1:我可以在拖动时获得可拖动箭头的动画 - 但 onDrop 事件不会触发。
  • 实现 2:在第二个实现中,我可以实现下降效果,但不能实现动画。

永远不要他们两个! 帮助! ;<

代码里面有更详细的解释。

我已经尝试过的:

  • react-dnd - 也不起作用,因为它基于与 native 浏览器拖放 API 相同的 DOM 事件系统(或者可能(并且可能)我做错了?)。

最佳答案

给你:

要绘制Xarrow,您需要一个起点和一个终点

  • 起点:始终从
  • 拖动
  • 终点:你要去的地方

这里我们有 2 个 refs ,

  • ref0 : 开始拖动点(这将是盒子)
  • ref1 : 可拖动点(将是可拖动点)

这是需要更改的代码,请同时阅读注释,这将使流程清晰。

const ConnectPointsWrapper = ({ boxId, handler, ref0 }) => {
    const ref1 = useRef();

    const [position, setPosition] = useState({});
    const [beingDragged, setBeingDragged] = useState(false);
    return (
        <React.Fragment>
            <div
                className="connectPoint"
                ref={ref1} // <---- referencing the point
                style={{
                    ...connectPointStyle,
                    ...connectPointOffset[handler],
                    ...position // <----- Updating the position as we drags
                }}
                draggable
                onDragStart={e => {
                    setBeingDragged(true);
                    e.dataTransfer.setData("arrow", boxId);
                }}
                onDrag={e => {
                    setPosition({ // <---- Setting up the position to draw line as we drags
                        position: "fixed",
                        left: e.clientX,
                        top: e.clientY,
                        transform: "none",
                        opacity: 0
                    });
                }}
                onDragEnd={e => {
                    setPosition({});
                    setBeingDragged(false);
                }}
            />
            {beingDragged ? <Xarrow start={ref0} end={ref1} /> : null} // <---- this will draw the arrow b/w ref0 and ref1
        </React.Fragment>
    );
};

const Box = ({ text, handler, addArrow, boxId }) => {
    const ref0 = useRef(); 
    return (
        <div
            id={boxId}
            style={boxStyle}
            ref={ref0} // <---- referencing the box it self
            onDragOver={e => e.preventDefault()}
            onDrop={e => {
                if (e.dataTransfer.getData("arrow") === boxId) {
                    console.log(e.dataTransfer.getData("arrow"), boxId);
                } else {
                    const refs = { start: e.dataTransfer.getData("arrow"), end: boxId };
                    addArrow(refs);
                }
            }}
        >
            {text}
            <ConnectPointsWrapper {...{ boxId, handler, ref0 }} /> // <---- Passing the `ref0` to `ConnectPointsWrapper` to draw line from point
        </div>
    );
};

工作演示:

Edit #SO-drag-anim-done


NOTE :

I was trying to set style just via ref1 and not with setPosition, you can check the below code snippet for that,

<div
        className="connectPoint"
        style={{
          ...connectPointStyle,
          ...connectPointOffset[handler]
        }}
        draggable
        onDragStart={e => {
          setBeingDragged(true);
          e.dataTransfer.setData("arrow", boxId);
        }}
        onDrag={e => {
          setPosition({}); // <---- just to force re-rendering, to draw arrow with updated value
          ref1.current.style.position = "fixed";
          ref1.current.style.left = e.clientX + "px";
          ref1.current.style.top = e.clientY + "px";
          ref1.current.style.transform = "none";
          ref1.current.style.opacity = 0;
        }}
        ref={ref1}
        onDragEnd={e => {
          ref1.current.style.position = "absolute";
          ref1.current.style.left = connectPointOffset[handler].left;
          ref1.current.style.top = connectPointOffset[handler].top;
          ref1.current.style.transform = connectPointOffset[handler].transform;
          ref1.current.style.opacity = 0.5;
          setBeingDragged(false);
        }}
      />

WORKING DEMO : ( this is just another way )

Edit
#SO-drag-anim-optimization


编辑:

工作演示:(也有可拖动框)

Edit drag-anim-with-draggable

关于javascript - 用元素之间的动画对拖放和连接箭头(或其他任何东西)使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62408529/

相关文章:

javascript - 如何在 100% 的 body 上扩展一个子 div 而不是一个具有动态宽度的父 div

javascript - 使用效果及观看状态

iphone - 我想在缩放到新 View 时使用 UIViewAnimationOptionTransitionFlipFromRight

html - 动画返回 css 动画

javascript - 未输入必填字段时 Asp.net 布局中断(搞砸我的布局)

javascript - 检查所有数组项是否具有相同的构造函数 [lodash]

javascript - 网络音频API : frequency related to elapsed time

javascript - 在Javascript中,返回分号 'return;'是什么意思

javascript - 在React.js中从父组件调用子组件函数

Java每0.5秒在动画中 move jlabel