-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
113 lines (95 loc) · 2.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
const calculatePositionX = (i, radius, angle) => cos(angle * i) * radius;
const calculatePositionY = (i, radius, angle) => sin(angle * i) * radius;
const calcAngle = formResolution => radians(360 / formResolution);
const produceTiles = (angle, radius, formResolution) => {
return Array.from(new Array(formResolution), (a, i) => {
const x = calculatePositionX(i, radius, angle),
y = calculatePositionY(i, radius, angle);
return [x, y];
});
};
const hsbRgb = a => map(a, 0, 100, 0, 255);
const hsbToColor = a => color(a, hsbRgb(b), hsbRgb(c));
const retroColor = () => {
switch (round(random(0, 3))) {
case 1:
return color(9, 255, 195);
case 2:
return color(0, 232, 187);
default:
return color(255, 5, 243);
}
};
const calculateXLocation = (location, stepSize) =>
location[0] + random(-stepSize, stepSize);
const calculateYLocation = (location, stepSize) =>
location[1] + random(-stepSize, stepSize);
const calculateLocations = (stepSize, locations) => {
return locations.map(location => [
calculateXLocation(location, stepSize),
calculateYLocation(location, stepSize),
]);
};
const xLoc = (centerX, location) => location[0] + centerX;
const yLoc = (centerY, location) => location[1] + centerY;
const drawVertex = (centerX, centerY, location) => {
const x = xLoc(centerX, location),
y = yLoc(centerY, location);
curveVertex(x, y);
};
const iRadius = 800;
function mousePressed() {
angle = calcAngle(formResolution);
radius = iRadius * random(0.2, 1.0);
locations = produceTiles(angle, radius, formResolution);
}
let formResolution,
stepSize,
locations,
centerX,
centerY,
mode,
angle,
radius,
song,
amp;
function preload() {
song = loadSound('audio.mp3');
}
function setup() {
createCanvas(2550 / 2, 1440 / 2);
frameRate(30);
colorMode(RGB);
smooth();
background(255, 241, 76);
formResolution = 1000;
radius = iRadius;
stepSize = 50;
angle = calcAngle(formResolution);
locations = produceTiles(angle, radius, formResolution);
centerX = width / 2;
centerY = height / 2;
song.play();
amp = new p5.Amplitude();
mode = 0;
}
function draw() {
radius = map(amp.getLevel(), 0, 1, 100, 800);
locations = produceTiles(angle, radius, formResolution);
background(color(255, 241, 76));
centerX = width / 2;
centerY = height / 2;
locations = calculateLocations(stepSize, locations);
lastLocation = locations[locations.length - 1];
firstLocation = locations[0];
secondLocation = locations[1];
strokeWeight(10);
stroke(retroColor());
noFill();
beginShape();
drawVertex(centerX, centerY, lastLocation);
locations.map(location => drawVertex(centerX, centerY, location));
drawVertex(centerX, centerY, firstLocation);
drawVertex(centerX, centerY, secondLocation);
endShape();
}