Here's a complete implementation of the random anime pixel face generator using HTML, CSS, and JavaScript, with the specified layout and requirements. It encapsulates facial features such as hair, eyes, nose, mouth, and an outline, while adhering to the specified grid structure.
You can copy and paste this code into an HTML file and open it in a web browser to see it in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Anime Pixel Face Generator</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: #ffffff;
}
#grid {
display: grid;
grid-template-columns: repeat(17, 20px);
grid-template-rows: repeat(16, 20px);
margin: 20px;
border: 1px solid #000;
}
.pixel {
width: 20px;
height: 20px;
box-sizing: border-box;
border: 1px solid #f0f0f0;
background-color: white;
}
button {
margin: 10px;
}
</style>
</head>
<body>
<div id="grid"></div>
<button onclick="generateFace()">Generate Face</button>
<button onclick="clearGrid()">Clear Grid</button>
<script>
const gridSize = { rows: 16, cols: 17 };
// Define hairstyle, eye, nose, mouth indices based on provided schema
const hairstyles = [
[ 0, 4], [ 0, 5], [ 0, 6], [ 0, 7], [ 0, 8], [ 0, 9], [ 0, 10], [ 0, 11], [ 0, 12],
[ 1, 3], [ 1, 4], [ 1, 5], [ 1, 6], [ 1, 7], [ 1, 8], [ 1, 9], [ 1, 10], [ 1, 11], [ 1, 12], [ 1, 13],
[ 2, 2], [ 2, 3], [ 2, 4], [ 2, 5], [ 2, 6], [ 2, 7], [ 2, 8], [ 2, 9], [ 2, 10], [ 2, 11], [ 2, 12], [ 2, 13],
[ 3, 1], [ 3, 2], [ 3, 3], [ 3, 4], [ 3, 5], [ 3, 6], [ 3, 7], [ 3, 8], [ 3, 9], [ 3, 10], [ 3, 11], [ 3, 12], [ 3, 13], [ 3, 14],
[ 4, 0], [ 4, 1], [ 4, 2], [ 4, 3], [ 4, 4], [ 4, 5], [ 4, 6], [ 4, 7], [ 4, 8], [ 4, 9], [ 4, 10], [ 4, 11], [ 4, 12], [ 4, 13], [ 4, 14], [ 4, 15]
];
const eyeIndices = [
[ 7, 3], [ 7, 4], [ 7, 5], [ 7, 6], [ 7, 7], [ 7, 8], [ 7, 9], [ 7, 10],
[ 8, 3], [ 8, 4], [ 8, 5], [ 8, 6], [ 8, 7], [ 8, 8], [ 8, 9], [ 8, 10],
[ 9, 3], [ 9, 4], [ 9, 5], [ 9, 6], [ 9, 7], [ 9, 8], [ 9, 9], [ 9, 10]
];
const noseIndices = [
[10, 7], [10, 8], [10, 9]
];
const mouthIndices = [
[12, 4], [12, 5], [12, 6], [12, 7], [12, 8], [12, 9],
[13, 5], [13, 6], [13, 7], [13, 8], [13, 9],
[14, 6], [14, 7], [14, 8]
];
// Colors
const colors = {
hair: ['#8B4513', '#A52A2A', '#CD5C5C', '#D2691E'],
eyes: ['#000000', '#0000FF', '#00FFFF', '#FFFFFF'],
nose: ['#000000'],
mouth: ['#000000', '#8B0000'],
outline: ['#000000']
};
// Create grid
const gridElement = document.getElementById('grid');
let pixels = [];
for (let r = 0; r < gridSize.rows; r++) {
const row = [];
for (let c = 0; c < gridSize.cols; c++) {
const pixel = document.createElement('div');
pixel.className = 'pixel';
gridElement.appendChild(pixel);
row.push(pixel);
}
pixels.push(row);
}
function clearGrid() {
for (let r = 0; r < gridSize.rows; r++) {
for (let c = 0; c < gridSize.cols; c++) {
pixels[r][c].style.backgroundColor = 'white';
}
}
}
function getRandomColor(colorArray) {
return colorArray[Math.floor(Math.random() * colorArray.length)];
}
function generateFace() {
clearGrid();
// Hair
hairstyles.forEach(index => {
const [row, col] = index;
pixels[row][col].style.backgroundColor = getRandomColor(colors.hair);
});
// Eyes
eyeIndices.forEach(index => {
const [row, col] = index;
pixels[row][col].style.backgroundColor = getRandomColor(colors.eyes);
});
// Nose
noseIndices.forEach(index => {
const [row, col] = index;
pixels[row][col].style.backgroundColor = getRandomColor(colors.nose);
});
// Mouth
mouthIndices.forEach(index => {
const [row, col] = index;
pixels[row][col].style.backgroundColor = getRandomColor(colors.mouth);
});
// Outline around the face
for (let r = 5; r < gridSize.rows; r++) {
pixels[r][0].style.backgroundColor = colors.outline[0];
pixels[r][16].style.backgroundColor = colors.outline[0];
}
for (let c = 0; c < gridSize.cols; c++) {
pixels[5][c].style.backgroundColor = colors.outline[0];
}
}
</script>
</body>
</html>
Summary of Code Structure:
-
HTML Structure:
- A container div (
#grid) holds the 17x16 pixel grid.
- Buttons are included to generate a new face or clear the grid.
-
CSS Styles:
- The grid is created using CSS Grid Layout, with each pixel element having defined sizes and borders.
-
JavaScript Logic:
- The JavaScript part initializes the grid and provides functionalities to generate a random face.
- The specific indices for hairstyles, eyes, nose, and mouth are defined according to your requirements.
- Each facial feature area is color-filled based on random selection from predefined color arrays.
How to Test:
- Copy and paste the code into an HTML file (e.g.,
index.html).
- Open the file in a web browser, and click on "Generate Face" to see randomly generated pixel faces. The "Clear Grid" button resets the grid to white.