-
Notifications
You must be signed in to change notification settings - Fork 3
/
partyMode.js
87 lines (75 loc) · 2.64 KB
/
partyMode.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
var canvas = document.createElement("canvas");
canvas.style.position ="absolute";
canvas.style.top = 0;
canvas.style.left=0;
canvas.style.width="100%";
canvas.style.height="100%";
canvas.style.opacity="0.5";
document.body.appendChild(canvas);
var context = canvas.getContext("2d");
var lights = [];
var iframe = document.createElement("iframe");
iframe.src="https://www.youtube.com/embed/6Zbi0XmGtMw?rel=0&autoplay=1";
iframe.style.display = "none";
document.body.appendChild(iframe);
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
function createLights(lights){
while(lights.length < 8){
lights.push({x: (Math.random() * canvas.width) ,
y: (Math.random() * canvas.height),
r: (Math.floor(Math.random() *40) + 10),
speed_x : (Math.floor(Math.random() *2) *2-1) * 10,
speed_y : (Math.floor(Math.random() *2) *2-1) * 10,
speed_r : (Math.floor(Math.random() *2) *2-1),
c: getRandomColor()});
}
}
function animateLights(context, canvas, lights){
context.clearRect(0, 0, canvas.width, canvas.height);
updateElements(lights, canvas);
drawElements(lights, context);
requestAnimFrame(function() {
animateLights(context,canvas, lights);
});
}
function drawCircle(x,y,r,c,ctx){
ctx.beginPath();
ctx.arc(x,y,r,0,2*Math.PI);
ctx.fillStyle = c;
ctx.fill();
}
function drawElements(lights, context){
lights.forEach(function(light){
drawCircle(light.x,light.y,light.r,light.c, context);
});
}
function updateElements(lights, canvas){
lights.forEach(function(light){
console.log("%s,%s", light.x,light.y);
if(light.x < 20 || light.x > canvas.width-20) light.speed_x *= -1;
if(light.y < 20 || light.y > canvas.height-20) light.speed_y *= -1;
if(light.r < 11 || light.r > 50) light.speed_r *= -1;
light.x += light.speed_x;
light.y += light.speed_y;
light.r += light.speed_r;
});
}
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
createLights(lights);
requestAnimFrame(function() {
animateLights(context,canvas, lights);
});