Konva Vue JS текст слайд на сцену, а затем остановить - PullRequest
0 голосов
/ 26 сентября 2019

Привет, я использую konva js (https://konvajs.org) с vue, и я пытаюсь анимировать текст так, чтобы он скользил по экрану от сцены к определенной позиции, однако я не 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>

любые идеи, как я могузаставить текст появляться снаружи сцены, а затем остановиться в определенной точке холста?

1 Ответ

0 голосов
/ 26 сентября 2019

Вы можете просто использовать 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

...