canvas粒子球体内烟花碰撞爆炸动画效果代码
代码语言:html
所属分类:粒子
代码描述:canvas粒子球体内烟花碰撞爆炸动画效果代码
代码标签: canvas 粒子 球体 烟花 碰撞 爆炸 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> html, body { width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; } .container { position: relative; width: 100%; height: 100%; margin: 0; padding: 0; background-color: #000000; } .container>canvas { position: absolute; top: 0; left: 0; } @import url('https://fonts.googleapis.com/css2?family=Pacifico&display=swap'); .title { font-family: 'Pacifico', cursive; color: #fff; text-shadow: 6px 4px #735b13; position: fixed; color: white; font-size: 40pt; z-index: 1; top: 45%; left: 40%; text-align: center; display: none; } .count { font-family: 'Pacifico', cursive; color: #fff; text-shadow: 6px 4px #735b13; position: fixed; color: white; font-size: 100pt; z-index: 1; top: 40%; left: 48%; text-align: center; } </style> </head> <body> <!-- From CyberSoft with Love !!! --> <span class="count">5</span> <span class="title">Happy New Year <br /> 2023</span> <div id="jsi-sphere-container" class="container"> </div> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script> <script> let i = 6; let timeOut = setInterval(() => { i--; document.querySelector(".count").innerHTML = i; if (i == 0) { clearInterval(timeOut); document.querySelector(".title").style.display = 'block'; document.querySelector(".count").style.display = 'none'; } }, 1000); var RENDERER = { AUXILIARY_LINE_COUMT: 16, MAX_ROTATION_ANGLE: Math.PI / 200, FIREWORK_INTERVAL: { min: 30, max: 100 }, init: function () { this.setParameters(); this.setupData(); this.reconstructMethod(); this.bindEvent(); this.render(); }, setParameters: function () { this.$document = $(document); this.$window = $(window); this.$container = $("#jsi-sphere-container"); this.width = this.$container.width(); this.height = this.$container.height(); this.contextBackground = $("<canvas />") .attr({ width: this.width, height: this.height }) .appendTo(this.$container) .get(0) .getContext("2d"); this.contextForeground = $("<canvas />") .attr({ width: this.width, height: this.height }) .appendTo(this.$container) .get(0) .getContext("2d"); this.camera = CAMERA.init(this); this.angleX = this.MAX_ROTATION_ANGLE / 2; this.angleY = this.MAX_ROTATION_ANGLE / 2; this.points = []; this.fireworks = []; this.maxFireworkInterval = this.getRandomValue(this.FIREWORK_INTERVAL) | 0; this.fireworkInterval = this.maxFireworkInterval; this.base = Math.min(this.width, this.height); this.scatterRadius = (this.base * 3) / 2; }, reconstructMethod: function () { this.render = this.render.bind(this); this.changeDepth = this.changeDepth.bind(this); this.changeAngle = this.changeAngle.bind(this); }, getRandomValue: function (range) { return range.min + (range.max - range.min) * Math.random(); }, bindEvent: function () { this.$document.on( "onwheel" in document ? "wheel" : "onmousewheel" in document ? "mousewheel" : "DOMMouseScroll", this.changeDepth ); this.$container.on("mousemove", this.changeAngle); }, setupData: function () { for (var i = 1; i < this.AUXILIARY_LINE_COUMT; i++) { for ( var phi = 0, deltaPhi = (Math.PI * 2) / (this.AUXILIARY_LINE_COUMT * 2 - Math.abs(this.AUXILIARY_LINE_COUMT * 2 - i * 4)); phi < Math.PI * 2; phi += deltaPhi ) { this.points.push( new POINT( this.camera, this.getAxis3D((Math.PI / this.AUXILIARY_LINE_COUMT) * i, phi) ) ); } } }, changeDepth: function (event) { event.preventDefault(); this.camera.move( 0, 0, (event.originalEvent.deltaY || -event.originalEvent.wheelDelta || event.originalEvent.detail) >= 0 ? 3 : -2 ); }, changeAngle: function (event) { var offset = this.$container.offset(), x = event.clientX - offset.left + this.$window.scrollLeft(), y = event.clientY - offset.top + this.$window.scrollTop(); this.angleY = ((this.width / 2 - x) / this.width) * 2 * this.MAX_ROTATION_ANGLE; this.angleX = ((this.height / 2 - y) / this.height) * 2 * this.MAX_ROTATION_ANGLE; }, getAxis3D: function (theta, phi) { var cosTheta = Math.cos(theta), sinTheta = Math.sin(theta); return { x: this.scatterRadius * sinTheta * Math.cos(phi), y: this.scatterRadius * cosTheta, z: this.scatterRadius * sinTheta * Math.sin(phi) }; }, render: function () { requestAnimationFrame(this.render); this.contextForeground.clearRect(0, 0, this.width, this.height); this.contextBackground.fillStyle = "hsla(0, 0%, 0%, 0.2)"; this.contextBackground.fillRect(0, 0, this.width, this.height); for (var i = 0, length = this.points.length; i < length; i++) { this.points[i].rotateX(this.angleX); this.points[i].rotateY(this.angleY); } for (var i = 0, length = this.fireworks.length; i < length; i++) { this.fireworks[i].rotateX(this.angleX); this.fireworks[i].rotateY(this.angleY); } this.points.sort(function (point1, point2) { return point2.getAxis3D().z - point1.getAxis3D().z; }); this.fireworks.sort(function (firework1, firework2) { return firework2.getAxis3D().z - firework1.getAxis3D().z; }); for (var i = this.points.length - 1; i >= 0; i--) { this.points[i].render(this.contextForeground); } for (var i = this.fireworks.length - 1; i >= 0; i--) { if (!this.fireworks[i].render(this.contextBackground)) { this.fireworks.splice(i, 1); } } if (--this.fireworkInterval == 0) { this.fireworks.push(new FIREWORK(this, this.camera)); this.maxFireworkInterval = this.getRandomValue(this.FIREWORK_INTERVAL) | 0; this.fireworkInterval = this.maxFireworkInterval; } } }; var CAMERA = { INIT_AXIS: { X: 0, Y: 0, Z: -2300 }, MIN_Z: -3000, FOCUS_DISTANCE: 500, MAGNIFICATION: 50, OFFSET: 100, init: function (renderer) { this.renderer = renderer; this.x = this.INIT_AXIS.X; this.y = this.INIT_AXIS.Y; this.z = this.INIT_AXIS.Z; return this; .........完整代码请登录后点击上方下载按钮下载查看
网友评论0