Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <style>
- body {
- display: flex;
- justify-content: center;
- align-items: flex-start;
- height: 100vh;
- margin: 0;
- background-color: #000;
- }
- #grid {
- display: grid;
- grid-template-columns: repeat(40, 15px);
- grid-template-rows: repeat(40, 15px);
- width: 600px;
- height: 600px;
- margin-top: 2px;
- }
- .cell {
- width: 15px;
- height: 15px;
- }
- </style>
- <title>Gradient Grid</title>
- </head>
- <body>
- <div id="grid"></div>
- <script>
- const WIDTH = 600;
- const HEIGHT = 600;
- const CELL_SIZE = 15;
- const GRID_SIZE = WIDTH / CELL_SIZE;
- const num_steps = GRID_SIZE * GRID_SIZE;
- const step_size = Math.floor((256 ** 3) / (num_steps - 1));
- const grid = document.getElementById('grid');
- function convertIntToRgb(value) {
- let b = value % 256;
- value = Math.floor(value / 256);
- let g = value % 256;
- value = Math.floor(value / 256);
- let r = value % 256;
- return `rgb(${r}, ${g}, ${b})`;
- }
- let COLORS = [];
- let steps = 0;
- while (COLORS.length < num_steps) {
- COLORS.push(convertIntToRgb(steps));
- steps += step_size;
- }
- COLORS[COLORS.length - 1] = 'rgb(255, 255, 255)';
- let cells = [];
- let initPattern = [];
- function pattern() {
- for (let i = 0; i < GRID_SIZE; i++) {
- let row = [];
- let initRow = [];
- for (let j = 0; j < GRID_SIZE; j++) {
- let div = document.createElement('div');
- let color = COLORS.pop();
- div.classList.add('cell');
- div.style.backgroundColor = color;
- grid.appendChild(div);
- row.push(div);
- initRow.push(color);
- }
- cells.push(row);
- initPattern.push(initRow);
- }
- }
- pattern();
- function colorDifference(color1, color2) {
- let [r1, g1, b1] = color1.match(/\d+/g).map(Number);
- let [r2, g2, b2] = color2.match(/\d+/g).map(Number);
- return Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2);
- }
- function findBestNeighbor(x, y) {
- const currentColor = cells[x][y].style.backgroundColor;
- let bestNeighbor = null;
- let target = null;
- let minDifference = Infinity;
- const neighbors = [[-1, 0], [1, 0], [0, -1], [0, 1]];
- for (let [dx, dy] of neighbors) {
- let x2 = x + dx;
- let y2 = y + dy;
- for (let i = -1; i <= 1; i++) {
- for (let j = 1; j <= 3; j++) {
- let nx = dx ? x2 + j * dx : x + i;
- let ny = dy ? y2 + j * dy : y + i;
- if (nx >= 0 && ny >= 0 && nx < GRID_SIZE && ny < GRID_SIZE) {
- let neighborColor = cells[nx][ny].style.backgroundColor;
- let difference = colorDifference(currentColor, neighborColor);
- if (difference < minDifference) {
- minDifference = difference;
- bestNeighbor = [x2, y2];
- target = [nx, ny];
- }
- }
- }
- }
- }
- return { bestNeighbor, target };
- }
- function shuffle(array) {
- for (let i = array.length - 1; i > 0; i--) {
- let j = Math.floor(Math.random() * (i + 1));
- [array[i], array[j]] = [array[j], array[i]];
- }
- }
- const XYo = [];
- for (let y = 0; y < GRID_SIZE; y++) {
- for (let x = 0; x < GRID_SIZE; x++) {
- XYo.push([x, y]);
- }
- }
- function plot() {
- shuffle(XYo);
- for (let [x, y] of XYo) {
- let { bestNeighbor, target } = findBestNeighbor(x, y);
- if (bestNeighbor && target) {
- let [nx, ny] = bestNeighbor;
- let [dx, dy] = target;
- let tempColor = cells[x][y].style.backgroundColor;
- cells[x][y].style.backgroundColor = cells[nx][ny].style.backgroundColor;
- cells[nx][ny].style.backgroundColor = tempColor;
- // requestAnimationFrame()
- }
- }
- }
- document.addEventListener('keydown', (event) => {
- if (event.code === 'Space') {
- for (let i = 0; i < GRID_SIZE; i++) {
- for (let j = 0; j < GRID_SIZE; j++) {
- // alert(1234);
- cells[i][j].style.backgroundColor = initPattern[i][j];
- }
- }
- }
- });
- setInterval(plot, 0);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement