-
Notifications
You must be signed in to change notification settings - Fork 0
/
animated-stripes.pde
204 lines (165 loc) · 4.79 KB
/
animated-stripes.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* ----------
Name: Animated stripes
Code: Maxime Benoit
Original idea: Sanders Sturing
Last revision: 19/12/2023
Steps:
1 - A random image is loaded from an external link and displayed on the canvas.
2 - The user can create a selection area by clicking and dragging the mouse.
3 - Upon mouse release:
a. An array of colors is created based on the selected area.
b. A Shape consisting of lines is generated and immediately added to an ArrayList.
c. The shapes stored in the ArrayList are rendered onto the canvas.
4 - If the user presses "ENTER", an animation is triggered using the array index and the modulo operator.
---------- */
// Color blocks related var.
ArrayList<Shape> shapes;
int middlePickerX;
int middlePickerY;
int rectWidth;
int rectHeight;
// Selector related var.
boolean selection = false;
int mouseStartPosX;
int mouseStartPosY;
int mouseEndPosX;
int mouseEndPosY;
// Images related var.
PImage seed;
String url = "https://picsum.photos/512";
// Color array related var.
int pixelIndex;
color[] colors;
boolean isHorizontal;
// Animation related var.
boolean isAnimated;
void setup() {
size(512, 512);
//pixelDensity(displayDensity()); // Too laggy when activated
colorMode(RGB, 255, 255, 255, 100);
seed = loadImage(url, "jpg");
// Init array list
shapes = new ArrayList<Shape>();
}
void draw() {
image(seed, 0, 0);
// Draw the shapes
for (int i = 0; i < shapes.size(); i++) {
Shape shape = shapes.get(i);
shape.display();
if (isAnimated) {
shape.animate();
}
}
// Draw the selector
if (selection) {
drawSelector();
}
}
void createArrayOfColors(boolean isHorizontal) {
seed.loadPixels();
if (rectWidth > 0) { // Prevent sketch from from crashing if shape is drawn backward.
if (isHorizontal) {
colors = new color[rectWidth];
for (int i = 0; i < rectWidth; ++i) {
pixelIndex = (mouseStartPosX + i) + (mouseStartPosY + rectHeight / 2) * seed.width;
if (pixelIndex >= 0 && pixelIndex < seed.pixels.length) {
colors[i] = seed.pixels[pixelIndex];
}
}
} else {
colors = new color[rectHeight];
for (int i = 0; i < rectHeight; ++i) {
pixelIndex = (mouseStartPosX + rectWidth / 2) + (mouseStartPosY + i) * seed.width;
if (pixelIndex >= 0 && pixelIndex < seed.pixels.length) {
colors[i] = seed.pixels[pixelIndex];
}
}
}
}
}
void drawSelector() {
// Draw the rectangle selector
fill(0, 0, 0, 20);
strokeWeight(1);
stroke(255, 255, 255, 100);
beginShape();
vertex(mouseStartPosX, mouseStartPosY);
vertex(mouseX, mouseStartPosY);
vertex(mouseX, mouseY);
vertex(mouseStartPosX, mouseY);
endShape(CLOSE);
// Draw the pointer
fill(255, 0, 0, 100);
noStroke();
ellipse(mouseX, mouseY, 10, 10);
}
class Shape {
// Variable that defines block size and position
int blk_width;
int blk_height;
int blk_mouseStartPosX;
int blk_mouseStartPosY;
int blk_mouseEndPosX;
int blk_mouseEndPosY;
boolean isHorizontal;
color[] blk_color;
int incr;
int speed = 2;
Shape(int w, int h, int mspX, int mspY, int mepX, int mepY, boolean orientation, color[] colors) {
blk_width = w;
blk_height = h;
blk_mouseStartPosX = mspX;
blk_mouseStartPosY = mspY;
blk_mouseEndPosX = mepX;
blk_mouseEndPosY = mepY;
isHorizontal = orientation;
blk_color = colors;
}
void animate() {
incr += speed;
}
void display() {
noFill();
strokeWeight(2);
if (isHorizontal) {
for (int i = 0; i < blk_width; i++) {
stroke(blk_color[(i + incr) % blk_width]);
line(blk_mouseStartPosX + i, blk_mouseStartPosY, blk_mouseStartPosX + i, blk_mouseEndPosY);
//beginShape();
//vertex(blk_mouseStartPosX + i, blk_mouseStartPosY);
//vertex(blk_mouseStartPosX + i, blk_mouseEndPosY);
//endShape();
}
} else {
for (int i = 0; i < blk_height; i++) {
stroke(blk_color[(i + incr) % blk_height]);
line(blk_mouseStartPosX, blk_mouseStartPosY + i, blk_mouseEndPosX, blk_mouseStartPosY + i);
//beginShape();
//vertex(blk_mouseStartPosX, blk_mouseStartPosY + i);
//vertex(blk_mouseEndPosX, blk_mouseStartPosY + i);
//endShape();
}
}
}
}
void mousePressed() {
mouseStartPosX = mouseX;
mouseStartPosY = mouseY;
selection = true;
}
void mouseReleased() {
mouseEndPosX = mouseX;
mouseEndPosY = mouseY;
selection = false;
rectWidth = mouseEndPosX - mouseStartPosX;
rectHeight = mouseEndPosY - mouseStartPosY;
isHorizontal = rectWidth > rectHeight;
createArrayOfColors(isHorizontal);
shapes.add(new Shape(rectWidth, rectHeight, mouseStartPosX, mouseStartPosY, mouseEndPosX, mouseEndPosY, isHorizontal, colors));
}
void keyTyped() {
if (key == ENTER) {
isAnimated = !isAnimated;
}
}