【问题标题】:Vue-Konva: Is there a way to reorder layers on the fly?Vue-Konva:有没有办法即时重新排序图层?
【发布时间】:2020-04-22 07:21:52
【问题描述】:

所以,我一直在使用 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 组件接收到另一个组件的事件时?

【问题讨论】:

  • 您可以为此使用 Konva z-index 属性。见konvajs.org/docs/groups_and_layers/zIndex.html。还有 shape.moveUp()、shape.moveDown、shape.moveToTop、shape.moveToBottom 函数,它们按照名称执行。所以 animationRect.moveToTop() 会把那个形状放在其他形状的“上方”。

标签: konvajs konva vue-konva


【解决方案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

【讨论】:

  • 啊,谢谢你的例子。我想我现在可以更好地理解它了。一个后续问题,当我不仅在图层中有一堆矩形而且还有其他形状(如我想一次重新排序的线条和圆形)时,这是否可行?
  • 你可以做很多形状。您只需要为每个项目保存某种“类型”并根据该类型选择所需的组件。
  • 不好意思再问一次,那么在这种情况下我是在 v-layer 级别 v-for 还是如何?例如,如果我想渲染(从下到上)rect-rect-rect-lines-circles,然后更改为 rect-lines-circles-rect-rect 等。这样一劳永逸,一个新的基于演示在这种情况下将不胜感激。
  • 您可能需要创建接收项目的自定义组件。在其中,您正在检查一个类型,并呈现所需的形状(可能使用 v-if)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
相关资源
最近更新 更多