xxxxxxxxxx
96
const screen_width = 1024;
const screen_height = 1024;
let drawing = true;
let timestep = 0;
let lastY = 0;
let s_count = 0; //time to sustain;
let s = 0.75;
let r = 0.0025;
let stage = 0;
let y = 0.0;
let expo;
let releaseStartY = s;
function setup() {
console.log('start');
createCanvas(screen_width, screen_height);
background(220);
}
function expoCalc(startTC, endTC, t) {
const maxY = (1-Math.pow(Math.E, (-endTC))); //scaling factor
if (endTC > startTC) return (1-Math.pow(Math.E, (t*-endTC)))/maxY;
return (Math.pow(Math.E, (t*-startTC)));
}
function mousePressed() {
if (!drawing) background(220);
drawing = true;
stage = 0;
//reset position. translate current pos to % of attack
const maxY = (1-Math.pow(Math.E, (-2.5))); //scaling factor
y = Math.log(Math.abs((expo*maxY)-1))/-2.5;///(-0.435);
console.log({maxY, expo, y});
expo = y;
}
function draw() {
if (!drawing) return;
//let a = exp(mouseX /screen_width)/200 -.005;
let a = 0.01;
//if (mouseX <= 0) a =.01;
//let d = 0.01;
let d = exp(mouseX /screen_width)/200 -.005;
if (mouseX <= 0) d =.01;
for (i = timestep; i < (timestep + 4); i++) {
expo = y;
if (!mouseIsPressed) {
releaseStartY = y;
stage = 3;
}
if(stage == 0) { //atack
y += a;
if (y >= 1) {
stage = 1;
y = 1;
}
expo = expoCalc(0, 2.5, y);
} else if (stage == 1) { //decay
y -= d;
expo = (expoCalc(10, 0, 1-y) * (1-s)) + s;
//expo = y;
if (expo - s < 0.0002) stage = 2;
} else if (stage == 2) { //sustain
y = s;
expo = s;
//if (s_count++ >= 1) stage = 3;
} else if (stage == 3) { //release
y -= r;
if (y < 0.0001) stage = 4;
expo = expoCalc(10, 0, 1-y/releaseStartY)*releaseStartY;
}
stroke(0);
line (i-1, (1-lastY)*screen_height, i, (1-expo)*screen_height);
lastY = expo;
stroke(255,0,0);
point(i, (1-y)*screen_height);
}
timestep = i;
stroke(255,0,0);
if (timestep >= screen_width) {
timestep = 0;
drawing = false;
expo = 0;
}
}