|
| 1 | +import { Point } from "./point.js"; |
| 2 | + |
| 3 | +export class Wave { |
| 4 | + constructor(index, totalPoints, color) { |
| 5 | + this.index = index; |
| 6 | + this.totalPoints = totalPoints; |
| 7 | + this.color = color; |
| 8 | + this.points = []; |
| 9 | + } |
| 10 | + |
| 11 | + resize(stageWidth, stageHeight) { |
| 12 | + this.stageWidth = stageWidth; |
| 13 | + this.stageHeight = stageHeight; |
| 14 | + |
| 15 | + this.centerX = stageWidth / 2; |
| 16 | + this.centerY = stageHeight / 2; |
| 17 | + |
| 18 | + this.pointGap = this.stageWidth / (this.totalPoints - 1); |
| 19 | + |
| 20 | + this.init(); |
| 21 | + } |
| 22 | + |
| 23 | + init() { |
| 24 | + // this.point = new Point(this.centerX, this.centerY); |
| 25 | + this.points = []; |
| 26 | + |
| 27 | + for (let i = 0; i < this.totalPoints; i++) { |
| 28 | + const point = new Point(this.index + i, this.pointGap * i, this.centerY); |
| 29 | + this.points[i] = point; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + draw(ctx) { |
| 34 | + ctx.beginPath(); |
| 35 | + ctx.fillStyle = this.color; |
| 36 | + |
| 37 | + let prevX = this.points[0].x; |
| 38 | + let prevY = this.points[0].y; |
| 39 | + |
| 40 | + ctx.moveTo(prevX, prevY); |
| 41 | + |
| 42 | + for (let i = 1; i < this.totalPoints; i++) { |
| 43 | + if (i < this.totalPoints - 1) { |
| 44 | + this.points[i].update(); |
| 45 | + } |
| 46 | + |
| 47 | + const cx = (prevX + this.points[i].x) / 2; |
| 48 | + const cy = (prevY + this.points[i].y) / 2; |
| 49 | + |
| 50 | + ctx.quadraticCurveTo(prevX, prevY, cx, cy); |
| 51 | + |
| 52 | + prevX = this.points[i].x; |
| 53 | + prevY = this.points[i].y; |
| 54 | + } |
| 55 | + |
| 56 | + ctx.lineTo(prevX, prevY); |
| 57 | + ctx.lineTo(this.stageWidth, this.stageHeight); |
| 58 | + ctx.lineTo(this.points[0].x, this.stageHeight); |
| 59 | + ctx.fill(); |
| 60 | + ctx.closePath(); |
| 61 | + |
| 62 | + // this.point.update(); |
| 63 | + |
| 64 | + // ctx.arc(this.point.x, this.point.y, 30, 0, 2 * Math.PI); |
| 65 | + // ctx.fill(); |
| 66 | + } |
| 67 | +} |
0 commit comments