javascript - 使用 react-konva 捕捉网格,拖放

我正在使用 React-Konva 创建一个 RPG 桌面游戏。我通过拖放创建了一个无限网格和一些图像标记。

所以,它工作正常。但是,在寻找另一个功能时,我使用 Canvas 发现了这个功能,它几乎与我所做的一样,但是使用 spap,形状像磁性一样被吸引到 Rect 上!

举个例子: Snap with Canvas
Snap with Konva.js

for (var i = 0; i < (600 / grid); i++) {
  canvas.add(new fabric.Line([ i * grid, 0, i * grid, 600], { stroke: '#ccc', selectable: false }));
  canvas.add(new fabric.Line([ 0, i * grid, 600, i * grid], { stroke: '#ccc', selectable: false }))
} 

有一种方法可以对 React-Konva 做同样的事情吗?我发现了与 Konva.js 类似的东西

已编辑:

我快到了!!!

const [x, setX] = useState(50)
const [y, setY] = useState(50)

const grid = 70
const gridWidth = 1100

const linesA = []
const linesB = []

  for (let i = 0; i < gridWidth / grid; i++) {
    linesA.push(
      <Line
        strokeWidth={2}
        stroke={'black'}
        points={[i * grid, 0, i * grid, gridWidth]}
      />
    )

    linesB.push(
      <Line
        strokeWidth={2}
        stroke={'black'}
        points={[0, i * grid, gridWidth, i * grid]}
      />
    )
  }

  <Layer>
    {linesA}
    {linesB}        
  </Layer>

我用线条制作了网格,现在我对 dragEnd 事件 X、Y 有点困惑。

我的选择

  <Rect
        onDragEnd={e => {
          e.target.to({
            x: Math.round(x / grid) * grid,
            y: Math.round(y / grid) * grid,
          })
        }}
        x={x}
        y={y}
        draggable
        width={60}
        height={60}
        fill="rgba(0, 0, 0, 1)"
      />

真的快到了。

最佳答案

完成了!我会发布我自己的答案来帮助别人。

const grid = 70
const gridWidth = 1000

const linesA = []
const linesB = []

  for (let i = 0; i < gridWidth / grid; i++) {
    linesA.push(
      <Line
        strokeWidth={2}
        stroke={'black'}
        points={[i * grid, 0, i * grid, gridWidth]}
      />
    )

    linesB.push(
      <Line
        strokeWidth={2}
        stroke={'black'}
        points={[0, i * grid, gridWidth, i * grid]}
      />
    )
  }

  <Stage
        x={stagePos.x}
        y={stagePos.y}
        width={window.innerWidth}
        height={window.innerHeight}
        draggable
        onDragEnd={e => {
          setStagePos(e.currentTarget.position())
        }}
        onMouseDown={handleMouseDown}
        onMouseUp={handleMouseUp}
        onMouseMove={handleMouseMove}
        onContextMenu={e => {
          e.evt.preventDefault()
        }}
      >
        {/* <Layer>
          <Image image={map} opacity={1} />
        </Layer> */}

        <Layer>
          {linesA}
          {linesB}
        </Layer>

       <Rect
            onDragEnd={e => {
              e.target.to({
                x: Math.round(e.target.x() / grid) * grid,
                y: Math.round(e.target.y() / grid) * grid,
              })
            }}
            x={x}
            y={y}
            draggable
            width={65}
            height={65}
            fill="rgba(0, 0, 0, 1)"
          />

 </Stage>

我在这里做了一个活生生的例子! Snap Grid React-Konva

https://stackoverflow.com/questions/63687603/

相关文章:

r - separate_rows 在结果周围生成引号

r - 使用 dplyr::mutate(across()) 将多列应用于自定义函数

python - 在列表字典中查找最大列表范围的更好(更简洁)方法是什么

docker - 如何查看在我的 Google Cloud Platform Cloud Run 服

python - Python 中的日期字符串格式化

python - 类型错误 : request() missing 1 required posit

c# - 使用 ConcurrentBag 的并行 ForEach 未按预期工作

vue.js - 将插槽从 Vue 2 迁移到 Vue 3

c - main的地址是什么?

r - 如何通过提取将列拆分为两列?