Here's a complete HTML, CSS, and JavaScript example for a random anime pixel face generator following the specified schema. This code sets up a grid layout and generates a random face based on the provided features' indices.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Anime Pixel Face Generator</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
#grid {
display: grid;
grid-template-columns: repeat(17, 20px);
grid-template-rows: repeat(16, 20px);
gap: 1px;
}
.pixel {
width: 20px;
height: 20px;
background-color: white;
border: 1px solid #e0e0e0;
}
</style>
</head>
<body>
<div id="grid"></div>
<button onclick="generateFace()">Generate Face</button>
<script>
const hairColors = ['#8B4513', '#A52A2A', '#CD5C5C', '#D2691E'];
const eyeColors = ['#000000', '#0000FF', '#00FFFF', '#FFFFFF'];
const noseColor = '#000000';
const mouthColors = ['#000000', '#8B0000'];
const outlineColor = '#000000';
const hairIndices = [...Array(17).keys()].map(i => [0, 1, 2, 3, 4].map(r => [r, i]));
const eyeIndices = [...Array(2).keys()].map(i => [3, i + 7]);
const noseIndices = [[7, 8], [7, 9]];
const mouthIndices = [...Array(3).keys()].map(i => [6 + i, 6, 7, 8, 9, 10 + i]);
const outlineIndices = [...Array(16).keys()].map(r => {
if (r < 5) {
return [...Array(17).keys()].map(c => [r, c]);
}
return [[r, 0], [r, 16]];
}).flat().concat([
...[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].map(r => [[r, 1], [r, 15]]),
...[11, 10, 9, 8, 7].map((r, i) => [[10 - i, 15 - i], [10 - i, 1 + i]])
]);
// Clear grid function
function clearGrid() {
const pixels = document.querySelectorAll('.pixel');
pixels.forEach(pixel => {
pixel.style.backgroundColor = 'white';
});
}
// Get random color from array
function getRandomColor(colorArray) {
return colorArray[Math.floor(Math.random() * colorArray.length)];
}
// Generate Face function
function generateFace() {
clearGrid();
// Draw Outline
outlineIndices.forEach(index => {
document.querySelector(`#grid .pixel:nth-child(${index[0] * 17 + index[1] + 1})`).style.backgroundColor = outlineColor;
});
// Draw Hair
hairIndices.forEach(index => {
document.querySelector(`#grid .pixel:nth-child(${index[0][0] * 17 + index[0][1] + 1})`).style.backgroundColor = getRandomColor(hairColors);
});
// Draw Eyes
eyeIndices.forEach(index => {
document.querySelector(`#grid .pixel:nth-child(${index[0] * 17 + index[1] + 1})`).style.backgroundColor = getRandomColor(eyeColors);
});
// Draw Nose
noseIndices.forEach(index => {
document.querySelector(`#grid .pixel:nth-child(${index[0][0] * 17 + index[0][1] + 1})`).style.backgroundColor = noseColor;
});
// Draw Mouth
mouthIndices.forEach(index => {
index.forEach(col => {
document.querySelector(`#grid .pixel:nth-child(${col * 17 + col + 1})`).style.backgroundColor = getRandomColor(mouthColors);
});
});
}
// Initialize grid
const grid = document.getElementById('grid');
for (let i = 0; i < 272; i++) { // 17 x 16 = 272 pixels
const pixel = document.createElement('div');
pixel.className = 'pixel';
grid.appendChild(pixel);
}
</script>
</body>
</html>
Explanation:
- The HTML structure includes a grid for the pixel representation of the face and a button to generate random faces.
- The CSS styles define the size of the grid and individual pixels.
- JavaScript functions handle clearing the grid, selecting random colors from predefined arrays, and populating the grid with facial features based on specified indices. The face generation considers facial outlines, hair, eyes, a nose, and a mouth.
In this implementation:
- For every button click, the current grid is cleared and a new random face is generated, respecting the specified indices for features.
- You can simply copy this code into an HTML file and open it in a browser to see the random anime pixel faces being generated.