wave.js
1.57 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
import {
Point
} from './point.js'
export class Wave {
constructor(index, totalPoints, color) {
this.index = index;
this.totalPoints = totalPoints;
this.color = color;
this.points = [];
}
resize(stageWidth, stageHeight){
this.stageWidth = stageWidth;
this.stageHeight = stageHeight;
this.centerX = stageWidth /2;
this.centerY = stageHeight /2;
this.pointGap = this.stageWidth/ (this.totalPoints - 1);
this.init();
}
init(){
this.points = [];
for (let i = 0; i < this.totalPoints; i++){
const point = new Point(
this.index +i,
this.pointGap * i,
this.centerY,
);
this.points[i] = point;
}
}
draw(ctx) {
ctx.beginPath();
ctx.fillStyle = this.color;
let prevX = this.points[0].x;
let prevY = this.points[0].y;
ctx.moveTo(prevX,prevY);
for(let i = 1; i < this.totalPoints; i++){
if (i < this.totalPoints - 1){
this.points[i].update();
}
const cx = (prevX + this.points[i].x) / 2;
const cy = (prevY + this.points[i].y) / 2;
ctx.quadraticCurveTo(prevX, prevY, cx,cy);
prevX = this.points[i].x;
prevY = this.points[i].y;
}
ctx.lineTo(prevX,prevY);
ctx.lineTo(this.stageWidth,this.stageHeight);
ctx.lineTo(this.points[0].x,this.stageHeight);
ctx.fill();
ctx.closePath();
}
}