voronoi.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. console.log('hi voronoi');
  2. const canvas = document.getElementById('canvas'),
  3. canvwidth = canvas.width,
  4. canvheight = canvas.height,
  5. ctx = canvas.getContext('2d');
  6. const minDist = 20,
  7. edgeBounds = 20;
  8. function drawSite(x, y) {
  9. ctx.save();
  10. ctx.beginPath();
  11. ctx.arc(x, y, 2, 0, Math.PI * 2, false);
  12. ctx.closePath();
  13. ctx.fillStyle = 'black';
  14. ctx.fill();
  15. ctx.restore();
  16. }
  17. function drawLine(x1, y1, x2, y2) {
  18. ctx.save();
  19. ctx.beginPath();
  20. ctx.strokeStyle = 'black';
  21. ctx.lineWidth = 1;
  22. ctx.moveTo(x1, y1);
  23. ctx.lineTo(x2, y2);
  24. ctx.stroke();
  25. ctx.restore();
  26. }
  27. function drawLineO(p1, p2) {
  28. drawLine(p1.x, p1.y, p2.x, p2.y);
  29. }
  30. function distance(p1, p2) {
  31. return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
  32. }
  33. function generateSites(n = 10) {
  34. const sites = [],
  35. safety = n * 2,
  36. randMultWidth = canvwidth - (edgeBounds * 2),
  37. randMultHeight = canvheight - (edgeBounds * 2),
  38. origin = {x:0, y:0};
  39. let count = 0;
  40. while( sites.length < n && count < safety ) {
  41. count += 1;
  42. const x = Math.floor(Math.random() * randMultWidth) + edgeBounds,
  43. y = Math.floor(Math.random() * randMultHeight) + edgeBounds,
  44. min = sites.reduce(function checkDist(minSoFar, existing) {
  45. const d = distance(existing, {x, y});
  46. if( d < minSoFar ) {
  47. return d;
  48. }
  49. else {
  50. return minSoFar;
  51. }
  52. }, canvwidth * canvheight);
  53. if( min > minDist ) {
  54. sites.push( {x, y} );
  55. }
  56. }
  57. sites.sort(function (a, b) {
  58. return distance(origin, a) - distance(origin, b);
  59. });
  60. return sites;
  61. }
  62. function randIndivC(a = 0, b = 255) {
  63. return Math.floor(Math.random() * (b-a) + a);
  64. }
  65. function cts(c) {
  66. const r = c.r < 16 ? '0' + c.r.toString(16) : c.r.toString(16),
  67. g = c.g < 16 ? '0' + c.g.toString(16) : c.g.toString(16),
  68. b = c.b < 16 ? '0' + c.b.toString(16) : c.b.toString(16);
  69. return '#' + r + g + b;
  70. }
  71. function primaryOrSecondary() {
  72. const t = Math.random(),
  73. w = Math.floor(Math.random() * 3);
  74. if( t < 0.45 ) {
  75. const r = w === 0 ? 255 : 0,
  76. g = w === 1 ? 255 : 0,
  77. b = w === 2 ? 255 : 0;
  78. return {r, g, b};
  79. }
  80. else if( t < 0.9 ) {
  81. const r = w !== 0 ? 255 : 0,
  82. g = w !== 1 ? 255 : 0,
  83. b = w !== 2 ? 255 : 0;
  84. return {r, g, b};
  85. }
  86. else {
  87. //return {r: 255, g:255, b:255};
  88. const r = randIndivC(100, 255),
  89. g = randIndivC(100, 255),
  90. b = randIndivC(100, 255);
  91. return {r, g, b};
  92. }
  93. }
  94. function randomColor() {
  95. const t = Math.random(),
  96. w = Math.floor(Math.random() * 3);
  97. if( t < 0.45 ) {
  98. const r = w === 0 ? randIndivC(200, 255) : randIndivC(10, 100),
  99. g = w === 1 ? randIndivC(200, 255) : randIndivC(10, 100),
  100. b = w === 2 ? randIndivC(200, 255) : randIndivC(10, 100);
  101. return {r, g, b};
  102. }
  103. else if( t < 0.9 ) {
  104. const r = w !== 0 ? randIndivC(200, 255) : randIndivC(10, 100),
  105. g = w !== 1 ? randIndivC(200, 255) : randIndivC(10, 100),
  106. b = w !== 2 ? randIndivC(200, 255) : randIndivC(10, 100);
  107. return {r, g, b};
  108. }
  109. else {
  110. const r = randIndivC(100, 200),
  111. g = randIndivC(100, 200),
  112. b = randIndivC(100, 200);
  113. return {r, g, b};
  114. }
  115. }
  116. function mixedColor(base) {
  117. let randr = Math.floor(Math.random() * 200 + 55),
  118. randg = Math.floor(Math.random() * 200 + 55),
  119. randb = Math.floor(Math.random() * 200 + 55);
  120. if( base ) {
  121. randr = Math.floor((base.r + randr) / 2);
  122. randg = Math.floor((base.g + randg) / 2);
  123. randb = Math.floor((base.b + randb) / 2);
  124. }
  125. return cts({r:randr, g:randg, b:randb});
  126. }
  127. const sites = generateSites(50).map(function assignRandomColour(site) {
  128. //site.color = cts(randomColor());
  129. //site.color = cts(primaryOrSecondary());
  130. site.color = mixedColor(primaryOrSecondary());
  131. site.sectors = [];
  132. return site;
  133. });
  134. sites.forEach(function renderSites(site) {
  135. drawSite(site.x, site.y);
  136. });
  137. function bruteForceRow(row, sites) {
  138. sites = sites.map(function addNewRow(site) {
  139. site.sectors[row] = [];
  140. return site;
  141. })
  142. for( let i = 0; i < canvwidth; i += 1 ) {
  143. const point = {x:i, y:row},
  144. cind = sites.slice(1).reduce(function check(closest, site, ind) {
  145. const d = distance(site, point);
  146. if( d < closest.d ) {
  147. return {c:(ind + 1), d};
  148. }
  149. return closest;
  150. }, {c:0, d:distance(sites[0], point)});
  151. sites[cind.c].sectors[row].push(point);
  152. }
  153. return sites;
  154. }
  155. function spot(x, y, colour) {
  156. ctx.save();
  157. ctx.fillStyle = colour;
  158. ctx.fillRect(x, y, 1, 1);
  159. ctx.restore();
  160. }
  161. function goRow(i, sites) {
  162. const newSites = bruteForceRow(i, sites);
  163. newSites.filter(x => x.sectors[i].length)
  164. .forEach(function parse(leader) {
  165. leader.sectors[i].forEach(function draw(place) {
  166. spot(place.x, place.y, leader.color);
  167. })
  168. });
  169. if( i < canvheight ) {
  170. setTimeout(goRow, 10, i + 1, newSites);
  171. }
  172. else {
  173. setTimeout(function action() {
  174. // temp redraw sites
  175. sites.forEach(function renderSites(site) {
  176. drawSite(site.x, site.y);
  177. });
  178. }, 100);
  179. }
  180. }
  181. goRow(0, sites);