From 0d350e10738e19e4562f847b6b8f233605194ba4 Mon Sep 17 00:00:00 2001
From: shoe <shoe@tilde.town>
Date: Fri, 21 Mar 2025 21:38:47 +0000
Subject: [PATCH] Erase the trail as it goes, including deleting array items

---
 random-walkers.js | 70 +++++++++++++++++++++++------------------------
 1 file changed, 34 insertions(+), 36 deletions(-)

diff --git a/random-walkers.js b/random-walkers.js
index 4d8d49f..253db93 100644
--- a/random-walkers.js
+++ b/random-walkers.js
@@ -65,35 +65,46 @@ function init_walk() {
   setInterval(draw, 20);
   // TODO: it would be cool to have this changable, but I may just ... not
   setInterval(move_walker, controls.step_time);
+}
 
-  /*
-
-  // Set default values for walker pos and empty the trail
-  // TODO: ACTUAL DEFAULT VALUE AT CENTER.
-  walker_position = {x: 0, y: 0};
-  trail_positions = [];
-  // Create array of walkers with set parameters, start walkers in center of canvas
-  walker_array = []
-  for (var i = 0; i < 1; i++) {
-    var walker = {
-      x_position: canvas.width / 2,
-      y_position: canvas.height / 2,
-      line_width: 1,
-      color: controls.color,
-      speed: 5,
-      angle: Math.random() * 360,
-      halt: false,
+// check if the trails array has a position.
+// return index if found, or null if not.
+function trails_find_position(row, col) {
+  for (var i = 0; i < trails.length; i++) {
+    var t = trails[i];
+    if (t.row === row && t.col === col) {
+      return i;
     }
-    walker.x_velocity = walker.speed * Math.cos(walker.angle)
-    walker.y_velocity = walker.speed * Math.sin(walker.angle)
-    walker_array.push(walker)
   }
-  */
+  return null;
 }
 
 function move_walker() {
-  // leave a trail
-  trails.push({row: walker.row, col: walker.col, color: walker.color});
+  // leave a trail behind
+  var index = trails_find_position(walker.row, walker.col);
+  if (index) {
+    trails[index].color = walker.color;
+  } else {
+    trails.push({row: walker.row, col: walker.col, color: walker.color});
+  }
+
+  // for all existing trails, make them darker
+  for (var i = 0; i < trails.length; i++) {
+    var t = trails[i];
+    t.color = tweak_color_luminance(t.color, -0.1);
+
+    // delete any trails with 0 for first digit of RGB
+    // (think of them as near-black)
+    // e.g. #050500
+    if (t.color[1] === "0" && t.color[3] == "0" && t.color[5] == "0") {
+      // draw black there just to be safe
+      draw_grid_square(t.row, t.col, "black");
+
+      // delete the item from the array
+      trails.splice(i, 1);
+    }
+  }
+  console.log(`${trails.length} items in trails array`);
 
   // move in random dir
   add_random_direction(walker);
@@ -163,9 +174,6 @@ function draw() {
     console.log(`dimensions changed from ${old_width}x${old_height} to ${ctx.canvas.width}x${ctx.canvas.height}`);
   }
 
-  // grid of all one color.
-  draw_grid();
-
   // draw a colored square at the walker location
   draw_grid_square(walker.row, walker.col, walker.color);
 
@@ -188,16 +196,6 @@ function draw_grid_square(row, col, color) {
   ctx.fillRect(pos_h, pos_v, controls.cell_size - (2*margin), controls.cell_size - (2*margin));
 }
 
-
-function draw_grid() {
-  // Actually draw the cells lol
-  for (var row = 0; row < n_rows; row++) {
-    for (var col = 0; col < n_cols; col++) {
-      draw_grid_square(row, col, "#282828");
-    }
-  }
-}
-
 /****************************************************************************
  * ON SCRIPT LOAD                                                           *
  ****************************************************************************/