191 lines
6.0 KiB
JavaScript
191 lines
6.0 KiB
JavaScript
/****************************************************************************
|
|
* GUI Setup *
|
|
****************************************************************************/
|
|
|
|
window.onload = function() {
|
|
var gui = new dat.GUI({
|
|
closed: false,
|
|
remembered: {
|
|
undefined: {
|
|
"0": {},
|
|
},
|
|
},
|
|
folders: {},
|
|
load: JSON,
|
|
width: 400,
|
|
autoPlace: false,
|
|
});
|
|
|
|
gui.add(controls, "step_time", 200, 2000).name("step time ms").step(1);
|
|
gui.add(controls, "fadeout", 0, 20).name("trail fadeout length (0 to disable)").step(1);
|
|
//gui.add(controls, "cell_size", 10, 100).name("cell size").step(1);
|
|
gui.addColor(controls, "color").name("guy color");
|
|
gui.add(controls, "restart_button").name("start again");
|
|
|
|
var customContainer = document.getElementById("controls-container");
|
|
customContainer.append(gui.domElement);
|
|
};
|
|
|
|
var controls = new(function() {
|
|
this.step_time = 500;
|
|
this.fadeout = 10;
|
|
this.cell_size = 22;
|
|
this.color = "#FFB000";
|
|
|
|
this.restart_button = function() {
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
|
init_walk();
|
|
}
|
|
})()
|
|
|
|
/****************************************************************************
|
|
* TODO: CATEOGORIZE THESE BULLSHITS *
|
|
****************************************************************************/
|
|
|
|
// Define a new mod function that behaves different on negatives.
|
|
// now, (-1).mod(4) -> 3. not -1.
|
|
// taken from https://stackoverflow.com/a/4467559, which itself took from elsewhere
|
|
Number.prototype.mod = function (n) {
|
|
"use strict";
|
|
return ((this % n) + n) % n;
|
|
};
|
|
|
|
// Perform integer division
|
|
Number.prototype.div = function(divisor) {
|
|
"use strict";
|
|
return Math.floor(this / divisor);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Random walk implementation (movement functions) *
|
|
****************************************************************************/
|
|
|
|
function init_walk() {
|
|
// Draw a grid of boxes on the canvas
|
|
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,
|
|
}
|
|
walker.x_velocity = walker.speed * Math.cos(walker.angle)
|
|
walker.y_velocity = walker.speed * Math.sin(walker.angle)
|
|
walker_array.push(walker)
|
|
}
|
|
*/
|
|
}
|
|
|
|
function move_walker() {
|
|
add_random_direction(walker_pos);
|
|
}
|
|
|
|
function add_random_direction(pos) {
|
|
let rand = Math.floor(Math.random() * 4);
|
|
if (rand === 0) {
|
|
pos.row++;
|
|
pos.row = pos.row.mod(n_rows);
|
|
} else if (rand === 1) {
|
|
pos.row--;
|
|
pos.row = pos.row.mod(n_rows);
|
|
} else if (rand === 2) {
|
|
pos.col++;
|
|
pos.col = pos.col.mod(n_cols);
|
|
} else {
|
|
pos.col--;
|
|
pos.col = pos.col.mod(n_cols);
|
|
}
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Drawing *
|
|
****************************************************************************/
|
|
|
|
function draw() {
|
|
// pixel width and height
|
|
var old_width = ctx.canvas.width;
|
|
var old_height = ctx.canvas.height;
|
|
canvas.width = window.innerWidth;
|
|
canvas.height = window.innerHeight;
|
|
|
|
if (old_width !== canvas.width || old_height != canvas.height) {
|
|
var old_n_rows = n_rows;
|
|
var old_n_cols = n_cols;
|
|
n_rows = canvas.height.div(controls.cell_size);
|
|
n_cols = canvas.width.div(controls.cell_size);
|
|
offset_h = (canvas.width - (controls.cell_size * n_cols)).div(2);
|
|
offset_v = (canvas.height - (controls.cell_size * n_rows)).div(2);
|
|
offset = {h: offset_h, v: offset_v};
|
|
// TODO: for every walker and every trail, adjust their position to the new canvas size
|
|
// we wanna just mod it, probably. that way if you shrink it you don't
|
|
// lose anything.
|
|
|
|
// TODO: HANDLE RESIZE
|
|
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_pos.row, walker_pos.col, "red");
|
|
|
|
// TODO CODE
|
|
}
|
|
|
|
function draw_grid_square(row, col, color) {
|
|
var margin = 2;
|
|
|
|
ctx.fillStyle = color;
|
|
var pos_h = offset.h + (controls.cell_size * col) + margin;
|
|
var pos_v = offset.v + (controls.cell_size * row) + 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 *
|
|
****************************************************************************/
|
|
|
|
// find the canvas and set its initial size
|
|
var canvas = document.getElementById("walker-canvas")
|
|
var ctx = canvas.getContext('2d')
|
|
canvas.width = window.innerWidth;
|
|
canvas.height = window.innerHeight;
|
|
|
|
// compute the initial rows and columns
|
|
var n_rows = canvas.height.div(controls.cell_size);
|
|
var n_cols = canvas.width.div(controls.cell_size);
|
|
var offset_h = (canvas.width - (controls.cell_size * n_cols)).div(2);
|
|
var offset_v = (canvas.height - (controls.cell_size * n_rows)).div(2);
|
|
var offset = {h: offset_h, v: offset_v};
|
|
|
|
// set start position of walker and start the random walk
|
|
var walker_pos = {row: n_rows.div(2), col: n_cols.div(2)};
|
|
init_walk();
|