-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
214 lines (175 loc) · 5.12 KB
/
script.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
jQuery(document).ready(function($){
window.StartPutukan = (function(){
return window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 60);
};
})();
var canvas = document.getElementById("myCanv"),
canvas2d = canvas.getContext('2d'),
canvasHeight = window.innerHeight,
canvasWidth = window.innerWidth,
paputoks = [],
particles = [],
kulay = 120,
limitClickTick = 0,
limitTotal =10,
timeLaunchAuto = 15,
timeLaunchTick = 0,
mousedown = false,
mouseY,
mouseX;
PutukanNa();
function setCanvas(){
canvas.width = canvasWidth;
canvas.height = canvasHeight;
}
function randomShit(min, max){
return Math.random() *(max - min) + min;
}
function computeDistance( p1y, p1x, p2y, p2x){
var yDistance = p1y - p2y,
xDistance = p1x - p2x;
return Math.sqrt( Math.pow( xDistance, 2) + Math.pow( yDistance, 2));
}
//left
function Paputok(sx, sy, tx, ty){
//actual coor
this.y = sy;
this.x = sx;
// start of coordinates
this.sy = sy;
this.sx = sx;
//target by cursor
this.ty = ty;
this.tx = tx;
//distance to targeted from starting point
this.targetDistance = computeDistance(sx, sy, tx, ty);
this.targetDistanceTraveled = 0;
//past coordinates
this.coordinates = [];
this.coordinatesCount = 3;
//will populate initial coordinates
while( this.coordinatesCount--){
this.coordinates.push([this.x, this.y]);
}
this.angle = Math.atan2( ty - sy, tx - sx);
this.speed = 10;
this.accel = 1;
this.brightness = randomShit( 40, 70);
//will circle targeted point
this.radius = 1.5;
}
Paputok.prototype.update = function (index){
//will remove last item in the array
this.coordinates.pop();
//will add coordinates in the array
this.coordinates.unshift([this.x, this.y]);
//will speed up the firework
this.speed *= this.accel;
//current velocities
var vx = Math.cos(this.angle) * this.speed,
vy = Math.sin(this.angle) * this.speed;
//how far will the paputok velocity applied
this.distanceTraveled = computeDistance(this.sy, this.sx, this.y + vy, this.x + vx);
//check if the destination is reached
if(this.distanceTraveled >= this.targetDistance){
createParticles(this.tx, this.ty);
//will remove the paputok
paputoks.splice(index, 1);
} else {
//keep travel
this.x += vx;
this.y += vy;
}
}
Paputok.prototype.draw = function(){
canvas2d.beginPath();
//move last coordinate set and then draw x and y
canvas2d.moveTo(this.coordinates[this.coordinates.length -1][0], this.coordinates[this.coordinates.length -1][1]);
canvas2d.lineTo(this.x, this.y);
canvas2d.strokeStyle = 'hsl(' + kulay + ', 100%, ' + this.brightness + '%)';
canvas2d.stroke();
//if you did target a certain point
canvas2d.beginPath();
canvas2d.arc(this.x, this.y, this.radius, 0.5, Math.PI * 3);
canvas2d.stroke();
}
function Particle( x, y){
this.x = x;
this.y = y;
//track past coordinates
this.coordinates = [];
this.coordinatesCount = 10;
while(this.coordinatesCount--){
this.coordinates.push([this.x, this.y]);
}
this.angle = randomShit(0, Math.PI * 2);
this.speed = randomShit(1, 10);
this.friction = 0.95;
this.gravity = 1;
this.kulay = randomShit( kulay - 20, kulay + 20);
this.brightness = 50;
this.alpha = 1;
this.decay = randomShit(0.015 , 0.03);
this.radius = 1.5
}
Particle.prototype.update = function(index){
this.coordinates.pop();
this.coordinates.unshift([this.x, this.y]);
this.speed *= this.friction;
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed + this.gravity;
this.alpha -= this.decay;
if(this.alpha <= this.decay){
particles.splice(index, 1);
}
}
Particle.prototype.draw = function(index){
canvas2d.beginPath();
canvas2d.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1]);
canvas2d.lineTo(this.x, this.y);
canvas2d.strokeStyle = 'hsla(' + this.kulay + '100%, ' + this.brightness + '%, ' + this.alpha + ')';
canvas2d.stroke();
}
function createParticles(x, y){
var particleCount = 35;
while(particleCount--){
particles.push(new Particle(x ,y));
}
}
function PutukanNa(){
setCanvas();
//run endlessly
StartPutukan(PutukanNa);
//so that paputok will be different as time passes by
kulay += 6;
//will clear firework via opacity
canvas2d.globalCompositeOperation = 'destination-out';
//trails
canvas2d.fillStyle = 'rgba(0, 7, 0, 7)';
canvas2d.fillRect(0, 0, canvasWidth, canvasHeight);
//changes the firework
canvas2d.globalCompositeOperation = 'lighter';
//loop every firework
var m = paputoks.length;
while(m--){
paputoks[m].draw();
paputoks[m].update(m);
}
var mp = particles.length;
while(mp--){
particles[mp].draw();
particles[mp].update(mp);
}
//lauch paputok automatically
if(timeLaunchTick >= timeLaunchAuto){
paputoks.push(new Paputok(canvasWidth /2 , canvasHeight, randomShit(0, canvasWidth), randomShit(0, canvasHeight / 2)));
timeLaunchTick = 0;
} else{
timeLaunchTick++;
}
}
});