Creating a centered pixel rhombus in HTML, CSS, and JavaScript while ensuring each pixel is accurately positioned to adhere to varying scales can be achieved with the following approach. The provided example implements a canvas where each "pixel" is a square with a size based on the defined scales, and uses a color array to apply random colors to each index.
Here's the code:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Pixel Rhombus</title>
<style>
body, html {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.canvas {
position: relative;
width: 300px; /* Set a width suitable for your design */
height: 300px; /* Adjust height according to the design */
}
.pixel {
position: absolute;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="canvas" id="canvas"></div>
<script>
const scalarRhombus = [
" 0 ",
" 010 ",
" 01210 ",
" 0123210 ",
" 012343210 ",
" 01234543210 ",
" 0123456543210 ",
"012345676543210",
" 0123456543210 ",
" 01234543210 ",
" 012343210 ",
" 0123210 ",
" 01210 ",
" 010 ",
" 0 ",
];
const scaleMapping = {
'0': 1,
'1': 2,
'2': 3,
'3': 4,
'4': 5,
'5': 6,
'6': 7,
'7': 8,
};
const colors = [
'#FF5733', '#33FF57', '#3357FF', '#F1C40F',
'#9B59B6', '#E74C3C', '#2980B9', '#D35400'
];
const canvas = document.getElementById('canvas');
const pixelSize = 30; // Base pixel size
function drawRhombus() {
const centerX = Math.floor(scalarRhombus[0].length / 2);
const centerY = Math.floor(scalarRhombus.length / 2);
scalarRhombus.forEach((row, i) => {
[...row].forEach((char, j) => {
if (char.trim() !== '') {
const scale = scaleMapping[char];
const size = pixelSize * scale; // Size based on scale
const pixel = document.createElement('div');
pixel.classList.add('pixel');
pixel.style.width = `${size}px`;
pixel.style.height = `${size}px`;
pixel.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
const offsetX = (j - centerX) * pixelSize * scale + (size - pixelSize * scale) / 2;
const offsetY = (i - centerY) * pixelSize * scale + (size - pixelSize * scale) / 2;
pixel.style.left = `${canvas.clientWidth / 2 + offsetX}px`;
pixel.style.top = `${canvas.clientHeight / 2 + offsetY}px`;
canvas.appendChild(pixel);
}
});
});
}
drawRhombus();
</script>
</body>
</html>
Explanation:
- HTML Structure: The code creates a basic HTML structure with a single
div for the pixel canvas.
- Styling: CSS styles are set to ensure the body's margin and padding are zero, and the
canvas is centered both vertically and horizontally.
- JavaScript Logic:
- The
scalarRhombus array defines the rhombus shape with values indicating pixel scales.
- The
scaleMapping object maps each character to its corresponding scale.
- The
colors array contains colors to apply randomly to each pixel.
- The
drawRhombus function calculates the correct positioning for each pixel square based on its scale, ensuring that all pixels line up edge-to-edge, centered correctly.
- Position Calculation: For each character in the
scalarRhombus, it calculates the position based on its index, scaling appropriately and offsetting to make sure they are all centered.
This code will render a beautifully centered and scalable pixel rhombus made up of colorful squares. Adjust the pixelSize and canvas dimensions as necessary for your specific design requirements!