Advertisement
here2share

Nearest Pixels.html

Apr 7th, 2023
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <meta charset="UTF-8">
  5.     <title>Nearest Pixels</title>
  6.     <style>
  7.       canvas {
  8.         border: 1px solid black;
  9.         image-rendering: pixelated;
  10.       }
  11.     </style>
  12.   </head>
  13.   <body>
  14.     <canvas width="580" height="580" id="canvas"></canvas>
  15.  
  16.     <script>
  17.       const canvas = document.getElementById("canvas");
  18.       const ctx = canvas.getContext("2d");
  19.      
  20.       const pixelSize = 5; // size of each pixel
  21.       const numPixels = canvas.width / pixelSize; // number of pixels in each row/column
  22.      
  23.       // create a 2D array to hold the pixel colors
  24.       const pixels = new Array(numPixels);
  25.       for (let i = 0; i < numPixels; i++) {
  26.         pixels[i] = new Array(numPixels);
  27.       }
  28.      
  29.       // initialize the pixel colors with random values
  30.       for (let i = 0; i < numPixels; i++) {
  31.         for (let j = 0; j < numPixels; j++) {
  32.           pixels[i][j] = getRandomColor();
  33.         }
  34.       }
  35.      
  36.       // draw the pixels on the canvas
  37.       for (let i = 0; i < numPixels; i++) {
  38.         for (let j = 0; j < numPixels; j++) {
  39.           ctx.fillStyle = blendColors(i, j);
  40.           ctx.fillRect(i * pixelSize, j * pixelSize, pixelSize, pixelSize);
  41.         }
  42.       }
  43.      
  44.       // a helper function to get a random color
  45.       function getRandomColor() {
  46.         const red = Math.floor(Math.random() * 256);
  47.         const green = Math.floor(Math.random() * 256);
  48.         const blue = Math.floor(Math.random() * 256);
  49.         return `rgb(${red},${green},${blue})`;
  50.       }
  51.      
  52.       // a function to blend the color of a pixel with its neighbors
  53.       function blendColors(x, y) {
  54.         const top = y <= 0 ? y + numPixels - 1 : y - 1;
  55.         const left = x <= 0 ? x + numPixels - 1 : x - 1;
  56.         const right = x >= numPixels - 1 ? x - numPixels + 1 : x + 1;
  57.         const bottom = y >= numPixels - 1 ? y - numPixels + 1 : y + 1;
  58.        
  59.         const neighborColors = [
  60.           pixels[left][top],
  61.           pixels[x][top],
  62.           pixels[right][top],
  63.           pixels[left][y],
  64.           pixels[right][y],
  65.           pixels[left][bottom],
  66.           pixels[x][bottom],
  67.           pixels[right][bottom]
  68.         ];
  69.        
  70.         // calculate the average color of the pixel and its neighbors
  71.         let sumR = 0;
  72.         let sumG = 0;
  73.         let sumB = 0;
  74.         for (const neighborColor of neighborColors) {
  75.           const rgba = neighborColor.replace(/[^\d,]/g, "").split(",");
  76.           sumR += parseInt(rgba[0]);
  77.           sumG += parseInt(rgba[1]);
  78.           sumB += parseInt(rgba[2]);
  79.         }
  80.         const avgR = Math.round(sumR / neighborColors.length);
  81.         const avgG = Math.round(sumG / neighborColors.length);
  82.         const avgB = Math.round(sumB / neighborColors.length);
  83.         return `rgb(${avgR},${avgG},${avgB})`;
  84.       }
  85.     </script>
  86.   </body>
  87. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement