I am new to programming using the site openprocessing.org.
The code given does the following on my iPad:
- Draw a red circle
- Wait until I finger tap near the red circle’s center
- Erase the red circle
- Pause for a few seconds
- Draw a white circle
Any comments on my program would be appreciated.
let x1, y1;
let x2, y2;
let CircleDiameter;
let MoveState = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(60);
CircleDiameter = floor(.50*windowWidth);
x1=floor(.25*windowWidth);
y1=floor(.50*windowHeight);
x2=floor(.75*windowWidth);
y2=floor(.50*windowHeight);
background('black');
fill('Red');
circle(x1,y1,CircleDiameter);
fill('Yellow');
circle(x1,y1,10);
}
function touchStarted() {
if( MoveState === 0 && abs(mouseX-x1) <= 25 && abs(mouseY-y1) <= 25) {
MoveState = 1;
}
}
function draw() {
if( MoveState === 1 ) {
MoveState = 2;
InitialframeCount = frameCount;
EraseStart = InitialframeCount + 1;
EraseEnd = InitialframeCount + CircleDiameter;
DrawStart = EraseEnd + 1 + 3*60;
DrawEnd = EraseEnd + CircleDiameter + 3*60;
} else if ( MoveState === 2) {
if( frameCount >= EraseStart && frameCount <= EraseEnd) {
fill('Red');
circle(x1,y1, CircleDiameter - (frameCount-EraseStart) - 1 );
}
if( frameCount >= DrawStart && frameCount <= DrawEnd) {
fill('White');
circle(x2,y2, (frameCount-DrawStart) + 1 );
}
if( frameCount === DrawEnd) {
MoveState = 999;
}
}
}
Here is an animated gif of the output:
animation of red circle being erased before white circle is drawn
1 Answer 1
A few minor tweaks
EraseStart
isn't needed, it's basically InitialframeCount
(you could also then get rid of the -1
in the red circle). The second if
should be an else
which doesn't need to worry about && frameCount < DrawEnd
because you change MoveState
later on. I'd add clear
for the shrinking red circle, as I saw artifacts without.
if( MoveState === 1 ) {
MoveState = 2;
InitialframeCount = frameCount;
EraseEnd = InitialframeCount + CircleDiameter;
DrawStart = EraseEnd + 1 + 3*60;
DrawEnd = EraseEnd + CircleDiameter + 3*60;
} else if ( MoveState === 2) {
if(frameCount <= EraseEnd) {
clear()
fill('Red');
circle(x1,y1, CircleDiameter - (frameCount-InitialframeCount));
} else if( frameCount >= DrawStart) {
fill('White');
circle(x2,y2, (frameCount-DrawStart) + 1 );
}
if( frameCount === DrawEnd) {
MoveState = 999;
}
}
I believe adding a FrameRate
variable at the top would allow you to speed up/slow down the animation speed from one spot:
let FrameRate = 30
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(FrameRate);
...
DrawStart = EraseEnd + 1 + 3*FrameRate;
DrawEnd = EraseEnd + CircleDiameter + 3*FrameRate;
...
setTimeout
function as an alternative to busy-waiting. \$\endgroup\$const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
and then instead of thefor
loop with a call torandom()
one could instead calldelay(3000);
after declaringmousePressed()
with theasync
keyword. Perhaps the loop with the call torandom()
takes more than a few seconds on "common hardware for todays users"... \$\endgroup\$