【问题标题】:How to animate svg elements in a for loop with vue?如何使用vue在for循环中为svg元素设置动画?
【发布时间】:2021-05-25 22:56:37
【问题描述】:

现在正在使用 vue-konva(基于 svg 的画布库)。我正在尝试为所有由 v-loop 定义的形状设置动画。在尝试使用 Konva Animation 库函数时,我收到“无法读取未定义的属性 'getNode'”错误。我假设这是因为 ref 必须具有一个特定元素并且不能在 v-for 循环内进行寻址。如何同时为所有多边形设置动画?

SVG / 画布元素:

<v-regular-polygon
  v-for="el in results"
  ref="hexagon"
  :key="el.index"
  :config="{
    x: 200 * Math.abs(el.land),
    y: 200,
    sides: 6,
    radius: 20,
    fill: 'red',
    stroke: 'black',
    strokeWidth: 4,
  }"
/>

负责动画的函数

mounted() {
  this.fetchTemperature()
  const vm = this
  const amplitude = 100
  const period = 5000
  // in ms
  const centerX = vm.$refs.stage.getNode().getWidth() / 2

  const hexagon = this.$refs.hexagon.getNode()

  // example of Konva.Animation
  const anim = new Konva.Animation(function (frame) {
    hexagon.setX(amplitude * Math.sin((frame.time * 2 * Math.PI) / period) + centerX)
  }, hexagon.getLayer())

  anim.start()
},

【问题讨论】:

    标签: vue.js animation svg nuxt.js konva


    【解决方案1】:

    您可以通过添加索引为每个多边形设置唯一的参考。

    <v-regular-polygon
              v-for="(el, i) in results"
              :ref="`hexagon_${i}`"
    ...
    

    这是一个例子:

    <template>
      <div id="app">
        <v-stage :config="configKonva">
          <v-layer>
            <v-circle
              v-for="(item, i) in items"
              :config="item"
              :key="i"
              :ref="`circle_${i}`"
            ></v-circle>
          </v-layer>
        </v-stage>
      </div>
    </template>
    
    <script>
    import Konva from "konva";
    
    export default {
      name: "App",
      data() {
        return {
          items: [
            {
              x: 30,
              y: 100,
              radius: 20,
              fill: "red",
              stroke: "black",
              strokeWidth: 2,
            },
            {
              x: 100,
              y: 100,
              radius: 20,
              fill: "red",
              stroke: "black",
              strokeWidth: 2,
            },
          ],
          configKonva: {
            width: 200,
            height: 200,
          },
        };
      },
      mounted() {
        for (let i = 0; i < this.items.length; i++) {
          const node = this.$refs[`circle_${i}`][0].getNode();
          const period = 1000;
          const amplitude = 10;
          const anim = new Konva.Animation((frame) => {
            node.setX(
              amplitude * Math.sin((frame.time * 2 * Math.PI) / period) +
                this.items[i].x
            );
          }, node.getLayer());
    
          anim.start();
        }
      },
    };
    </script>
    

    Codesandbox

    【讨论】:

    • 非常感谢!试过了,虽然我得到了同样的错误。根据您的建议,我是否必须更改动画方法中的引用?在这里: const hexagon = this.$refs.hexagon.getNode() 考虑到新的 ref 是 :ref="hexagon_${i}"
    • 当然,你也应该像 const hexagon = this.$refs[hexagon_${i}].getNode() 一样更改 ref getter,其中 i 是多边形数组循环中的索引。
    • 你能举个例子吗?如果我想一次“批量”为所有六边形设置动画,那么它们都应该具有相同的动画。坦克!
    • 我添加了一个使用 Konva 框架动画的圆圈示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2014-10-13
    • 2013-01-09
    • 2012-09-24
    • 1970-01-01
    相关资源
    最近更新 更多