Erase the trail as it goes, including deleting array items
This commit is contained in:
parent
555735966e
commit
0d350e1073
@ -65,35 +65,46 @@ function init_walk() {
|
|||||||
setInterval(draw, 20);
|
setInterval(draw, 20);
|
||||||
// TODO: it would be cool to have this changable, but I may just ... not
|
// TODO: it would be cool to have this changable, but I may just ... not
|
||||||
setInterval(move_walker, controls.step_time);
|
setInterval(move_walker, controls.step_time);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
// check if the trails array has a position.
|
||||||
|
// return index if found, or null if not.
|
||||||
// Set default values for walker pos and empty the trail
|
function trails_find_position(row, col) {
|
||||||
// TODO: ACTUAL DEFAULT VALUE AT CENTER.
|
for (var i = 0; i < trails.length; i++) {
|
||||||
walker_position = {x: 0, y: 0};
|
var t = trails[i];
|
||||||
trail_positions = [];
|
if (t.row === row && t.col === col) {
|
||||||
// Create array of walkers with set parameters, start walkers in center of canvas
|
return i;
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
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() {
|
function move_walker() {
|
||||||
// leave a trail
|
// leave a trail behind
|
||||||
trails.push({row: walker.row, col: walker.col, color: walker.color});
|
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
|
// move in random dir
|
||||||
add_random_direction(walker);
|
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}`);
|
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 a colored square at the walker location
|
||||||
draw_grid_square(walker.row, walker.col, walker.color);
|
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));
|
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 *
|
* ON SCRIPT LOAD *
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user