Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. // Welcome! require() some modules from npm (like you were using browserify)
  2. // and then hit Run Code to run your code on the right side.
  3. // Modules get downloaded from browserify-cdn and bundled in your browser.
  4. const hexGrid = require('@quarterto/perturbed-hex-grid');
  5.  
  6. const {triangulate} = require('delaunay-fast');
  7. const c = document.getElementById('c');
  8. const ctx = c.getContext('2d');
  9.  
  10. c.width = c.height = 800;
  11.  
  12. const grid = hexGrid({rows: 50, cols: 50 * Math.sqrt(3) / 2, size: 16});
  13.  
  14. ctx.fillStyle = 'black';
  15. grid.forEach(([x, y]) => ctx.fillRect(x, y, 1, 1));
  16.  
  17. const tris = triangulate(grid);
  18.  
  19. for(let i = 0, l = tris.length; i < l; i += 3) {
  20. const [x1, y1] = grid[tris[i]];
  21. const [x2, y2] = grid[tris[i+1]];
  22. const [x3, y3] = grid[tris[i+2]];
  23.  
  24. ctx.beginPath();
  25. ctx.moveTo(x1, y1);
  26. ctx.lineTo(x2, y2);
  27. ctx.lineTo(x3, y3);
  28. ctx.lineTo(x1, y1);
  29. ctx.stroke();
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement