【问题标题】:Refreshing Konva shape state in Vue component刷新 Vue 组件中的 Konva 形状状态
【发布时间】:2020-03-22 13:34:56
【问题描述】:

拖动并释放一个形状后,我希望它捕捉到一个接近的位置。为了测试这一点,我在{x:100, y:100} 创建了一个形状,然后将其拖动到0,0,但只是第一次拖动它。下次它会忽略我设置x,y

我可能在这里遗漏了一些基本的东西?也许我没有以正确的方式改变商店。在下面的代码中,您可以看到在handleDragend 中设置 x 和 y 的三次尝试。

<template>
  <div>
    <v-stage
      ref="stage"
      :config="configKonva"
      @dragstart="handleDragstart"
      @dragend="handleDragend"
    >
      <v-layer ref="layer">
        <v-regular-polygon
          v-for="item in list"
          :key="item.id"        
          :config="{  
            x: item.x,
            y: item.y,
            sides: 6,
            rotation: item.rotation,
            id: item.id,
            radius: 50,
            outerRadius: 50,
            fill: 'green',
            draggable: true,
          }"
        ></v-regular-polygon>
      </v-layer>
    </v-stage>
  </div>
</template>

<script>

const width = window.innerWidth;
const height = window.innerHeight;

export default {
  data() {
    return {
      list: [],
      dragItemId: null,
      configKonva: {
        width: width,
        height: height,       
      }
    };
  },
  methods: {
    handleDragstart(e) {
        //
    },
    handleDragend(e) {
        let item = this.list.find(i => i.id === e.target.id());

        let snapTo = { x: 0, y: 0}

        // Attempt 1
        Vue.set(this.list, 0, {
            ...item,
            x: snapTo.x,
            y: snapTo.y,
        })

        // Attempt 2    
        this.list = this.list.map(function(shape) {
            if(shape.id === item.id) {
                return {
                    ...item,
                    x: snapTo.x,
                    y: snapTo.y,
                }
            }
        })
    },
  },

  mounted() {
    this.list.push({
        id: 1,
        x: 100,
        y: 100,
    });
  }
};
</script>

【问题讨论】:

    标签: vue.js konvajs konva


    【解决方案1】:

    vue-konva 仅在您的模板发生更改时更新节点。

    在第一个快照中,模板(和存储)中的坐标从 {100, 100} 更改为 {0, 0}

    当您第二次拖动节点时,存储区仍将{0, 0} 保留在内存中。因此不会触发任何更改,节点也不会向后移动。

    有两种方法可以解决这个问题:

    (1) 手动更新Konva节点位置

    handleDragend(e) {
          let item = this.list.find(i => i.id === e.target.id());
    
          let snapTo = { x: 0, y: 0 };
          e.target.position(snapTo);
          e.target.getLayer().batchDraw();
          Vue.set(this.list, 0, {
            ...item,
            x: snapTo.x,
            y: snapTo.y
          });
    }
    

    (2) 使存储与节点位置保持同步

    您可能需要将所有位置更改注册到存储中:

    handleDragMove(e) {
          // do this on every "dragmove"
          let item = this.list.find(i => i.id === e.target.id());
    
          Vue.set(this.list, 0, {
            ...item,
            x: e.target.x(),
            y: e.target.y()
          });
    }
    

    演示:https://codesandbox.io/s/nifty-northcutt-v52ue

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 2019-08-28
      • 1970-01-01
      • 2022-07-16
      • 1970-01-01
      • 2019-08-06
      • 2019-03-13
      • 2019-12-26
      • 1970-01-01
      相关资源
      最近更新 更多