JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas"></canvas>
CSS
* { margin: 0; padding: 0;}
#canvas {
display: block;
position: fixed;
}
body {
background: url(http://www.zingerbug.com/Backgrounds/background_images/small_white_bricks_seamless_pattern.gif)
}
JavaScript
$(function() {
var canvas = $("#canvas")[0];
var ctx = canvas.getContext("2d");
var minSpeed = 3;
var maxSpeed = 6;
var frequency = 0.1;
var color = 0;
var shifts = [0, 2, 4];
var keepLines = 100;
var timer = null;
var W = window.innerWidth, H = window.innerHeight;
canvas.width = W;
canvas.height = H;
var ps = [];
var lines = [];
ps.push(new particle());
ps.push(new particle());
function line(x1, y1, x2, y2)
{
color += frequency;
this.rgb = {
r: Math.sin(color + shifts[0]) * 127 + 128,
g: Math.sin(color + shifts[1]) * 127 + 128,
b: Math.sin(color + shifts[2]) * 127 + 128
};
this.coords = {x1: x1, x2: x2, y1: y1, y2: y2};
this.a = 1;
this.getColor = function() {
var c = "rgba(" + Math.floor(this.rgb.r) + "," + Math.floor(this.rgb.g) + "," + Math.floor(this.rgb.b) + "," + this.a + ")";
this.a -= 1 / keepLines;
if (this.a < 0) this.a = 0;
return c;
}
}
function particle()
{
var speed, angle;
this.pos = {x: Math.random()*W, y: Math.random()*H};
this.updateSpeedAndAngle = function(minAngle, maxAngle) {
var minAngle = minAngle || 0;
var maxAngle = maxAngle || 360;
speed = (maxSpeed - minSpeed) * Math.random() + minSpeed;
angle = Math.random()*(maxAngle - minAngle) + minAngle;
this.d = {
x: speed*Math.cos(angle*Math.PI/180),
y: speed*Math.sin(angle*Math.PI/180)
};
}
this.updateSpeedAndAngle();
this.updatePos = function() {
this.pos.x += this.d.x;
this.pos.y += this.d.y;
if (this.pos.x < 0) {
this.pos.x = 0;
this.updateSpeedAndAngle(0, 180);
}
if (this.pos.y < 0) {
this.pos.y = 0;
this.updateSpeedAndAngle(-90, 90);
}
...