Absolutely! Drawing a row of ">" symbols at once instead of individually can significantly improve performance by reducing the number of setTransform and fillText calls. We can use a single loop for the y-axis and draw an entire row of symbols in one go.
Here's how we can implement this optimization:
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;
const gridStep = 40;
const charOffset = 20;
const maxScroll = 300;
// Determine the range of base coordinates needed for y
let yStart, yEnd;
if (sin >= 0) {
// Scrolls down or horizontally: content appears from top
yStart = -(maxScroll * sin) - charOffset - 2 * gridStep;
yEnd = height - charOffset + 2 * gridStep;
} else {
// Scrolls up: content appears from bottom
yStart = -charOffset - 2 * gridStep;
yEnd = height - (maxScroll * sin) - charOffset + 2 * gridStep;
}
// Calculate how many ">" symbols we need per row
// We need to cover the entire width plus overflow
const symbolsPerRow = Math.ceil((width + 2 * maxScroll + 4 * gridStep) / gridStep);
// Create a row of ">" symbols
const rowOfSymbols = ">".repeat(symbolsPerRow);
// Save the context state once
bgCtx.save();
// Loop for y-axis only
for (let y = yStart; y < yEnd; y += gridStep) {
const yCanvasPos = y + charOffset + (bgScrollOffset * sin);
// Calculate the x position for the start of the row
// This ensures we start far enough to the left to cover all possible scroll positions
let xStart;
if (cos >= 0) {
xStart = -(maxScroll * cos) - charOffset - 2 * gridStep;
} else {
xStart = -charOffset - 2 * gridStep;
}
const xCanvasPos = xStart + charOffset + (bgScrollOffset * cos);
// Apply transformation for the entire row
bgCtx.setTransform(cos, sin, -sin, cos, xCanvasPos, yCanvasPos);
// Draw the entire row of symbols at once
// We use a monospace font, so each symbol will be evenly spaced
for (let i = 0; i < symbolsPerRow; i++) {
bgCtx.fillText(">", i * gridStep, 0);
}
}
// Restore the context state once
bgCtx.restore();
console.timeEnd("background");
}
Wait, I see an issue with my approach. The problem is that we can't simply draw a row of ">" symbols in a straight line because the transformation matrix rotates each symbol. Let me revise this approach:
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;
const gridStep = 40;
const charOffset = 20;
const maxScroll = 300;
// Determine the range of base coordinates needed for y
let yStart, yEnd;
if (sin >= 0) {
yStart = -(maxScroll * sin) - charOffset - 2 * gridStep;
yEnd = height - charOffset + 2 * gridStep;
} else {
yStart = -charOffset - 2 * gridStep;
yEnd = height - (maxScroll * sin) - charOffset + 2 * gridStep;
}
// Determine x range
let xStart, xEnd;
if (cos >= 0) {
xStart = -(maxScroll * cos) - charOffset - 2 * gridStep;
xEnd = width - charOffset + 2 * gridStep;
} else {
xStart = -charOffset - 2 * gridStep;
xEnd = width - (maxScroll * cos) - charOffset + 2 * gridStep;
}
// Calculate number of symbols needed in x direction
const xCount = Math.ceil((xEnd - xStart) / gridStep);
// Save the context state once
bgCtx.save();
// Loop for y-axis
for (let y = yStart; y < yEnd; y += gridStep) {
const yCanvasPos = y + charOffset + (bgScrollOffset * sin);
// Create and draw a row of symbols
bgCtx.save();
// Set initial position for this row
const startX = xStart + charOffset + (bgScrollOffset * cos);
const startY = yCanvasPos;
// Apply the rotation part of the transformation
bgCtx.translate(startX, startY);
bgCtx.transform(cos, sin, -sin, cos, 0, 0);
// Draw all symbols in this row with a single transformation
for (let i = 0; i < xCount; i++) {
bgCtx.fillText(">", i * gridStep, 0);
}
bgCtx.restore();
}
// Restore the context state once
bgCtx.restore();
console.timeEnd("background");
}
Actually, I see that this approach still requires a loop for each symbol in the row. Let me try a different approach using a pattern or pre-rendered row:
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;
const gridStep = 40;
const charOffset = 20;
const maxScroll = 300;
// Create or get a cached pattern row
// This could be optimized further by caching this canvas between frames
const patternWidth = Math.ceil(width + 2 * maxScroll + 4 * gridStep);
const patternCanvas = document.createElement('canvas');
patternCanvas.width = patternWidth;
patternCanvas.height = gridStep;
const patternCtx = patternCanvas.getContext('2d');
patternCtx.fillStyle = "#181818";
patternCtx.font = "20px monospace";
// Draw a row of ">" symbols on the pattern canvas
for (let x = 0; x < patternWidth; x += gridStep) {
patternCtx.fillText(">", x + charOffset, charOffset);
}
// Determine the range of base coordinates needed for y
let yStart, yEnd;
if (sin >= 0) {
yStart = -(maxScroll * sin) - charOffset - 2 * gridStep;
yEnd = height - charOffset + 2 * gridStep;
} else {
yStart = -charOffset - 2 * gridStep;
yEnd = height - (maxScroll * sin) - charOffset + 2 * gridStep;
}
// Save the context state once
bgCtx.save();
// Loop for y-axis only
for (let y = yStart; y < yEnd; y += gridStep) {
const yCanvasPos = y + charOffset + (bgScrollOffset * sin);
// Calculate the x position for the start of the row
let xStart;
if (cos >= 0) {
xStart = -(maxScroll * cos) - charOffset - 2 * gridStep;
} else {
xStart = -charOffset - 2 * gridStep;
}
const xCanvasPos = xStart + charOffset + (bgScrollOffset * cos);
// Apply transformation for the entire row
bgCtx.setTransform(cos, sin, -sin, cos, xCanvasPos, yCanvasPos);
// Draw the entire pattern row at once
bgCtx.drawImage(patternCanvas, 0, 0);
}
// Restore the context state once
bgCtx.restore();
console.timeEnd("background");
}
I see the issue now. The challenge is that we need to apply the rotation transformation to each individual symbol, not just to the row as a whole. This is because each symbol needs to be rotated around its own center.
Let's go back to the original approach but optimize it by reducing unnecessary calculations:
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;
const gridStep = 40;
const charOffset = 20;
const maxScroll = 300;
// Determine the range of base coordinates needed for y
let yStart, yEnd;
if (sin >= 0) {
yStart = -(maxScroll * sin) - charOffset - 2 * gridStep;
yEnd = height - charOffset + 2 * gridStep;
} else {
yStart = -charOffset - 2 * gridStep;
yEnd = height - (maxScroll * sin) - charOffset + 2 * gridStep;
}
// Determine x range
let xStart, xEnd;
if (cos >= 0) {
xStart = -(maxScroll * cos) - charOffset - 2 * gridStep;
xEnd = width - charOffset + 2 * gridStep;
} else {
xStart = -charOffset - 2 * gridStep;
xEnd = width - (maxScroll * cos) - charOffset + 2 * gridStep;
}
// Precompute the scroll offset components
const xScrollOffset = bgScrollOffset * cos;
const yScrollOffset = bgScrollOffset * sin;
// Save the context state once
bgCtx.save();
// Outer loop for y-axis
for (let y = yStart; y < yEnd; y += gridStep) {
const yCanvasPos = y + charOffset + yScrollOffset;
// Inner loop for x-axis
for (let x = xStart; x < xEnd; x += gridStep) {
const xCanvasPos = x + charOffset + xScrollOffset;
// Apply transformation
bgCtx.setTransform(cos, sin, -sin, cos, xCanvasPos, yCanvasPos);
// Draw the text
bgCtx.fillText(">", 0, 0);
}
}
// Restore the context state once
bgCtx.restore();
console.timeEnd("background");
}
This version maintains the nested loops but optimizes by:
- Precomputing the scroll offset components (
xScrollOffset and yScrollOffset)
- Minimizing calculations inside the loops
- Ensuring the loop boundaries are optimized to cover only the necessary area
Unfortunately, we can't easily eliminate the nested loops in this case because each symbol needs its own transformation. The transformation matrix rotates each symbol around its own center, which requires individual positioning for each symbol.
If you want to further optimize this, you could consider:
- Pre-rendering the entire background pattern to an off-screen canvas and then just applying a translation to that canvas
- Using WebGL for rendering, which would be much more efficient for this type of transformation
- Using CSS transforms instead of canvas transformations, which might be hardware-accelerated
But within the constraints of the current approach, this optimized version should provide good performance while ensuring full coverage of the background.