Jan 11, 2025 • 5 min read
new
test
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid Selector</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Arial', sans-serif;
line-height: 1.6;
background-color: #f0f2f5;
color: #333;
}
.container {
max-width: 1000px;
margin: 40px auto;
padding: 20px;
background-color: white;
border-radius: 12px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #1a73e8;
margin-bottom: 20px;
font-size: 2.5em;
}
.settings {
text-align: center;
margin-bottom: 20px;
padding: 15px;
background-color: #f8f9fa;
border-radius: 8px;
}
.settings label {
font-size: 1.1em;
margin-right: 10px;
}
.settings input {
width: 60px;
padding: 5px;
font-size: 1em;
margin-right: 10px;
}
.grid-container {
display: flex;
justify-content: center;
margin-bottom: 30px;
overflow-x: auto;
padding: 10px;
}
.grid {
display: inline-grid;
gap: 8px;
padding: 20px;
background-color: #f8f9fa;
border-radius: 8px;
}
.cell {
width: 60px;
height: 60px;
border: 2px solid #dadce0;
border-radius: 8px;
cursor: pointer;
background-color: white;
transition: all 0.2s ease-in-out;
}
.cell:hover {
transform: scale(1.05);
border-color: #1a73e8;
}
.cell.selected {
background-color: #1a73e8;
border-color: #1a73e8;
}
.buttons-container {
display: flex;
justify-content: center;
gap: 12px;
margin-bottom: 20px;
flex-wrap: wrap;
}
button {
padding: 12px 24px;
font-size: 16px;
cursor: pointer;
border: none;
border-radius: 6px;
transition: all 0.2s ease;
font-weight: 500;
}
button:hover {
transform: translateY(-2px);
}
#nextBtn {
background-color: #1a73e8;
color: white;
}
#finishBtn {
background-color: #34a853;
color: white;
}
#resetBtn {
background-color: #ea4335;
color: white;
}
#copyBtn {
background-color: #fbbc04;
color: white;
display: none;
}
#output {
margin-top: 20px;
padding: 20px;
border: 1px solid #dadce0;
border-radius: 8px;
background-color: #f8f9fa;
font-family: monospace;
white-space: pre-wrap;
display: none;
max-height: 300px;
overflow-y: auto;
word-break: break-all;
}
.grid-counter {
text-align: center;
margin-bottom: 20px;
font-size: 1.2em;
color: #5f6368;
}
@media (max-width: 600px) {
.cell {
width: 40px;
height: 40px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Grid Selector</h1>
<div class="settings">
<label for="gridSize">Grid Size:</label>
<input type="number" id="gridSize" min="2" max="10" value="5">
<button id="updateSize">Update Grid</button>
</div>
<div class="grid-counter">
Saved Grids: <span id="gridCount">0</span>
</div>
<div class="grid-container">
<div class="grid" id="grid"></div>
</div>
<div class="buttons-container">
<button id="nextBtn">Next Grid</button>
<button id="finishBtn">Finish</button>
<button id="resetBtn">Reset All</button>
<button id="copyBtn">Copy Arrays</button>
</div>
<div id="output"></div>
</div>
<script>
let gridSize = 5;
let currentGrid = [];
let allGrids = [];
function initializeGrid(size) {
gridSize = size;
const gridElement = document.getElementById('grid');
gridElement.style.gridTemplateColumns = `repeat(${size}, 60px)`;
gridElement.innerHTML = '';
for (let i = 0; i < size * size; i++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.row = Math.floor(i / size);
cell.dataset.col = i % size;
cell.addEventListener('click', toggleCell);
gridElement.appendChild(cell);
}
currentGrid = Array(size).fill().map(() => Array(size).fill(0));
}
function isDuplicateGrid(newGrid) {
return allGrids.some(existingGrid =>
JSON.stringify(existingGrid) === JSON.stringify(newGrid)
);
}
function updateGridCounter() {
document.getElementById('gridCount').textContent = allGrids.length;
}
function toggleCell(e) {
const row = parseInt(e.target.dataset.row);
const col = parseInt(e.target.dataset.col);
currentGrid[row][col] = currentGrid[row][col] === 0 ? 1 : 0;
e.target.classList.toggle('selected');
}
document.getElementById('updateSize').addEventListener('click', () => {
const newSize = parseInt(document.getElementById('gridSize').value);
if (newSize >= 2 && newSize <= 10) {
if (confirm('Changing grid size will reset all saved grids. Continue?')) {
allGrids = [];
initializeGrid(newSize);
updateGridCounter();
document.getElementById('output').style.display = 'none';
document.getElementById('copyBtn').style.display = 'none';
}
} else {
alert('Please enter a grid size between 2 and 10');
}
});
document.getElementById('nextBtn').addEventListener('click', () => {
if (!isDuplicateGrid(currentGrid)) {
allGrids.push(JSON.parse(JSON.stringify(currentGrid)));
clearGrid();
updateGridCounter();
} else {
alert('This grid pattern already exists!');
}
});
document.getElementById('finishBtn').addEventListener('click', () => {
if (currentGrid.some(row => row.some(cell => cell === 1)) && !isDuplicateGrid(currentGrid)) {
allGrids.push(JSON.parse(JSON.stringify(currentGrid)));
updateGridCounter();
}
const output = document.getElementById('output');
output.style.display = 'block';
// Format arrays without newlines for inner arrays
//const formattedArrays = JSON.stringify(allGrids, null, 4);
const formattedArrays = JSON.stringify(allGrids).replace(/\],\[/g, '],\n[');
output.textContent = formattedArrays;
document.getElementById('copyBtn').style.display = 'block';
});
document.getElementById('resetBtn').addEventListener('click', () => {
if (confirm('Are you sure you want to reset all grids?')) {
allGrids = [];
clearGrid();
document.getElementById('output').style.display = 'none';
document.getElementById('copyBtn').style.display = 'none';
updateGridCounter();
}
});
document.getElementById('copyBtn').addEventListener('click', () => {
navigator.clipboard.writeText(JSON.stringify(allGrids))
.then(() => {
const btn = document.getElementById('copyBtn');
btn.textContent = 'Copied!';
setTimeout(() => {
btn.textContent = 'Copy Arrays';
}, 2000);
})
.catch(err => console.error('Failed to copy:', err));
});
function clearGrid() {
currentGrid = Array(gridSize).fill().map(() => Array(gridSize).fill(0));
document.querySelectorAll('.cell').forEach(cell => {
cell.classList.remove('selected');
});
}
// Initialize the grid on load
initializeGrid(gridSize);
</script>
</body>
</html>