html5 悬浮屏幕中心,html5 canvas悬浮的圆点动画特效

特效描述:html5canvas 悬浮的圆点 动画特效。html5基于canvas绘制悬浮的圆点随机游动动画特效

代码结构

1. HTML代码

const settings = {

pointCount: 50,

startSpeed: 1.3,

availableColors: ["#aaa01f", "#bba301", "#eaf2a1", "#aacccc", "#faf3aa"]

};

let context;

let objects;

let mousePos = {};

const canvas = document.createElement("canvas");

const init = () => {

canvas.width = window.innerWidth;

canvas.height = window.innerHeight;

objects = [];

document.body.appendChild(canvas);

context = canvas.getContext("2d");

for (let i = 0; i <= settings.pointCount; i++) {

let plusOrMinusX = Math.random() < 0.5 ? -1 : 1;

let plusOrMinusY = Math.random() < 0.5 ? -1 : 1;

const x = Math.random() * canvas.width;

const y = Math.random() * canvas.height;

const z = Math.random() * - 150;

const v = {

x: plusOrMinusX * Math.random() * settings.startSpeed,

y: plusOrMinusY * Math.random() * settings.startSpeed,

z: Math.random() * settings.startSpeed * 4,

};

const colorIndex = Math.floor(

Math.random() * settings.availableColors.length

);

objects.push(

new Point(canvas, settings.availableColors[colorIndex], x, y, z, v)

);

}

requestAnimationFrame(() => {

draw();

});

bindMouseMove();

}

class Point {

constructor(canvas, color, x, y, z, v) {

this.canvas = canvas;

this.color = color;

this.x = x;

this.y = y;

this.z = z;

this.v = v;

this.radius = this.calulateRadius();

}

calulateRadius() {

return Math.abs(this.z / 120);

}

calculatePosition() {

const diameter = 2 * this.radius;

if (

this.x > this.canvas.width ||

this.y > this.canvas.height ||

this.x + diameter < 0 ||

this.y + diameter < 0

) {

this.x = this.canvas.width / 2;

this.y = this.canvas.height / 2;

this.z = -40;

}

this.radius = this.calulateRadius();

this.x += this.v.x;

this.y += this.v.y;

this.z += this.v.z;

}

adjustTowardsMouse(distToMouse, mouseX, mouseY) {

if (mouseX > this.x) {

this.v.x += 10 / distToMouse;

}

if (mouseY > this.Y) {

this.v.y += 10 / distToMouse;

}

if (mouseX <= this.x) {

this.v.x -= 10 / distToMouse;

}

if (mouseY <= this.Y) {

this.v.y -= 10 / distToMouse;

}

}

draw(context) {

context.beginPath();

context.strokeStyle = this.color;

context.fillStyle = this.color;

context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);

context.fill();

}

}

const draw = () => {

context.clearRect(0, 0, canvas.width, canvas.height);

objects.forEach(object => {

object.calculatePosition();

if (mousePos) {

const xDiff = object.x - mousePos.x;

const yDiff = object.y - mousePos.y;

const pointDist = Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));

if (pointDist < (150 + object.radius)) {

object.adjustTowardsMouse(pointDist, mousePos.x, mousePos.y);

}

}

object.draw(context);

});

requestAnimationFrame(draw);

};

const bindMouseMove = () => {

listener = document.addEventListener('mousemove', e => {

//document.removeEventListener('mousemove', listener);

mousePos.x = e.clientX;

mousePos.y = e.clientY;

//bindMouseMove();

});

}

init();

window.addEventListener('resize', () => {

init();

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值