当前位置: 首页 > news >正文

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.defaultpet.msg = msg
}const attckPet = () => {if (isListenMouseMove) returnchangeSrc('Attacked', '啊!!!')window.addEventListener('mousemove', listenMouseMove)isListenMouseMove = truepetPosition.x = petContainer.value?.offsetLeftpetPosition.y = petContainer.value?.offsetTopsetTimeout(() => {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 / speedpetPosition.y += mousePosition.y / speedpetRef.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>

参考博客

参考博客

http://www.lryc.cn/news/307173.html

相关文章:

  • 2.22 作业
  • office word保存pdf高质量设置
  • 微服务设计模式
  • 10.网络游戏逆向分析与漏洞攻防-游戏网络架构逆向分析-接管游戏发送数据的操作
  • 将SU模型导入ARCGIS,并获取高度信息,多面体转SHP文件(ARCMAP)
  • 【电子通识】为什么单片机芯片上会有多组VDD电源?
  • 跟我学C++中级篇——单实例和静态化
  • 下载 axios.js 文件到本地【linux】
  • 一些matlab的常用用法。在MATLAB中,如何实现数据的导入和导出?
  • 数学建模【插值与拟合】
  • 汽修专用产品---选型介绍 汽修示波器 汽车示波器 汽车电子 汽修波形 汽车传感器波形 汽车检测
  • 如何将简历项目部署到自己的域名下
  • Redisson - 实现Java的Redis分布式和可扩展解决方案
  • 如何利用EXCEL批量插入图片
  • django rest framework 学习笔记-实战商城3
  • WPF真入门教程29--MVVM常用框架之MvvmLight
  • QT-Day4
  • 代码随想录算法训练营第三天
  • 蓝桥杯刷题1
  • 前端学习---- 前端HTML基本元素的介绍
  • 力扣思路题:丑数
  • C# this关键字的作用
  • Ubuntu18.04虚拟机磁盘扩容-lvm
  • 低代码开发:数字赋能智能制造的未来
  • janus-gateway的videoroom插件的RTP包录制功能源码详解
  • nginx+keepalived实现nginx高可用集群以及nginx实现Gateway网关服务集群
  • 主键、外键、建表范式、MySQL索引、用户管理
  • 探究前端路由hash和history的实现原理(包教包会)
  • 幻兽帕鲁服务器多少钱?有买过的吗?
  • MCU独立按键单控LED实现