Turned the background into a rain simulation
This commit is contained in:
parent
f797271fa5
commit
dcdf5f8bd8
2 changed files with 72 additions and 58 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2023 YOUR_NAME_HERE
|
Copyright (c) 2023 Warwick New
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@
|
||||||
let canvas = document.createElement("canvas");
|
let canvas = document.createElement("canvas");
|
||||||
document.body.appendChild(canvas);
|
document.body.appendChild(canvas);
|
||||||
canvas.style.cssText = `
|
canvas.style.cssText = `
|
||||||
background: var(--background);
|
background: var(--background);
|
||||||
position:fixed;
|
position:fixed;
|
||||||
left:0;
|
left:0;
|
||||||
top:0;
|
top:0;
|
||||||
z-index:-99999;
|
z-index:-99999;
|
||||||
`
|
`
|
||||||
// set it's size
|
// set it's size
|
||||||
canvas.width = window.innerWidth;
|
canvas.width = window.innerWidth;
|
||||||
canvas.height = window.innerHeight;
|
canvas.height = window.innerHeight;
|
||||||
|
|
@ -17,70 +17,84 @@ let context = canvas.getContext('2d');
|
||||||
// this will be populated and used later
|
// this will be populated and used later
|
||||||
drawableObjectList = [];
|
drawableObjectList = [];
|
||||||
|
|
||||||
|
class Splash {
|
||||||
|
constructor(distance, x, y) {
|
||||||
|
this.distance = distance;
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.time = 0;
|
||||||
|
}
|
||||||
|
draw() {
|
||||||
|
context.fillStyle = "#2a232300";
|
||||||
|
context.strokeStyle = `rgba(42, 35, 35, ${this.distance + 0.1})`;
|
||||||
|
|
||||||
|
let splashSize = this.distance * (this.time/100);
|
||||||
|
|
||||||
|
context.beginPath();
|
||||||
|
context.ellipse(this.x, this.y, 100 * splashSize , 50 * splashSize, 0, 0, Math.PI * 2);
|
||||||
|
context.fill();
|
||||||
|
context.stroke();
|
||||||
|
|
||||||
|
if (this.time > 100) {
|
||||||
|
drawableObjectList = drawableObjectList.filter((item) => {return item !== this});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.time += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
// this is the object that'll look cool
|
// this is the object that'll look cool
|
||||||
class Point {
|
class Drop {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.x = Math.random() * window.innerWidth;
|
this.x = Math.random() * window.innerWidth;
|
||||||
this.y = Math.random() * window.innerHeight;
|
this.y = Math.random() * window.innerHeight;
|
||||||
|
|
||||||
this.dx = (Math.random()) - 0.5;
|
this.distance = Math.random();
|
||||||
this.dy = (Math.random()) - 0.5;
|
}
|
||||||
}
|
draw() {
|
||||||
draw() {
|
context.fillStyle = `rgba(42, 35, 35, ${this.distance + 0.1})`;
|
||||||
context.fillStyle = "#2a2323"
|
context.strokeStyle = `rgba(42, 35, 35, ${this.distance + 0.1})`;
|
||||||
context.strokeStyle = "#2a2323"
|
|
||||||
// find and draw links
|
// draw point
|
||||||
drawableObjectList.forEach((point) => {
|
context.beginPath();
|
||||||
if (point.x > this.x - 100 && point.x < this.x + 100 &&
|
context.moveTo(this.x,this.y);
|
||||||
point.y > this.y - 100 && point.y < this.y + 100) {
|
context.lineTo(this.x,this.y-this.distance*100);
|
||||||
context.beginPath()
|
context.stroke();
|
||||||
context.moveTo(this.x, this.y)
|
// move point
|
||||||
context.lineTo(point.x, point.y)
|
this.y += (this.distance + 0.3) * 4;
|
||||||
context.stroke();
|
|
||||||
}
|
// keep point in bounds
|
||||||
});
|
if (this.y > window.innerHeight - (1 - this.distance) * 300) {
|
||||||
|
// Add splash
|
||||||
// draw point
|
drawableObjectList.push(new Splash(this.distance, this.x, this.y))
|
||||||
context.beginPath();
|
|
||||||
context.arc(this.x, this.y, 3, 0, Math.PI * 2, false);
|
this.y = 0
|
||||||
context.fill();
|
this.x = Math.random() * window.innerWidth;
|
||||||
context.stroke();
|
this.distance = Math.random();
|
||||||
|
|
||||||
// move point
|
|
||||||
this.x += this.dx;
|
|
||||||
this.y += this.dy;
|
|
||||||
|
|
||||||
// keep point in bounds
|
|
||||||
if (this.x > window.innerWidth || this.x < 0) {
|
|
||||||
this.dx *= -1
|
|
||||||
}
|
|
||||||
if (this.y > window.innerHeight || this.y < 0) {
|
|
||||||
this.dy *= -1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// populate element array
|
// populate element array
|
||||||
for (let i = 0; i < 100; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
drawableObjectList.push(new Point());
|
drawableObjectList.push(new Drop());
|
||||||
}
|
}
|
||||||
|
|
||||||
function loop() {
|
function loop() {
|
||||||
// Loop and clear frame
|
// Loop and clear frame
|
||||||
requestAnimationFrame(loop);
|
requestAnimationFrame(loop);
|
||||||
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
|
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
|
||||||
|
|
||||||
// Draw objects.
|
// Draw objects.
|
||||||
drawableObjectList.forEach((point) => {
|
drawableObjectList.forEach((point) => {
|
||||||
point.draw();
|
point.draw();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle page size change
|
// Handle page size change
|
||||||
if (canvas.width != window.innerWidth) {
|
if (canvas.width != window.innerWidth) {
|
||||||
canvas.width = window.innerWidth;
|
canvas.width = window.innerWidth;
|
||||||
}
|
}
|
||||||
if (canvas.height != window.innerHeight) {
|
if (canvas.height != window.innerHeight) {
|
||||||
canvas.height = window.innerHeight;
|
canvas.height = window.innerHeight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
loop();
|
loop();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue