Git Product home page Git Product logo

Comments (5)

Nick-Lucas avatar Nick-Lucas commented on June 11, 2024 1

Interesting, how would we go about rendering the node on the canvas like this while cursor down? Just concat into the nodes list?

Seems potentially quite render intensive to have reactflow re-render the entire list of nodes for every frame of the drag?

I will definitely have a try and report back here, just couldn't find any references to a solution to this so best case we just document a solution in this issue!

from xyflow.

Nick-Lucas avatar Nick-Lucas commented on June 11, 2024 1

Okay yep you're right @peterkogo this is possible in userland.

there are some weird visual glitches, not sure what that's about but seemingly canvas related? (see the persistent red lines that stick around sometimes)

Apr-10-2024 22-16-51

Not sure how well it would respond to a more expensive nodes list given this may force ReactFlow to do quite a bit of rendering internally, but it definitely works :)

Key gubbins from my prototyping:

const reactFlowInstance = useReactFlow() // obviously need to wrap the component in ReactFlowProvider to hoist this value upward
const [drawing, setDrawing] = useState<false | { x: number; y: number }>(
  false,
)
const [drawingPosition, setDrawingPosition] = useState<
  false | { x: number; y: number }
>(false)

return <ReactFlow
  nodes={
    drawing && drawingPosition
      ? nodes.concat({
          id: 'DRAWING',
          type: 'box',
          position: {
            x: Math.min(drawingPosition.x, drawing.x),
            y: Math.min(drawingPosition.y, drawing.y),
          },
          data: { },
          style: {
            width: Math.abs(drawingPosition.x - drawing.x),
            height: Math.abs(drawingPosition.y - drawing.y),
          },
        })
      : nodes
  }
  selectionOnDrag={false}
  onPointerDown={(e) => {
    const element = e.target as HTMLElement | undefined
    if (element?.className.includes('react-flow__pane')) {
      const x = e.clientX
      const y = e.clientY

      const position = reactFlowInstance.screenToFlowPosition({
        x,
        y,
      })

      setDrawing(position)
      setDrawingPosition(position)

      console.log('DOWN', e.target, e.clientX, e.clientY)
    }
  }}
  onPointerLeave={() => {
    setDrawing(false)
    setDrawingPosition(false)
  }}
  onPointerMove={(e) => {
    if (drawing) {
      const x = e.clientX
      const y = e.clientY

      const position = reactFlowInstance.screenToFlowPosition({
        x,
        y,
      })

      setDrawingPosition(position)
    }
  }}
  onPointerUp={(e) => {
    console.log('UP', e.target, e.clientX, e.clientY)
    setDrawing(false)
    setDrawingPosition(false)
  }}
/>

from xyflow.

peterkogo avatar peterkogo commented on June 11, 2024 1

As an addition you can also just add a CustomNode onPointerDown, and have an absolutely positioned child of the CustomNode that you resize. This should not trigger dimension updates inside ReactFlow and might be a bit more efficient.

You can also render a custom component inside the viewport through the portal. (there is an extra ViewportPortal you can use in v12 for this, but it is essentially the same thing)
With this approach you can bail out of react flows logic while resizing.

from xyflow.

peterkogo avatar peterkogo commented on June 11, 2024

Why is this currently not achievable in user-land?
If you set panOnDrag to false whenever your node creation is happening and start listening to pointerdown & pointermove events on the React Flow dom node you should be able to achieve this

from xyflow.

Nick-Lucas avatar Nick-Lucas commented on June 11, 2024

Thanks for the extra details! Those do both sound like better options. Looking forward to v12 as there are so many great improvements there! 🙂

from xyflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.