• vue3个人网站电子宠物


    预览

    在这里插入图片描述

    在这里插入图片描述

    具体代码

    Attack.gif

    请添加图片描述

    Attacked.gif

    请添加图片描述

    Static.gif

    请添加图片描述

    Walk.gif

    请添加图片描述

    Attack.gif

    请添加图片描述

    Static.gif

    请添加图片描述

    Attacked.gif

    请添加图片描述

    Walk.gif

    请添加图片描述

    <template>
      <div class="pet-container" ref="petContainer">
        <p class="pet-msg">{{ pet.msg }}</p>
        <img ref="petRef" @click="debounce(attckPet, 150)()" class="pet" :src="pet.src" alt="" srcset="">
      </div>
    </template>
    
    <script setup lang="ts">
    import { reactive, onMounted, ref } from 'vue'
    import { debounce } from 'lodash'
    const imgSrc = {
      Static: import('../../../assets/images/pet/Static.gif'),
      Attack: import('../../../assets/images/pet/Attack.gif'),
      Attacked: import('../../../assets/images/pet/Attacked.gif'),
      Walk: import('../../../assets/images/pet/Walk.gif'),
    }
    const pet = reactive({
      msg: '打我啊',
      src: imgSrc.Static,
    })
    const petContainer = ref<HTMLElement | null>()
    const petRef = ref<HTMLElement | null>()
    const mousePosition = reactive({
      x: 0,
      y: 0,
    })
    const petPosition = reactive({
      x: 0,
      y: 0,
    })
    const deg = ref<number>(0)
    const deg_y = ref<number>(0)
    const count = ref<number>(0)
    const speed = 50
    let timer = null
    let isListenMouseMove = false
    onMounted(async () => {
      changeSrc('Static', '打我啊')
    })
    
    const changeSrc = async (key, msg) => {
      let res = await imgSrc[key];
      pet.src = res.default
      pet.msg = msg
    }
    
    const attckPet = () => {
      if (isListenMouseMove) return
      changeSrc('Attacked', '啊!!!')
      window.addEventListener('mousemove', listenMouseMove)
      isListenMouseMove = true
      petPosition.x = petContainer.value?.offsetLeft
      petPosition.y = petContainer.value?.offsetTop
      setTimeout(() => {
        changeSrc('Walk', '我和你拼了')
        timer = setInterval(() => {
          move()
        }, 10);
      }, 300);
    }
    
    const listenMouseMove = (e: MouseEvent) => {
      // 需要移动的x轴距离 = 当前鼠标位置-距离浏览器左边的距离-宠物相对于浏览器页面宽度/2(宽的中心位置)
      mousePosition.x = e.x - petContainer.value.offsetLeft - petContainer.value.clientWidth / 2;
      mousePosition.y = e.y - petContainer.value.offsetTop - petContainer.value.clientHeight / 2;
      let speed = Math.ceil((Math.pow(mousePosition.x, 2) + Math.pow(mousePosition.y, 2)) / 1000);
      // 需要的旋转角度计算
      deg.value = 360 * Math.atan(mousePosition.y / mousePosition.x) / (2 * Math.PI);
      // 这里的event.clientX 返回当事件被触发时鼠标指针相对于浏览器页面(或客户区)的水平坐标。
      // 这里有关于图片位置的设置,注意你的gif图的方向,原图方向向左,那么这里就是小于,原图方向向右,这里就是大于。
      // 翻转图片
      if (petContainer.value.offsetLeft > e.clientX) {
        deg_y.value = - 180;
      } else {
        deg_y.value = 0;
      }
      //这里每一次移动鼠标都要重新计算距离,所以这里的count需要清零
      count.value = 0;
    }
    
    const move = () => {
      if (count.value < speed) {
        petPosition.x += mousePosition.x / speed
        petPosition.y += mousePosition.y / speed
        petRef.value.style.transform = "rotateZ(" + deg.value + "deg) rotateY(" + deg_y.value + "deg)"
        petContainer.value.style.left = petPosition.x + "px"
        petContainer.value.style.top = petPosition.y + "px"
        count.value++
      } else {
        window.removeEventListener('mousemove', listenMouseMove)
        changeSrc('Attack', '打死你')
        setTimeout(() => {
          changeSrc('Static', '打我啊')
          timer = clearInterval(timer);
          count.value = 0;
          isListenMouseMove = false;
        }, 1000);
      }
    }
    </script>
    
    <style scoped lang="scss">
    .pet-container {
      position: fixed;
      top: calc(100% - 100px);
      left: 0;
    
      .pet {
        width: 50px;
        height: 65px;
        cursor: pointer;
      }
    
      .pet-msg {
        padding: 5px;
        background: #8f8888;
        color: #fff;
      }
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120

    参考博客

    参考博客

  • 相关阅读:
    第三章 ArcGIS Pro创建 python 脚本工具(二)
    【MySQL入门到精通-黑马程序员】MySQL基础篇-函数
    pt28django教程
    Harmony Ble 蓝牙App (一)扫描
    Kafka中的acks机制——一次由错误资料引发的源码学习
    【365天深度学习训练营】第三周 天气识别
    什么是数据库索引?它的类型有哪些
    Nginx下PHP连接到GBase 8s数据库 - PDO_GBASEDBT方式
    Java进阶 ——— Java多线程(三)之多线程同步问题
    腾讯云服务器端口localhost可以访问,外部无法访问解决
  • 原文地址:https://blog.csdn.net/xinbaiyu/article/details/136288070