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…

前端面试01总结

1.Js 中!x为true 时,x可能为哪些值 答: 1.false&#xff1a;布尔值false 2.0或-0&#xff1a;数字零 3.""或’或 &#xff08;空字符串&#xff09;&#xff1a;长度为0的字符串 4.null&#xff1a;表示没有任何值的特殊值 5.undefined&#xff1a;变量未定义时的默认…

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

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

java中的正则表达式匹配

匹配单个字符 \d&#xff1a;匹配一个数字字符。 \w&#xff1a;匹配一个字母、数字或下划线字符。 \s&#xff1a;匹配一个空白字符&#xff08;空格、制表符、换行符等&#xff09;。 .&#xff1a;匹配除换行符外的任意字符。量词 *&#xff1a;匹配前一个元素零次或多次。 …

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

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

Java面试题:解释Java中的项目Jigsaw以及其对Java平台的影响

Java中的项目Jigsaw是Java 9中引入的一个重要特性&#xff0c;它为Java语言和平台带来了模块化的支持。Jigsaw项目的主要目标是引入模块化概念&#xff0c;创建Java 9中的模块&#xff0c;然后将其应用于JDK。这一创新性的设计使得Java应用程序能够更高效地管理依赖关系&#x…

基于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…

【AIGC调研系列】rerank3是什么

Rerank 3是一个针对企业搜索和检索辅助生成&#xff08;RAG&#xff09;系统优化的新型基础模型&#xff0c;它支持多语种、多结构数据搜索&#xff0c;并提供高精度的语义重排。通过这种方式&#xff0c;Rerank 3能够大幅提升响应准确度和降低延迟&#xff0c;同时大幅降低成本…

Mac m1 安装虚拟机+docker 2024

由于最近需要学习docker,k8s&#xff0c;用到虚拟机测试&#xff1b;场景各不相同&#xff0c;慎用&#xff0c;我在mac m1的机器下已经安装运行成功&#xff0c;参考了网上的几篇文章&#xff0c;也给到相应的思路&#xff0c;大部分要么是镜像不完整&#xff0c;安装完发现缺…

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…

Android APP 禁用深色模式

从Android10&#xff08;API 29&#xff09;开始&#xff0c;在原有的主题适配的基础上&#xff0c;Google开始提供了Force Dark机制&#xff0c;在系统底层直接对颜色和图片进行转换处理&#xff0c;原生支持深色模式。当系统设置深色主题背景或者进入省电模式情况下会进入深色…

活动预告|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家…

【MYSQL】索引优化思考题

假设有一张订单表 order&#xff0c;主要包含了主键订单编码 order_no、订单状态 status、提交时间 create_time 等列&#xff0c;并且创建了 status 列索引和 create_time 列索引。此时通过创建时间降序获取状态为 1 的订单编码&#xff0c;以下是具体实现代码&#xff1a; s…

熟悉GC回收算法

GC&#xff08;Garbage Collection&#xff0c;垃圾回收&#xff09;回收算法是Java等高级语言中的一个重要概念&#xff0c;用于自动管理内存。 1&#xff1a;请简述什么是垃圾回收&#xff1f; 答案&#xff1a;垃圾回收是编程语言提供的一种内存管理机制&#xff0c;它自动…

香港银行个人账户开户实用指南

内地居民申请香港银行个人账户指南 内地居民申请香港银行个人账户&#xff0c;可以通过以下两种方式&#xff1a; 自行前往香港银行申请开户&#xff0c;需要携带齐全的开户资料&#xff0c;在规定开户行进行面谈&#xff0c;一般银行会要求客户购买理财产品或是做大额的资金…

【菜狗学前端】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;可以直接影响网站的用户保留。那么如何做出特殊的网站设…