html--烟花3

html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas烟花粒子</title>
<meta name="keywords" content="canvas烟花"/>
<meta name="description" content="canvas动画/">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<canvas id="canvas">Canvas is not supported by your browser.</canvas>
<script src="js/index.js">
</script>
</body>
</html>

css

* { margin: 0; padding: 0; }html, body { height: 100%; }body {background: #000;
}canvas {display: block;cursor: crosshair;
}

在这里插入图片描述

js


// Options
var options = {/* Which hue should be used for the first batch of rockets? */startingHue: 120,/* How many ticks the script should wait before a new firework gets spawned, if the user is holding down his mouse button. */clickLimiter: 5,/* How fast the rockets should automatically spawn, based on ticks */timerInterval: 40,/* Show pulsing circles marking the targets? */showTargets: true,/* Rocket movement options, should be self-explanatory */rocketSpeed: 2,rocketAcceleration: 1.03,/* Particle movement options, should be self-explanatory */particleFriction: 0.95,particleGravity: 1,/* Minimum and maximum amount of particle spawns per rocket */particleMinCount: 25,particleMaxCount: 40,/* Minimum and maximum radius of a particle */particleMinRadius: 3,particleMaxRadius: 5
};// Local variables
var fireworks = [];
var particles = [];
var mouse = {down: false, x: 0, y: 0};
var currentHue = options.startingHue;
var clickLimiterTick = 0;
var timerTick = 0;
var cntRocketsLaunched = 0;// Helper function for canvas animations
window.requestAnimFrame = (function() {return window.requestAnimationFrame ||window.webkitRequestAnimationFrame ||window.mozRequestAnimationFrame ||function(cb) {window.setTimeout(callback, 1000 / 60);}
})();// Helper function to return random numbers within a specified range
function random(min, max) {return Math.random() * (max - min) + min;
}// Helper function to calculate the distance between 2 points
function calculateDistance(p1x, p1y, p2x, p2y) {var xDistance = p1x - p2x;var yDistance = p1y - p2y;return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
}// Setup some basic variables
var canvas = document.getElementById('canvas');
var canvasCtx = canvas.getContext('2d');
var canvasWidth = window.innerWidth;
var canvasHeight = window.innerHeight;// Resize canvas
canvas.width = canvasWidth;
canvas.height = canvasHeight;// Firework class
function Firework(sx, sy, tx, ty) {  // Set coordinates (x/y = actual, sx/sy = starting, tx/ty = target)this.x = this.sx = sx;this.y = this.sy = sy;this.tx = tx; this.ty = ty;// Calculate distance between starting and target pointthis.distanceToTarget = calculateDistance(sx, sy, tx, ty);this.distanceTraveled = 0;// To simulate a trail effect, the last few coordinates will be storedthis.coordinates = [];this.coordinateCount = 3;// Populate coordinate array with initial datawhile(this.coordinateCount--) {this.coordinates.push([this.x, this.y]);}// Some settings, you can adjust them if you'd like to do so.this.angle = Math.atan2(ty - sy, tx - sx);this.speed = options.rocketSpeed;this.acceleration = options.rocketAcceleration;this.brightness = random(50, 80);this.hue = currentHue;this.targetRadius = 1;this.targetDirection = false;  // false = Radius is getting bigger, true = Radius is getting smaller// Increase the rockets launched countercntRocketsLaunched++;
};// This method should be called each frame to update the firework
Firework.prototype.update = function(index) {// Update the coordinates arraythis.coordinates.pop();this.coordinates.unshift([this.x, this.y]);// Cycle the target radius (used for the pulsing target circle)if(!this.targetDirection) {if(this.targetRadius < 8)this.targetRadius += 0.15;elsethis.targetDirection = true;  } else {if(this.targetRadius > 1)this.targetRadius -= 0.15;elsethis.targetDirection = false;}// Speed up the firework (could possibly travel faster than the speed of light) this.speed *= this.acceleration;// Calculate the distance the firework has travelled so far (based on velocities)var vx = Math.cos(this.angle) * this.speed;var vy = Math.sin(this.angle) * this.speed;this.distanceTraveled = calculateDistance(this.sx, this.sy, this.x + vx, this.y + vy);// If the distance traveled (including velocities) is greater than the initial distance// to the target, then the target has been reached. If that's not the case, keep traveling.if(this.distanceTraveled >= this.distanceToTarget) {createParticles(this.tx, this.ty);fireworks.splice(index, 1);} else {this.x += vx;this.y += vy;}
};// Draws the firework
Firework.prototype.draw = function() {var lastCoordinate = this.coordinates[this.coordinates.length - 1];// Draw the rocketcanvasCtx.beginPath();canvasCtx.moveTo(lastCoordinate[0], lastCoordinate[1]);canvasCtx.lineTo(this.x, this.y);canvasCtx.strokeStyle = 'hsl(' + this.hue + ',100%,' + this.brightness + '%)';canvasCtx.stroke();// Draw the target (pulsing circle)if(options.showTargets) {canvasCtx.beginPath();canvasCtx.arc(this.tx, this.ty, this.targetRadius, 0, Math.PI * 2);canvasCtx.stroke();}
};// Particle class
function Particle(x, y) {// Set the starting pointthis.x = x;this.y = y;// To simulate a trail effect, the last few coordinates will be storedthis.coordinates = [];this.coordinateCount = 5;// Populate coordinate array with initial datawhile(this.coordinateCount--) {this.coordinates.push([this.x, this.y]);}// Set a random angle in all possible directions (radians)this.angle = random(0, Math.PI * 2);this.speed = random(1, 10);// Add some friction and gravity to the particlethis.friction = options.particleFriction;this.gravity = options.particleGravity;// Change the hue to a random numberthis.hue = random(currentHue - 20, currentHue + 20);this.brightness = random(50, 80);this.alpha = 1;// Set how fast the particles decaythis.decay = random(0.01, 0.03);
}// Updates the particle, should be called each frame
Particle.prototype.update = function(index) {// Update the coordinates arraythis.coordinates.pop();this.coordinates.unshift([this.x, this.y]);// Slow it down (based on friction)this.speed *= this.friction;// Apply velocity to the particlethis.x += Math.cos(this.angle) * this.speed;this.y += Math.sin(this.angle) * this.speed + this.gravity;// Fade out the particle, and remove it if alpha is low enoughthis.alpha -= this.decay;if(this.alpha <= this.decay) {particles.splice(index, 1);}
}// Draws the particle
Particle.prototype.draw = function() {var lastCoordinate = this.coordinates[this.coordinates.length - 1];var radius = Math.round(random(options.particleMinRadius, options.particleMaxRadius));// Create a new shiny gradientvar gradient = canvasCtx.createRadialGradient(this.x, this.y, 0, this.x, this.y, radius);gradient.addColorStop(0.0, 'white');gradient.addColorStop(0.1, 'white');gradient.addColorStop(0.1, 'hsla(' + this.hue + ',100%,' + this.brightness + '%,' + this.alpha + ')');gradient.addColorStop(1.0, 'black');// Draw the gradientcanvasCtx.beginPath();canvasCtx.fillStyle = gradient;canvasCtx.arc(this.x, this.y, radius, Math.PI * 2, false);canvasCtx.fill();
}// Create a bunch of particles at the given position
function createParticles(x, y) {var particleCount = Math.round(random(options.particleMinCount, options.particleMaxCount));while(particleCount--) {particles.push(new Particle(x, y));}
}// Add an event listener to the window so we're able to react to size changes
window.addEventListener('resize', function(e) {canvas.width = canvasWidth = window.innerWidth;canvas.height = canvasHeight = window.innerHeight;
});// Add event listeners to the canvas to handle mouse interactions
canvas.addEventListener('mousemove', function(e) {e.preventDefault();mouse.x = e.pageX - canvas.offsetLeft;mouse.y = e.pageY - canvas.offsetTop;
});canvas.addEventListener('mousedown', function(e) {e.preventDefault();mouse.down = true;
});canvas.addEventListener('mouseup', function(e) {e.preventDefault();mouse.down = false;
});// Main application / script, called when the window is loaded
function gameLoop() {// This function will rund endlessly by using requestAnimationFrame (or fallback to setInterval)requestAnimFrame(gameLoop);// Increase the hue to get different colored fireworks over timecurrentHue += 0.5;// 'Clear' the canvas at a specific opacity, by using 'destination-out'. This will create a trailing effect.canvasCtx.globalCompositeOperation = 'destination-out';canvasCtx.fillStyle = 'rgba(0, 0, 0, 0.5)';canvasCtx.fillRect(0, 0, canvasWidth, canvasHeight);canvasCtx.globalCompositeOperation = 'lighter';// Loop over all existing fireworks (they should be updated & drawn)var i = fireworks.length;while(i--) {fireworks[i].draw();fireworks[i].update(i);}// Loop over all existing particles (they should be updated & drawn)var i = particles.length;while(i--) {particles[i].draw();particles[i].update(i);}// Draw some textcanvasCtx.fillStyle = 'white';canvasCtx.font = '14px Arial';canvasCtx.fillText('Rockets launched: ' + cntRocketsLaunched, 10, 24);// Launch fireworks automatically to random coordinates, if the user does not interact with the sceneif(timerTick >= options.timerInterval) {if(!mouse.down) {fireworks.push(new Firework(canvasWidth / 2, canvasHeight, random(0, canvasWidth), random(0, canvasHeight / 2)));timerTick = 0;}} else {timerTick++;}// Limit the rate at which fireworks can be spawned by mouseif(clickLimiterTick >= options.clickLimiter) {if(mouse.down) {fireworks.push(new Firework(canvasWidth / 2, canvasHeight, mouse.x, mouse.y));clickLimiterTick = 0;}} else {clickLimiterTick++;}
}window.onload = gameLoop();

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/309741.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

代码学习记录42---动态规划

随想录日记part42 t i m e &#xff1a; time&#xff1a; time&#xff1a; 2024.04.14 主要内容&#xff1a;今天开始要学习动态规划的相关知识了&#xff0c;今天的内容主要涉及&#xff1a;最长递增子序列 &#xff1b;最长连续递增序列 &#xff1b;最长重复子数组 ;最长公…

mac基础操作、快捷、软件快捷方式

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 mac基础操作、快捷、软件快捷方式 前言mac快捷操作快捷查找切换页面页面缩略访达和命令端切换创建文件夹创建文件删除文件/文件夹获取文件的路径移动文件或文件夹复制文件命令端常用命令 前言 主要是方…

B端:请说出你的设计依据,咋办?来吧,尼尔森法则告诉他。

在B端界面设计中&#xff0c;客户经常会问这样设计的依据是什么&#xff0c;许多设计师回答不上来&#xff0c;或者是答非所问&#xff0c;流于表面&#xff0c;这是时候就需要请出来尼尔森用户体验设计的十大法则&#xff0c;那么问题来了&#xff0c;如何让这10大法则和界面相…

Python学习笔记23 - 目录操作

os模块操作目录相关函数 os.path模块操作目录相关函数 案例1 —— 列出指定目录下的所有.py文件 案例2 —— walk()

论文笔记:NEFTune: Noisy Embeddings Improve Instruction Finetuning

iclr 2024 reviewer 评分 5666 1 论文思路 论文的原理很简单&#xff1a;在finetune过程的词向量中引入一些均匀分布的噪声即可明显地提升模型的表现 2 方法评估

c# .net 香橙派 Orangepi GPIO高低电平、上升沿触发\下降沿触发 监听回调方法

c# .net 香橙派GPIO高低电平、上升沿触发\下降沿触发 监听回调方法 通过gpio readall 查看 gpio编码 这里用orangepi zero3 ,gpio= 70为例 当gpio 70 输入高电平时,触发回调 c# .net 代码 方法1: Nuget 包 System.Device.Gpio ,微软官方库对香橙派支持越来越好了,用得…

2024年文化、历史与人文艺术与社会发展国际会议(CHHASD2024)

2024年文化、历史与人文艺术与社会发展国际会议(CHHASD2024) 会议简介 2024年国际文化、历史、人文、艺术与社会发展会议&#xff08;CHHASD2024&#xff09;将在中国武汉举行&#xff0c;主题为“文化、历史&#xff0c;人文、艺术和社会发展”。CHHASD2024汇集了来自世界各…

c++中常用库函数

大小写转换 islower/isupper函数 char ch1 A; char ch2 b;//使用islower函数判断字符是否为小写字母 if(islower(ch1)){cout << ch1 << "is a lowercase letter." << end1; } else{cout << ch1 << "is not a lowercase lette…

图形学基础:二维三维刚体的移动、缩放和旋转矩阵

一、二维 1.1 缩放矩阵 x&#xff0c;y分别表示在x轴&#xff0c;y轴缩放的倍数 示例&#xff1a; 点(2,1)在x&#xff0c;y轴上分别缩放x倍&#xff0c;y倍 1.2 平移矩阵 x&#xff0c;y分表表示在x轴&#xff0c;y轴上移动的距离 示例&#xff1a;点&#xff08;2,1&#xf…

AI天使汇联合150家顶级基金、战投,征集优秀AI创业项目

