Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>

<head>
</head>

<body>
</body>

</html>
              
            
!

CSS

              
                body {
    padding: 0;
    margin: 0;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    background: black;
}

canvas {
    position: fixed;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

              
            
!

JS

              
                //console.clear();
let currentActionStart;
let currentActionIndex;
let currentAction;
let currentElapsedTime;
let currentPercentage;

let switchAnimationBtn;
let animationIndex;

let animationFunctions = [];
let play = false;
let sessionStart;
let sessionElapsedTime;
let playBtn;
let sessionLength;
let sessionLengthMs;

const actionTypes = {
    INHALE: 0,
    INHALE_PAUSE: 1,
    EXHALE: 2,
    EXHALE_PAUSE: 3
};

const actionInhale = {
    text: "Inhale",
    type: actionTypes.INHALE,
    time: 4000
};
const actionInhalePause = {
    text: "Inhale Pause",
    type: actionTypes.INHALE_PAUSE,
    time: 1000
};
const actionExhale = {
    text: "Exhale",
    type: actionTypes.EXHALE,
    time: 6000
};
const actionExhalePause = {
    text: "Exhale Pause",
    type: actionTypes.EXHALE_PAUSE,
    time: 1000
};

const actions = [
    actionInhale,
    actionInhalePause,
    actionExhale,
    actionExhalePause
];

function startSession() {
    play = true;
    currentActionIndex = 0;
    currentActionStart = millis();
    sessionStart = millis();
}

function setup() {
    createCanvas(700, 700);
    currentActionIndex = 0;
    currentActionStart = millis();

    switchAnimationBtn = createButton("Change animation");
    switchAnimationBtn.mouseClicked(() => {
        animationIndex = (animationIndex + 1) % animationFunctions.length;
    });
    animationIndex = 3;

    playBtn = createButton("Start");
    playBtn.mousePressed(startSession);

    sessionElapsedTime = 0;
    sessionLength = 1;
    sessionLengthMs = sessionLength * 60 * 1000;

    doTransition();
    startSession();
    setInterval(() => {
        animationIndex = (animationIndex + 1) % animationFunctions.length;
    }, 12000);
}

function draw() {
    background(225);
    noFill();

    if (play) {
        doTransition();
    }

    const currentAction = actions[currentActionIndex];
    backgroundColorChange();

    const animationFunction = animationFunctions[animationIndex];
    animationFunction(width / 2, height / 2);

    animationTimer();
}

function MsToMandS(ms) {
    ms = ms / 1000;
    return { m: parseInt(ms / 60), s: parseInt(ms % 60) };
}

function animationTimer() {
    sessionElapsedTime = millis() - sessionStart;
    /*
    console.log({
        sessionStart,
        sessionLength,
        sessionLengthMs,
        sessionElapsedTime
    });
    */
    const remaining = sessionLengthMs - sessionElapsedTime;
    stroke(0);
    textSize(15);
    fill(0);
    const { m, s } = MsToMandS(remaining);
    text(`remaining ${m} : ${s}`, 10, 10);
}

function backgroundColorChange() {
    const r = 125;
    const g = map(currentPercentage, 0, 1, 50, 200);
    const b = 200;
    background(r, g, b);
}

function animationDNA(x, y) {
    const minH = 20;
    const maxH = 680;
    const amplitude = 20;
    const frequency = 10;
    const H = map(currentPercentage, 0, 1, minH, maxH);

    beginShape();
    for (let _x = x; _x < H; _x++) {
        let _y = y + amplitude * cos((x + _x) / frequency);
        vertex(_x, _y);
        _y = y + amplitude * cos(PI + (x + _x) / frequency);
        vertex(_x, _y);
    }
    endShape();
}
animationFunctions.push(animationDNA);

function animationPolygon(x, y) {
    const minL = 20;
    const maxL = 200;
    const r = map(currentPercentage, 0, 1, minL, maxL);
    const nbVertexes = 10;

    const vertexes = [];
    const v = new p5.Vector(-10, 0);
    v.setMag(r);

    for (let i = 0; i <= nbVertexes; i++) {
        vertexes.push(v.copy());
        v.rotate(PI / nbVertexes);
    }
    vertexes.push(new p5.Vector(0, 0));
    vertexes.push(vertexes[0].copy());

    push();
    translate(x, y);
    beginShape();
    for (const v of vertexes) {
        vertex(v.x, v.y);
    }
    endShape();
    pop();
}
animationFunctions.push(animationPolygon);

function animationPolygonAngle(x, y, maxAngle) {
    const minL = 20;
    const maxL = 200;

    if (!maxAngle) {
        maxAngle = PI;
    }
    const nbVertexes = 10;
    const angle = map(currentPercentage, 0, 1, 0, maxAngle / nbVertexes);

    const vertexes = [];
    const v = new p5.Vector(-10, 0);
    v.setMag(100);

    for (let i = 0; i <= nbVertexes; i++) {
        vertexes.push(v.copy());
        v.rotate(angle);
    }
    vertexes.push(new p5.Vector(0, 0));
    vertexes.push(vertexes[0].copy());

    push();
    translate(x, y);
    beginShape();
    for (const v of vertexes) {
        vertex(v.x, v.y);
    }
    endShape();
    pop();
}
animationFunctions.push(animationPolygonAngle);

function animationCircle(x, y) {
    const minR = 20;
    const maxR = 200;
    const r = map(currentPercentage, 0, 1, minR, maxR);
    fill(120, 50, 50);
    ellipse(x, y, r);
}
animationFunctions.push(animationCircle);

function actionText(x, y, size) {
    textSize(30);
    console.log(currentAction);
    text(currentAction.text, x, y);
}

function doTransition() {
    const currentTime = millis();
    currentAction = actions[currentActionIndex];
    currentElapsedTime = currentTime - currentActionStart;
    currentPercentage = getPercentage();

    if (currentElapsedTime < currentAction.time) {
        return;
    }

    currentActionIndex = (currentActionIndex + 1) % actions.length;
    currentActionStart = currentTime;
}

function getPercentage() {
    const minR = 0;
    const maxR = 1;

    let r;
    switch (currentAction.type) {
        case actionTypes.INHALE:
            r = map(currentElapsedTime, 0, currentAction.time, minR, maxR);
            break;
        case actionTypes.INHALE_PAUSE:
            r = maxR;
            break;
        case actionTypes.EXHALE:
            r = map(currentElapsedTime, 0, currentAction.time, maxR, minR);
            break;
        case actionTypes.EXHALE_PAUSE:
            r = minR;
            break;
        default:
            console.error("unknown action in animationCirle()");
    }
    return r;
}

              
            
!
999px

Console