<script>
const radialDownscaleFromTheCenter = [
" 0 ",
" 010 ",
" 01210 ",
" 0123210 ",
" 012343210 ",
" 01234543210 ",
" 0123456543210 ",
"012345676543210",
" 0123456543210 ",
" 01234543210 ",
" 012343210 ",
" 0123210 ",
" 01210 ",
" 010 ",
" 0 ",
];
const generateGrid = (structure) => {
const grid = document.getElementById('pixelGrid');
grid.innerHTML = ''; // Clear existing elements
structure.forEach((row) => {
row.split('').forEach((character) => {
const div = document.createElement('div');
const size = getPixelSize(character); // Get size based on character
const pixel = document.createElement('div');
pixel.className = 'pixel'; // Set class for pixel styling
pixel.style.width = `${size}px`; // Set dynamic width
pixel.style.height = `${size}px`; // Set dynamic height
pixel.style.backgroundColor = getPixelColor(character); // Set color
div.className = 'grid-item'; // Assign grid item class
div.appendChild(pixel); // Append pixel to grid item
grid.appendChild(div); // Append grid item to grid
});
});
};
const getPixelSize = (char) => {
switch (char) {
case '0': return 10; // Size for center
case '1': return 15; // Size for first ring
case '2': return 20; // Size for second ring
case '3': return 25; // Size for third ring
case '4': return 30; // Size for fourth ring
case '5': return 35; // Size for fifth ring
case '6': return 40; // Size for sixth ring
case '7': return 45; // Size for seventh ring
case '8': return 50; // Size for eighth ring
case '9': return 55; // Size for ninth ring
default: return 0; // No size for undefined characters
}
};
const getPixelColor = (char) => {
switch (char) {
case '0': return '#FFC107'; // Center
case '1': return '#FF5722'; // First ring (eyes)
case '2': return '#8BC34A'; // Second ring (hair)
case '3': return '#3F51B5'; // Third ring (hair)
case '4': return '#FFEB3B'; // Fourth ring (hair)
case '5': return '#303F9F'; // Fifth ring (hair)
case '6': return '#FF5722'; // Sixth ring (eyes)
case '7': return '#FF5722'; // Seventh ring (eyes)
case '8': return '#FF5722'; // Eighth ring (eyes)
case '9': return '#FF5722'; // Ninth ring (eyes)
default: return 'transparent'; // No color
}
};
// Generate the radial downscale grid on load
generateGrid(radialDownscaleFromTheCenter);
</script>
To repeat the pixel grid as a background pattern across the entire viewport, you can take advantage of CSS's background property combined with a few styles. Since the original grid is intended to be dynamic, we'll create an additional function to generate a background image using the radialDownscaleFromTheCenter array, and then use it in the CSS.
We'll modify your existing code to include a method that can repeat the grid as a background pattern throughout the viewport. Here's how you can do that:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Radial Pixel Grid</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="150" height="150" viewBox="0 0 150 150">${generateSVGBackground(radialDownscaleFromTheCenter)}</svg>');
background-size: 150px 150px; /* Use the size of one complete grid */
background-repeat: repeat;
}
.grid {
display: grid;
grid-template-columns: repeat(15, 1fr); /* Adjust as necessary */
grid-template-rows: repeat(15, 1fr); /* Adjust as necessary */
gap: 0; /* No gap */
}
.grid-item {
display: flex; /* Use flexbox to center contents */
justify-content: center;
align-items: center;
position: relative; /* Position relative for child elements */
}
.pixel {
box-sizing: border-box;
border-radius: 50%; /* Make pixels circular */
}
</style>
</head>
<body>
<div class="grid" id="pixelGrid"></div>
<script>
const radialDownscaleFromTheCenter = [
" 0 ",
" 010 ",
" 01210 ",
" 0123210 ",
" 012343210 ",
" 01234543210 ",
" 0123456543210 ",
"012345676543210",
" 0123456543210 ",
" 01234543210 ",
" 012343210 ",
" 0123210 ",
" 01210 ",
" 010 ",
" 0 ",
];
const generateGrid = (structure) => {
const grid = document.getElementById('pixelGrid');
grid.innerHTML = ''; // Clear existing elements
structure.forEach((row) => {
row.split('').forEach((character) => {
const div = document.createElement('div');
const size = getPixelSize(character); // Get size based on character
const pixel = document.createElement('div');
pixel.className = 'pixel'; // Set class for pixel styling
pixel.style.width = `${size}px`; // Set dynamic width
pixel.style.height = `${size}px`; // Set dynamic height
pixel.style.backgroundColor = getPixelColor(character); // Set color
div.className = 'grid-item'; // Assign grid item class
div.appendChild(pixel); // Append pixel to grid item
grid.appendChild(div); // Append grid item to grid
});
});
};
const getPixelSize = (char) => {
switch (char) {
case '0': return 10; // Size for center
case '1': return 15; // Size for first ring
case '2': return 20; // Size for second ring
case '3': return 25; // Size for third ring
case '4': return 30; // Size for fourth ring
case '5': return 35; // Size for fifth ring
case '6': return 40; // Size for sixth ring
case '7': return 45; // Size for seventh ring
case '8': return 50; // Size for eighth ring
case '9': return 55; // Size for ninth ring
default: return 0; // No size for undefined characters
}
};
const getPixelColor = (char) => {
switch (char) {
case '0': return '#FFC107'; // Center
case '1': return '#FF5722'; // First ring (eyes)
case '2': return '#8BC34A'; // Second ring (hair)
case '3': return '#3F51B5'; // Third ring (hair)
case '4': return '#FFEB3B'; // Fourth ring (hair)
case '5': return '#303F9F'; // Fifth ring (hair)
case '6': return '#FF5722'; // Sixth ring (eyes)
case '7': return '#FF5722'; // Seventh ring (eyes)
case '8': return '#FF5722'; // Eighth ring (eyes)
case '9': return '#FF5722'; // Ninth ring (eyes)
default: return 'transparent'; // No color
}
};
const generateSVGBackground = (structure) => {
let svgPixels = '';
structure.forEach((row, rowIndex) => {
row.split('').forEach((character, colIndex) => {
const size = getPixelSize(character);
const color = getPixelColor(character);
const x = colIndex * (size + 1); // Add space for separation
const y = rowIndex * (size + 1); // Add space for separation
if(size > 0) {
svgPixels += `<rect x="${x}" y="${y}" width="${size}" height="${size}" fill="${color}" />`;
}
});
});
return svgPixels;
};
// Generate the radial downscale grid on load
generateGrid(radialDownscaleFromTheCenter);
</script>
</body>
</html>
Explanation of Changes:
Background Image: Added a background-image property to the body that uses an inline SVG. The SVG is generated dynamically based on the radialDownscaleFromTheCenter array, resulting in a pattern that repeats.
generateSVGBackground Function: This function constructs the SVG rectangles for each character in the pixel grid array, calculating the correct positions and sizes based on the same logic for colors and sizes you have in getPixelSize and getPixelColor.
Repeating the Background: The background-size property ensures each grid block is the size of one complete grid based on the pixel sizes.
With this setup, you get a repeating grid pattern as the background of your entire viewport, while still retaining the interactive grid in the center of the screen.
To repeat the grid structure in the entire viewport as a background, you can achieve this by setting the grid as a background image in a different way. Instead of using SVG, we can directly create a small representation of the pixel grid using HTML and CSS which will smoothly repeat itself across the viewport.
Here's how you can modify your code to achieve this effect:
Create a smaller version of the grid as a separate element and set it to repeat using CSS.
Use CSS Grid to style this smaller representation.
Below is the modified version of your original code:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Radial Pixel Grid</title>
<style>
body {
margin: 0;
height: 100vh;
overflow: hidden; /* To avoid scrollbars */
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
background-image: repeating-linear-gradient(to right, transparent, transparent 75px, rgba(240, 240, 240, 0.5) 75px, rgba(240, 240, 240, 0.5) 150px);
background-size: 150px 150px; /* Match these dimensions to the size of your grid */
}
.grid {
display: grid;
grid-template-columns: repeat(15, 1fr);
grid-template-rows: repeat(15, 1fr);
gap: 0;
}
.grid-item {
display: flex; /* Use flexbox to center contents */
justify-content: center;
align-items: center;
position: relative;
}
.pixel {
box-sizing: border-box;
border-radius: 50%; /* Make pixels circular */
}
</style>
</head>
<body>
<div class="grid" id="pixelGrid"></div>
<script>
const radialDownscaleFromTheCenter = [
" 0 ",
" 010 ",
" 01210 ",
" 0123210 ",
" 012343210 ",
" 01234543210 ",
" 0123456543210 ",
"012345676543210",
" 0123456543210 ",
" 01234543210 ",
" 012343210 ",
" 0123210 ",
" 01210 ",
" 010 ",
" 0 ",
];
const generateGrid = (structure) => {
const grid = document.getElementById('pixelGrid');
grid.innerHTML = ''; // Clear existing elements
structure.forEach((row) => {
row.split('').forEach((character) => {
const div = document.createElement('div');
const size = getPixelSize(character); // Get size based on character
const pixel = document.createElement('div');
pixel.className = 'pixel'; // Set class for pixel styling
pixel.style.width = `${size}px`; // Set dynamic width
pixel.style.height = `${size}px`; // Set dynamic height
pixel.style.backgroundColor = getPixelColor(character); // Set color
div.className = 'grid-item'; // Assign grid item class
div.appendChild(pixel); // Append pixel to grid item
grid.appendChild(div); // Append grid item to grid
});
});
};
const getPixelSize = (char) => {
switch (char) {
case '0': return 10; // Size for center
case '1': return 15; // Size for first ring
case '2': return 20; // Size for second ring
case '3': return 25; // Size for third ring
case '4': return 30; // Size for fourth ring
case '5': return 35; // Size for fifth ring
case '6': return 40; // Size for sixth ring
case '7': return 45; // Size for seventh ring
case '8': return 50; // Size for eighth ring
case '9': return 55; // Size for ninth ring
default: return 0; // No size for undefined characters
}
};
const getPixelColor = (char) => {
switch (char) {
case '0': return '#FFC107'; // Center
case '1': return '#FF5722'; // First ring (eyes)
case '2': return '#8BC34A'; // Second ring (hair)
case '3': return '#3F51B5'; // Third ring (hair)
case '4': return '#FFEB3B'; // Fourth ring (hair)
case '5': return '#303F9F'; // Fifth ring (hair)
case '6': return '#FF5722'; // Sixth ring (eyes)
case '7': return '#FF5722'; // Seventh ring (eyes)
case '8': return '#FF5722'; // Eighth ring (eyes)
case '9': return '#FF5722'; // Ninth ring (eyes)
default: return 'transparent'; // No color
}
};
// Generate the radial downscale grid on load
generateGrid(radialDownscaleFromTheCenter);
</script>
</body>
</html>
Key Changes Made:
Body Background: The CSS background-image is set to repeating-linear-gradient, which creates an alternating pattern for the grid. Adjust background-size to match the dimensions of your grid.
Grid Configuration: The grid-template-columns and grid-template-rows remain intact as it will control how each pixel is rendered in the grid itself.
Removed SVG Background Logic: This approach simplifies the rendering by focusing purely on CSS, improving performance and reducing complexity.
Result:
This setup will fill the entire viewport with a repeating pattern that resembles your original pixel grid design while maintaining it as a separate interactive element in the center. The exact pixel sizes and colors are taken from your original grid configuration. Adjust the background-size as needed to match your desired repetition rate.