-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas.tsx
133 lines (109 loc) · 3.13 KB
/
Canvas.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React, { useEffect, useRef } from 'react';
import './App.css';
export const Canvas: React.FC = () => {
const canvas = useRef<HTMLCanvasElement>(null)
let particles: any[] = [];
const velocity = 2;
interface Particle {
x: number,
y: number,
velocityX: number,
velocityY: number,
};
class Particle {
x: number;
y: number;
velocityX: number;
velocityY: number;
indexOfParticle: number;
constructor(
x: number,
y: number,
velocityX: number,
velocityY: number,
indexOfParticle: number
) {
this.x = x;
this.y = y;
this.velocityX = velocityX;
this.velocityY = velocityY;
this.indexOfParticle = indexOfParticle;
};
};
const drawParticles = (ctx: any): void => {
ctx.clearRect(0, 0, window.innerWidth, window.innerHeight)
for(let i = 0; i < particles.length; i++) {
let indexOfParticle = particles[i].indexOfParticle;
ctx.beginPath()
ctx.arc(particles[i].x, particles[i].y, 3, 0, Math.PI * 2)
ctx.fillStyle = 'red';
ctx.fill()
ctx.closePath()
drawLines(
particles[indexOfParticle].x,
particles[indexOfParticle].y
)
}
};
const renderParticles = (): void => {
const ctx = canvas.current?.getContext('2d');
if(!particles.length) {
for(let i = 0; i < 60; i++) {
let x: number = Math.random() * window.innerWidth;
let y: number = Math.random() * window.innerHeight;
let randomIndexOfParticle: number = Math.floor(Math.random() * particles.length);
let particle: Particle = new Particle(
x,
y,
Math.random() * velocity - velocity / 1.6,
Math.random() * velocity - velocity / 1.6,
randomIndexOfParticle
);
particles.push(particle)
drawParticles(ctx)
};
};
};
const moveParticles = (): void => {
if(particles.length) {
particles.map(p => {
p.x += p.velocityX;
p.y += p.velocityY;
if(p.x > window.innerWidth || p.x < 0) {
p.velocityX *= -1;
}
if(p.y > window.innerHeight || p.y < 0) {
p.velocityY *= -1;
}
drawParticles(canvas.current!.getContext('2d'))
});
requestAnimationFrame(moveParticles)
};
};
const drawLines = (toX: number, toY: number): void => {
const ctx: any = canvas.current?.getContext('2d')
ctx.beginPath()
for(let i = 0; i < particles.length; i++) {
let lineWidth: number = Math.abs(toX / particles[i].x) <= 2 ? Math.abs(toX / particles[i].x) : 2;
if(Math.abs(particles[i].x - toX) <= 200 && Math.abs(particles[i].y - toY) <= 200) {
ctx.moveTo(particles[i].x, particles[i].y)
ctx.lineTo(toX, toY)
ctx.lineWidth = lineWidth;;
}
}
ctx.stroke()
ctx.strokeStyle = 'red';
ctx.closePath()
};
useEffect(() => {
canvas.current!.width = window.innerWidth;
canvas.current!.height = window.innerHeight;
renderParticles()
moveParticles()
}, [])
return (
<canvas
ref={canvas}
></canvas>
)
};