html让圆点闪烁,canvas闪烁的圆点灯光特效

使用方法:

1、head引入css文件

body {

width: 100%;

margin: 0;

overflow: hidden;

background: hsla(0, 5%, 5%, 1);

background-repeat: no-repeat;

background-attachment: fixed;

background-image: linear-gradient(to right top, hsla(0, 5%, 15%, 0.5), hsla(0, 5%, 5%, 1));

background-image: -moz-linear-gradient(to right top, hsla(0, 5%, 15%, 0.5), hsla(0, 5%, 5%, 1));

}

p{

text-align:center;

width:100%;

color:hsla(0,50%,50%,1);

font-size:6em;

text-shadow:1px 1px hsla(0,0%,5%,1),

-1px -1px hsla(0,0%,5%,1);

font-family: 'Poiret One', cursive;

letter-spacing: 6px;

text-align: center;

position: relative;

margin-top: 40vh;

}

2、body引入HTML代码

window.requestAnimFrame = (function() {

return window.requestAnimationFrame ||

window.webkitRequestAnimationFrame ||

window.mozRequestAnimationFrame ||

window.oRequestAnimationFrame ||

window.msRequestAnimationFrame ||

function(callback) {

window.setTimeout(callback, 1000 / 60);

};

})();

var c = document.getElementById('canv');

var $ = c.getContext('2d');

var w = c.width = window.innerWidth;

var h = c.height = window.innerHeight;

var _w = w * 0.5;

var _h = h * 0.5;

var arr = [];

var cnt = 0;

window.addEventListener('load', resize);

window.addEventListener('resize', resize, false);

function resize() {

c.width = w = window.innerWidth;

c.height = h = window.innerHeight;

c.style.position = 'absolute';

c.style.left = (window.innerWidth - w) *

.01 + 'px';

c.style.top = (window.innerHeight - h) *

.01 + 'px';

}

function anim() {

cnt++;

if (cnt % 6) draw();

window.requestAnimFrame(anim);

}

anim();

function draw() {

var splot = {

x: rng(_w - 900, _w + 900),

y: rng(_h - 900, _h + 900),

r: rng(20, 80),

spX: rng(-1, 1),

spY: rng(-1, 1)

};

arr.push(splot);

while (arr.length > 100) {

arr.shift();

}

$.clearRect(0, 0, w, h);

for (var i = 0; i < arr.length; i++) {

splot = arr[i];;

$.fillStyle = rndCol();

$.beginPath();

$.arc(splot.x, splot.y, splot.r, 0, Math.PI * 2, true);

$.shadowBlur = 80;

$.shadowOffsetX = 2;

$.shadowOffsetY = 2;

$.shadowColor = rndCol();

$.globalCompositeOperation = 'lighter';

$.fill();

splot.x = splot.x + splot.spX;

splot.y = splot.y + splot.spY;

splot.r = splot.r * 0.96;

}

}

function rndCol() {

var r = Math.floor(Math.random() * 180);

var g = Math.floor(Math.random() * 60);

var b = Math.floor(Math.random() * 100);

return "rgb(" + r + "," + g + "," + b + ")";

}

function rng(min, max) {

return Math.floor(Math.random() * (max - min + 1)) + min;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 HTML5 Canvas 的全屏酷炫星光闪烁 3D 视差背景动画特效HTML 代码: ```html <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Canvas Star Field</title> <style> canvas { background-color: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), stars = [], particles = [], maxStars = 1300, maxParticles = 100; // 创建星星 for (var i = 0; i < maxStars; i++) { stars.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, radius: Math.random() * 1.5, depth: Math.random() * 2000 + 500 }); } // 创建粒子 function emitParticle() { if (particles.length < maxParticles) { var particle = { x: canvas.width / 2, y: canvas.height / 2, vx: Math.random() * 2 - 1, vy: Math.random() * 2 - 1, radius: Math.random() * 2 + 2, alpha: Math.random() * 0.5 + 0.5, life: Math.random() * 200 + 100 }; particles.push(particle); } } // 更新粒子 function updateParticles() { particles.forEach(function(particle, index) { particle.x += particle.vx; particle.y += particle.vy; particle.life--; particle.alpha -= 0.01; if (particle.life <= 0 || particle.alpha <= 0) { particles.splice(index, 1); } }); } // 绘制星星 function drawStar(star) { var x = (star.x - canvas.width / 2) * (star.depth / canvas.width), y = (star.y - canvas.height / 2) * (star.depth / canvas.width), radius = star.radius * (star.depth / canvas.width); context.beginPath(); context.arc(x + canvas.width / 2, y + canvas.height / 2, radius, 0, Math.PI * 2); context.fillStyle = '#fff'; context.fill(); } // 绘制粒子 function drawParticle(particle) { context.beginPath(); context.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2); context.fillStyle = 'rgba(255, 255, 255, ' + particle.alpha + ')'; context.fill(); } // 绘制 function draw() { context.clearRect(0, 0, canvas.width, canvas.height); // 绘制星星 stars.forEach(function(star) { drawStar(star); }); // 绘制粒子 particles.forEach(function(particle) { drawParticle(particle); }); } // 循环 function loop() { emitParticle(); updateParticles(); draw(); requestAnimationFrame(loop); } // 初始化画布 function initCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; window.addEventListener('resize', function() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }); } // 初始化 function init() { initCanvas(); loop(); } // 执行初始化 init(); </script> </body> </html> ``` 这段代码创建了一个全屏的 Canvas 画布,并在其中绘制了星星和粒子。通过调整粒子的数量和大小,以及星星的深度和大小,可以得到不同的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值