Last active
January 17, 2023 22:17
-
-
Save edmorais/add4de3c603a3492428f20fec9859e48 to your computer and use it in GitHub Desktop.
Genuary2023 17: Random flowers & bezier curves, movement with parallax effect (v2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
____ __ __ __ ___ _ | |
/ __/__/ /_ _____ ________/ /__ / |/ /__ _______ _(_)__ | |
/ _// _ / // / _ `/ __/ _ / _ \ / /|_/ / _ \/ __/ _ `/ (_-< | |
/___/\_,_/\_,_/\_,_/_/ \_,_/\___/ /_/ /_/\___/_/ \_,_/_/___/ | |
Genuary 2023! 17 | |
* by Eduardo Morais 2023 - www.eduardomorais.pt | |
*/ | |
// libs | |
// import processing.javafx.*; | |
// config | |
int $appWinWidth = 1024; | |
int $appWinHeight = 1024; | |
color $appWinBG = #000000; // #110D92; | |
color $appDefStroke = #FFFFFF; | |
int $saveFrameNum = 0; // 0 to deactivate | |
int $saveFrameOffset = 300; // wait before start recording | |
// constants | |
int MAXRAD = 128; | |
int ROTATIONS = 4; | |
int MAXSTROKE = 6; | |
int MAXPETALS = 12; | |
float MAXSPD = 4; | |
int FLOWER_NUM = 32; | |
int CURVE_NUM = 48; | |
// globals | |
Flower[] flowers; | |
Curve[] curves; | |
int cellX, cellY, maxRad, minRad; | |
/* | |
SETTINGS & SETUP | |
*/ | |
void settings() { | |
// brute MS Surface display density fix: | |
size($appWinWidth / displayDensity(), $appWinHeight / displayDensity()); | |
MAXRAD/=displayDensity(); | |
MAXSTROKE/=displayDensity(); | |
} | |
color fgcolor(int hs, int he) { | |
colorMode(HSB, 360, 100, 100); | |
return color(random(hs, he), random(50, 100), random(50, 100)); | |
} | |
void setup() { | |
colorMode(HSB, 360, 100, 100); | |
$appWinBG = color(random(150, 210), random(50, 100), 16); | |
background($appWinBG); | |
noFill(); | |
noCursor(); | |
frameRate(30); | |
minRad = MAXRAD / 8; | |
flowers = new Flower[FLOWER_NUM]; | |
curves = new Curve[CURVE_NUM]; | |
colorMode(HSB, 360, 100, 100); | |
for (int i=0; i<FLOWER_NUM; i++) { | |
flowers[i] = new Flower( | |
int(random(width,width*2)), int(random(0,height)), // pos | |
minRad, random(minRad,MAXRAD), // radius | |
int(random(2, random(MAXPETALS/2, MAXPETALS))), // petals | |
random(-PI, PI), // st ang | |
random(1, MAXSPD), // linear vel | |
fgcolor(0, 120), int(random(2, MAXSTROKE)) // color, stroke | |
); | |
} | |
for (int i=0; i<CURVE_NUM; i++) { | |
curves[i] = new Curve( | |
int(random(width,width*2)), int(random(0,height)), // pos | |
int(random(width,width*2)), int(random(0,height)), // pos2 | |
random(minRad, MAXRAD*4), // radius | |
random(-PI, PI), // st ang | |
random(1, MAXSPD), random(1, MAXSPD), // linear x vel, max y vel | |
fgcolor(300, 420), int(random(1, MAXSTROKE/2)) // color, stroke | |
); | |
} | |
} | |
/* | |
DRAW | |
*/ | |
void draw() { | |
for (int i=0; i<curves.length; i++) { | |
curves[i].draw(); | |
} | |
windowBlur($appWinBG, 96); | |
for (int i=0; i<flowers.length; i++) { | |
flowers[i].draw(); | |
} | |
if ($saveFrameNum > 0 && | |
frameCount > $saveFrameOffset && frameCount - $saveFrameOffset <= $saveFrameNum) | |
saveFrame("frames/#####.tga"); | |
} | |
/* curves */ | |
class Curve { | |
PVector position, position2, angvec, drawfr, drawto; | |
float dia, strot, rot, avel, xvel, yvel1, yvel2; | |
int strokew, ct; | |
color col; | |
Curve(int x, int y, int x1, int y1, float r, float sr, float xv, float yvmax, color c, int sw) { | |
ct = 0; | |
position = new PVector(x,y); | |
position2 = new PVector(x1,y1); | |
dia = r*2; | |
strot = sr; | |
rot = 0; | |
xvel = xv; | |
yvel1 = random(-yvmax, yvmax); | |
yvel2 = random(-yvmax, yvmax); | |
avel = xv/360; | |
avel = sr >=0 ? avel : 0-avel; | |
col = c; | |
strokew = sw; | |
} | |
void draw() { | |
if (abs(rot) < TAU) ct++; | |
position.x-=xvel; | |
position.y+=yvel1; | |
position2.x-=xvel; | |
position2.y+=yvel2; | |
strot+=avel / 8; | |
if (position.x < 0-dia && position2.x < 0-dia) { | |
ct = 0; | |
position = new PVector(int(random(width,width*2)), int(random(0,height))); | |
position2 = new PVector(int(random(width,width*2)), int(random(0,height))); | |
avel = 0 - avel; | |
strokew = int(random(2, MAXSTROKE)); | |
return; | |
} | |
blendMode(SCREEN); | |
stroke(col, 8); | |
strokeWeight(strokew); | |
noFill(); | |
for (int i = 0; i < ct; i+=4) { | |
rot+=avel / 16; | |
angvec = PVector.fromAngle(strot).setMag(dia/2); | |
drawfr = PVector.add(position, angvec); | |
angvec = PVector.fromAngle(strot+rot).setMag(dia/2); | |
drawto = PVector.add(position2, angvec); | |
bezier(position.x, position.y, drawfr.x,drawfr.y, | |
drawto.x,drawto.y, position2.x, position2.y); | |
} | |
} | |
} | |
/* our flower figure */ | |
class Flower { | |
PVector position, angvec, drawto; | |
float minrad, maxrad, rad, radf, strot, rot, avel, xvel; | |
int strokew, petals, ct; | |
color col; | |
Flower(int x, int y, float mird, float mxrd, int p, float sr, float xv, color c, int sw) { | |
ct = 0; | |
position = new PVector(x, y); | |
minrad = mird; | |
maxrad = mxrd; | |
rad = random(minrad, maxrad); | |
radf = random(minrad, maxrad); | |
strot = sr; | |
rot = 0; | |
xvel = xv; | |
avel = xv/30; | |
avel = sr >=0 ? avel : 0-avel; | |
petals = p; | |
col = c; | |
strokew = sw; | |
} | |
void draw() { | |
if (abs(rot) < TAU) ct++; | |
strot+=avel/10; | |
rot = 0; | |
position.x-=xvel; | |
radf = lerp(minrad, maxrad, abs(sin(strot)))/maxrad; | |
if (position.x < 0-maxrad*2) { | |
ct = 0; | |
position = new PVector(int(random(width,width*2)), int(random(0,height))); | |
avel = 0 - avel; | |
strokew = int(random(2, MAXSTROKE)); | |
petals = int(random(2, MAXPETALS)); | |
return; | |
} | |
blendMode(SCREEN); | |
stroke(col, 96); | |
strokeWeight(strokew); | |
noFill(); | |
for (int i = 0; i < ct; i++) { | |
rot+=avel; | |
rad = lerp(minrad, maxrad, abs(sin(rot*petals))); | |
angvec = PVector.fromAngle(strot+rot).setMag(rad*radf); | |
drawto = PVector.add(position, angvec); | |
line(position.x, position.y, | |
drawto.x, drawto.y); | |
} | |
} | |
} | |
/* Full window blur */ | |
void windowBlur(color bg, int opacity) { | |
blendMode(BLEND); | |
noStroke(); | |
fill(bg, opacity); | |
rectMode(CORNER); | |
rect(0,0, width, height); | |
noFill(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment