// Clear the canvas
bgCtx.clearRect(0, 0, bgCanvas.width, bgCanvas.height);
// Draw the background color
bgCtx.fillStyle = "black";
bgCtx.fillRect(0, 0, width, height);
// Set up styles
bgCtx.fillStyle = "#181818";
bgCtx.font = "20px monospace";
// Precompute constants
const radBgAngle = angle * Math.PI / 180;
trigFnValues[radBgAngle] ??= {
cos: Math.cos(radBgAngle),
sin: Math.sin(radBgAngle)
}
const {sin, cos} = trigFnValues[radBgAngle]
bgScrollOffset = (bgScrollOffset + speed) % 300;
// Save the context state once
bgCtx.save();
// Outer loop for y-axis
for (let y = -height / 2; y < height * 1.5; y += 40) {
const yOffset = y + 20 + (bgScrollOffset * sin);
// Inner loop for x-axis
for (let x = -width / 2; x < width * 1.5; x += 40) {
const xOffset = x + 20 + (bgScrollOffset * cos);
// Apply transformation
bgCtx.setTransform(cos, sin, -sin, cos, xOffset, yOffset);
// Draw the text
bgCtx.fillText(">", 0, 0);
}
}
// Restore the context state once
bgCtx.restore();
console.timeEnd("background");
function drawBackground(angle, speed) {
console.time("background");
// Clear the canvas
bgCtx.clearRect(0, 0, bgCanvas.width, bgCanvas.height);
// Draw the background color
bgCtx.fillStyle = "black";
bgCtx.fillRect(0, 0, width, height);
// Set up styles
bgCtx.fillStyle = "#181818";
bgCtx.font = "20px monospace";
// Precompute constants
const radBgAngle = angle * Math.PI / 180;
trigFnValues[radBgAngle] ??= {
cos: Math.cos(radBgAngle),
sin: Math.sin(radBgAngle)
};
const { sin, cos } = trigFnValues[radBgAngle];
bgScrollOffset = (bgScrollOffset + speed) % 300;
// Determine scroll direction
const isHorizontal = Math.abs(cos) > Math.abs(sin);
const isVertical = Math.abs(sin) > Math.abs(cos);
// Calculate number of overflow tiles
const overflowTiles = Math.ceil(300 / 40); // 8 tiles
// Initialize loop ranges
let xStart = -width / 2;
let xEnd = width * 1.5;
let yStart = -height / 2;
let yEnd = height * 1.5;
if (isHorizontal) {
if (cos > 0) {
// Scroll to the right: add overflow on the left
xStart = -width / 2 - overflowTiles * 40;
} else {
// Scroll to the left: add overflow on the right
xEnd = width * 1.5 + overflowTiles * 40;
}
} else if (isVertical) {
if (sin > 0) {
// Scroll up: add overflow on the bottom
yEnd = height * 1.5 + overflowTiles * 40;
} else {
// Scroll down: add overflow on the top
yStart = -height / 2 - overflowTiles * 40;
}
}
// Save the context state once
bgCtx.save();
// Outer loop for y-axis
for (let y = yStart; y < yEnd; y += 40) {
const yOffset = y + 20 + (bgScrollOffset * sin);
// Inner loop for x-axis
for (let x = xStart; x < xEnd; x += 40) {
const xOffset = x + 20 + (bgScrollOffset * cos);
// Apply transformation
bgCtx.setTransform(cos, sin, -sin, cos, xOffset, yOffset);
// Draw the text
bgCtx.fillText(">", 0, 0);
}
}
// Restore the context state once
bgCtx.restore();
console.timeEnd("background");
}