动态颗粒背景,适合VUE、HTML前端显示

2024-02-05 11:44

本文主要是介绍动态颗粒背景,适合VUE、HTML前端显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

动态颗粒背景,适合做背景使用,VUE、HTML前端显示直接看效果
在这里插入图片描述
在这里插入图片描述

废话不多说直接上代码;

一、html 代码部分

<template><div id="login"><div class="container"><div class="login-form">登录表单部分</div><div class="lgBGimg"><div class="starBgc"><div class="star" v-for="(item,index) in starsCount" :key="index" ref="star"></div></div></div></div><div class="cavbg"><canvas id="spacebg"></canvas></div></div>
</template>

二、脚本部分

<script>
export default {setup() {return {starsCount:800,//数量distance:600,//间距};},mounted() {const starArr=this.$refs.starstarArr.forEach(item=>{let speed = 0.2+(Math.random()*1)let distance = this.distance+(Math.random()*300)item.style.transformOrigin=`0 0 ${distance}px`item.style.transform=`translate3d(0,0,-${distance}px) rotateY(${(Math.random()*360)}deg) rotateX(${(Math.random()*-50)}deg) scale(${speed},${speed})`});window.requestAnimFrame = (function(){return  window.requestAnimationFrame})();let canvas = document.getElementById("spacebg");let ct2d = canvas.getContext("2d");let numStars = 1800;let radius = '0.'+Math.floor(Math.random() * 9) + 1  ;let focalLength = canvas.width *2;let warp = 0;let centerX;let centerY;let stars = [];let star;let i;let animate = true;initializeStars();function executeFrame(){if(animate)requestAnimFrame(executeFrame);moveStars();drawStars();}function initializeStars(){centerX = canvas.width / 2;centerY = canvas.height / 2;stars = [];for(i = 0; i < numStars; i++){star = {x: Math.random() * canvas.width,y: Math.random() * canvas.height,z: Math.random() * canvas.width,o: '0.'+Math.floor(Math.random() * 99) + 1};stars.push(star);}}function moveStars(){for(i = 0; i < numStars; i++){star = stars[i];star.z--;if(star.z <= 0){star.z = canvas.width;}}}function drawStars(){var pixelX, pixelY, pixelRadius;if(canvas.width != window.innerWidth || canvas.width != window.innerWidth){canvas.width = window.innerWidth;canvas.height = window.innerHeight;initializeStars();}window.onresize = () => {if(canvas.width != window.innerWidth || canvas.width != window.innerWidth){canvas.width = window.innerWidth;canvas.height = window.innerHeight;initializeStars();}}if(warp==0){ct2d.fillStyle = "rgba(0,10,20,1)";// let lineGradient = ct2d.createLinearGradient (100, 10, 100, 60);//第一张图效果let lineGradient = ct2d.createLinearGradient (500, 50, 100, 600);//第二张图效果lineGradient.addColorStop(0, '#083c6f');lineGradient.addColorStop(1, '#010516');ct2d.fillStyle = lineGradient;ct2d.fillRect(0,0, canvas.width, canvas.height);}ct2d.fillStyle = "rgba(209, 255, 255, "+radius+")";for(i = 0; i < numStars; i++){star = stars[i];pixelX = (star.x - centerX) * (focalLength / star.z);pixelX += centerX;pixelY = (star.y - centerY) * (focalLength / star.z);pixelY += centerY;pixelRadius = 1 * (focalLength / star.z);ct2d.fillRect(pixelX, pixelY, pixelRadius, pixelRadius);ct2d.fillStyle = "rgba(209, 255, 255, "+star.o+")";}}executeFrame();}
};
</script>

三、样式部分代码

<style lang="less">
#login {width: 100%;height: 100%;position: relative;overflow: hidden;.container{width: 100%;height: 100%;position: relative;&:before{position: absolute;width:100%;height:100%;background:url(../../common/assets/image/background.svg);background-size: cover;opacity:0.08;display: block;content: '';z-index:2;}}.desc {width: 100% !important;text-align: center !important;color: gray !important;height: 60px !important;line-height: 60px !important;}.cavbg{position:absolute; left:0; top:0; z-index:0;}.lgBGimg{position: absolute;top:0px;left:0px;right:0px;bottom:0px;width:100%;height:100%;z-index:1;.starBgc{position: absolute;left:48%;bottom: -99px;transform: perspective(500px);transform-style: preserve-3d;perspective-origin: 50% 100%;animation: rotate 90s infinite linear;opacity:0.75;.star{width: 2px;height: 2px;background: #f7f7b8;position: absolute;top: 0;left: 0;backface-visibility: hidden;}@keyframes rotate {0%{transform: perspective(400px) rotateZ(20deg) rotateX(-40deg) rotateY(0);}100%{transform: perspective(400px) rotateZ(20deg) rotateX(-40deg) rotateY(-360deg);}}}}
}
</style>

效果使用了3种,分别是GIF动效、CSS3动效、2DJS动效动态元素结合,开发而成的视频视频效果

搞完手工,最后给大家放上个视频看看效果吧,欢迎留言交了讨论一下吧

动态颗粒元素背景

这篇关于动态颗粒背景,适合VUE、HTML前端显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/680805

相关文章

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

前端缓存策略的自解方案全解析

《前端缓存策略的自解方案全解析》缓存从来都是前端的一个痛点,很多前端搞不清楚缓存到底是何物,:本文主要介绍前端缓存的自解方案,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、为什么“清缓存”成了技术圈的梗二、先给缓存“把个脉”:浏览器到底缓存了谁?三、设计思路:把“发版”做成“自愈”四、代码

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

Spring Gateway动态路由实现方案

《SpringGateway动态路由实现方案》本文主要介绍了SpringGateway动态路由实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前沿何为路由RouteDefinitionRouteLocator工作流程动态路由实现尾巴前沿S

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

vue监听属性watch的用法及使用场景详解

《vue监听属性watch的用法及使用场景详解》watch是vue中常用的监听器,它主要用于侦听数据的变化,在数据发生变化的时候执行一些操作,:本文主要介绍vue监听属性watch的用法及使用场景... 目录1. 监听属性 watch2. 常规用法3. 监听对象和route变化4. 使用场景附Watch 的

前端导出Excel文件出现乱码或文件损坏问题的解决办法

《前端导出Excel文件出现乱码或文件损坏问题的解决办法》在现代网页应用程序中,前端有时需要与后端进行数据交互,包括下载文件,:本文主要介绍前端导出Excel文件出现乱码或文件损坏问题的解决办法,... 目录1. 检查后端返回的数据格式2. 前端正确处理二进制数据方案 1:直接下载(推荐)方案 2:手动构造

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路