-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: Custom Example for the Library Template #68
Comments
Hi there, looking forward to it! @SableRaf your requirements sound good to me. @stixan The SayHello returns the "hello library." text. |
Just to be clear, I am free to ditch both SayHello and RandomCircles and come up with new objects, as long as I keep the same overall structure (one main .pde sketch that includes X number of .java files compiled into one library)? |
I should think so but I'll let @mingness confirm for sure. |
You are free to ditch both SayHello and RandomCircles and come up with your own objects in the library if you wish - the objective is to keep these objects in the library relatively simple and clear. The objective of the example library in template is to show briefly how it might be structured in a working example, so if your library objects are similarly lean like the existing ones, that's great. I still think it is instructive to use an external dependency in the library - is this something that fits in your ideas? |
I’ll come up with an idea that includes an external dependency ✅ |
Here's what I've come up with so far. /* ---------------- MAIN SKETCH ---------------- */
Palette p;
Grid g;
void setup() {
size(800, 800); // This sketch assumes a square canvas
p = new Palette();
makeGrid();
}
void draw() {
background(0);
g.show();
}
void mouseClicked() {
makeGrid();
}
void makeGrid() {
g = new Grid(p.getPalette());
}
/* ---------------- LIBRARY CLASS: DOT GRID ---------------- */
class Grid {
ArrayList<Dot> dots;
Grid(color[] palette) {
dots = new ArrayList<Dot>();
int gridSize = int(random(2, 12));
float cellSize = width / float(gridSize);
float dotSpeed = random(0.008, 0.064);
for (int i = 0; i < gridSize * gridSize; i++) {
int x = i % gridSize;
int y = i / gridSize;
float xpos = x * cellSize + cellSize / 2;
float ypos = y * cellSize + cellSize / 2;
color dotColor = palette[int(random(palette.length))];
dots.add(new Dot(xpos, ypos, cellSize, dotSpeed, dotColor));
}
}
void show() {
for (Dot d : dots) {
d.update();
d.display();
}
}
class Dot {
float xpos, ypos;
float diameterOriginal, diameterCurrent;
float pulseValue, pulseSpeed;
color colorFill;
Dot(float x, float y, float d, float s, color c) {
xpos = x;
ypos = y;
diameterOriginal = d;
diameterCurrent = diameterOriginal;
pulseValue = random(TWO_PI);
pulseSpeed = random(s * 0.5, s * 2.0);
colorFill = c;
}
void display() {
pushStyle();
fill(colorFill);
noStroke();
circle(xpos, ypos, diameterCurrent);
popStyle();
}
void update() {
pulseValue += pulseSpeed;
diameterCurrent = map(sin(pulseValue), -1, 1, diameterOriginal * 0.1, diameterOriginal * 0.9);
}
}
}
/* ---------------- LIBRARY CLASS: PALETTE GENERATOR ---------------- */
class Palette {
Palette() {
}
color[] getPalette() {
int numColors = 5;
color[] palette = new color[numColors];
pushStyle();
colorMode(HSB, 1.0, 1.0, 1.0);
color mix = color(random(1.0), 1.0, 1.0);
float h, s, b;
for (int i=0; i<palette.length; i++) {
h = (random(1.0) + hue(mix)) / 2;
s = 1.0;
b = random(1.0) < 0.2 ? 0.2 : 1.0;
palette[i] = color(h, s, b);
}
popStyle();
return palette;
}
} I have made numerous failed attempts, all of which have exploded in size because of my vain desire to show off. In this sketch, I have attempted to strike a balance between simplicity and aesthetics. Let me know what you think before I have a go at turning it into an actual library. I may need a bit of guidance, though – @mingness, are you available for questions? |
(I'll add external dependencies when I move the sketch to the library template) |
I've incorporated a fair number of Processing functions into library classes. I think it's important to highlight that this is possible—especially for potential library developers who are familiar with Processing but lack experience with Java (myself and many of my students included). |
Hey @stigmollerhansen, This looks fantastic! I really appreciate the effort you’ve put into it 😃 One thing to adjust: the example needs to fit a landscape 4:3 or 16:9 ratio (currently it assumes a square canvas). The other requirement is to include the text "hello library" in the sketch. Here's my attempt at a modified version: /* ---------------- MAIN SKETCH ---------------- */
Palette p;
Grid g;
void setup() {
size(1280, 720); // Adjusted for a 16:9 aspect ratio
p = new Palette();
makeGrid();
}
void draw() {
background(0);
g.show();
displayText();
}
void mouseClicked() {
makeGrid();
}
void makeGrid() {
g = new Grid(p.getPalette());
}
void displayText() {
fill(255);
textAlign(CENTER, CENTER);
textSize(200);
text("Hello Library", width / 2, height / 2 - 30);
}
/* ---------------- LIBRARY CLASS: DOT GRID ---------------- */
class Grid {
ArrayList<Dot> dots;
Grid(color[] palette) {
dots = new ArrayList<Dot>();
int gridSizeX = int(random(3, 16)); // Adjusted for aspect ratio
int gridSizeY = int(gridSizeX * (9.0 / 16.0)); // Adjusted for aspect ratio
float cellSizeX = width / float(gridSizeX);
float cellSizeY = height / float(gridSizeY);
float dotSpeed = random(0.008, 0.064);
for (int y = 0; y < gridSizeY; y++) {
for (int x = 0; x < gridSizeX; x++) {
float xpos = x * cellSizeX + cellSizeX / 2;
float ypos = y * cellSizeY + cellSizeY / 2;
color dotColor = palette[int(random(palette.length))];
dots.add(new Dot(xpos, ypos, min(cellSizeX, cellSizeY), dotSpeed, dotColor));
}
}
}
void show() {
for (Dot d : dots) {
d.update();
d.display();
}
}
class Dot {
float xpos, ypos;
float diameterOriginal, diameterCurrent;
float pulseValue, pulseSpeed;
color colorFill;
Dot(float x, float y, float d, float s, color c) {
xpos = x;
ypos = y;
diameterOriginal = d;
diameterCurrent = diameterOriginal;
pulseValue = random(TWO_PI);
pulseSpeed = random(s * 0.5, s * 2.0);
colorFill = c;
}
void display() {
pushStyle();
fill(colorFill);
noStroke();
circle(xpos, ypos, diameterCurrent);
popStyle();
}
void update() {
pulseValue += pulseSpeed;
diameterCurrent = map(sin(pulseValue), -1, 1, diameterOriginal * 0.1, diameterOriginal * 0.9);
}
}
}
/* ---------------- LIBRARY CLASS: PALETTE GENERATOR ---------------- */
class Palette {
Palette() {
}
color[] getPalette() {
int numColors = 5;
color[] palette = new color[numColors];
pushStyle();
colorMode(HSB, 1.0, 1.0, 1.0);
color mix = color(random(1.0), 1.0, 1.0);
float h, s, b;
for (int i=0; i<palette.length; i++) {
h = (random(1.0) + hue(mix)) / 2;
s = 1.0;
b = random(1.0) < 0.2 ? 0.2 : 1.0;
palette[i] = color(h, s, b);
}
popStyle();
return palette;
}
} |
I have small suggestions for some color tweaks to soften things a bit:
|
Ha ha, I fell victim to the one thing I always tell my students: "READ THE BRIEF!" 😂 Thanks for reminding me of the specs I even asked for myself and then forgot all about, @SableRaf. I agree with your suggestions on both colors and code. I'll add them to the code. |
I might also tweak how the number of rows and columns are calculated to match the aspect ratio and fill out the entire canvas. |
This should already be the case in my tweaked code but of course feel free to adjust as you see fit :) |
Context
The talented @stixan (@stigmollerhansen on Instagram) kindly agreed to create a custom example for the library template 💙
Here were the requirements I shared (let me know if I missed anything):
Motivation
I'm creating this issue mainly so that we can answer Stig's technical questions about the template and library example.
Open questions
The text was updated successfully, but these errors were encountered: