-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
223 lines (197 loc) · 7.78 KB
/
sketch.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
let asciiart;
let gfx;
let ascii_arr;
let currentAnimation;
let zoomLevel = 1; // Start with no zoom
let baseFrequencySine = 0.005; // Base frequency for sine wave
let baseFrequencyTriangle = 0.005; // Base frequency for triangle wave
let amplitude = 80; // Initial amplitude
let distortionFactor = 1; // No distortion initially
let keyIsHeld = false;
let lastActionTime = 0; // Time since the last action for continuous press
let initialDelay = 2000; // Initial delay before starting continuous action, in milliseconds
let repeatInterval = 200; // Interval between continuous actions, in milliseconds
let currentKeyCode; // Store the current key code being pressed
let shouldSortPixels = false;
let customFont;
// function preload() {
// customFont1 = loadFont('fonts/C64_Pro_Mono-STYLE.ttf');
// }
const charset = "╭╭░░░░░╮╮▇▚▚▚▞▞▇█▌▐▄▀░▒▓▓ "; // From darker to lighter
function setup() {
createCanvas(600, 600);
gfx = createGraphics(600, 600);
gfx.pixelDensity(3);
asciiart = new AsciiArt(this, 'monospace', 8, NORMAL); // Directly setting font properties
textFont('Courier New');
textSize(12); // Adjusted text size for better visibility
textLeading(12); // Adjusted text leading to match the textSize
currentAnimation = drawSineWave; // Retain user input features
}
function draw() {
if (shouldSortPixels) {
sortPixels();
shouldSortPixels = false; // Reset flag after sorting
}
gfx.background(0);
currentAnimation(); // Execute the current animation based on user input
// Convert gfx content to ASCII art
let ascii_arr = asciiart.convert(gfx, gfx.width / 5, gfx.height / 5); // Adjust for desired ASCII resolution
let asciiImage = asciiart.convert2dArrayToString(ascii_arr); // Convert the 2D array to a string
background(0); // Clear the main canvas
fill(255); // Set text color to white
textSize(8); // Match ASCII art resolution
textLeading(7); // Adjust line spacing to match textSize
text(asciiImage, 10, 10); // Display the ASCII image on the canvas
// Check if a key is held down for continuous action
if (keyIsHeld && millis() - lastActionTime > initialDelay) {
if (millis() - lastActionTime >= repeatInterval) {
processKey(false); // Process the held key
lastActionTime = millis(); // Update the last action time
}
}
}
function convertToAsciiCustom(gfx) {
gfx.loadPixels();
let asciiImage = "";
for (let y = 0; y < gfx.height; y++) {
for (let x = 0; x < gfx.width; x++) {
let index = (y * gfx.width + x) * 4;
let avg = (gfx.pixels[index] + gfx.pixels[index+1] + gfx.pixels[index+2]) / 3;
asciiImage += mapBrightnessToChar(avg);
}
asciiImage += '\n';
}
return asciiImage;
}
// Maps brightness to character from the charset
function mapBrightnessToChar(brightness) {
let index = Math.floor(map(brightness, 0, 255, charset.length-1, 0));
let selectedChar = charset[index];
console.log(`Brightness: ${brightness}, Index: ${index}, Selected Char: '${charset[index]}'`);
return selectedChar;
}
function drawSineWave() {
gfx.stroke(255);
gfx.noFill();
let frequency = (baseFrequencySine / zoomLevel) * distortionFactor;
gfx.beginShape();
for (let x = 0; x < gfx.width; x++) {
let angle = TWO_PI * frequency * x + frameCount * 0.02; // Adding time variation
let y = gfx.height / 2 + amplitude * sin(angle);
gfx.vertex(x, y);
}
gfx.endShape();
}
function drawTriangleWave() {
gfx.stroke(255);
gfx.noFill();
let frequency = (baseFrequencyTriangle / zoomLevel) * distortionFactor;
gfx.beginShape();
for (let x = 0; x < gfx.width; x++) {
let angle = TWO_PI * frequency * x + frameCount * 0.02; // Adding time variation
// Triangle waveform calculation (kept as is or adjust similarly to sine wave for animation)
let y = gfx.height / 2 + amplitude * (2 * abs(2 * (angle / TWO_PI - floor(angle / TWO_PI + 0.5))) - 1);
gfx.vertex(x, y);
}
gfx.endShape();
}
function drawDynamicWave() {
console.log("Drawing dynamic wave.");
gfx.background(0);
gfx.stroke(255);
gfx.noFill();
let waveHeight = 80; // Amplitude of the wave
let waveCount = 6; // How many waves
let waveSpacing = gfx.width / waveCount; // Spacing between waves
for (let i = 0; i < waveCount; i++) {
gfx.beginShape();
for (let x = 0; x < gfx.width; x++) {
let angleOffset = (TWO_PI / gfx.width) * waveSpacing * i;
let y = gfx.height / 3 + sin((x * .01 * baseFrequencySine) + frameCount * 0.02 + angleOffset) * waveHeight * distortionFactor;
gfx.vertex(x, y);
}
gfx.endShape();
}
}
function keyPressed() {
keyIsHeld = true;
currentKeyCode = keyCode; // Store the keyCode of the currently pressed key
lastActionTime = millis(); // Reset the timer on a new key press
processKey(true); // Initial key press action
// Trigger pixel sorting when 'P' is pressed
if (key === 'P' || key === 'p') {
sortPixels();
}
}
function keyReleased() {
keyIsHeld = false;
}
function resetControls() {
zoomLevel = 1; // Reset zoom level
amplitude = 80; // Reset amplitude
distortionFactor = 1; // Reset distortion factor
// Add any other controls you'd like to reset
}
function processKey(isInitialPress) {
let adjustmentFactor = keyIsDown(SHIFT) ? 10 : 1; // Adjust the factor based on Shift key
// Key actions based on keyCode
switch (currentKeyCode) {
case 48: // '0' key for reset
if (isInitialPress) {
resetControls();
}
break;
case 49: // '1' key for Sine Wave
if (isInitialPress) currentAnimation = drawSineWave;
break;
case 50: // '2' key for Triangle Wave
if (isInitialPress) currentAnimation = drawTriangleWave;
break;
case 51: // Assuming '3' key switches to the sphere animation
if (isInitialPress) currentAnimation = drawDynamicWave;
break;
case 187: // '+' key to increase zoom level (main keyboard, requires shift)
case 107: // Numpad '+' key to increase zoom level
zoomLevel = max(1, zoomLevel * (2 * adjustmentFactor));
break;
case 189: // '-' key to decrease zoom level (main keyboard)
case 109: // Numpad '-' key to decrease zoom level
zoomLevel = max(1, zoomLevel / (2 * adjustmentFactor));
break;
case UP_ARROW: // Up arrow to increase amplitude
amplitude += 10 * adjustmentFactor;
break;
case DOWN_ARROW: // Down arrow to decrease amplitude
amplitude = max(10, amplitude - 10 * adjustmentFactor);
break;
case RIGHT_ARROW: // Right arrow to increase distortion factor
distortionFactor += 0.1 * adjustmentFactor;
break;
case LEFT_ARROW: // Left arrow to decrease distortion factor
distortionFactor = max(1, distortionFactor - 0.1 * adjustmentFactor);
break;
}
}
function sortPixels() {
console.log("Sorting pixels now.");
gfx.loadPixels();
// Sort pixels based on brightness
for (let y = 0; y < gfx.height; y++) {
for (let x = 0; x < gfx.width - 1; x++) {
let index = (x + y * gfx.width) * 4;
let nextIndex = (x + 1 + y * gfx.width) * 4;
let brightnessCurrent = (gfx.pixels[index] + gfx.pixels[index + 1] + gfx.pixels[index + 2]) / 3;
let brightnessNext = (gfx.pixels[nextIndex] + gfx.pixels[nextIndex + 1] + gfx.pixels[nextIndex + 2]) / 3;
if (brightnessCurrent > brightnessNext) {
// Swap pixels
for (let i = 0; i < 4; i++) {
let temp = gfx.pixels[index + i];
gfx.pixels[index + i] = gfx.pixels[nextIndex + i];
gfx.pixels[nextIndex + i] = temp;
}
}
}
}
gfx.updatePixels();
}