-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
85 lines (71 loc) · 2.28 KB
/
code.js
File metadata and controls
85 lines (71 loc) · 2.28 KB
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
window.onload = function() {
const canvas = document.getElementById("mycanvas");
const ctx = canvas.getContext("2d");
const TIME_INTERVAL = 33;
const SPOT_NUMBER = 100;
let spot1 = []; // wave 1 spots
let spot2 = []; // wave 2 spots
let spot3 = []; // wave 3 spots
let color = ['rgba(255,100,165,0.5)','rgba(165,255,100,0.5)','rgba(100,165,255,0.5)'];
for(let i = 0 ; i < SPOT_NUMBER; i ++){ // reset spot
let x = (canvas.width / (SPOT_NUMBER-1)) * i;
spot1[i] = new Spot(x,200,i);
spot2[i] = new Spot(x,200,i+20);
spot3[i] = new Spot(x,200,i+40);
}
window.onclick = function() {
for(let i = 0; i < 3; i ++){
color[i] = setColorAsRandom(50,255);
}
}
setInterval(function(){
ctx.fillStyle = 'rgb(255,255,255)';
ctx.fillRect(0,0,canvas.width,canvas.height);
drawDraw(spot1,color[0]);
drawDraw(spot2,color[1]);
drawDraw(spot3,color[2]);
},TIME_INTERVAL);
function drawDraw(spot,color){ // draw wave function
let gapY = spot[0].setGapY(SPOT_NUMBER);
ctx.fillStyle= color;
ctx.beginPath();
ctx.moveTo(0,200);
for(let i = 0 ; i < SPOT_NUMBER; i ++){
spot[i].setPosition(SPOT_NUMBER,gapY).wave().draw(ctx,i);
}
ctx.arc(300,200,300,0,Math.PI,false);
ctx.fill();
}
};
function setColorAsRandom(lmin,lmax){
let R = Math.floor(lmin+Math.random()*(lmax - lmin));
let G = Math.floor(lmin+Math.random()*(lmax - lmin));
let B = Math.floor(lmin+Math.random()*(lmax - lmin));
return 'rgba('+R+','+B+','+G+',0.5)';
}
function Spot(x,y,tmpX){
this.x = x;
this.y = y;
this.tmpX = tmpX;
}
Spot.prototype = {
draw: function(ctx){ // draw wave line
ctx.lineTo(this.x,this.y);
return this;
},
wave: function(){ // make wave
this.tmpX ++;
return this;
},
setPosition: function(num,gap){ // set y position
if(this.tmpX == 0){
this.y = 200;
}else{
this.y = gap + Math.sin(Math.PI*2/num*this.tmpX) * 40;
}
return this;
},
setGapY: function(num){
return 200 - Math.sin(Math.PI*2/num*this.tmpX) * 40;
}
};