Skip to content

Instantly share code, notes, and snippets.

@mustmodify
Created February 23, 2024 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustmodify/664289673e8647e5b3159e5be5779a78 to your computer and use it in GitHub Desktop.
Save mustmodify/664289673e8647e5b3159e5be5779a78 to your computer and use it in GitHub Desktop.
This code will generate some stripes on an HTML page simulating what I saw on a Jackbox game background. Very cool.
<!DOCTYPE html>
<html>
<head>
<title>Animated Canvas Diagonal Stripes</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="myCanvas"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const COLORS = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF'];
const STRIPE_COUNT = 10;
const MIN_OPACITY = 0.1;
const MAX_OPACITY = 0.8;
const STRIPE_WIDTH = 120; // Adjust stripe width as needed
const SKEW = 0.1;
const LINE_SPEED = 0.5; // Pixels per frame, adjust for speed
const bgStripes = [];
const stripes = [];
const LEFT_BOUNDARY = 0 - STRIPE_WIDTH;
const RIGHT_BOUNDARY = window.innerWidth + STRIPE_WIDTH + (canvas.width * SKEW);
// Full screen canvas
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function drawStripe(stripe) {
ctx.beginPath();
ctx.moveTo(stripe.x, stripe.y);
ctx.lineTo(stripe.x + (canvas.width * SKEW), stripe.y + (1.4 * canvas.height));
ctx.strokeStyle = `rgba(${parseInt(stripe.color.slice(1, 3), 16)}, ${parseInt(stripe.color.slice(3, 5), 16)}, ${parseInt(stripe.color.slice(5, 7), 16)}, ${stripe.opacity})`;
ctx.lineWidth = STRIPE_WIDTH;
ctx.stroke();
}
function generateFixedStripes() {
let x = LEFT_BOUNDARY;
while (x < RIGHT_BOUNDARY) {
console.log({ x: x })
const color = COLORS[Math.floor(Math.random() * COLORS.length)];
// const opacity = Math.random() * (MAX_OPACITY - MIN_OPACITY) + MIN_OPACITY;
const opacity = MAX_OPACITY;
const y = canvas.height * (-0.2);
bgStripes.push({ x, y, opacity: 1, color, direction: 0 })
x = x + STRIPE_WIDTH;
}
}
function generateMovingStripes() {
for (let i = 0; i < STRIPE_COUNT; i++) {
const color = COLORS[Math.floor(Math.random() * COLORS.length)];
const opacity = Math.random() * (MAX_OPACITY - MIN_OPACITY) + MIN_OPACITY;
const x = Math.random() * (canvas.width * 1.3) - (0.5 * canvas.width);
const y = canvas.height * (-0.2);
const direction = Math.random() > 0.5 ? 1 : -1; // Randomly choose a direction
stripes.push({ x, y, opacity, color, direction });
}
}
function updateAndDrawStripes() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
bgStripes.forEach(stripe => {
drawStripe(stripe);
})
stripes.forEach(stripe => {
if (stripe.x > RIGHT_BOUNDARY || stripe.x < LEFT_BOUNDARY) {
stripe.direction = stripe.direction * -1;
}
stripe.x += LINE_SPEED * stripe.direction; // Update position based on direction and speed
drawStripe(stripe);
});
requestAnimationFrame(updateAndDrawStripes);
}
generateFixedStripes();
generateMovingStripes();
requestAnimationFrame(updateAndDrawStripes);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment