Advertisement
here2share

Nearest Pixels Animation.html

Apr 7th, 2023 (edited)
1,051
0
Never
1
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 Animation</title>
  6.     <style>
  7.       canvas {
  8.         border: 1px solid black;
  9.         image-rendering: pixelated;
  10.       }
  11.     </style>
  12.   </head>
  13.   <body>
  14.     <canvas width="540" height="540" id="canvas"></canvas>
  15.  
  16.     <script>
  17.       const canvas = document.getElementById("canvas");
  18.       const ctx = canvas.getContext("2d");
  19.  
  20.       const pixelSize = 3; // size of each pixel
  21.       const numPixels = Math.floor(canvas.width / pixelSize); // number of pixels in each row/column
  22.      
  23.       // create a 2D array to hold the pixel colors
  24.       const pixels = Array.from({length:numPixels}, _ => Array.from({length:numPixels}, _ => getRandomColor()));
  25.      
  26.       // draw the pixels on the canvas
  27.       function drawPixels() {
  28.         for (let i = 0; i < numPixels; i++) {
  29.           for (let j = 0; j < numPixels; j++) {
  30.             ctx.fillStyle = blendColors(i, j);
  31.             ctx.fillRect(i * pixelSize, j * pixelSize, pixelSize, pixelSize);
  32.           }
  33.         }
  34.         requestAnimationFrame(drawPixels);
  35.       }    
  36.       requestAnimationFrame(drawPixels);
  37.      
  38.       // a helper function to get a random value between -1.0 and 1.0 for each component
  39.       function getRandomColor() {
  40.         return {
  41.           r: Math.random() * 2 - 1,
  42.           g: Math.random() * 2 - 1,
  43.           b: Math.random() * 2 - 1
  44.         };
  45.       }
  46.      
  47.       // a function to blend the color of a pixel with its neighbors
  48.       function blendColors(x, y) {
  49.         const top = y <= 0 ? y + numPixels - 1 : y - 1;
  50.         const left = x <= 0 ? x + numPixels - 1 : x - 1;
  51.         const right = x >= numPixels - 1 ? x - numPixels + 1 : x + 1;
  52.         const bottom = y >= numPixels - 1 ? y - numPixels + 1 : y + 1;
  53.  
  54.         const neighborColors = [
  55.           pixels[left][top],
  56.           pixels[x][top],
  57.           pixels[right][top],
  58.           pixels[left][y],
  59.           pixels[right][y],
  60.           pixels[left][bottom],
  61.           pixels[x][bottom],
  62.           pixels[right][bottom]
  63.         ];
  64.         // calculate the average color of the pixel and its neighbors for each component
  65.         const [rAvg,gAvg,bAvg] = neighborColors.reduce((acc,nc) => [acc[0]+ nc.r, acc[1]+ nc.g, acc[2]+ nc.b], [0,0,0]).map(avg => avg / neighborColors.length);
  66.      
  67.         // update the color of the pixel with a random value between -0.1 and 0.1 for each component
  68.         pixels[x][y] = {
  69.           r: Math.min(1, Math.max(-1, rAvg + (Math.random() - 0.5) * 0.2)),
  70.           g: Math.min(1, Math.max(-1, gAvg + (Math.random() - 0.5) * 0.2)),
  71.           b: Math.min(1, Math.max(-1, bAvg + (Math.random() - 0.5) * 0.2))
  72.         };
  73.        
  74.         // map the color values to RGB values between 0 and 255
  75.         const r = Math.round((pixels[x][y].r + 1) * 127.5);
  76.         const g = Math.round((pixels[x][y].g + 1) * 127.5);
  77.         const b = Math.round((pixels[x][y].b + 1) * 127.5);
  78.         return `rgb(${r},${g},${b})`;
  79.       }
  80.     </script>
  81.   </body>
  82. </html>
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement