JSFiddle - React, Tailwind, and code Playground

by Darby Rathbone

JavaScript

/*An object has a "life" value that is random between 16 and 32 when it's created. For each logic tick, 1 is subtracted from this value. When it's at 0, the object's life is over. I'm trying to determine an algorithm to decide the scaling of these objects so that it looks like they grow from nothing (0.0) up to their max size (1.0) when life is at its peak (middle), back down to 0.0 at the end of life.*/

var l = createLife(16, 32);
do {
    document.body.innerHTML += "<div>" + l.at + " : " + l.size + "</div>";
} while (l.tick());
l.tick();
document.body.innerHTML += "<div>" + l.at + " : " + l.size + "</div>";




function createLife(min, max) {
    var life = ((Math.random() * (max - min)) + min) | 0;
    return {
        lifespan: life,
        at: life,
        midlife: life / 2,
        tick: function () {
            this.at--;
            if (this.at === 0) return null;
            this.scale();
            return this;
        },
        size: 0,
        scale: function () {
            this.size = (Math.abs(Math.abs(this.at - this.midlife) / (this.lifespan - this.midlife))); 
                             this.size = 1-(this.size * this.size);
            return this;
            }
        };
    }