jQuery addClass example
Change class name on click in jQuery
by jtw90210
CSS
body {
background: #c0FEFE;
padding: 20px;
font-family: Arial, Helvetica;
}
JavaScript
const settings = {
xmax: 300,
ymax: 300,
critters: 2400, //2400,
cycles: 100, //100
separation: 100, // number of pixels that is considered a collision
cps: 2, // cycles per second, use zero to run without interrupts
fps: 4, // frames per second, use zero to turn off animation
roammax: 50, // the 0<chance<100 that they'll roam on a particular cycle
speedmax: 2, // how far they might move with each iteration if they roam
}
class Utility {
static getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
static coinToss(pTrue = 50) { // 0<pTrue<100
if (pTrue <= 0) return false;
if (pTrue >= 100) return true;
return ((Math.random() * 100) < pTrue);
}
static log(...theArgs) {
console.log(Utility.withSpaces(theArgs));
}
// this takes a number and formats it to the specified precision, then uses the label if provided to plural it
static niceNumber(number, precision = 0, label = "", plural = "") {
let text = "";
text += number.toFixed(precision);
if (label !== "") text += (" " + Utility.plural(number, label, plural));
return text;
}
// uses the singular if it's 1, plural if not
static plural(number, singular = "", plural = "") {
if (plural === "") plural = singular + "s"; // standard English plural
return (number == 1) ? singular : plural;
}
// this takes a series of arguments and separates them with spaces
static withSpaces(...theArgs) {
theArgs.forEach(arg => {
return Array.isArray(arg) ? arg.join(" ") : arg;
});
return theArgs.join(" ");
}
static nicePercent(numerator, denominator, precision = 0) {
return (denominator == 0) ? "" : "(" + Utility.asPercent(numerator, denominator, precision) + "%)";
}
static asPercent(numerator, denominator, precision = 0) {
return (denominator == 0) ? "&infinity;" : (100 * numerator / denominator).toFixed(precision);
}
}
class...