Konva vue js文本滑入舞台然后停下来

问题描述 投票:0回答:1

[嗨,我在vue中使用konva js(https://konvajs.org),我正在尝试对文本进行动画处理,以使其从舞台下滑到屏幕中的某个位置,但是我不确定100%如何这应该可行,我已经可以在此处进行演示了;

https://codesandbox.io/s/vue-konva-template-i2em3

Vue公司;

  <v-stage ref="stage" :config="stageSize">
    <v-layer ref="layer">
      <v-text
        ref="text1"
        :config="{
            text: 'Draggable Text',
            x: 50,
            y: 50,
            draggable: true,
            fill: 'black'
          }"
      />
    </v-layer>
  </v-stage>
</template>


<script>
const width = window.innerWidth;
const height = window.innerHeight;

export default {
  data() {
    return {
      stageSize: {
        width: width,
        height: height
      }
    };
  },
  methods: {
    changeSize(e) {
      // to() is a method of `Konva.Node` instances
      e.target.to({
        scaleX: Math.random() + 0.8,
        scaleY: Math.random() + 0.8,
        duration: 0.2
      });
    }
  },
  mounted() {
    const vm = this;
    const amplitude = 100;
    const period = 5000;
    // in ms
    const centerX = vm.$refs.stage.getStage().getWidth() / 2;

    const text = this.$refs.text1.getStage();

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

    anim.start();
  }
};
</script>

关于如何使文本从舞台外部显示然后在画布中的特定位置停下来的任何想法?

javascript vue.js canvas konvajs
1个回答
0
投票

您可以只使用node.to()方法来动画化任何Konva.Node

mounted() {
    const textNode = this.$refs.text1.getNode();
    const endX = width / 2 - textNode.width() / 2;
    // run the animation
    textNode.to({
      x: endX,
      onFinish: () => {
        // update the state at the end
        this.textPos.x = endX;
      }
    })
  }

演示:https://codesandbox.io/s/vue-konva-animation-demo-lr4qw

© www.soinside.com 2019 - 2024. All rights reserved.