xxxxxxxxxx
class rail {
constructor(pace) {
this.pace = pace;
this.railWidth = this.pace * 0.1548;
this.trkWidth = this.pace * 3.074;
this.tieLength = this.trkWidth * 1.72;
this.tieWidth = this.tieLength * 0.1;
this.offset = (this.tieLength-this.trkWidth)/2;
}
display(x,y, units) {
for (let i = 0; i < units; i++) {
// ties/sleepers
fill(127); noStroke();
rect(x + i * this.pace, y-this.offset, this.tieWidth, this.tieLength);
// rails
stroke(255);
strokeWeight(this.railWidth);
line(x + i * this.pace, y, x + i * this.pace + this.pace, y);
line(x + i * this.pace, y + this.trkWidth, x + i * this.pace + this.pace, y + this.trkWidth);
}
}
}
let track = new rail(33);
function setup() {
createCanvas(windowWidth,windowHeight);
pixelDensity(2);
}
function draw() {
background(0);
if(mouseY>100) {
track = new rail(round(mouseY/20));
track.display(30,50, round(mouseX/33)); // x,y, units
track = undefined;
}
}