Advertisement
Guest User

Drawing thing

a guest
Oct 17th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (async function() {
  2.     var canvas = document.createElement("canvas");
  3.  
  4.     canvas.width = 256;
  5.     canvas.height = 128;
  6.     canvas.style.imageRendering = "pixelated";
  7.  
  8.     while (document.body.firstChild)
  9.         document.body.removeChild(document.body.firstChild);
  10.     document.body.appendChild(canvas);
  11.  
  12.     var ctx = canvas.getContext("2d");
  13.  
  14.     var data = ctx.createImageData(256, 128);
  15.  
  16.     var colors = new Int16Array(32768);
  17.    
  18.     for (let i = 0; i < 32768; i++)
  19.         colors[i] = i;
  20.    
  21.     for (let j, i = 32767; i >= 0; i--) {
  22.         j = Math.random() * (i + 1) | 0;
  23.         [colors[i], colors[j]] = [colors[j], colors[i]];
  24.     }
  25.    
  26.     var unused = [...colors];
  27.     var original = Array(16);
  28.    
  29.     var pixels = new Int16Array(32768).fill(-1);
  30.    
  31.     var to_pos = function(pixel) {
  32.         return [pixel % 256, pixel / 256 | 0];
  33.     };
  34.    
  35.     var from_pos = function(pos) {
  36.         return pos[1] * 256 + pos[0];
  37.     };
  38.    
  39.     var find_color = function(color) {
  40.         var color_dist = function(from, to) {
  41.             var red = Math.abs(((from & 31744) >> 10) - ((to & 31744) >> 10));
  42.             var green = Math.abs(((from & 992) >> 5) - ((to & 992) >> 5));
  43.             var blue = Math.abs((from & 31) - (to & 31));
  44.  
  45.             return red + green + blue;
  46.         };
  47.        
  48.         var max = [0, Infinity];
  49.        
  50.         for (let d, i = 0; i < unused.length; i++) {
  51.             d = color_dist(color, unused[i]) + (Math.random() * 8 | 0);
  52.  
  53.             if (d <= max[1])
  54.                 max = [i, d];
  55.         }
  56.  
  57.         color = unused[max[0]];
  58.        
  59.         unused = unused.filter(u => u != color);
  60.        
  61.         return color;
  62.     };
  63.    
  64.     var average_colors = function(pos) {
  65.         var weighted = original.map(c => [c[0], 2 ** (Math.sqrt((pos[0] - c[1][0]) ** 2 + (pos[1] - c[1][1]) ** 2) / 20)]);
  66.        
  67.         var a_red = Math.round(weighted.reduce((a, w) => a + ((w[0] & 31744) >> 10) / w[1], 0) / weighted.reduce((a, w) => a + 1 / w[1], 0));
  68.         var a_green = Math.round(weighted.reduce((a, w) => a + ((w[0] & 992) >> 5) / w[1], 0) / weighted.reduce((a, w) => a + 1 / w[1], 0));
  69.         var a_blue = Math.round(weighted.reduce((a, w) => a + (w[0] & 31) / w[1], 0) / weighted.reduce((a, w) => a + 1 / w[1], 0));
  70.        
  71.         return (a_red << 10) | (a_green << 5) | a_blue;
  72.     };
  73.    
  74.     var find_pixel = function() {
  75.         var pixel;
  76.        
  77.         do
  78.             pixel = Math.random() * 32768 | 0;
  79.         while (pixels[pixel] != -1);
  80.        
  81.         return pixel;
  82.     };
  83.    
  84.     var to_hex = function(not_hex) {
  85.         var red = ((not_hex & 31744) >> 7).toString(16).padStart(2, "0");
  86.         var green = ((not_hex & 992) >> 2).toString(16).padStart(2, "0");
  87.         var blue = ((not_hex & 31) << 3).toString(16).padStart(2, "0");
  88.        
  89.         return "#" + red + green + blue;
  90.     };
  91.    
  92.     for (let p, i = 0; i < 16; i++) {
  93.         original[i] = [Math.random() * 32768 | 0, to_pos(Math.random() * 32768 | 0)];
  94.     }
  95.    
  96.     await new Promise(resolve => setTimeout(resolve, 0));
  97.    
  98.     for (let p, i = 0; i < 32768; i++) {
  99.         ctx.fillStyle = to_hex(pixels[p = find_pixel()] = average_colors(to_pos(p)));
  100.         ctx.fillRect(...to_pos(p), 1, 1);
  101.        
  102.         if (!(i % 64)) {
  103.             await new Promise(resolve => setTimeout(resolve, 0));
  104.         }
  105.     }
  106.    
  107.     console.log("Done");
  108. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement