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.mzph.cn/news/817375.shtml

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

相关文章

股票开户佣金最低多少?万一!A股开户多少钱合适?

开户佣金 通常情况下&#xff0c;股票开户佣金只要在达成交易的前提才收手续的费用&#xff0c;即买入和卖出的时候。目前&#xff0c;国规定收取最高佣金的比例为千分之三。 也就是说&#xff0c;最高为成交金额的3%&#xff0c;一般都会小于这个比例。最低交易佣金是5元起&a…

如何为不同内容主题选择最适合的移动滑轨屏方案?

在数字化信息时代背景下&#xff0c;多媒体互动装置作为当前内容展示的常用手段&#xff0c;颇受大众的喜爱&#xff0c;比如应用在展厅、商业推广、活动会议等领域的滑轨屏&#xff0c;便是其中一种新颖的互动展示装置&#xff0c;并且它还能根据不同的内容主题&#xff0c;来…

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

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

基于springboot的综合成绩管理系统(含源码+sql+视频导入教程+文档+PPT)

&#x1f449;文末查看项目功能视频演示获取源码sql脚本视频导入教程视频 1 、功能描述 基于springboot的综合成绩管理系统2拥有三个角色 管理员&#xff1a;学生管理、班主任管理、班级管理、综合测评管理等 学生&#xff1a;综合测评/德育成绩/课程成绩/体育成绩的登记 班…

STM32H7的DMA双缓冲控制IO输出脉冲

STM32H7的DMA双缓冲控制IO输出脉冲 keil的sct文件配置MPU配置初始化DMA双缓冲初始化TIM12用处触发DMAMUX的请求发生器 keil的sct文件配置 ; ************************************************************* ; *** Scatter-Loading Description File generated by uVision ***…

kylin java.io.IOException: error=13, Permission denied

linux centos7.8 error13, Permission denied_linux open error13-CSDN博客 chmod -R 777 /home/zengwenfeng/kkFileView-4.2.1 2024-04-15 13:15:17.416 WARN 3400 --- [er-offprocmng-1] o.j.l.office.LocalOfficeProcessManager : An I/O error prevents us to determine…

ubuntu 20.04 更新显卡驱动

1. 问题描述 $ watch -n 1 nvidia-smi画面不动 而且运行 pytorch 代码时出现问题&#xff1a; UserWarning: CUDA initialization: The NVIDIA driver on your system is too old (found version 11070). Please update your GPU driver by downloading and installing a new…

活动预告|NineData 创始人CEO叶正盛将参加QCon全球软件开发大会,共话AI大模型技术在数据库DevOps的实践

4月13日下午&#xff0c;NineData创始人&CEO叶正盛即将参加InfoQ中国主办的『QCon全球软件开发大会北京站』的技术大会。在本次技术峰会上&#xff0c;叶正盛将以《AI大模型技术在数据库DevOps的实践》为主题&#xff0c;深入剖析AI大模型技术在数据库DevOps领域的最新进展…

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

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

【菜狗学前端】npm i -g nodemon 遇到的下载卡住及运行权限问题解决记录

一、下载nodemon原因 nodemon作用&#xff1a;用node环境运行js文件时可以实时刷新运行出结果 (即修改js代码后不需再手动重新运行js文件) 二、下载卡住 reify:semver:timing reifyNode:node_modules/nodemon Completed 卡住位置&#xff1a;reify:semver: timing reifyNode…

零基础学网页设计,6个特别案例带你入门!

网站设计是通过艺术手法呈现项目策划案例的内容、网站的主题模式和自己的理解。优秀的网站设计应该能够充分吸引访问者的注意力&#xff0c;让访问者有视觉愉悦感。因此&#xff0c;网站设计的特殊性非常重要&#xff0c;可以直接影响网站的用户保留。那么如何做出特殊的网站设…

CTF之comment

网站的登录框里有提示 账号&#xff1a;zhangwei 密码&#xff1a;zhangwei***&#xff08;后三位要自己猜&#xff09; 用burpsuit抓包爆破发现密码为zhangwei666 进去后就一个留言榜&#xff08;目前没发现怎么用&#xff09; 扫一下网站发现git泄露 1.下载 进入root用户&…

故障诊断 | Matlab实现基于小波包结合卷积神经网络DWT-CNN实现电缆故障诊断算法

故障诊断 | Matlab实现基于小波包结合卷积神经网络DWT-CNN实现电缆故障诊断算法 目录 故障诊断 | Matlab实现基于小波包结合卷积神经网络DWT-CNN实现电缆故障诊断算法分类效果基本介绍程序设计参考资料 分类效果 基本介绍 1.Matlab实现基于小波包结合卷积神经网络DWT-CNN实现电…

ssm052游戏攻略网站的设计与实现+vue

游戏攻略网站设计与实现 摘 要 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本游戏攻略网站就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间内处…

ssm+springboot校园勤工俭学助学兼职系统

本校园勤工俭学兼职系统主要包括三大功能模块&#xff0c;即管理员功能模块和学生功能模块及企业功能模块。 &#xff08;1&#xff09;管理员模块&#xff1a;系统中的核心用户是管理员&#xff0c;管理员登录后&#xff0c;通过管理员功能来管理后台系统。主要功能有&#xf…

【机器视觉】opencv教程、示例代码学习笔记汇总(建议收藏)

Microsoft Designer : https://designer.microsoft.com/design 注&#xff1a;文末附 AI对人生寄语的解析 通过对opencv的学习&#xff0c;机器视觉水平也从入门&#xff08;十分之&#xff09;二级提升到了入门&#xff08;十分之&#xff09;五级。 主页菜单已更新&#xff0…

Flink入门学习 | 大数据技术

⭐简单说两句⭐ ✨ 正在努力的小新~ &#x1f496; 超级爱分享&#xff0c;分享各种有趣干货&#xff01; &#x1f469;‍&#x1f4bb; 提供&#xff1a;模拟面试 | 简历诊断 | 独家简历模板 &#x1f308; 感谢关注&#xff0c;关注了你就是我的超级粉丝啦&#xff01; &…

仿真服务器介绍及应用

仿真服务器是一种高性能的计算设备&#xff0c;专门用于运行复杂的仿真软件和处理大量的计算任务。 仿真服务器通常具备以下特点&#xff1a; 1. 高性能硬件配置&#xff1a;为了满足仿真软件对计算能力的要求&#xff0c;仿真服务器通常配备高性能的CPU、大量的内存以及高速的…

Win11 使用 WSL2 安装 linux 子系统 ubuntu

Win11 使用 WSL2 安装 linux 子系统 ubuntu 段子手168 1、用 部署映像服务和管理工具 dism.exe 命令&#xff0c;开启 WSL2 按【WIN R】&#xff0c;打开【运行】&#xff0c;输入&#xff1a;【cmd】&#xff0c;管理员打开【命令行提示符】。 启用适用于 Linux 的 Windo…

正则表达式---【Python版】

目录 前言 一.正则表达式概括 1.1简介 1.2使用场景 二.正则表达式语法 2.1基本匹配 2.2元字符 2.2.1点运算符. 2.2.2字符类[] 2.2.3否定字符类 2.2.4*号 2.2.5号 2.2.6&#xff1f;号 2.2.7{}号 2.2.8()号 2.2.9|或运算 2.2.10转码特殊字符\ 2.2.11^和$ 2.3简…