-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor_res.js
552 lines (486 loc) · 14.9 KB
/
for_res.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
class Utils {
static getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
static getRGBColor(r, g, b) {
const rr = r || this.getRandomNumber(0, 255);
const rg = g || this.getRandomNumber(0, 255);
const rb = b || this.getRandomNumber(0, 255);
return 'rgb(' + rr + ', ' + rg + ', ' + rb + ')';
}
static getHSLColor(hue, saturation, lightness) {
const rHue = hue || this.getRandomNumber(0, 360);
const rSaturation = saturation || this.getRandomNumber(0, 100);
const rLightness = lightness || this.getRandomNumber(0, 100);
return 'hsl(' + rHue + ', ' + rSaturation + '%, ' + rLightness + '%)';
}
static getGradientColor(ctx, type, cr, cg, cb, ca, x, y, r, ex, ey) {
let col, g;
switch (type) {
case 'linear':
col = cr + "," + cg + "," + cb;
g = ctx.createLinearGradient(x, y, ex, ey);
g.addColorStop(0, "rgba(" + col + ", " + ca * 1 + ")");
g.addColorStop(0.5, "rgba(" + col + ", " + ca * 0.5 + ")");
g.addColorStop(1, "rgba(" + col + ", " + ca * 0 + ")");
return g;
break;
case 'radial':
col = cr + "," + cg + "," + cb;
g = ctx.createRadialGradient(x, y, 0, x, y, r);
g.addColorStop(0, "rgba(" + col + ", " + ca * 1 + ")");
g.addColorStop(0.5, "rgba(" + col + ", " + ca * 0.5 + ")");
g.addColorStop(1, "rgba(" + col + ", " + ca * 0 + ")");
return g;
break;
default:
console.log('mistaken params');
break;}
}
static getMultipleArray(yLength, xLength) {
const multArr = new Array(yLength);
for (let y = 0; y < yLength; y++) {
multArr[y] = new Array(xLength);
for (let x = 0; x < xLength; x++) {
multArr[y][x] = 0;
}
}
return multArr;
}}
class Vector2d {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(v, x, y) {
if (v instanceof Vector2d) {
return new Vector2d(this.x + v.x, this.y + v.y);
} else {
this.x += x;
this.y += y;
}
}
sub(v, x, y) {
if (v instanceof Vector2d) {
return new Vector2d(this.x - v.x, this.y - v.y);
} else {
this.x -= x;
this.y -= y;
}
}
mult(v, num) {
if (v instanceof Vector2d) {
return new Vector2d(this.x * v.x, this.y * v.x);
} else {
this.x *= num;
this.y *= num;
}
}
fromAngle(radian) {
this.x = Math.cos(radian);
this.y = Math.sin(radian);
}
negate() {
this.x = -this.x;
this.y = -this.y;
}
rotate(radian) {
const x = this.x;
const y = this.y;
const cosVal = Math.cos(radian);
const sinVal = Math.sin(radian);
this.x = x * cosVal - y * sinVal;
this.y = x * sinVal + y * cosVal;
}
length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
lengthSquared() {
return this.x * this.x + this.y * this.y;
}
normalize() {
const len = Math.sqrt(this.x * this.x + this.y * this.y);
if (len) {
this.x /= len;
this.y /= len;
}
return len;
}
dotProduct(v) {
return this.x * v.x + this.y * v.y;
}
edge(v) {
return this.sub(v);
}
perpendicular() {
const v = new Vector2d();
v.x = this.y;
v.y = 0 - this.x;
return v;
}
/*
* Return normalized vertical vector
*/
normal() {
const p = this.perpendicular();
return p.normalize();
}
/*
* Display vector by string
*/
toString() {
return '(' + this.x.toFixed(3) + ',' + this.y.toFixed(3) + ')';
}}
class CollideWithCircle {
constructor(array) {
this.array = array;
}
collide() {
const v = new Vector2d(0, 0);
let dist, obj1, obj2;
for (let c = 0; c < this.array.length; c++) {
obj1 = this.array[c];
for (let i = c + 1; i < this.array.length; i++) {
obj2 = this.array[i];
v.x = obj2.x - obj1.x;
v.y = obj2.y - obj1.y;
dist = v.length();
if (dist < obj1.radius + obj2.radius) {
v.normalize();
v.mult(null, obj1.radius + obj2.radius - dist);
v.negate();
obj1.x += v.x;
obj1.y += v.y;
this.bounce(obj1, obj2);
}
}
}
}
bounce(obj1, obj2) {
const colnAngle = Math.atan2(obj1.y - obj2.y, obj1.x - obj2.x);
const length1 = obj1.v.length();
const length2 = obj2.v.length();
const dirAngle1 = Math.atan2(obj1.v.y, obj1.v.x);
const dirAngle2 = Math.atan2(obj2.v.y, obj2.v.x);
const newVX1 = length1 * Math.cos(dirAngle1 - colnAngle);
const newVX2 = length2 * Math.cos(dirAngle2 - colnAngle);
obj1.v.y = length1 * Math.sin(dirAngle1 - colnAngle);
obj2.v.y = length2 * Math.sin(dirAngle2 - colnAngle);
obj1.v.x = ((obj1.radius - obj2.radius) * newVX1 + 2 * obj2.radius * newVX2) / (obj1.radius + obj2.radius);
obj2.v.x = ((obj2.radius - obj1.radius) * newVX2 + 2 * obj1.radius * newVX1) / (obj1.radius + obj2.radius);
obj1.v.rotate(colnAngle);
obj2.v.rotate(colnAngle);
}}
class Stopwatch {
constructor() {
this.startTime = null;
this.elapsedTime = null;
this.running = null;
this.elapsedTimeForCalc = null;
this.fps = null;
this.lastTime = null;
}
calcTime() {
const time = Date.now();
this.elapsedTimeForCalc = time - this.lastTime;
this.fps = 1000 / this.elapsedTimeForCalc;
this.lastTime = time;
}
start() {
this.startTime = Date.now();
this.elapsedTime = null;
this.running = true;
}
/*
* Stop stop watch
*/
stop() {
this.elapsedTime = Date.now() - this.startTime;
this.running = false;
}
getElapsedTime() {
if (this.running) {
return Date.now() - this.startTime;
} else {
return this.elapsedTime;
}
}
isRunning() {
return this.running;
}
reset() {
this.elapsedTime = null;
}}
class AnimationTimer {
constructor(duration) {
if (duration !== undefined) this.duration = duration;
this.timeWarp = this.setTimeWarpFunction();
this.stopwatch = new Stopwatch();
}
setTimeWarpFunction()
{return this.makeEaseInOut();}
start() {
this.stopwatch.start();
}
stop() {
this.stopwatch.stop();
}
getElapsedTime() {
const elapsedTime = this.stopwatch.getElapsedTime();
const percentComplete = elapsedTime / this.duration;
if (!this.stopwatch.running) return undefined;
if (this.timeWarp === undefined) return elapsedTime;
return [elapsedTime * (this.timeWarp(percentComplete) / percentComplete), this.timeWarp(percentComplete)];
}
isRunning() {
return this.stopwatch.running;
}
isOver() {
return this.stopwatch.getElapsedTime() > this.duration;
}
makeEaseIn(strength) {
const st = strength || 1;
return percentComplete => {
return Math.pow(percentComplete, st * 2);
};
}
makeEaseOut(strength) {
const st = strength || 1;
return percentComplete => {
return 1 - Math.pow(1 - percentComplete, st * 2);
};
}
makeEaseInOut() {
return percentComplete => {
return percentComplete - Math.sin(percentComplete * 2 * Math.PI) / (2 * Math.PI);
};
}
makeElastic(passes) {
const pas = passes || 4;
return percentComplete => {
return (1 - Math.cos(percentComplete * Math.PI * pas)) * (1 - percentComplete) + percentComplete;
};
}
makeBounce(bounces) {
const bo = bounces || 5;
const fn = this.makeElastic(bo);
return percentComplete => {
percentComplete = fn(percentComplete);
return percentComplete <= 1 ? percentComplete : 2 - percentComplete;
};
}
makeLinear() {
return percentComplete => {
return percentComplete;
};
}}
class Angle {
/*
* @constructor
* @param {number} angle
*/
constructor(angle) {
this.a = angle;
this.radian = this.a * Math.PI / 180;
}
getAngle() {
return this.a;
}
/*
* Return radian
*/
getRadian() {
return this.radian;
}
/*
* Increase angle
*/
increaseAngle(num) {
this.a += num;
}}
/**
* Canvas
*/
class Canvas {
constructor(bool) {
// create canvas.
this.canvas = document.createElement("canvas");
// if on screen.
if (bool === true) {
this.canvas.style.position = 'relative';
this.canvas.style.display = 'block';
this.canvas.style.zIndex = '-1';
this.canvas.style.backgroundColor = 'black';
this.canvas.style.top = '0';
this.canvas.style.left = '0';
document.getElementsByTagName("body")[0].appendChild(this.canvas);
}
this.ctx = this.canvas.getContext("2d");
this.width = this.canvas.width = window.innerWidth;
this.height = this.canvas.height = window.innerHeight;
// times
this.times = null;
// mouse infomation.
this.mouseX = null;
this.mouseY = null;
this.mouseZ = null;
// shape
this.numberOfShapes = null;
this.shapesArray = null;
}
init() {
this.numberOfShapes = 200;
this.shapesArray = [];
this.times = new Stopwatch();
for (let i = 0; i < this.numberOfShapes; i++) {
const s = new Shape(this.ctx, this.width, this.height, i);
this.shapesArray.push(s);
}
}
render() {
// times
this.times.calcTime();
// clear canvas
this.ctx.clearRect(0, 0, this.width, this.height);
// render
for (let i = 0; i < this.shapesArray.length; i++) {
this.shapesArray[i].render(Date.now());
}
// draw fps
this.drawFPS();
// itereation
requestAnimationFrame(() => {
this.render();
});
}
drawFPS() {
const ctx = this.ctx;
ctx.save();
ctx.fillStyle = 'white';
ctx.font = '16px sans-serif';
ctx.textAlign = 'right';
ctx.textBaseline = 'bottom';
ctx.fillText(this.times.fps.toFixed() + ' FPS', this.width, this.height);
ctx.restore();
}
resize() {
this.width = this.canvas.width = window.innerWidth;
this.height = this.canvas.height = window.innerHeight;
this.init();
}}
/**
* Shape class.
*/
class Shape {
constructor(ctx, ww, wh, index) {
this.ctx = ctx;
this.ww = ww;
this.wh = wh;
this.init(index);
}
init(index) {
this.index = index;
// Coordinate
this.x = this.ww * Math.random();
this.y = this.wh + 15;
// vector
this.v = new Vector2d(0, -Math.random());
// for animation
this.AnimationTimer = new AnimationTimer(
Utils.getRandomNumber(500, 5000),
document.getElementsByTagName('select')[0].value,
document.getElementsByTagName('input')[0].value);
this.AnimationTimer.start();
this.lastTime = null;
// shape settings
this.lineWidth = 1;
this.radius = Math.random() * 10 + 5;
this.fillColor = 'white';
this.strokeColor = 'white';
this.globalAlpha = 1;
this.scalar = 200;
}
draw(time) {
const ctx = this.ctx;
ctx.save();
ctx.globalAlpha = this.globalAlpha;
ctx.lineWidth = this.lineWidth;
ctx.fillStyle = this.fillColor;
ctx.strokeStyle = this.strokeColor;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.fill();
ctx.stroke();
ctx.restore();
}
updatePositionTimeBased(elapsedTime, percentComplete) {
const deltaX = this.v.x * this.scalar * elapsedTime;
const deltaY = this.v.y * this.scalar * elapsedTime;
this.x += deltaX;
this.y += deltaY;
this.globalAlpha = 1 - percentComplete;
}
judgeAnimation() {
if (this.AnimationTimer.isRunning()) {
const times = this.AnimationTimer.getElapsedTime();
const animationElapsed = times[0] || 0.01;
const percentComplete = times[1];
if (this.lastTime !== null) {
let elapsed = animationElapsed - this.lastTime;
this.updatePositionTimeBased(elapsed / 1000, percentComplete);
}
if (this.AnimationTimer.isOver()) {
this.AnimationTimer.stop();
this.init(this.index);
return;
}
this.lastTime = animationElapsed;
}
}
render(nowTime) {
this.draw(nowTime / 1000);
this.judgeAnimation();
}}
(() => {
'use strict';
window.addEventListener('load', () => {
console.clear();
const canvas = new Canvas(true);
canvas.init();
canvas.render();
window.addEventListener("resize", () => {
canvas.resize();
}, false);
canvas.canvas.addEventListener('mousemove', e => {
canvas.mouseX = e.clientX;
canvas.mouseY = e.clientY;
}, false);
// smartphone event
let touchStartX, touchMoveX, touchEndX,
touchStartY, touchMoveY, touchEndY;
canvas.canvas.addEventListener('touchstart', e => {
const touch = e.targetTouches[0];
touchStartX = touch.pageX;
touchStartY = touch.pageY;
}, false);
canvas.canvas.addEventListener('touchmove', e => {
const touch = e.targetTouches[0];
touchMoveX = touch.pageX;
touchMoveY = touch.pageY;
touchEndX = touchStartX - touchMoveX;
touchEndY = touchStartY - touchMoveY;
canvas.mouseX = touch.pageX;
canvas.mouseY = touch.pageY;
}, false);
canvas.canvas.addEventListener('touchend', e => {
touchStartX = null;
touchStartY = null;
touchMoveX = null;
touchMoveY = null;
touchEndX = null;
touchEndY = null;
canvas.mouseX = null;
canvas.mouseY = null;
}, false);
}, false);
})();