-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
175 lines (161 loc) · 5.05 KB
/
main.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
let canvas;
let ctx;
let canvasExperience;
let canvasExperienceAnimation;
const mouse = {x:0, y:0};
var bgSound = new Audio();
bgSound.src = 'sounds/Wintersong.mp3';
bgSound.volume = 0.2;
var sound = new Audio();
sound.src = 'sounds/twinkling.mp3';
sound.autoplay = true;
sound.volume = 0.1;
let size = document.getElementById('cellSize').value;
let speed = document.getElementById('speed').value;
let width = document.getElementById('lineWidth').value;
let zoomIn = document.getElementById('zoomIn').value;
let zoomOut = document.getElementById('zoomOut').value;
const onSizeChange = (value) => {
size = value;
canvasExperience.setCellSize(Number(size));
}
const onWidthChange = (value) => {
width = value;
canvasExperience.setLineWidth(Number(width));
}
const onSpeedChange = (value) => {
speed = value;
canvasExperience.setVr(Number(speed));
}
onZoomInChange = (value) => {
zoomIn = value;
canvasExperience.setZoomIn(Number(value));
}
onZoomOutChange = (value) => {
zoomOut = value;
canvasExperience.setZoomOut(Number(value));
}
//after loading the page
window.onload = function() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
canvas.width = window.innerWidth ;
canvas.height = window.innerHeight;
canvasExperience = new CanvasExperience(ctx,canvas.width, canvas.height, Number(size), Number(speed),Number(width),Number(zoomIn),Number(zoomOut));
canvasExperience.animate(0);
console.log(size,speed,width);
// bgSound.muted = false;
// bgSound.play();
}
bgSound.addEventListener('playing', function() {
console.log('playing');
})
//when resizing the window
window.addEventListener("resize", function(e) {
cancelAnimationFrame(canvasExperienceAnimation);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvasExperience = new CanvasExperience(ctx,canvas.width, canvas.height, size,speed,width, zoomIn, zoomOut);
canvasExperience.animate(0);
})
//when mouse is moving
window.addEventListener("mousemove", function(e) {
mouse.x = e.x;
mouse.y = e.y;
sound.muted = false;
sound.play();
})
window.addEventListener("mouseout", function(e) {
sound.muted = true;
sound.pause();
})
// Canvas Expereince
class CanvasExperience{
//private variables
#ctx;
#width;
#height;
//constructor
constructor(ctx,width,height, size, speed, lineWidth, zoomIn,zoomOut){
this.#ctx = ctx
this.#width = width ;
this.#height = height ;
this.cellSize = size;
this.vr = speed;
this.#ctx.lineWidth = lineWidth;
this.lastTime = 0;
this.interval = 1000/60;
this.timer = 0;
this.gradient;
this.#createGradient();
this.#ctx.strokeStyle = this.gradient;
this.radius = 0;
this.zoomIn = zoomIn;
this.zoomOut = zoomOut;
bgSound.muted = true;
bgSound.play();
}
//setter methods
setCellSize(value){
this.cellSize = value;
}
setLineWidth(value){
this.#ctx.lineWidth = value;
}
setVr(value){
this.vr = value;
}
setZoomIn(value){
this.zoomIn = value;
}
setZoomOut(value){
this.zoomOut = value;
}
//gradient
//further can be improved
#createGradient(){
this.gradient = this.#ctx.createLinearGradient(0,0, this.#width, this.#height);
this.gradient.addColorStop("0.1", '#ff5c33');
this.gradient.addColorStop("0.2", '#ff66b3');
this.gradient.addColorStop("0.4", '#ccccff');
this.gradient.addColorStop("0.6", '#b3ffff');
this.gradient.addColorStop("0.8", '#80ff80');
this.gradient.addColorStop("0.9", '#ffff30');
}
//method to draw line animation
#drawLine(angle, x,y){
let positionX = x ;
let positionY = y ;
let dx = mouse.x - positionX;
let dy = mouse.y - positionY;
let distance = (dx * dx + dy * dy) * 6;
if(distance > 600000) distance = 600000;
else if (distance < 50000) distance = 50000;
const length = distance/10000;
this.#ctx.beginPath();
this.#ctx.moveTo(x, y);
this.#ctx.lineTo(x + Math.cos(angle) * length,y +Math.sin(angle) * length);
this.#ctx.stroke();
}
//animation
animate(timeStamp){
const deltaTime = timeStamp - this.lastTime;
this.lastTime = timeStamp;
if(this.timer > this.interval){
this.#ctx.clearRect(0,0, this.#width, this.#height);
this.radius += this.vr;
if(this.radius > 5 || this.radius < -5) this.vr *= -1;
for(let y = 0; y < this.#height; y+=this.cellSize){
for(let x = 0; x < this.#width; x+=this.cellSize){
const angle = (Math.cos(x * this.zoomIn) + Math.sin(y * this.zoomOut)) * this.radius;
this.#drawLine(angle,x,y);
}
}
this.timer = 0;
}
else{
this.timer += deltaTime;
}
canvasExperienceAnimation = requestAnimationFrame((this.animate.bind(this)));
}
}