Skip to content

Commit 12fdeb1

Browse files
authored
Merge pull request #140 from Rahul-oops/matrix-rain-frontend
Matrix Rain Frontend Project Added
2 parents 88ec2d5 + 5b10d10 commit 12fdeb1

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Matrix Rain</title>
8+
<style>
9+
* {
10+
margin: 0;
11+
padding: 0;
12+
box-sizing: border-box;
13+
}
14+
15+
body {
16+
overflow: hidden;
17+
background: black;
18+
}
19+
20+
canvas {
21+
background: transparent;
22+
position: absolute;
23+
top: 0;
24+
left: 0;
25+
filter: brightness(2);
26+
}
27+
</style>
28+
</head>
29+
<body>
30+
<canvas id="canvas"></canvas>
31+
<script src="./index.js"></script>
32+
</body>
33+
</html>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
function Matrix(canvasName) {
2+
const { floor: f, random: rand } = Math;
3+
let canvas = document.getElementById(canvasName);
4+
5+
const ctx = canvas.getContext("2d");
6+
canvas.height = window.innerHeight;
7+
canvas.width = window.innerWidth;
8+
9+
let gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
10+
gradient.addColorStop(0, "red");
11+
gradient.addColorStop(0.2, "yellow");
12+
gradient.addColorStop(0.4, "green");
13+
gradient.addColorStop(0.6, "cyan");
14+
gradient.addColorStop(0.8, "blue");
15+
gradient.addColorStop(1, "magenta");
16+
17+
class Symbol {
18+
constructor(x, y, fontSize, canvasHeight) {
19+
this.characters =
20+
"アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
21+
this.x = x;
22+
this.y = y;
23+
this.fontSize = fontSize;
24+
this.font_size = this.fontSize;
25+
this.text = "";
26+
this.canvasHeight = canvasHeight;
27+
}
28+
draw(context) {
29+
this.text = this.characters.charAt(f(rand() * this.characters.length));
30+
context.fillText(
31+
this.text,
32+
this.x * this.fontSize,
33+
this.y * this.fontSize
34+
);
35+
if (this.y * this.fontSize > this.canvasHeight && rand() > 0.95) {
36+
this.y = 0;
37+
} else {
38+
this.y += 1;
39+
}
40+
}
41+
}
42+
43+
class Effect {
44+
constructor(canvasWidth, canvasHeight) {
45+
this.canvasWidth = canvasWidth;
46+
this.canvasHeight = canvasHeight;
47+
this.fontSize = 20;
48+
this.columns = this.canvasWidth / this.fontSize;
49+
this.symbols = [];
50+
this.#initialize();
51+
}
52+
#initialize() {
53+
for (let i = 0; i < this.columns + 1; i++) {
54+
this.symbols[i] = new Symbol(i, 0, this.fontSize, this.canvasHeight);
55+
}
56+
}
57+
resize(width, height) {
58+
this.canvasWidth = width;
59+
this.canvasHeight = height;
60+
this.columns = this.canvasWidth / this.fontSize;
61+
this.symbols = [];
62+
this.#initialize();
63+
}
64+
}
65+
66+
const effect = new Effect(canvas.width, canvas.height);
67+
68+
let lastTime = 0;
69+
const FPS = 62;
70+
const nextFrame = 1000 / FPS;
71+
let timer = 0;
72+
73+
function animate(timeStamp) {
74+
const deltaTime = timeStamp - lastTime;
75+
lastTime = timeStamp;
76+
77+
if (timer > nextFrame) {
78+
ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
79+
ctx.textAlign = "center";
80+
ctx.fillRect(0, 0, canvas.width, canvas.height);
81+
// ctx.fillStyle = "#0aff0a";
82+
ctx.fillStyle = gradient;
83+
ctx.font = effect.fontSize + "px monospace";
84+
effect.symbols.forEach((symbol) => symbol.draw(ctx));
85+
timer = 0;
86+
} else {
87+
timer += deltaTime;
88+
}
89+
90+
requestAnimationFrame(animate);
91+
}
92+
93+
animate(0);
94+
95+
window.addEventListener("resize", () => {
96+
canvas.width = window.innerWidth;
97+
canvas.height = window.innerHeight;
98+
gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
99+
gradient.addColorStop(0, "red");
100+
gradient.addColorStop(0.2, "yellow");
101+
gradient.addColorStop(0.4, "green");
102+
gradient.addColorStop(0.6, "cyan");
103+
gradient.addColorStop(0.8, "blue");
104+
gradient.addColorStop(1, "magenta");
105+
effect.resize(canvas.width, canvas.height);
106+
});
107+
}
108+
109+
Matrix("canvas");

0 commit comments

Comments
 (0)