Arrow functions

Hi. This code is from Roni Kaufman on OpenProcessing
Could anyone please explain in a beginner-understandable way what the function below does and how it works?

let noiseProg = (x) => (x);

Whole code:


// By Roni Kaufman

let kMax;
let step;
let n = 80; // number of blobs
let radius = 0; // diameter of the circle
let inter = 0.1; // difference between the sizes of two blobs
let maxNoise = 500;

let noiseProg = (x) => (x);

function setup() {
  createCanvas(600, 600);
  angleMode(DEGREES);
  noFill();
  kMax = random(0.6, 1.0);
  step = 0.01;
  noStroke();
}

function draw() {
  blendMode(BLEND);
  background(0);
  blendMode(ADD);
  let t = frameCount/100;
  for (let i = n; i > 0; i--) {
	let alpha = pow(1 - noiseProg(i / n), 3);
	let size = radius + i * inter;
	let k = kMax * sqrt(i/n);
	let noisiness = maxNoise * noiseProg(i / n);
		
	fill(255, 0, 0, alpha*255);
    blob(size, width/2, height/2, k, t - i * step, noisiness);
		
	fill(0, 255, 0, alpha*255);
    blob(size, width/2, height/2, k, t - i * step + 1, noisiness);
		
	fill(0, 0, 255, alpha*255);
    blob(size, width/2, height/2, k, t - i * step + 2, noisiness);
  }
}

function blob(size, xCenter, yCenter, k, t, noisiness) {
  beginShape();
  let angleStep = 360 / 12;
  for (let theta = 0; theta <= 360 + 2 * angleStep; theta += angleStep) {
    let r1, r2;
    r1 = cos(theta)+1;
    r2 = sin(theta)+1;
    let r = size + noise(k * r1,  k * r2, t) * noisiness;
    let x = xCenter + r * cos(theta);
    let y = yCenter + r * sin(theta);
    curveVertex(x, y);
  }
  endShape();
}

The statement let noiseProg = (x) => (x);, which btW can be re-written as const noiseProg = x => x;, does absolutely NOTHING!

More specifically, it’s a function which returns the passed argument w/o doing anything to it; so it’s moot!

Maybe the author did it as an easy way to modify the “noise” behavior later on.

But the way the code is right now you can simply delete it.

And then replace all instances of noiseProg(i / n) w/ just i / n.

More about JS fat arrow functions:

2 Likes