import * as React from 'react';
export default class LoginBgCanvas extends React.Component {
componentDidMount() {
this.drawCanvas();
}
public drawCanvas = () => {
// tslint:disable-next-line: one-variable-per-declaration
let canvas = document.querySelector('canvas')!,
ctx = canvas.getContext('2d')!;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.lineWidth = 0.3;
ctx.strokeStyle = new Color(150).style;
let dotsNomber = 400;
if (canvas.width < 1400) {
dotsNomber = 200;
}
let mousePosition = {
x: (30 * canvas.width) / 100,
y: (30 * canvas.height) / 100
};
let dots = {
nb: dotsNomber,
distance: 100,
d_radius: 150,
array: []
};
function colorValue(min) {
return Math.floor(Math.random() * 255 + min);
}
function createColorStyle(r, g, b) {
return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
}
function mixComponents(comp1, weight1, comp2, weight2) {
return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
}
function averageColorStyles(dot1, dot2) {
// tslint:disable-next-line: one-variable-per-declaration
let color1 = dot1.color,
color2 = dot2.color;
// tslint:disable-next-line: one-variable-per-declaration
let r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
}
function Color(min) {
min = min || 0;
this.r = colorValue(min);
this.g = colorValue(min);
this.b = colorValue(min);
this.style = createColorStyle(this.r, this.g, this.b);
}
function Dot() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.vx = -0.5 + Math.random();
this.vy = -0.5 + Math.random();
this.radius = Math.random() * 2;
this.color = new Color();
}
Dot.prototype = {
// tslint:disable-next-line: object-literal-shorthand
draw: function() {
ctx.beginPath();
ctx.fillStyle = this.color.style;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.fill();
}
};
function createDots() {
for (let i = 0; i < dots.nb; i++) {
dots.array.push(new Dot());
}
}
function moveDots() {
for (let i = 0; i < dots.nb; i++) {
let dot = dots.array[i];
if (dot.y < 0 || dot.y > canvas.height) {
dot.vx = dot.vx;
dot.vy = -dot.vy;
} else if (dot.x < 0 || dot.x > canvas.width) {
dot.vx = -dot.vx;
dot.vy = dot.vy;
}
dot.x += dot.vx;
dot.y += dot.vy;
}
}
function connectDots() {
for (let i = 0; i < dots.nb; i++) {
for (let j = 0; j < dots.nb; j++) {
let i_dot = dots.array[i];
let j_dot = dots.array[j];
if (
i_dot.x - j_dot.x < dots.distance &&
i_dot.y - j_dot.y < dots.distance &&
i_dot.x - j_dot.x > -dots.distance &&
i_dot.y - j_dot.y > -dots.distance
) {
if (
i_dot.x - mousePosition.x < dots.d_radius &&
i_dot.y - mousePosition.y < dots.d_radius &&
i_dot.x - mousePosition.x > -dots.d_radius &&
i_dot.y - mousePosition.y > -dots.d_radius
) {
ctx.beginPath();
ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
ctx.moveTo(i_dot.x, i_dot.y);
ctx.lineTo(j_dot.x, j_dot.y);
ctx.stroke();
ctx.closePath();
}
}
}
}
}
function drawDots() {
for (let i = 0; i < dots.nb; i++) {
let dot = dots.array[i];
dot.draw();
}
}
function animateDots() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
moveDots();
connectDots();
drawDots();
requestAnimationFrame(animateDots);
}
document
.querySelector('canvas')!
.addEventListener('mousemove', function(e) {
mousePosition.x = e.pageX;
mousePosition.y = e.pageY;
});
document
.querySelector('canvas')!
.addEventListener('mouseleave', function(e) {
mousePosition.x = canvas.width / 3;
mousePosition.y = canvas.height / 3;
});
createDots();
requestAnimationFrame(animateDots);
};
render() {
return <canvas />;
}
}
用 raect 写一个 等离子特效背景。
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 问题:Revit样例文件中的默认三维空间没有颜色 问题原因:该三维视图应用了阶段化过滤器 解决方案:1.在视图属性...
- 文/凉亦歌 成长就是一副洒洒脱脱、不动声色的模样,在你出色之前,没有人会在意你的彷徨。 一个人的时候,必须酷起来。...