Animation for 24W22 code


2024, Jun 05 edited
tags: code animation p5js 


This animation was discovered while following the coding train's challenge #86. As it happens quite often while following Coding train videos, the result turned out nicely so I decided to keep it.

in a few line of codes, some simple mathematical formulas, exploiting trigonometry, allow displaying this quite nice animation. Below the corresponding code wrote in javascript using the p5js platform, is presented. The draw function of this p5js animation is detailled below.




The animation



It looks like a white paper garland that turns on itself.




The code




// angle parameter
let angle=0;
// mini rectangle width
let w=20;

function draw() {
    // Draw code here
    // change background color of the canvas
     background(150,0,0);
    // change rectMode setting to CENTER
    rectMode(CENTER);
    // initialization of the angle setting 
    var pas=0 ;
    for(var x=0;x<width;x+=w){
        a=angle+pas;
        rotateX(a);
        let h=map(sin(a), -1,1,0,100);
        fill(255);
        rect(x-width/2+w/2,0,w-2,h);
        pas+= 0.001;
    }
    angle+=0.01;
}
The fuel of coding inspiration:music, travel, photography and whatever drives creativity to code.

You might also like