-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_12.js
165 lines (127 loc) · 3.69 KB
/
day_12.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
// (c) 2021 Joseph HENRY
// This code is licensed under MIT license (see LICENSE for details)
// Initialize activity with empty object
let activity = {activity:"", type:""};
/**
* Get new activity as JSON from : http://www.boredapi.com/api/activity/
* using xhr request
*/
function getNewActivity() {
const requestURL = "https://www.boredapi.com/api/activity/";
const request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = function() {
activity = request.response;
}
}
/**
* Display a bone like shape where (x, y) is the origin
* length and rotation
*/
function vThickLine(x, y, d1, d2, length, rotation) {
push();
translate(x, y);
rotate(rotation);
// Upper half circle
beginShape();
const r2 = d2 / 2;
for (let angle = PI; angle < TWO_PI; angle += 0.1) {
vertex(cos(angle) * r2, sin(angle) * r2 - length);
}
// Down half circle
const r1 = d1 / 2;
for (let angle = 0; angle < PI; angle += 0.1) {
vertex(cos(angle) * r1, sin(angle) * r1);
}
// Connect the two
endShape(CLOSE);
pop();
}
/**
* Draw a bored person
*/
function bored(x, y, vSize, factor) {
push();
translate(x, y);
noStroke();
fill(255, 200);
// Too much consts and variables here, not optimized at all
// but it's procedural anyway
const headWidth = 60;
const headHeight = 80;
const headY = headHeight / 2 + (vSize - headHeight / 2) * factor;
const armLength = vSize - headHeight / 2;
const lowerDiameter = 30;
const upperDiameter = 10;
const handDiameter = upperDiameter / 2;
const handX = (headWidth + upperDiameter) / 2;
const armY = headY - headHeight / 2;
const armOffset = sqrt((armLength ** 2) - (armY ** 2));
const armRotation = acos(armOffset / armLength) - HALF_PI;
const armX = handX + armOffset;
// Shoulders
const shoulderDiameter = lowerDiameter * 1.2;
vThickLine(-headWidth / 2, -(headY - headHeight / 2), shoulderDiameter, lowerDiameter, armLength, -armRotation + PI);
vThickLine(headWidth / 2, -(headY - headHeight / 2), shoulderDiameter, lowerDiameter, armLength, armRotation + PI);
// Arms
vThickLine(-armX, 0, lowerDiameter, upperDiameter, armLength, -armRotation);
vThickLine(armX, 0, lowerDiameter, upperDiameter, armLength, armRotation);
// Hands
vThickLine(-handX, -armY, upperDiameter, handDiameter, headHeight / 1.5, 0);
vThickLine(handX, -armY, upperDiameter, handDiameter, headHeight / 1.5, 0);
// Head
ellipse(0, -headY, headWidth, headHeight);
// Eyes
stroke("#ff9900");
strokeWeight(10);
point(-headWidth / 4, -headY);
point(headWidth / 4, -headY);
// Mouth
strokeWeight(2);
noFill();
arc(0, -headY + headHeight / 5, 20, factor * 20, 0, PI);
// eyes black dots
stroke(0);
strokeWeight(5);
point(-headWidth / 4 + cos(armRotation * 4) * 2.5, -headY);
point(headWidth / 4 + cos(armRotation * 4) * 2.5, -headY);
pop();
}
let offset = 0;
let time = 0;
const incr = 0.03;
function preload() {
getNewActivity();
}
function setup() {
createCanvas(500, 500);
}
function draw() {
background("#003344");
translate(width / 2, height / 2 + 50);
bored(0, 0, 150, (sin(offset) + 1) / 2);
// Text display
textSize(8);
stroke("#ff9900");
strokeWeight(50);
line(-200, 105, 200, 105);
textAlign(CENTER, CENTER);
fill(255);
noStroke();
textSize(15);
textStyle(NORMAL);
text("You're bored? Just : ", 0, -220);
fill(255);
textStyle(BOLD);
text(activity.activity, 0, 100);
textSize(8);
text("type : " + activity.type, 0, 120);
// Get new activity each cycle
if (offset - time > TWO_PI) {
getNewActivity();
time = offset;
}
offset += incr;
}