如何使用vue2做动态背景特效

268 阅读1分钟

参考地址掘金

vanta.js官网地址

一 首先安包

npm install vanta@0.5.24

npm install three@0.121.0

import * as THREE from 'three'

import BIRDS from 'vanta/src/vanta.birds'

 mounted() {
    this.vantaEffect = BIRDS({
      el: this.$refs.vantaRef,
      THREE: THREE
    })
  },
  beforeDestroy() {
    if (this.vantaEffect) {
      this.vantaEffect.destroy()
    }
  },

举例说明

<template>
<div>
   <div ref="vantaRef" style="width:100%;height:100vh"></div>
   <div class="my_title">Hello,World</div>
</div>

</template>

<script>
import * as THREE from 'three'
import Net from 'vanta/src/vanta.net'
export default {   
  name: "hello",
  data() {
    return {
      
    };
  },
  mounted() {
    this.vantaEffect = Net({
      el: this.$refs.vantaRef,
      THREE: THREE
    })

    VANTA.NET({
  el: this.$refs.vantaRef,
  mouseControls: true,
  touchControls: true,
  gyroControls: false,
  minHeight: 200.00,
  minWidth: 200.00,
  scale: 1.00,
  scaleMobile: 1.00,
  color: 0xdec790,
  backgroundColor: 0xc04393,
  spacing: 12.00
})


  },
  beforeDestroy() {
    if (this.vantaEffect) {
      this.vantaEffect.destroy()
    }
  },
  methods: {
    
  },

};
</script>

<style>
.my_title{
  z-index: 999;
  position: fixed;
  top: 40%;
  left: 10%;
  color: aquamarine;
  font-size: 100px;
  font-weight: bolder;
}
</style>