html的鼠标的星星怎么制作,html5 canvas鼠标经过星星一颗一颗连成线特效

特效描述:html5 canvas 鼠标经过 星星连成线特效。html5星星一颗一颗连接特效。

代码结构

1. HTML代码

function Star(id, x, y){

this.id = id;

this.x = x;

this.y = y;

this.r = Math.floor(Math.random()*2)+1;

var alpha = (Math.floor(Math.random()*10)+1)/10/2;

this.color = "rgba(255,255,255,"+alpha+")";

}

Star.prototype.draw = function() {

ctx.fillStyle = this.color;

ctx.shadowBlur = this.r * 2;

ctx.beginPath();

ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);

ctx.closePath();

ctx.fill();

}

Star.prototype.move = function() {

this.y -= .15;

if (this.y <= -10) this.y = HEIGHT + 10;

this.draw();

}

Star.prototype.die = function() {

stars[this.id] = null;

delete stars[this.id];

}

function Dot(id, x, y, r) {

this.id = id;

this.x = x;

this.y = y;

this.r = Math.floor(Math.random()*5)+1;

this.maxLinks = 2;

this.speed = .5;

this.a = .5;

this.aReduction = .005;

this.color = "rgba(255,255,255,"+this.a+")";

this.linkColor = "rgba(255,255,255,"+this.a/4+")";

this.dir = Math.floor(Math.random()*140)+200;

}

Dot.prototype.draw = function() {

ctx.fillStyle = this.color;

ctx.shadowBlur = this.r * 2;

ctx.beginPath();

ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);

ctx.closePath();

ctx.fill();

}

Dot.prototype.link = function() {

if (this.id == 0) return;

var previousDot1 = getPreviousDot(this.id, 1);

var previousDot2 = getPreviousDot(this.id, 2);

var previousDot3 = getPreviousDot(this.id, 3);

if (!previousDot1) return;

ctx.strokeStyle = this.linkColor;

ctx.moveTo(previousDot1.x, previousDot1.y);

ctx.beginPath();

ctx.lineTo(this.x, this.y);

if (previousDot2 != false) ctx.lineTo(previousDot2.x, previousDot2.y);

if (previousDot3 != false) ctx.lineTo(previousDot3.x, previousDot3.y);

ctx.stroke();

ctx.closePath();

}

function getPreviousDot(id, stepback) {

if (id == 0 || id - stepback < 0) return false;

if (typeof dots[id - stepback] != "undefined") return dots[id - stepback];

else return false;//getPreviousDot(id - stepback);

}

Dot.prototype.move = function() {

this.a -= this.aReduction;

if (this.a <= 0) {

this.die();

return

}

this.color = "rgba(255,255,255,"+this.a+")";

this.linkColor = "rgba(255,255,255,"+this.a/4+")";

this.x = this.x + Math.cos(degToRad(this.dir))*this.speed,

this.y = this.y + Math.sin(degToRad(this.dir))*this.speed;

this.draw();

this.link();

}

Dot.prototype.die = function() {

dots[this.id] = null;

delete dots[this.id];

}

var canvas = document.getElementById('canvas'),

ctx = canvas.getContext('2d'),

WIDTH,

HEIGHT,

mouseMoving = false,

mouseMoveChecker,

mouseX,

mouseY,

stars = [],

initStarsPopulation = 80,

dots = [],

dotsMinDist = 2,

maxDistFromCursor = 50;

setCanvasSize();

init();

function setCanvasSize() {

WIDTH = document.documentElement.clientWidth,

HEIGHT = document.documentElement.clientHeight;

canvas.setAttribute("width", WIDTH);

canvas.setAttribute("height", HEIGHT);

}

function init() {

ctx.strokeStyle = "white";

ctx.shadowColor = "white";

for (var i = 0; i < initStarsPopulation; i++) {

stars[i] = new Star(i, Math.floor(Math.random()*WIDTH), Math.floor(Math.random()*HEIGHT));

//stars[i].draw();

}

ctx.shadowBlur = 0;

animate();

}

function animate() {

ctx.clearRect(0, 0, WIDTH, HEIGHT);

for (var i in stars) {

stars[i].move();

}

for (var i in dots) {

dots[i].move();

}

drawIfMouseMoving();

requestAnimationFrame(animate);

}

window.onmousemove = function(e){

mouseMoving = true;

mouseX = e.clientX;

mouseY = e.clientY;

clearInterval(mouseMoveChecker);

mouseMoveChecker = setTimeout(function() {

mouseMoving = false;

}, 100);

}

function drawIfMouseMoving(){

if (!mouseMoving) return;

if (dots.length == 0) {

dots[0] = new Dot(0, mouseX, mouseY);

dots[0].draw();

return;

}

var previousDot = getPreviousDot(dots.length, 1);

var prevX = previousDot.x;

var prevY = previousDot.y;

var diffX = Math.abs(prevX - mouseX);

var diffY = Math.abs(prevY - mouseY);

if (diffX < dotsMinDist || diffY < dotsMinDist) return;

var xVariation = Math.random() > .5 ? -1 : 1;

xVariation = xVariation*Math.floor(Math.random()*maxDistFromCursor)+1;

var yVariation = Math.random() > .5 ? -1 : 1;

yVariation = yVariation*Math.floor(Math.random()*maxDistFromCursor)+1;

dots[dots.length] = new Dot(dots.length, mouseX+xVariation, mouseY+yVariation);

dots[dots.length-1].draw();

dots[dots.length-1].link();

}

//setInterval(drawIfMouseMoving, 17);

function degToRad(deg) {

return deg * (Math.PI / 180);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码,可以实现鼠标移动时,产生星星特效的效果: HTML代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>鼠标粒子星星特效</title> <style> canvas { position: absolute; top: 0; left: 0; z-index: -1; } </style> </head> <body> <h1>鼠标粒子星星特效</h1> <canvas id="canvas"></canvas> <script src="stars.js"></script> </body> </html> ``` JS代码(保存为stars.js文件): ```javascript var canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'), stars = [], maxStars = 100; canvas.width = window.innerWidth; canvas.height = window.innerHeight; function Star() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.radius = Math.random() * 2; this.vx = Math.random() * 0.5 - 0.25; this.vy = Math.random() * 0.5 - 0.25; } Star.prototype.move = function() { this.x += this.vx; this.y += this.vy; if (this.x < -this.radius || this.x > canvas.width + this.radius) { this.vx = -this.vx; } if (this.y < -this.radius || this.y > canvas.height + this.radius) { this.vy = -this.vy; } } Star.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = '#fff'; ctx.fill(); } function drawStar() { for (var i = 0; i < maxStars; i++) { stars[i].move(); stars[i].draw(); } } function init() { for (var i = 0; i < maxStars; i++) { stars[i] = new Star(); } setInterval(function() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawStar(); }, 30); } init(); canvas.addEventListener('mousemove', function(e) { for (var i = 0; i < maxStars; i++) { var dx = e.clientX - stars[i].x, dy = e.clientY - stars[i].y, distance = Math.sqrt(dx * dx + dy * dy), force = -distance / 1000; if (distance < 100) { stars[i].vx += dx * force; stars[i].vy += dy * force; } } }); ``` 这段代码实现了鼠标移动时,产生星星特效的效果。具体实现方式是:首先定义一个Star对象,包含星星的位置、大小、速度等属性,并定义move和draw方法分别用于更新星星的位置和绘制星星。然后在init函数中,初始化100个星星,并使用setInterval定时更新画面。最后,使用canvas的mousemove事件,来控制星星的运动方向和速度。 这只是一个简单的示例代码,如果需要更加复杂的效果,可以根据实际需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值