-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (159 loc) · 4.77 KB
/
index.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
var config;
var hasStarted = false
var howManyFacesX;
var howManyFacesY;
var faceRows = [];
var startingFaceY = 0;
var dancingHead;
var backgroundImage;
var music;
class Timer{
constructor(seconds, onIncriment, callOnceFinished){
this.ms = seconds*1000;
this.onIncriment = onIncriment;
this.callOnceFinished = callOnceFinished;
this.msElapsed = 0;
this.startingTimestamp = 0;
}
update(timestamp){
if(this.startingTimestamp == 0)
this.startingTimestamp = timestamp
console.log(timestamp);
this.msElapsed = timestamp-this.startingTimestamp
}
}
class FaceFallRow{
constructor(faceImages, x, y, columns, radius, rowIndex){
this.x = x+width/40;
this.y = y+height/20;
this.padding = 20;
this.degrees = 360;
this.columns = columns;
this.rowIndex = rowIndex;
this.radius = radius;
this.faceImages = faceImages;
this.faceImagesOrdered = [];
for(var column = 0; column < this.columns; column++){
this.faceImagesOrdered.push(faceImages[Math.floor(Math.random()*this.faceImages.length)]);
}
}
update(){
this.y = this.y+1;
this.x = Math.cos(this.degrees*5*Math.PI/180) * 5;
this.degrees = this.degrees-1;
if(this.y > height+this.radius){
faceRows[faceRows.indexOf(this)].y = startingFaceY+40;
}
}
display(){
for(var column = 0; column < this.columns; column++){
image(this.faceImagesOrdered[column], this.x+(((this.radius*2)+this.padding)*column), this.y, this.radius*2, this.radius*2);
}
}
}
class DancingHead{
constructor(headImageUrl, x, y){
this.headImage = loadImage(headImageUrl);
this.x = x;
this.y = y;
this.rotationIncriment = 0;
this.angle = 0;
this.scale = 0;
this.scaleTimer = new Timer(13, function(secondsElapsed, secondsRemaining){
console.log(secondsElapsed/13);
}, function(){
})
}
update(){
this.rotationIncriment+=1;
if(this.scale <1)
this.scale += deltaTime/12000;
this.angle=Math.cos(this.rotationIncriment*7.13*Math.PI/180) * 30
}
display(){
imageMode(CENTER);
translate((this.x)+(636/2), (this.y)+(720/2));
rotate(PI/180*this.angle);
scale(lerp(0, 1, this.scale));
image(this.headImage, 0, 0, 636, 720);
rotate(PI/180*0);
translate(0, 0);
imageMode(CORNER);
}
}
function start(config){
if(config.music.type == "file"){
document.getElementById('canvas').classList.add('reveal');
document.getElementById("musicPlayer").play()
hasStarted = true
} else if (config.music.type == "youtube"){
var player;
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: config.music.src,
events: {
'onReady': function(event){
document.getElementById('canvas').classList.add('reveal');
event.target.playVideo();
event.target.setVolume(50);
hasStarted = true
},
'onStateChange':function(event){
if(event.data === YT.PlayerState.ENDED){
event.target.playVideo();
}
}
}
});
}
}
function setup() {
fetch('configs/default.json').then(res => res.json()).then(function(response){
config = response
var yPadding = 20;
var canvas = createCanvas(windowWidth, windowHeight);
canvas.parent("canvas")
circleRadius = 40;
howManyFacesX = Math.ceil(width/(circleRadius*2))+1;
howManyFacesY = Math.floor(height/(circleRadius*2));
startingFaceY = height-(howManyFacesY*((circleRadius*2)+20))
var rainImages = [];
document.title = config.title;
if(config.music.type == "file"){
music = document.createElement("audio");
music.setAttribute("src",config.music.src);
music.setAttribute("id", "musicPlayer");
music.setAttribute("loop", true);
document.body.appendChild(music);
}
for(const image of config.rainImages){
rainImages.push(loadImage(image))
}
for(var row = 0; row < howManyFacesY; row++){
faceRows.push(new FaceFallRow(rainImages, 0, startingFaceY+(((circleRadius*2)+yPadding)*row), howManyFacesX, circleRadius));
}
dancingHead = new DancingHead(config.dancingHead, (windowWidth/2)-(636/2), (windowHeight/2)-(720/2), 636, 720);
backgroundImage = loadImage(config.background)
Swal.fire({
title:'Disclaimer',
text:'This site contains a highly potent dose of funk and overall badassery. Please use responsibly and consult a licensed medical professional before proceeding.'
}).then(function(){
start(config);
});
});
}
function draw() {
if(!config)
return
background(220);
image(backgroundImage, 0, 0, windowWidth, windowHeight)
for(let row of faceRows){
row.update();
row.display();
}
if(hasStarted){
dancingHead.update();
dancingHead.display();
}
}