0

所以,我一直在使用 vue-konva,我有这样的事情:

<v-container>
    <v-stage ref="stage">
        <v-layer ref="baseImage">
            <v-image>
        </v-layer>
        <v-layer ref="annotationLayer">
            <v-rect ref="eventBox">
            <v-rect ref="rubberBox">
            <v-rect ref="annotationRect">
        </v-layer>
    </v-stage>
<v-container>

目前,如果我想绘制新框,当图像上已有其他 annotationRects 时,会出现一些问题。因为它们在技术上位于 eventBox 和 Rubberbox 之上,所以当光标位于现有 annotationRect 之上时,它们会“阻止”这两个层。

但是,我不想总是让 eventBox 和 RubberBox 在 annotationRect 之上,因为我需要能够与 annotationRect 交互以移动它们、调整它们的大小等。

有没有办法让我重新排序 eventBox、rubberBox 和 annotationRect,即将顺序从原始 eventBox-rubberBox-annotationRect 更改为(从下到上)annotationRect-eventBox-rubberBox 并返回,例如当 vue组件从另一个组件接收事件?

4

1 回答 1

1

您需要在应用程序的状态下定义您的eventBoxrubberBoxannotationRect内部订单数组。然后你可以使用v-for指令来渲染数组中的项目:

<template>
  <div>
    <v-stage ref="stage" :config="stageSize" @click="changeOrder">
      <v-layer>
        <v-text :config="{text: 'Click me to change order', fontSize: 15}"/>
        <v-rect v-for="item in items" v-bind:key="item.id" :config="item"/>
      </v-layer>
      <v-layer ref="dragLayer"></v-layer>
    </v-stage>
  </div>
</template>

<script>
const width = window.innerWidth;
const height = window.innerHeight;
export default {
  data() {
    return {
      stageSize: {
        width: width,
        height: height
      },
      items: [
        { id: 1, x: 10, y: 50, width: 100, height: 100, fill: "red" },
        { id: 2, x: 50, y: 70, width: 100, height: 100, fill: "blue" }
      ]
    };
  },
  methods: {
    changeOrder() {
      const first = this.items[0];
      // remove first item:
      this.items.splice(0, 1);
      // add it to the top:
      this.items.push(first);
    }
  }
};
</script>

演示: https ://codesandbox.io/s/vue-konva-list-render-l70vs ?file=/src/App.vue

于 2020-04-22T18:44:54.207 回答