鉴于AI天使汇主办的2024年3月期优秀项目征集活动效果超出预期&#xff0c;3月活动最后TOP20路演者中已有多家快速拿到了TS。 路演活动质量受到了AI创业公司和基金/战投伙伴的高度评价&#xff0c;现在开始四月期活动报名! 本期征集活动联合的顶级基金和战投数量增加到了150家…

LabVIEW无线快速存取记录器(WQAR)测试平台

LabVIEW无线快速存取记录器&#xff08;WQAR&#xff09;测试平台 随着民用航空业的迅速发展&#xff0c;航空安全的保障日益成为公众和专业领域的关注焦点。无线快速存取记录器&#xff08;WirelessQuick Access Recorder, WQAR&#xff09;作为记录飞行数据、监控飞行品质的…

使用这几款插件,GitHub阅读代码效率噌噌噌

** octotree&#xff1a;生成仓库目录 ** 这可能是我用得最多的一款插件了&#xff0c;大家有没有遇到过这种情况。每次点击一个文件后&#xff0c;整个文件列表就会被隐藏&#xff0c;想查看其它文件只能回退后再次进入。别提有多蛋疼了…… 而这款插件就完美解决了这个问题…

Day101:漏洞发现-漏扫项目篇NucleiYakitGobyAfrogXrayAwvs联动中转被动

目录 特征类-三方Poc调用&模版Poc调用 案例1&#xff1a;单点对某特征点进行安全评估 Goby-综合类 Nuclei-较综合类 Afrog-特征类 Yakit-可特征可综合 案例2&#xff1a;新型对某特征点进行安全评估 综合类-主动漏扫&中转联动&被动联动 案例1&#xff1a;…

【学习笔记十四】EWM发货流程概述及相关配置

一、EWM发货流程与ERP集成配置 1.将凭证类型从 ERP 系统映射至 EWM ERP交货单凭证类型LF映射到EWM凭证类型OUTB 2.从 ERP 系统映射项目类型至 EWM ERP交货单凭证类型+ERP交货单项目类型TAN映射到EWM项目类型是ODLV 3.定义出库交货的参数文件 ①定义外向交货处理的凭证类型OUT…

Linux查看进程

Linux查看进程 引言查看进程1.快速查看运行中的进程列表2. 查看所有用户的所有进程3.显示所有进程的完整格式。4.动态显示进程的信息5.根据进程名查找进程ID6.以树状图的方式显示进程间的父子关系7.查找指定名字的进程id 引言 Linux查看进程在日常的使用中比较常见&#xff0c…

gcn代码处理出现的问题

README 版本不一致 python 2.7 PYTHON 3.7 切换 TensorFlow系统的学习使用 数据集下载

c++ - 类的默认成员函数

文章目录 前言一、构造函数二、析构函数三、拷贝构造函数四、重载赋值操作符五、取地址及const取地址操作符重载 前言 默认成员函数是编译器自动生成的&#xff0c;也可以自己重写&#xff0c;自己重写之后编译器就不再生成&#xff0c;下面是深入了解这些成员函数。 一、构造…

执行npm命令一直出现sill idealTree buildDeps怎么办?

一、问题 今天在运行npm时候一直出项sill idealTree buildDeps问题 二、 解决 1、网上查了一下&#xff0c;有网友说先删除用户界面下的npmrc文件&#xff08;注意一定是用户C:\Users\{账户}\下的.npmrc文件下不是nodejs里面&#xff09;&#xff0c;进入到对应目录下&#x…

3dmax制作小熊猫的基本流程

1.透视图插入面片&#xff0c;改高度宽度&#xff0c;把参考图放进面片里。 2.角度捕捉切换&#xff0c;角度改为90 3.shift旋转&#xff0c;旋转面片&#xff0c;复制一个出来 4.在前视图&#xff0c;把参考图片中的正式图小熊猫的一半的位置&#xff08;可以是眼睛&#x…

自己操作逆向案例一——某竞赛网登录密码加密,超级简单,泪目了!

网址&#xff1a;aHR0cHM6Ly9leGFtem9uZS5zYWlrci5jb20vcXVlc3Rpb24vZXhwbG9yZQ 打开开发者工具&#xff0c;点击账号密码登录&#xff0c;进行抓包 先进行搜索&#xff0c;发现一下子就找到了&#xff0c;且看上去很像MD5加密&#xff0c;打上断点&#xff0c;再次点击登录。…