1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | class Wave {
|
9 | constructor({
|
10 | canvasWidth,
|
11 | canvasHeight,
|
12 | waveWidth = 0.055,
|
13 | waveHeight = 6,
|
14 | xOffset = 0,
|
15 | speed = 0.04,
|
16 | colors = ['#DBB77A', '#BF8F3B'],
|
17 | } = {}) {
|
18 | this.points = [];
|
19 | this.startX = 0;
|
20 | this.canvasWidth = canvasWidth;
|
21 | this.canvasHeight = canvasHeight;
|
22 | this.waveWidth = waveWidth;
|
23 | this.waveHeight = waveHeight;
|
24 | this.xOffset = xOffset;
|
25 | this.speed = speed;
|
26 | this.colors = colors;
|
27 | }
|
28 | getChartColor(ctx) {
|
29 | const radius = this.canvasWidth / 2;
|
30 | const grd = ctx.createLinearGradient(radius, radius, radius, this.canvasHeight);
|
31 | grd.addColorStop(0, this.colors[0]);
|
32 | grd.addColorStop(1, this.colors[1]);
|
33 | return grd;
|
34 | }
|
35 | draw(ctx) {
|
36 | ctx.save();
|
37 | const points = this.points;
|
38 | ctx.beginPath();
|
39 | for (let i = 0; i < points.length; i += 1) {
|
40 | const point = points[i];
|
41 | ctx.lineTo(point[0], point[1]);
|
42 | }
|
43 | ctx.lineTo(this.canvasWidth, this.canvasHeight);
|
44 | ctx.lineTo(this.startX, this.canvasHeight);
|
45 | ctx.lineTo(points[0][0], points[0][1]);
|
46 | ctx.fillStyle = this.getChartColor(ctx);
|
47 | ctx.fill();
|
48 | ctx.restore();
|
49 | }
|
50 | update({
|
51 | nowRange,
|
52 | } = {}) {
|
53 | this.points = [];
|
54 | const {
|
55 | startX,
|
56 | waveHeight,
|
57 | waveWidth,
|
58 | canvasWidth,
|
59 | canvasHeight,
|
60 | xOffset,
|
61 | } = this;
|
62 | for (let x = startX; x < startX + canvasWidth; x += 20 / canvasWidth) {
|
63 | const y = Math.sin(((startX + x) * waveWidth) + xOffset);
|
64 | const dY = canvasHeight * (1 - (nowRange / 100));
|
65 | this.points.push([x, dY + (y * waveHeight)]);
|
66 | }
|
67 | this.xOffset += this.speed;
|
68 | }
|
69 | }
|
70 |
|
71 | export default Wave; |
\ | No newline at end of file |