Advertisement
DayDun

Outline

Sep 24th, 2018
2,888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. canvas.width = window.innerWidth;
  4. canvas.height = window.innerHeight;
  5.  
  6. ctx.translate(50, 50);
  7.  
  8. let scale = 16;
  9.  
  10. let gwidth = 16;
  11. let gheight = 16;
  12.  
  13. let palette = [
  14.     "#fff",
  15.     "#f00",
  16.     "#0f0"
  17. ];
  18.  
  19. let grid = new Uint32Array([
  20.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21.     0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  22.     0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23.     0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24.     0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25.     0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  26.     0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30.     0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31.     0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  32.     0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  33.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  36. ]);
  37.  
  38. function calcPath() {
  39.     let paths = [];
  40.    
  41.     function addPath(x1, y1, x2, y2) {
  42.         let start = true;
  43.         let end = true;
  44.         let c = [
  45.             [x1, y1],
  46.             [x2, y2]
  47.         ];
  48.         for (let i=0; i<paths.length; i++) {
  49.             if (
  50.                 start &&
  51.                 paths[i][paths[i].length - 1][0] === c[0][0] &&
  52.                 paths[i][paths[i].length - 1][1] === c[0][1]
  53.             ) {
  54.                 start = false;
  55.                 paths[i].pop();
  56.                 c = paths[i] = paths[i].concat(c);
  57.                 continue;
  58.             }
  59.            
  60.             if (
  61.                 end &&
  62.                 paths[i][0][0] === c[c.length - 1][0] &&
  63.                 paths[i][0][1] === c[c.length - 1][1]
  64.             ) {
  65.                 end = false;
  66.                 c.pop();
  67.                 c = paths[i] = c.concat(paths[i]);
  68.             }
  69.         }
  70.        
  71.         if (start && end) {
  72.             paths.push(c);
  73.         }
  74.        
  75.         //console.log(JSON.stringify(paths).split(",[["));
  76.     }
  77.    
  78.     for (let y=0; y<gheight; y++) {
  79.         for (let x=0; x<gwidth; x++) {
  80.             let i = y * gwidth + x;
  81.             let px = grid[i];
  82.             if (!px) continue;
  83.            
  84.             //console.log(x, y);
  85.            
  86.             if (grid[i - gwidth] !== px) { // top
  87.                 addPath(x, y, x + 1, y);
  88.             }
  89.            
  90.             if (grid[i + 1] !== px) { // right
  91.                 addPath(x + 1, y, x + 1, y + 1);
  92.             }
  93.            
  94.             if (grid[i - 1] !== px) { // left
  95.                 addPath(x, y + 1, x, y);
  96.             }
  97.            
  98.             if (grid[i + gwidth] !== px) { // bottom
  99.                 addPath(x + 1, y + 1, x, y + 1);
  100.             }
  101.         }
  102.     }
  103.    
  104.     return paths;
  105. }
  106.  
  107. let path = calcPath();
  108.  
  109. function render() {
  110.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  111.    
  112.     ctx.beginPath();
  113.     for (let i=0; i<path.length; i++) {
  114.         ctx.moveTo(path[i][0][0] * scale, path[i][0][1] * scale);
  115.         for (let j=1; j<path[i].length; j++) {
  116.             ctx.lineTo(path[i][j][0] * scale, path[i][j][1] * scale);
  117.         }
  118.     }
  119.    
  120.     ctx.fillStyle = palette[1];
  121.     ctx.fill();
  122.     ctx.stroke();
  123.    
  124.     window.requestAnimationFrame(render);
  125. }
  126.  
  127. window.requestAnimationFrame(render);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement