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

React项目首页中用canvas实现星空

文章目录

  • 前言
  • 代码
  • 使用
  • 后言

前言

hello world欢迎来到前端的新世界


😜当前文章系列专栏:前端系列文章
🐱‍👓博主在前端领域还有很多知识和技术需要掌握,正在不断努力填补技术短板。(如果出现错误,感谢大家指出)🌹
💖感谢大家支持!您的观看就是作者创作的动力

代码

下方代码复制粘贴到ts文件中 然后下面是使用方式

export default function initLoginBg(){var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
// var windowWidth = window.clientWidth;
// var windowHeight = window.clientHeight;var canvas = document.getElementById('canvas') as HTMLCanvasElement,ctx = canvas.getContext('2d') as CanvasRenderingContext2D,w = canvas.width = windowWidth,h = canvas.height = windowHeight,hue = 217,stars:IntStart[] = [],count = 0,maxStars = 500;//星星数量var canvas2 = document.createElement('canvas') ,ctx2 = canvas2.getContext('2d') as CanvasRenderingContext2D;canvas2.width = 100;canvas2.height = 100;var half = canvas2.width / 2,gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half);gradient2.addColorStop(0.025, '#CCC');gradient2.addColorStop(0.1, 'hsl(' + hue + ', 61%, 33%)');gradient2.addColorStop(0.25, 'hsl(' + hue + ', 64%, 6%)');gradient2.addColorStop(1, 'transparent');ctx2.fillStyle = gradient2;ctx2.beginPath();ctx2.arc(half, half, half, 0, Math.PI * 2);ctx2.fill();// End cachefunction random(min:number, max=0) {if (arguments.length < 2) {max = min;min = 0;}if (min > max) {var hold = max;max = min;min = hold;}return Math.floor(Math.random() * (max - min + 1)) + min;}function maxOrbit(x:number, y:number) {var max = Math.max(x, y),diameter = Math.round(Math.sqrt(max * max + max * max));return diameter / 2;
//星星移动范围,值越大范围越小,}interface IntStart{orbitRadius:number;radius:number;orbitX:number;orbitY:numbertimePassed:number;speed:number;alpha:number;draw:()=>void;}var Star = function(this: IntStart) {this.orbitRadius = random(maxOrbit(w, h));this.radius = random(60, this.orbitRadius) / 18;
//星星大小this.orbitX = w / 2;this.orbitY = h / 2;this.timePassed = random(0, maxStars);this.speed = random(this.orbitRadius) / 500000;
//星星移动速度this.alpha = random(2, 10) / 10;count++;stars[count] = this;}Star.prototype.draw = function() {var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX,y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY,twinkle = random(10);if (twinkle === 1 && this.alpha > 0) {this.alpha -= 0.05;} else if (twinkle === 2 && this.alpha < 1) {this.alpha += 0.05;}ctx.globalAlpha = this.alpha;ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);this.timePassed += this.speed;}for (var i = 0; i < maxStars; i++) {new Star.prototype.constructor();}function animation() {ctx.globalCompositeOperation = 'source-over';ctx.globalAlpha = 0.5; //尾巴ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)';ctx.fillRect(0, 0, w, h)ctx.globalCompositeOperation = 'lighter';for (var i = 1, l = stars.length; i < l; i++) {stars[i].draw();};window.requestAnimationFrame(animation);}animation();
}

在这里插入图片描述

有动态效果的哦

使用

// 引入文件
import initLoginBg from "./init.ts";
import {useEffect} from "react";
const View = ()=>{// 加载完这个组件之后,加载背景useEffect(() => {initLoginBg()window.onresize = function () {initLoginBg()}}, [])return (<div><canvas id="canvas" style={{display: "block"}}></canvas></div>)
}

后言

创作不易,要是本文章对广大读者有那么一点点帮助 不妨三连支持一下,您的鼓励就是博主创作的动力

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

相关文章:

  • flutter ios Exception : No Impeller Context is Available
  • [PHP]写个简单的分页静态接口用宝塔部署到Nginx
  • 表单提交是
  • Qt的委托代理机制
  • OpenCV入门5——OpenCV的算术与位运算
  • 好用的开源项目地址
  • 深度学习(五)softmax 回归之:分类算法介绍,如何加载 Fashion-MINIST 数据集
  • 单稳态中间继电器\UEG/A-2H/220V 8A导轨安装 JOSEF约瑟
  • 2311rust到20版本更新
  • 基于Spring、SpringMVC、MyBatis的漫画网站
  • MySQL数据库八股文
  • 利用WebSocket +MQ发送紧急订单消息,并在客户端收到消息的用户的页面自动刷新列表
  • R语言——taxize(第一部分)
  • 【Spring Cloud】黑马头条 用户服务创建、登录功能实现
  • 聚观早报 |英伟达发布H200;夸克发布自研大模型
  • 15项基本SCADA技术技能
  • Golang 发送邮件
  • 【ARM Trace32(劳特巴赫) 使用介绍 5-- Trace32 通过 JTAG 命令获取数据寄存器 IDCODE的值】
  • Python之while/for,continue/break
  • 卷积神经网络(CNN)衣服图像分类的实现
  • odoo16前端框架源码阅读——env.js
  • 浙大恩特客户资源管理系统 SQL注入漏洞复现
  • ESP32网络开发实例-BME280传感器数据保存到InfluxDB时序数据库
  • C++中sort()函数的greater<int>()参数
  • 2024有哪些免费的mac苹果电脑内存清理工具?
  • 线性表的概念
  • 锐捷练习-ospf虚链路及rip路由相互引入
  • 【机器学习】线性回归算法:原理、公式推导、损失函数、似然函数、梯度下降
  • Word中NoteExpress不显示的问题
  • 连接池的大体介绍,常用配置及在springboot项目中的应用